示例#1
0
 protected function validate(Context $context)
 {
     if ($context->language !== $this->rule->language) {
         $this->setValid(false);
         return false;
     }
     if (!$context->has($this->name)) {
         if (!$this->rule->validator->validate($context)) {
             $this->setValid(false);
         } else {
             $this->_valid = true;
             $this->_end->_valid = false;
         }
     } else {
         if (!$this->rule->validator->validate($context, [$this->name => Validator::CONTEXT_IN])) {
             $this->setValid(false);
         } else {
             $this->_valid = false;
             $this->_end->_valid = true;
         }
     }
     $this->_end->_start = false;
     $this->_end = false;
     return true;
 }
示例#2
0
 public function testFind()
 {
     $token = $this->getTokenMock('token.name');
     $context = new Context();
     $context->push($token);
     $this->assertSame($token->id, $context->find('token.name'));
     $this->assertFalse($context->find('foo'));
 }
 public function testValidation()
 {
     $start = $this->_factory->create('test', ['pos' => 10, 'length' => 1, 'rule' => $this->_rule]);
     $startEnd = $start->getEnd();
     $endStart = $this->_factory->create('test', ['pos' => 12, 'length' => 1, 'rule' => $this->_rule]);
     $end = $endStart->getEnd();
     $this->assertTrue($start->isValid(Context::fromArray([], $this->_language)));
     $this->assertFalse($startEnd->isValid(Context::fromArray(['test'], $this->_language)));
     $this->assertFalse($endStart->isValid(Context::fromArray(['test'], $this->_language)));
     $this->assertTrue($end->isValid(Context::fromArray(['test'], $this->_language)));
     $this->assertTrue($start->isStart());
     $this->assertTrue($endStart->isStart());
     $this->assertTrue($end->isEnd());
     $this->assertTrue($startEnd->isEnd());
 }
示例#4
0
 public function testTokenValidation()
 {
     /** @var Language $language */
     $language = $this->getMockBuilder('Kadet\\Highlighter\\Language\\Language')->disableOriginalConstructor()->getMock();
     $validator = $this->getMock(Validator::class);
     $context = Context::fromArray([], $language);
     $validator->expects($this->once())->method('validate')->with($context, []);
     $token = $this->_factory->create('test.name', ['pos' => 15, 'length' => 10, 'rule' => new Rule(null, ['language' => $language, 'context' => $validator])]);
     $token->isValid($context);
 }
示例#5
0
 protected function processStart(Context $context, Language $language, Result $result, TokenIterator $tokens)
 {
     $context->push($this);
     return true;
 }
示例#6
0
 public function testCallableValidator()
 {
     $validator = new DelegateValidator(function ($context) {
         return in_array('bar', $context->stack) && !in_array('foo', $context->stack);
     });
     $this->assertFalse($validator->validate(Context::fromArray(['test'])));
     $this->assertTrue($validator->validate(Context::fromArray(['bar'])));
     $this->assertTrue($validator->validate(Context::fromArray(['bar', 'smth'])));
     $this->assertFalse($validator->validate(Context::fromArray(['bar', 'foo'])));
 }
示例#7
0
 protected function processEnd(Context $context, Language $language, Result $result, TokenIterator $tokens)
 {
     if ($this->_start) {
         $context->pop($this->_start);
     } else {
         if (($start = $context->find($this->name)) !== false) {
             $this->setStart($tokens[$start]);
             unset($context->stack[$start]);
         }
     }
     if (!$this->_start instanceof MetaToken) {
         $result->append($this);
     }
     return true;
 }