public function testCallable() { $source = 'abc'; $expected = Console::open(['color' => 'red']) . 'abc' . Console::close() . Console::reset(); $token = $this->_factory->create('token', ['pos' => 0, 'length' => 3]); $iterator = new Result($source); $iterator->merge([$token, $token->getEnd()]); $mock = $this->getMock('stdClass', ['call']); $mock->expects($this->once())->method('call')->with($token)->willReturn(['color' => 'red']); $formatter = new CliFormatter(['token' => [$mock, 'call']]); $this->assertEquals($expected, $formatter->format($iterator)); }
public function testRender() { $source = 'abc + test'; $expected = <<<EXPECTED <span class="token">abc</span> <span class="operator">+</span> <span class="token second">test</span> EXPECTED; $first = $this->_factory->create('token', ['pos' => 0, 'length' => 3]); $operator = $this->_factory->create('operator', ['pos' => 4, 'length' => 1]); $second = $this->_factory->create('token.second', ['pos' => 6, 'length' => 4]); $iterator = new Result($source); $iterator->merge([$first, $first->getEnd(), $operator, $operator->getEnd(), $second, $second->getEnd()]); $formatter = new HtmlFormatter(); $this->assertEquals($expected, $formatter->format($iterator)); }
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()); }
public function testTokenSortingEndPrecedesStart() { $tokens = []; $tokens[0] = $token = $this->_factory->create('token.1', ['pos' => 2, 'length' => 0]); $tokens[1] = $token->getEnd(); $tokens[2] = $token = $this->_factory->create('token.3', ['pos' => 5, 'length' => 4]); $tokens[3] = $token->getEnd(); $tokens[4] = $token = $this->_factory->create('token.2', ['pos' => 2, 'length' => 3]); $tokens[5] = $token->getEnd(); $list = new UnprocessedTokens(); $list->add($tokens[0]); $list->add($tokens[2]); $list->add($tokens[4]); $list->sort(); $this->assertEquals([$tokens[0], $tokens[1], $tokens[4], $tokens[5], $tokens[2], $tokens[3]], array_values($list->toArray())); }
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); }
public function testSubNaming() { $factory = new TokenFactory(Token::class); $factory->setBase('token'); $this->assertEquals('token.name', $factory->create('$.name')->name); }