* @date 2021/1/22 13:52 */ namespace JerryYan\DSL\Test\Token; use JerryYan\DSL\Token\TokenLogicAnd; use JerryYan\DSL\Token\TokenLogicEqual; use JerryYan\DSL\Token\TokenUseVariable; use JerryYan\DSL\Token\TokenInterface; use JerryYan\DSL\Token\TokenLogicOr; use JerryYan\DSL\Token\TokenUndefined; use JerryYan\DSL\Token\TokenVariable; use PHPUnit\Framework\TestCase; class TokenInterfaceTest extends TestCase { /** @var class-string[] TokenClass */ private $tokenTypes = [ TokenLogicAnd::class, TokenLogicEqual::class, TokenUseVariable::class, TokenLogicOr::class, TokenUndefined::class, TokenVariable::class, ]; protected $chainFirst; protected $chainLast; protected function setUp(): void { /** @var ?TokenInterface $last */ $last = NULL; foreach ($this->tokenTypes as $cls) { /** @var TokenInterface $current */ $current = new $cls(''); $current->setPrevToken($last); if ($last !== NULL) $last->setNextToken($current); $last = $current; } $this->chainLast = $last; $this->chainFirst = $last->getFirstToken(); } public function testGetFirstToken() { $this->assertInstanceOf($this->tokenTypes[0], $this->chainLast->getFirstToken(), '链表头类型非预期'); } public function testGetPrevToken() { $this->assertInstanceOf($this->tokenTypes[count($this->tokenTypes)-2], $this->chainLast->getPrevToken(), '链表尾前一类型非预期'); $this->assertNull($this->chainFirst->getPrevToken(), '链表头前一类型非空'); } public function testCount() { $this->assertEquals($this->chainFirst->count(), $this->chainLast->count(), '链表头尾计数不同'); $this->assertEquals(count($this->tokenTypes), $this->chainLast->count(), '链表尾的长度不同'); } public function testGetNextToken() { $this->assertInstanceOf($this->tokenTypes[1], $this->chainFirst->getNextToken(), '链表头后一类型非预期'); $this->assertNull($this->chainLast->getNextToken(), '链表尾前一类型非空'); } public function testGetLastToken() { $this->assertInstanceOf($this->tokenTypes[count($this->tokenTypes)-1], $this->chainFirst->getLastToken(), '链表尾类型非预期'); } public function testHasPrevToken() { $this->assertTrue($this->chainLast->hasPrevToken()); $this->assertFalse($this->chainFirst->hasPrevToken()); } public function testHasNextToken() { $this->assertTrue($this->chainFirst->hasNextToken()); $this->assertFalse($this->chainLast->hasNextToken()); } public function testGetNextTokenLength() { $this->assertEquals(count($this->tokenTypes) - 2, $this->chainFirst->getNextToken()->getNextTokenLength(), '链表头后一后长度不对'); $this->assertEquals(0, $this->chainLast->getNextTokenLength(), '链表尾后长度不对'); } public function testGetPrevTokenLength() { $this->assertEquals(count($this->tokenTypes) - 2, $this->chainLast->getPrevToken()->getPrevTokenLength(), '链表尾前一前长度不对'); $this->assertEquals(0, $this->chainFirst->getPrevTokenLength(), '链表头前长度不对'); } }