Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/question/type/calculated/simpletest/testvariablesubstituter.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00027 defined('MOODLE_INTERNAL') || die();
00028 
00029 require_once($CFG->dirroot . '/question/type/calculated/question.php');
00030 
00031 
00038 class qtype_calculated_variable_substituter_test extends UnitTestCase {
00039     public function test_simple_expression() {
00040         $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.');
00041         $this->assertEqual(3, $vs->calculate('{a} + {b}'));
00042     }
00043 
00044     public function test_simple_expression_negatives() {
00045         $vs = new qtype_calculated_variable_substituter(array('a' => -1, 'b' => -2), '.');
00046         $this->assertEqual(1, $vs->calculate('{a}-{b}'));
00047     }
00048 
00049     public function test_cannot_use_nonnumbers() {
00050         $this->expectException();
00051         $vs = new qtype_calculated_variable_substituter(array('a' => 'frog', 'b' => -2), '.');
00052     }
00053 
00054     public function test_invalid_expression() {
00055         $this->expectException();
00056         $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.');
00057         $vs->calculate('{a} + {b}?');
00058     }
00059 
00060     public function test_tricky_invalid_expression() {
00061         $this->expectException();
00062         $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.');
00063         $vs->calculate('{a}{b}'); // Have to make sure this does not just evaluate to 12.
00064     }
00065 
00066     public function test_replace_expressions_in_text_simple_var() {
00067         $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.');
00068         $this->assertEqual('1 + 2', $vs->replace_expressions_in_text('{a} + {b}'));
00069     }
00070 
00071     public function test_replace_expressions_in_confusing_text() {
00072         $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.');
00073         $this->assertEqual("(1) 1\n(2) 2", $vs->replace_expressions_in_text("(1) {a}\n(2) {b}"));
00074     }
00075 
00076     public function test_replace_expressions_in_text_formula() {
00077         $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.');
00078         $this->assertEqual('= 3', $vs->replace_expressions_in_text('= {={a} + {b}}'));
00079     }
00080 
00081     public function test_replace_expressions_in_text_negative() {
00082         $vs = new qtype_calculated_variable_substituter(array('a' => -1, 'b' => 2), '.');
00083         $this->assertEqual('temperatures -1 and 2',
00084                 $vs->replace_expressions_in_text('temperatures {a} and {b}'));
00085     }
00086 
00087     public function test_replace_expressions_in_text_commas_for_decimals() {
00088         $vs = new qtype_calculated_variable_substituter(
00089                 array('phi' => 1.61803399, 'pi' => 3.14159265), ',');
00090         $this->assertEqual('phi (1,61803399) + pi (3,14159265) = 4,75962664',
00091                 $vs->replace_expressions_in_text('phi ({phi}) + pi ({pi}) = {={phi} + {pi}}'));
00092     }
00093 
00094     public function test_format_float_dot() {
00095         $vs = new qtype_calculated_variable_substituter(array('a' => -1, 'b' => 2), '.');
00096         $this->assertIdentical('0.12345', $vs->format_float(0.12345));
00097 
00098         $this->assertIdentical('0', $vs->format_float(0.12345, 0, 1));
00099         $this->assertIdentical('0.12', $vs->format_float(0.12345, 2, 1));
00100         $this->assertIdentical('0.1235', $vs->format_float(0.12345, 4, 1));
00101 
00102         $this->assertIdentical('0.12', $vs->format_float(0.12345, 2, 2));
00103         $this->assertIdentical('0.0012', $vs->format_float(0.0012345, 4, 1));
00104     }
00105 
00106     public function test_format_float_comma() {
00107         $vs = new qtype_calculated_variable_substituter(array('a' => -1, 'b' => 2), ',');
00108         $this->assertIdentical('0,12345', $vs->format_float(0.12345));
00109 
00110         $this->assertIdentical('0', $vs->format_float(0.12345, 0, 1));
00111         $this->assertIdentical('0,12', $vs->format_float(0.12345, 2, 1));
00112         $this->assertIdentical('0,1235', $vs->format_float(0.12345, 4, 1));
00113 
00114         $this->assertIdentical('0,12', $vs->format_float(0.12345, 2, 2));
00115         $this->assertIdentical('0,0012', $vs->format_float(0.0012345, 4, 1));
00116     }
00117 }
 All Data Structures Namespaces Files Functions Variables Enumerations