Esempio n. 1
0
 /**
  * @expectedException \Tbm\Peval\EvaluationException
  */
 public function testInvalidBooleanExpressionException()
 {
     /*
      * These tests involve invalid expressions.
      */
     $evaluator = new Evaluator();
     $evaluator->evaluate1(new String("3 3 < 3"));
     $evaluator->evaluate1(new String("3 << 3"));
     $evaluator->evaluate1(new String("3 <> 4"));
     $evaluator->evaluate1(new String("!!(3 <> 4)"));
     $evaluator->evaluate1(new String("3 = 4"));
     $evaluator->evaluate1(new String("2 < 3 && 1 = 1"));
     $evaluator->evaluate1(new String("(3) (3 < 3)"));
     $evaluator->evaluate1(new String("3 (<<) 3"));
     $evaluator->evaluate1(new String("(2 < 3) && (1 = 1"));
 }
Esempio n. 2
0
 /**
  * @expectedException Tbm\Peval\EvaluationException
  */
 public function testInvalidMathExpressionException()
 {
     /*
      * These tests involve invalid expressions.
      */
     $evaluator = new Evaluator();
     $evaluator->evaluate1(new String("-"));
     $evaluator->evaluate1(new String("4 + "));
     $evaluator->evaluate1(new String("4 - "));
     $evaluator->evaluate1(new String("4 + -"));
     $evaluator->evaluate1(new String("--4"));
     $evaluator->evaluate1(new String("4 * / 3"));
     $evaluator->evaluate1(new String("* 3"));
     $evaluator->evaluate1(new String("((4"));
     $evaluator->evaluate1(new String("4 ("));
     $evaluator->evaluate1(new String("(4))"));
     $evaluator->evaluate1(new String("((4 + 3)) * 2)"));
     $evaluator->evaluate1(new String("4 ()"));
     $evaluator->evaluate1(new String("4 (+) 3"));
 }
Esempio n. 3
0
 public function testCharAtFunction()
 {
     $evaluator = new Evaluator();
     $this->assertEquals("'s'", $evaluator->evaluate1(new String("charAt('test', 2)")));
     $this->assertEquals("'A'", $evaluator->evaluate1(new String("'A'")));
 }
Esempio n. 4
0
 /**
  * Test expressions containing string operations.
  */
 public function testEvaluateStringOperations()
 {
     $evaluator = new Evaluator();
     try {
         /*
          * These tests involve valid expressions.
          */
         //$this->assertEquals("'A'", $evaluator->evaluate1(new String("'A'")));
         $this->assertEquals("'AC'", $evaluator->evaluate1(new String("'A' + 'C'")));
         $this->assertEquals("'A + C'", $evaluator->evaluate1(new String("'A + C'")));
         $this->assertEquals("'ABC'", $evaluator->evaluate1(new String("'AB' + 'C'")));
         $this->assertEquals(EvaluationConstants . BOOLEAN_STRING_TRUE, $evaluator->evaluate1(new String("'A' < 'C'")));
         $this->assertEquals(EvaluationConstants . BOOLEAN_STRING_FALSE, $evaluator->evaluate1(new String("'C' < 'A'")));
         $this->assertEquals(EvaluationConstants . BOOLEAN_STRING_TRUE, $evaluator->evaluate1(new String("'C' < 'F'")));
         $this->assertEquals(EvaluationConstants . BOOLEAN_STRING_TRUE, $evaluator->evaluate1(new String("'C' < 'c'")));
         $this->assertEquals(EvaluationConstants . BOOLEAN_STRING_FALSE, $evaluator->evaluate1(new String("'c' < 'C'")));
         $this->assertEquals(EvaluationConstants . BOOLEAN_STRING_FALSE, $evaluator->evaluate1(new String("'A' > 'C'")));
         $this->assertEquals(EvaluationConstants . BOOLEAN_STRING_TRUE, $evaluator->evaluate1(new String("'C' > 'A'")));
         $this->assertEquals(EvaluationConstants . BOOLEAN_STRING_FALSE, $evaluator->evaluate1(new String("'C' > 'F'")));
         $this->assertEquals(EvaluationConstants . BOOLEAN_STRING_TRUE, $evaluator->evaluate1(new String("'A' <= 'A'")));
         $this->assertEquals(EvaluationConstants . BOOLEAN_STRING_FALSE, $evaluator->evaluate1(new String("'C' <= 'A'")));
         $this->assertEquals(EvaluationConstants . BOOLEAN_STRING_TRUE, $evaluator->evaluate1(new String("'C' <= 'F'")));
         $this->assertEquals(EvaluationConstants . BOOLEAN_STRING_TRUE, $evaluator->evaluate1(new String("'A' >= 'A'")));
         $this->assertEquals(EvaluationConstants . BOOLEAN_STRING_TRUE, $evaluator->evaluate1(new String("'C' >= 'A'")));
         $this->assertEquals(EvaluationConstants . BOOLEAN_STRING_FALSE, $evaluator->evaluate1(new String("'C' >= 'F'")));
         $this->assertEquals("'A'", $evaluator->evaluate1(new String("('A')")));
         $this->assertEquals("'ABC'", $evaluator->evaluate1(new String("('AB' + 'C')")));
         $this->assertEquals("'123ABC'", $evaluator->evaluate1(new String("'123' + ('AB' + 'C')")));
         $this->assertEquals(EvaluationConstants . BOOLEAN_STRING_TRUE, $evaluator->evaluate1(new String("(('C' >= 'A'))")));
     } catch (Exception $e) {
         throw new Exception($e);
     }
 }
Esempio n. 5
0
 /**
  * This method process nested function calls that may be in the arguments
  * passed into a higher level function.
  *
  * @param String|\Tbm\Peval\Types\String $arguments
  *
  * @throws \Tbm\Peval\EvaluationException
  * @return \Tbm\Peval\Types\String The arguments with any nested function calls evaluated.
  *
  */
 public function processNestedFunctions(string $arguments)
 {
     $evaluatedArguments = new String();
     // Process nested function calls.
     if ($arguments->length() > 0) {
         $argumentsEvaluator = new Evaluator($this->quoteCharacter, $this->loadMathVariables, $this->loadMathFunctions, $this->loadStringFunctions, $this->processNestedFunctions);
         $this->argumentsEvaluator->setFunctions(getFunctions());
         $this->argumentsEvaluator->setVariables(getVariables());
         $this->argumentsEvaluator->setVariableResolver(getVariableResolver());
         $tokenizer = new ArgumentTokenizer($arguments, EvaluationConstants::FUNCTION_ARGUMENT_SEPARATOR);
         $evaluatedArgumentList = new ArrayList();
         while ($tokenizer . hasMoreTokens()) {
             $argument = $tokenizer->nextToken()->trim();
             try {
                 $argument = $argumentsEvaluator->evaluate1($argument);
             } catch (Exception $e) {
                 throw new EvaluationException($e->getMessage(), $e);
             }
             $evaluatedArgumentList->add($argument);
         }
         $evaluatedArgumentIterator = $evaluatedArgumentList->iterator();
         while ($evaluatedArgumentIterator->valid()) {
             if ($evaluatedArguments->length() > 0) {
                 $evaluatedArguments->append(EvaluationConstants::FUNCTION_ARGUMENT_SEPARATOR);
             }
             $evaluatedArgumentIterator->next();
             $evaluatedArgument = (string) $evaluatedArgumentIterator->current();
             $evaluatedArguments . append($evaluatedArgument);
         }
     }
     return $evaluatedArguments;
 }