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