MediaWiki REL1_34
ExpressionTest.php
Go to the documentation of this file.
1<?php
2
4
9class ExpressionTest extends MediaWikiUnitTestCase {
10
14 public function testExpression( $input, $expected ) {
15 $parser = new ExprParser();
16 $this->assertEquals(
17 $expected,
18 $parser->doExpression( $input )
19 );
20 }
21
22 public function provideExpressions() {
23 return [
24 [ '1 or 0', '1' ],
25 [ 'not (1 and 0)', '1' ],
26 [ 'not 0', '1' ],
27 [ '4 < 5', '1' ],
28 [ '-5 < 2', '1' ],
29 [ '-2 <= -2', '1' ],
30 [ '4 > 3', '1' ],
31 [ '4 > -3', '1' ],
32 [ '5 >= 2', '1' ],
33 [ '2 >= 2', '1' ],
34 [ '1 != 2', '1' ],
35 [ '-4 * -4 = 4 * 4', '1' ],
36 [ 'not (1 != 1)', '1' ],
37 [ '1 + 1', '2' ],
38 [ '-1 + 1', '0' ],
39 [ '+1 + 1', '2' ],
40 [ '4 * 4', '16' ],
41 [ '(1/3) * 3', '1' ],
42 [ '3 / 1.5', '2' ],
43 [ '3 / 0.2', '15' ],
44 [ '3 / ( 2.0 * 0.1 )', '15' ],
45 [ '3 / ( 2.0 / 10 )', '15' ],
46 [ '3 / (- 0.2 )', '-15' ],
47 [ '3 / abs( 0.2 )', '15' ],
48 [ '3 mod 2', '1' ],
49 [ '1e4', '10000' ],
50 [ '1e-2', '0.01' ],
51 [ '4.0 round 0', '4' ],
52 [ 'ceil 4', '4' ],
53 [ 'floor 4', '4' ],
54 [ '4.5 round 0', '5' ],
55 [ '4.2 round 0', '4' ],
56 [ '-4.2 round 0', '-4' ],
57 [ '-4.5 round 0', '-5' ],
58 [ '-2.0 round 0', '-2' ],
59 [ 'ceil -3', '-3' ],
60 [ 'floor -6.0', '-6' ],
61 [ 'ceil 4.2', '5' ],
62 [ 'ceil -4.5', '-4' ],
63 [ 'floor -4.5', '-5' ],
64 [ 'abs(-2)', '2' ],
65 [ 'ln(exp(1))', '1' ],
66 [ 'trunc(4.5)', '4' ],
67 [ 'trunc(-4.5)', '-4' ],
68 [ '123 fmod (2^64-1)', '123' ],
69 [ '5.7 mod 1.3', '0' ],
70 [ '5.7 fmod 1.3', '0.5' ],
71 [ 'pi + 1', '4.1415926535898' ],
72 [ 'sin(0)', '0' ],
73 [ 'cos(0)', '1' ],
74 [ 'tan(0)', '0' ],
75 [ 'asin(0)', '0' ],
76 [ 'acos(1)', '0' ],
77 [ 'atan(0)', '0' ],
78 [ 'sqrt(4)', '2' ],
79 ];
80 }
81
87 public function testExpressionThrows( $input ) {
88 $parser = new ExprParser();
89 $parser->doExpression( $input );
90 }
91
92 public function provideExpressionThrows() {
93 $longExpression = str_repeat( 'ln(', 1001 ) . '1' . str_repeat( ')', 1001 );
94 return [
95 'Expression too long' => [ $longExpression ],
96 'Unexpected number' => [ '1 2' ],
97 'Unrecognised word' => [ 'foo' ],
98 'Unexpected number: pi' => [ '1 pi' ],
99 'Unexpected operator' => [ '1 sin' ],
100 'Unexpected operator: (' => [ '1 (' ],
101 'Unexpected closing bracket' => [ '1 + 1)' ],
102 'Unexpected punctuation' => [ '1, 2' ],
103 'Unexpected binary operator' => [ '<1' ],
104 'Unclosed bracket' => [ '(1' ],
105 'Missing operand: unary -' => [ '-' ],
106 'Missing operand: unary +' => [ '+' ],
107 'Missing operand: *' => [ '1*' ],
108 'Missing operand: /' => [ '1/' ],
109 'Division by zero: /' => [ '1/0' ],
110 'Missing operand: mod' => [ '1 mod' ],
111 'Division by zero: mod' => [ '1 mod 0' ],
112 'Missing operand: fmod' => [ '1 fmod' ],
113 'Division by zero: fmod' => [ '1 fmod 0' ],
114 'Missing operand: +' => [ '1+' ],
115 'Missing operand: -' => [ '1-' ],
116 'Missing operand: and' => [ '1 and' ],
117 'Missing operand: or' => [ '1 or' ],
118 'Missing operand: =' => [ '1 =' ],
119 'Missing operand: not' => [ '1 not' ],
120 'Missing operand: round' => [ '1 round' ],
121 'Missing operand: <' => [ '1<' ],
122 'Missing operand: >' => [ '1>' ],
123 'Missing operand: <=' => [ '1<=' ],
124 'Missing operand: >=' => [ '1>=' ],
125 'Missing operand: <>' => [ '1<>' ],
126 'Missing operand: sin' => [ 'sin()' ],
127 'Missing operand: cos' => [ 'cos()' ],
128 'Missing operand: tan' => [ 'tan()' ],
129 'Missing operand: asin' => [ 'asin()' ],
130 'Invalid operand: asin' => [ 'asin(3) ' ],
131 'Missing operand: acos' => [ 'acos()' ],
132 'Invalid operand: acos' => [ 'acos(-1.1)' ],
133 'Missing operand: atan' => [ 'atan()' ],
134 'Missing operand: exp' => [ 'exp()' ],
135 'Missing operand: ln' => [ 'ln()' ],
136 'Invalid operand: ln' => [ 'ln(-1)' ],
137 'Missing operand: abs' => [ 'abs()' ],
138 'Missing operand: floor' => [ 'floor' ],
139 'Missing operand: trunc' => [ 'trunc' ],
140 'Missing operand: ceil' => [ 'ceil' ],
141 'Missing operand: ^' => [ '1 ^' ],
142 // 'Invalid operand: ^' => [ '-1 ^ 5.5.5' ], // Not testable?
143 'Missing operand: sqrt' => [ 'sqrt' ],
144 'Result is not a number: sqrt' => [ 'sqrt(-1)' ],
145 ];
146 }
147}
@group ParserFunctions @covers \MediaWiki\Extensions\ParserFunctions\ExprParser
testExpression( $input, $expected)
@dataProvider provideExpressions
testExpressionThrows( $input)
@dataProvider provideExpressionThrows