コード例 #1
0
 /**
  * Test the functionality of the assertion syntax matcher
  */
 public function testMatchAssertion()
 {
     // Case 1: Correct assertion
     $assertionTokenStream = [['token' => 'T_IDENTIFIER', 'match' => 'testIdentifier'], ['token' => 'T_OP_EQ'], ['token' => 'T_LITERAL', "match" => '"Some Literal"']];
     $parser = new Parser();
     $parser->setTokenIndex(-1);
     $parser->setTokenStream($assertionTokenStream);
     $result = $parser->matchAssertion();
     $this->assertTrue($result instanceof ASTAssertion, "Result of matchAssertion should be a ASTAssertion");
     $this->assertEquals("testIdentifier", $result->getIdentifier());
     $this->assertEquals('"Some Literal"', $result->getValue());
     // Case 2: Not an assertion (first element not a literal)
     $nonAssertionTokenStream = [['token' => 'T_LOGIC_AND']];
     $parser = new Parser();
     $parser->setTokenIndex(-1);
     $parser->setTokenStream($nonAssertionTokenStream);
     $result = $parser->matchAssertion();
     $this->assertFalse($result, "Result for non-matching assertion should be false");
     $invalidAssertionTokenStream = [['token' => 'T_IDENTIFIER', 'match' => 'valid'], ['token' => 'T_IDENTIFIER', 'match' => 'invalid']];
     // Case 3: Wrongly formatted assertion
     $parser = new Parser();
     $parser->setTokenIndex(-1);
     $parser->setTokenStream($invalidAssertionTokenStream);
     try {
         $parser->matchAssertion();
         $this->fail("Matching invalid assertion token stream should raise UQLSyntaxError");
     } catch (UQLSyntaxError $e) {
         // Caught the exception. Pass the test.
     }
     $invalidAssertionTokenStream = [['token' => 'T_IDENTIFIER', 'match' => 'valid'], ['token' => 'T_OP_GTE'], ['token' => 'T_LOGIC_AND', 'match' => 'invalid']];
     $parser = new Parser();
     $parser->setTokenIndex(-1);
     $parser->setTokenStream($invalidAssertionTokenStream);
     try {
         $parser->matchAssertion();
         $this->fail("Matching invalid assertion token stream should raise UQLSyntaxError");
     } catch (UQLSyntaxError $e) {
         // Caught the exception. Pass the test.
     }
 }