Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
namespace qtype_calculated;
18
 
19
use qtype_calculated_variable_substituter;
20
 
21
defined('MOODLE_INTERNAL') || die();
22
 
23
global $CFG;
24
require_once($CFG->dirroot . '/question/type/calculated/question.php');
25
require_once($CFG->dirroot . '/question/type/calculated/questiontype.php');
26
 
27
/**
28
 * Unit tests for {@link qtype_calculated_variable_substituter}.
29
 *
30
 * @package    qtype_calculated
31
 * @copyright  2011 The Open University
32
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
1441 ariadna 34
final class variablesubstituter_test extends \advanced_testcase {
11 efrain 35
    public function test_simple_expression(): void {
1 efrain 36
        $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.');
37
        $this->assertEquals(3, $vs->calculate('{a} + {b}'));
38
    }
39
 
11 efrain 40
    public function test_simple_expression_negatives(): void {
1 efrain 41
        $vs = new qtype_calculated_variable_substituter(array('a' => -1, 'b' => -2), '.');
42
        $this->assertEquals(1, $vs->calculate('{a}-{b}'));
43
    }
44
 
11 efrain 45
    public function test_cannot_use_nonnumbers(): void {
1 efrain 46
        $this->expectException(\moodle_exception::class);
47
        $vs = new qtype_calculated_variable_substituter(array('a' => 'frog', 'b' => -2), '.');
48
    }
49
 
11 efrain 50
    public function test_invalid_expression(): void {
1 efrain 51
        $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.');
52
        $this->expectException(\moodle_exception::class);
53
        $vs->calculate('{a} + {b}?');
54
    }
55
 
11 efrain 56
    public function test_tricky_invalid_expression(): void {
1 efrain 57
        $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.');
58
        $this->expectException(\moodle_exception::class);
59
        $vs->calculate('{a}{b}'); // Have to make sure this does not just evaluate to 12.
60
    }
61
 
11 efrain 62
    public function test_division_by_zero_expression(): void {
1 efrain 63
        $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 0), '.');
64
        $this->expectException(\moodle_exception::class);
65
        $vs->calculate('{a} / {b}');
66
    }
67
 
11 efrain 68
    public function test_replace_expressions_in_text_simple_var(): void {
1 efrain 69
        $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.');
70
        $this->assertEquals('1 + 2', $vs->replace_expressions_in_text('{a} + {b}'));
71
    }
72
 
11 efrain 73
    public function test_replace_expressions_in_confusing_text(): void {
1 efrain 74
        $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.');
75
        $this->assertEquals("(1) 1\n(2) 2", $vs->replace_expressions_in_text("(1) {a}\n(2) {b}"));
76
    }
77
 
11 efrain 78
    public function test_replace_expressions_in_text_formula(): void {
1 efrain 79
        $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.');
80
        $this->assertEquals('= 3', $vs->replace_expressions_in_text('= {={a} + {b}}'));
81
    }
82
 
11 efrain 83
    public function test_expression_has_unmapped_placeholder(): void {
1 efrain 84
        $this->expectException('moodle_exception');
85
        $this->expectExceptionMessage(get_string('illegalformulasyntax', 'qtype_calculated', '{c}'));
86
        $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.');
87
        $vs->calculate('{c} - {a} + {b}');
88
    }
89
 
11 efrain 90
    public function test_replace_expressions_in_text_negative(): void {
1 efrain 91
        $vs = new qtype_calculated_variable_substituter(array('a' => -1, 'b' => 2), '.');
92
        $this->assertEquals('temperatures -1 and 2',
93
                $vs->replace_expressions_in_text('temperatures {a} and {b}'));
94
    }
95
 
11 efrain 96
    public function test_replace_expressions_in_text_commas_for_decimals(): void {
1 efrain 97
        $vs = new qtype_calculated_variable_substituter(
98
                array('phi' => 1.61803399, 'pi' => 3.14159265), ',');
99
        $this->assertEquals('phi (1,61803399) + pi (3,14159265) = 4,75962664',
100
                $vs->replace_expressions_in_text('phi ({phi}) + pi ({pi}) = {={phi} + {pi}}'));
101
    }
102
 
11 efrain 103
    public function test_format_float_dot(): void {
1 efrain 104
        $vs = new qtype_calculated_variable_substituter(array('a' => -1, 'b' => 2), '.');
105
        $this->assertSame('0.12345', $vs->format_float(0.12345));
106
 
107
        $this->assertSame('0', $vs->format_float(0.12345, 0, 1));
108
        $this->assertSame('0.12', $vs->format_float(0.12345, 2, 1));
109
        $this->assertSame('0.1235', $vs->format_float(0.12345, 4, 1));
110
 
111
        $this->assertSame('0.12', $vs->format_float(0.12345, 2, 2));
112
        $this->assertSame('0.0012', $vs->format_float(0.0012345, 4, 1));
113
    }
114
 
11 efrain 115
    public function test_format_float_comma(): void {
1 efrain 116
        $vs = new qtype_calculated_variable_substituter(array('a' => -1, 'b' => 2), ',');
117
        $this->assertSame('0,12345', $vs->format_float(0.12345));
118
 
119
        $this->assertSame('0', $vs->format_float(0.12345, 0, 1));
120
        $this->assertSame('0,12', $vs->format_float(0.12345, 2, 1));
121
        $this->assertSame('0,1235', $vs->format_float(0.12345, 4, 1));
122
 
123
        $this->assertSame('0,12', $vs->format_float(0.12345, 2, 2));
124
        $this->assertSame('0,0012', $vs->format_float(0.0012345, 4, 1));
125
    }
126
 
11 efrain 127
    public function test_format_float_nan_inf(): void {
1 efrain 128
        $vs = new qtype_calculated_variable_substituter([ ], '.');
129
 
130
        $this->assertSame('NAN', $vs->format_float(NAN));
131
        $this->assertSame('INF', $vs->format_float(INF));
132
        $this->assertSame('-INF', $vs->format_float(-INF));
133
    }
134
}