Skip to content

Commit cbada2b

Browse files
authored
Space should end open numbers (#113)
1 parent d1b0607 commit cbada2b

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

src/NXP/Classes/Tokenizer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ public function tokenize() : self
124124
continue 2;
125125

126126
case ' ' == $ch || "\n" == $ch || "\r" == $ch || "\t" == $ch:
127+
$this->emptyNumberBufferAsLiteral();
128+
$this->emptyStrBufferAsVariable();
127129
$this->tokens[] = new Token(Token::Space, '');
128130

129131
continue 2;

tests/MathTest.php

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class MathTest extends TestCase
2626
/**
2727
* @dataProvider providerExpressions
2828
*/
29-
public function testCalculating($expression) : void
29+
public function testCalculating(string $expression) : void
3030
{
3131
$calculator = new MathExecutor();
3232

@@ -250,18 +250,47 @@ public function providerExpressions()
250250
];
251251
}
252252

253-
public function testUnknownFunctionException() : void
253+
/**
254+
* @dataProvider incorrectExpressions
255+
*/
256+
public function testIncorrectExpressionException(string $expression) : void
254257
{
255258
$calculator = new MathExecutor();
256-
$this->expectException(UnknownFunctionException::class);
257-
$calculator->execute('1 * fred("wilma") + 3');
259+
$calculator->setVars(['a' => 12, 'b' => 24]);
260+
$this->expectException(IncorrectExpressionException::class);
261+
$calculator->execute($expression);
262+
}
263+
264+
/**
265+
* Incorrect Expressions data provider
266+
*
267+
* These expressions should not pass validation
268+
*/
269+
public function incorrectExpressions()
270+
{
271+
return [
272+
['1 * + '],
273+
[' 2 3'],
274+
['2 3 '],
275+
[' 2 4 3 '],
276+
['$a $b'],
277+
['$a [3, 4, 5]'],
278+
['$a (3 + 4)'],
279+
['$a "string"'],
280+
['5 "string"'],
281+
['"string" $a'],
282+
['$a round(12.345)'],
283+
['round(12.345) $a'],
284+
['4 round(12.345)'],
285+
['round(12.345) 4'],
286+
];
258287
}
259288

260-
public function testIncorrectExpressionException() : void
289+
public function testUnknownFunctionException() : void
261290
{
262291
$calculator = new MathExecutor();
263-
$this->expectException(IncorrectExpressionException::class);
264-
$calculator->execute('1 * + ');
292+
$this->expectException(UnknownFunctionException::class);
293+
$calculator->execute('1 * fred("wilma") + 3');
265294
}
266295

267296
public function testZeroDivision() : void

0 commit comments

Comments
 (0)