Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]
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. 13 // 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/>. 16 17 /** 18 * Unit tests for qtype_calculated_variable_substituter. 19 * 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 24 */ 25 26 27 defined('MOODLE_INTERNAL') || die(); 28 29 global $CFG; 30 require_once($CFG->dirroot . '/question/type/calculated/question.php'); 31 require_once($CFG->dirroot . '/question/type/calculated/questiontype.php'); 32 33 34 /** 35 * Unit tests for {@link qtype_calculated_variable_substituter}. 36 * 37 * @copyright 2011 The Open University 38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 39 */ 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}')); 44 } 45 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}')); 49 } 50 51 public function test_cannot_use_nonnumbers() { 52 $this->expectException(moodle_exception::class); 53 $vs = new qtype_calculated_variable_substituter(array('a' => 'frog', 'b' => -2), '.'); 54 } 55 56 public function test_invalid_expression() { 57 $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.'); 58 $this->expectException(moodle_exception::class); 59 $vs->calculate('{a} + {b}?'); 60 } 61 62 public function test_tricky_invalid_expression() { 63 $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.'); 64 $this->expectException(moodle_exception::class); 65 $vs->calculate('{a}{b}'); // Have to make sure this does not just evaluate to 12. 66 } 67 68 public function test_division_by_zero_expression() { 69 70 if (intval(PHP_VERSION) < 7) { 71 $this->markTestSkipped('Division by zero triggers a PHP warning before PHP 7.'); 72 } 73 74 $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 0), '.'); 75 $this->expectException(moodle_exception::class); 76 $vs->calculate('{a} / {b}'); 77 } 78 79 public function test_replace_expressions_in_text_simple_var() { 80 $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.'); 81 $this->assertEquals('1 + 2', $vs->replace_expressions_in_text('{a} + {b}')); 82 } 83 84 public function test_replace_expressions_in_confusing_text() { 85 $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.'); 86 $this->assertEquals("(1) 1\n(2) 2", $vs->replace_expressions_in_text("(1) {a}\n(2) {b}")); 87 } 88 89 public function test_replace_expressions_in_text_formula() { 90 $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.'); 91 $this->assertEquals('= 3', $vs->replace_expressions_in_text('= {={a} + {b}}')); 92 } 93 94 public function test_expression_has_unmapped_placeholder() { 95 $this->expectException('moodle_exception'); 96 $this->expectExceptionMessage(get_string('illegalformulasyntax', 'qtype_calculated', '{c}')); 97 $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.'); 98 $vs->calculate('{c} - {a} + {b}'); 99 } 100 101 public function test_replace_expressions_in_text_negative() { 102 $vs = new qtype_calculated_variable_substituter(array('a' => -1, 'b' => 2), '.'); 103 $this->assertEquals('temperatures -1 and 2', 104 $vs->replace_expressions_in_text('temperatures {a} and {b}')); 105 } 106 107 public function test_replace_expressions_in_text_commas_for_decimals() { 108 $vs = new qtype_calculated_variable_substituter( 109 array('phi' => 1.61803399, 'pi' => 3.14159265), ','); 110 $this->assertEquals('phi (1,61803399) + pi (3,14159265) = 4,75962664', 111 $vs->replace_expressions_in_text('phi ({phi}) + pi ({pi}) = {={phi} + {pi}}')); 112 } 113 114 public function test_format_float_dot() { 115 $vs = new qtype_calculated_variable_substituter(array('a' => -1, 'b' => 2), '.'); 116 $this->assertSame('0.12345', $vs->format_float(0.12345)); 117 118 $this->assertSame('0', $vs->format_float(0.12345, 0, 1)); 119 $this->assertSame('0.12', $vs->format_float(0.12345, 2, 1)); 120 $this->assertSame('0.1235', $vs->format_float(0.12345, 4, 1)); 121 122 $this->assertSame('0.12', $vs->format_float(0.12345, 2, 2)); 123 $this->assertSame('0.0012', $vs->format_float(0.0012345, 4, 1)); 124 } 125 126 public function test_format_float_comma() { 127 $vs = new qtype_calculated_variable_substituter(array('a' => -1, 'b' => 2), ','); 128 $this->assertSame('0,12345', $vs->format_float(0.12345)); 129 130 $this->assertSame('0', $vs->format_float(0.12345, 0, 1)); 131 $this->assertSame('0,12', $vs->format_float(0.12345, 2, 1)); 132 $this->assertSame('0,1235', $vs->format_float(0.12345, 4, 1)); 133 134 $this->assertSame('0,12', $vs->format_float(0.12345, 2, 2)); 135 $this->assertSame('0,0012', $vs->format_float(0.0012345, 4, 1)); 136 } 137 138 public function test_format_float_nan_inf() { 139 $vs = new qtype_calculated_variable_substituter([ ], '.'); 140 141 $this->assertSame('NAN', $vs->format_float(NAN)); 142 $this->assertSame('INF', $vs->format_float(INF)); 143 $this->assertSame('-INF', $vs->format_float(-INF)); 144 } 145 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body