Skip to content

Commit f588876

Browse files
author
Nicolas Oelgart
committed
Add additional type checks and tests
1 parent 54d2015 commit f588876

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

src/nicoSWD/Rules/Core/Functions/ParseFloat.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,19 @@
1111

1212
use nicoSWD\Rules\Core\CallableFunction;
1313
use nicoSWD\Rules\Tokens\TokenFloat;
14+
use nicoSWD\Rules\Tokens\TokenInteger;
1415

1516
final class ParseFloat extends CallableFunction
1617
{
18+
/**
19+
* {@inheritdoc}
20+
*/
1721
public function call($value = null) : TokenFloat
1822
{
23+
if ($value === null) {
24+
return new TokenFloat(NAN);
25+
}
26+
1927
return new TokenFloat(
2028
(float) $value->getValue(),
2129
$this->token->getOffset(),

src/nicoSWD/Rules/Core/Functions/ParseInt.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,15 @@
1414

1515
final class ParseInt extends CallableFunction
1616
{
17+
/**
18+
* {@inheritdoc}
19+
*/
1720
public function call($value = null) : TokenInteger
1821
{
22+
if ($value === null) {
23+
return new TokenInteger(NAN);
24+
}
25+
1926
return new TokenInteger(
2027
(int) $value->getValue(),
2128
$this->token->getOffset(),

tests/functions/ParseFloatTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,9 @@ public function testOnUserDefinedVariable()
3131
$this->assertTrue($this->evaluate('parseFloat(foo) === 3.4', ['foo' => '3.4']));
3232
$this->assertFalse($this->evaluate('parseFloat(foo) === "3.5"', ['foo' => 3.5]));
3333
}
34+
35+
public function testCallWithoutArgsShouldReturnNaN()
36+
{
37+
$this->assertFalse($this->evaluate('parseFloat() === 1'));
38+
}
3439
}

tests/functions/ParseIntTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,9 @@ public function testOnUserDefinedVariable()
3131
$this->assertTrue($this->evaluate('parseInt(foo) === 3', ['foo' => '3']));
3232
$this->assertFalse($this->evaluate('parseInt(foo) === "3"', ['foo' => 3]));
3333
}
34+
35+
public function testCallWithoutArgsShouldReturnNan()
36+
{
37+
$this->assertFalse($this->evaluate('parseInt() === 1'));
38+
}
3439
}

0 commit comments

Comments
 (0)