diff options
author | Alexander Kiryukhin <alexander@kiryukhin.su> | 2019-01-16 10:27:56 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-01-16 10:27:56 +0300 |
commit | 36b252b7c91fada20489be9ee60f173b49b823cb (patch) | |
tree | a2d1f4dd6504745596efe10620997bcc1ce06b5a /tests | |
parent | 5a26d651dd241f2457cda49206e3439513ea4788 (diff) | |
parent | e1b770d6c884d79b8c3c6226d2e6cc8cc5a633ca (diff) |
Merge pull request #44 from phpfui/masterv0.7.2
* Fixed comma operator
Added unit tests for expressions in function arguments.
Changed array_push to $var[] = native code.
* Fixed merge error
* Fixed typo in constant
Diffstat (limited to 'tests')
-rw-r--r-- | tests/MathTest.php | 40 |
1 files changed, 28 insertions, 12 deletions
diff --git a/tests/MathTest.php b/tests/MathTest.php index 8eb7827..3cf4dbd 100644 --- a/tests/MathTest.php +++ b/tests/MathTest.php @@ -135,32 +135,48 @@ class MathTest extends \PHPUnit_Framework_TestCase } public function testFunctionParameterOrder() - { - $calculator = new MathExecutor(); + { + $calculator = new MathExecutor(); - $calculator->addFunction('concat', function ($arg1, $arg2) {return $arg1.$arg2;}); - $this->assertEquals('testing', $calculator->execute('concat("test","ing")')); - $this->assertEquals('testing', $calculator->execute("concat('test','ing')")); - } + $calculator->addFunction('concat', function ($arg1, $arg2) {return $arg1.$arg2;}); + $this->assertEquals('testing', $calculator->execute('concat("test","ing")')); + $this->assertEquals('testing', $calculator->execute("concat('test','ing')")); + } public function testFunction() { $calculator = new MathExecutor(); $calculator->addFunction('round', function ($arg) {return round($arg);}); - /** @var float $phpResult */ - eval('$phpResult = round(100/30);'); - $this->assertEquals($phpResult, $calculator->execute('round(100/30)')); + $this->assertEquals(round(100/30), $calculator->execute('round(100/30)')); + } + + public function testEvaluateFunctionParameters() + { + $calculator = new MathExecutor(); + $calculator->addFunction('round', function ($value, $decimals) + { + return round($value, $decimals); + } + ); + $expression = 'round(100 * 1.111111, 2)'; + eval('$phpResult = ' . $expression . ';'); + $this->assertEquals($phpResult, $calculator->execute($expression)); + $expression = 'round((100*0.04)+(((100*1.02)+0.5)*1.28),2)'; + eval('$phpResult = ' . $expression . ';'); + $this->assertEquals($phpResult, $calculator->execute($expression)); } public function testQuotes() { $calculator = new MathExecutor(); $testString = "some, long. arg; with: different-separators!"; - $calculator->addFunction('test', function ($arg) use ($testString) { + $calculator->addFunction('test', function ($arg) use ($testString) + { $this->assertEquals($testString, $arg); - return 0;} + return 0; + } ); $calculator->execute('test("' . $testString . '")'); // single quotes $calculator->execute("test('" . $testString . "')"); // double quotes } -}
\ No newline at end of file +} |