From cbada2b920782bdfd2f18ef88c09e24ae4840f4a Mon Sep 17 00:00:00 2001 From: Bruce Wells Date: Sat, 28 May 2022 16:02:04 -0400 Subject: [PATCH] Space should end open numbers (#113) --- src/NXP/Classes/Tokenizer.php | 2 ++ tests/MathTest.php | 43 +++++++++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/NXP/Classes/Tokenizer.php b/src/NXP/Classes/Tokenizer.php index af041c1..a74bff6 100644 --- a/src/NXP/Classes/Tokenizer.php +++ b/src/NXP/Classes/Tokenizer.php @@ -124,6 +124,8 @@ public function tokenize() : self continue 2; case ' ' == $ch || "\n" == $ch || "\r" == $ch || "\t" == $ch: + $this->emptyNumberBufferAsLiteral(); + $this->emptyStrBufferAsVariable(); $this->tokens[] = new Token(Token::Space, ''); continue 2; diff --git a/tests/MathTest.php b/tests/MathTest.php index e68fdb8..7af7c59 100644 --- a/tests/MathTest.php +++ b/tests/MathTest.php @@ -26,7 +26,7 @@ class MathTest extends TestCase /** * @dataProvider providerExpressions */ - public function testCalculating($expression) : void + public function testCalculating(string $expression) : void { $calculator = new MathExecutor(); @@ -250,18 +250,47 @@ public function providerExpressions() ]; } - public function testUnknownFunctionException() : void + /** + * @dataProvider incorrectExpressions + */ + public function testIncorrectExpressionException(string $expression) : void { $calculator = new MathExecutor(); - $this->expectException(UnknownFunctionException::class); - $calculator->execute('1 * fred("wilma") + 3'); + $calculator->setVars(['a' => 12, 'b' => 24]); + $this->expectException(IncorrectExpressionException::class); + $calculator->execute($expression); + } + + /** + * Incorrect Expressions data provider + * + * These expressions should not pass validation + */ + public function incorrectExpressions() + { + return [ + ['1 * + '], + [' 2 3'], + ['2 3 '], + [' 2 4 3 '], + ['$a $b'], + ['$a [3, 4, 5]'], + ['$a (3 + 4)'], + ['$a "string"'], + ['5 "string"'], + ['"string" $a'], + ['$a round(12.345)'], + ['round(12.345) $a'], + ['4 round(12.345)'], + ['round(12.345) 4'], + ]; } - public function testIncorrectExpressionException() : void + public function testUnknownFunctionException() : void { $calculator = new MathExecutor(); - $this->expectException(IncorrectExpressionException::class); - $calculator->execute('1 * + '); + $this->expectException(UnknownFunctionException::class); + $calculator->execute('1 * fred("wilma") + 3'); } public function testZeroDivision() : void