MDL-62469 qtype_calculated: check remaining placeholders, see MDL-62275
[moodle.git] / question / type / calculated / tests / variablesubstituter_test.php
bloba42d426808015f6026143893648701f2e5c6b1b0
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Unit tests for qtype_calculated_variable_substituter.
20 * @package qtype
21 * @subpackage calculated
22 * @copyright 2011 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
30 require_once($CFG->dirroot . '/question/type/calculated/question.php');
31 require_once($CFG->dirroot . '/question/type/calculated/questiontype.php');
34 /**
35 * Unit tests for {@link qtype_calculated_variable_substituter}.
37 * @copyright 2011 The Open University
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class qtype_calculated_variable_substituter_test extends advanced_testcase {
41 public function test_simple_expression() {
42 $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.');
43 $this->assertEquals(3, $vs->calculate('{a} + {b}'));
46 public function test_simple_expression_negatives() {
47 $vs = new qtype_calculated_variable_substituter(array('a' => -1, 'b' => -2), '.');
48 $this->assertEquals(1, $vs->calculate('{a}-{b}'));
51 /**
52 * @expectedException moodle_exception
54 public function test_cannot_use_nonnumbers() {
55 $vs = new qtype_calculated_variable_substituter(array('a' => 'frog', 'b' => -2), '.');
58 /**
59 * @expectedException moodle_exception
61 public function test_invalid_expression() {
62 $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.');
63 $vs->calculate('{a} + {b}?');
66 /**
67 * @expectedException moodle_exception
69 public function test_tricky_invalid_expression() {
70 $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.');
71 $vs->calculate('{a}{b}'); // Have to make sure this does not just evaluate to 12.
74 /**
75 * @expectedException moodle_exception
77 public function test_division_by_zero_expression() {
79 if (intval(PHP_VERSION) < 7) {
80 $this->markTestSkipped('Division by zero triggers a PHP warning before PHP 7.');
83 $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 0), '.');
84 $vs->calculate('{a} / {b}');
87 public function test_replace_expressions_in_text_simple_var() {
88 $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.');
89 $this->assertEquals('1 + 2', $vs->replace_expressions_in_text('{a} + {b}'));
92 public function test_replace_expressions_in_confusing_text() {
93 $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.');
94 $this->assertEquals("(1) 1\n(2) 2", $vs->replace_expressions_in_text("(1) {a}\n(2) {b}"));
97 public function test_replace_expressions_in_text_formula() {
98 $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.');
99 $this->assertEquals('= 3', $vs->replace_expressions_in_text('= {={a} + {b}}'));
102 public function test_expression_has_unmapped_placeholder() {
103 $this->expectException('moodle_exception');
104 $this->expectExceptionMessage(get_string('illegalformulasyntax', 'qtype_calculated', '{c}'));
105 $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.');
106 $vs->calculate('{c} - {a} + {b}');
109 public function test_replace_expressions_in_text_negative() {
110 $vs = new qtype_calculated_variable_substituter(array('a' => -1, 'b' => 2), '.');
111 $this->assertEquals('temperatures -1 and 2',
112 $vs->replace_expressions_in_text('temperatures {a} and {b}'));
115 public function test_replace_expressions_in_text_commas_for_decimals() {
116 $vs = new qtype_calculated_variable_substituter(
117 array('phi' => 1.61803399, 'pi' => 3.14159265), ',');
118 $this->assertEquals('phi (1,61803399) + pi (3,14159265) = 4,75962664',
119 $vs->replace_expressions_in_text('phi ({phi}) + pi ({pi}) = {={phi} + {pi}}'));
122 public function test_format_float_dot() {
123 $vs = new qtype_calculated_variable_substituter(array('a' => -1, 'b' => 2), '.');
124 $this->assertSame('0.12345', $vs->format_float(0.12345));
126 $this->assertSame('0', $vs->format_float(0.12345, 0, 1));
127 $this->assertSame('0.12', $vs->format_float(0.12345, 2, 1));
128 $this->assertSame('0.1235', $vs->format_float(0.12345, 4, 1));
130 $this->assertSame('0.12', $vs->format_float(0.12345, 2, 2));
131 $this->assertSame('0.0012', $vs->format_float(0.0012345, 4, 1));
134 public function test_format_float_comma() {
135 $vs = new qtype_calculated_variable_substituter(array('a' => -1, 'b' => 2), ',');
136 $this->assertSame('0,12345', $vs->format_float(0.12345));
138 $this->assertSame('0', $vs->format_float(0.12345, 0, 1));
139 $this->assertSame('0,12', $vs->format_float(0.12345, 2, 1));
140 $this->assertSame('0,1235', $vs->format_float(0.12345, 4, 1));
142 $this->assertSame('0,12', $vs->format_float(0.12345, 2, 2));
143 $this->assertSame('0,0012', $vs->format_float(0.0012345, 4, 1));