fix formula modulo

This commit is contained in:
Yuri Kuznetsov
2021-03-06 16:25:04 +02:00
parent 24617f4a41
commit dff2e344a4
3 changed files with 30 additions and 13 deletions
@@ -38,18 +38,19 @@ class ModuloType extends BaseFunction
{
public function process(ArgumentList $args)
{
$result = 1;
foreach ($args as $subItem) {
$part = $this->evaluate($subItem);
if (!is_float($part) && !is_int($part)) {
$part = floatval($part);
}
$result %= $part;
if (count($args) < 2) {
$this->throwTooFewArguments();
}
$result = $this->evaluate($args[0]);
$part = $this->evaluate($args[1]);
if (!is_float($part) && !is_int($part)) {
$part = floatval($part);
}
$result %= $part;
return $result;
}
}
@@ -472,4 +472,20 @@ class EvaluatorTest extends \PHPUnit\Framework\TestCase
$this->assertNotEmpty($id);
}
public function testModulo1() : void
{
$expression = "123 % 5";
$actual = $this->evaluator->process($expression);
$this->assertEquals(123 % 5, $actual);
}
public function testModulo2() : void
{
$expression = "124 % 5";
$actual = $this->evaluator->process($expression);
$this->assertEquals(124 % 5, $actual);
}
}
+3 -3
View File
@@ -787,11 +787,11 @@ class FormulaTest extends \PHPUnit\Framework\TestCase
"value": [
{
"type": "value",
"value": 3
"value": 124
},
{
"type": "value",
"value": 2
"value": 5
}
]
}
@@ -799,7 +799,7 @@ class FormulaTest extends \PHPUnit\Framework\TestCase
$result = $this->createProcessor()->process($item);
$this->assertEquals(3 % 2, $result);
$this->assertEquals(124 % 5, $result);
}
function testIfThenElse1()