Skip to content

Commit

Permalink
Space should end open numbers (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
phpfui authored May 28, 2022
1 parent d1b0607 commit cbada2b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/NXP/Classes/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
43 changes: 36 additions & 7 deletions tests/MathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class MathTest extends TestCase
/**
* @dataProvider providerExpressions
*/
public function testCalculating($expression) : void
public function testCalculating(string $expression) : void
{
$calculator = new MathExecutor();

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit cbada2b

Please sign in to comment.