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