public function testParserWithStringConstants()
 {
     $context = new Context();
     $const = 'string constant';
     $context->def('const', $const, 'string');
     $actual = Parser::parse('const', $context);
     $this->assertEquals($const, $actual);
 }
 /**
  * @param $equation
  * @param array $constants
  *                         @param $expected
  *
  * @dataProvider equationAndConstantsProvider
  */
 public function testParserWithConstants($equation, array $constants, $expected)
 {
     $context = new Context();
     foreach ($constants as $key => $val) {
         $context->def($key, $val);
     }
     $actual = Parser::parse($equation, $context);
     $this->assertEquals($expected, $actual);
 }
 public function testFunctionDefinitionWithOptionalParams()
 {
     $context = new Context();
     $context->def('func', function ($param1, $param2 = 100) {
         return $param1 + $param2;
     });
     $actual = $context->fn('func', array(3));
     $this->assertEquals(103.0, $actual);
     $actual = $context->fn('func', array(3, 200));
     $this->assertEquals(203.0, $actual);
 }
 public function testWrapPHPFunction()
 {
     $context = new Context();
     $context->def('abs');
     $equation = 'abs(100)';
     $actual = Parser::parse($equation, $context);
     $expected = 100;
     $this->assertEquals($expected, $actual);
     $equation = 'abs(-100)';
     $actual = Parser::parse($equation, $context);
     $expected = 100;
     $this->assertEquals($expected, $actual);
 }
<?php

// Composer autoloading
if (file_exists('vendor/autoload.php')) {
    $loader = (include 'vendor/autoload.php');
}
use RR\Shunt\Parser;
use RR\Shunt\Context;
// einfach
$trm = '3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3';
print Parser::parse($trm);
// 3.0001220703125
print "\n";
// mit eigenen konstanten und funktionen
$ctx = new Context();
$ctx->def('abs');
// wrapper
$ctx->def('foo', 5);
$ctx->def('bar', function ($a, $b) {
    return $a * $b;
});
$trm = '3 + bar(4, 2) / (abs(-1) - foo) ^ 2 ^ 3';
print Parser::parse($trm, $ctx);
// 3.0001220703125
 /**
  * @expectedException \Exception
  */
 public function testNonNumericConstantDefinition()
 {
     $context = new Context();
     $context->def('const', 'Just a String That Causes Error #$#$%#@');
 }
 /**
  * @expectedException RR\Shunt\Exception\ParseError
  */
 public function testParserExceptionSurplusClosingBracket()
 {
     $context = new Context();
     $context->def('pi');
     $equation = 'pi())';
     $actual = Parser::parse($equation, $context);
 }
Example #8
0
// Composer autoloading
if (file_exists('vendor/autoload.php')) {
    $loader = (include 'vendor/autoload.php');
}
use RR\Shunt\Parser;
use RR\Shunt\Context;
// einfach
$trm = '3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3';
print Parser::parse($trm) . "\n";
// 3.0001220703125
// mit eigenen konstanten und funktionen
$ctx = new Context();
$ctx->def('abs');
// wrapper
$ctx->def('foo', 5);
$ctx->def('bar', function ($a, $b) {
    return $a * $b;
});
$trm = '3 + bar(4, 2) / (abs(-1) - foo) ^ 2 ^ 3';
print Parser::parse($trm, $ctx) . "\n";
// 3.0001220703125
// mit string konstanten
$ctx = new Context();
$ctx->def('groupA', 'A', 'string');
// string constant
$ctx->def('isgroupA', function ($g) {
    return $g == 'A' ? 1 : 0;
});
$trm = 'isgroupA(groupA)';
print Parser::parse($trm, $ctx) . "\n";
// 1
 public function testNonNumericConstantDefinition()
 {
     $context = new Context();
     $context->def('const', 'string constant', 'string');
 }