77 lines
2.8 KiB
PHP
77 lines
2.8 KiB
PHP
<?php
|
||
/**
|
||
* @filename StringReaderTest.php
|
||
* @author Jerry Yan <792602257@qq.com>
|
||
* @date 2020/12/18 12:19
|
||
*/
|
||
|
||
namespace JerryYan\DSL\Test\Reader;
|
||
|
||
use JerryYan\DSL\Reader\StringReader;
|
||
use PHPUnit\Framework\TestCase;
|
||
|
||
class StringReaderTest extends TestCase
|
||
{
|
||
protected $readerWithCn;
|
||
protected $reader;
|
||
protected function setUp(): void
|
||
{
|
||
$this->reader = new StringReader(" Ahhh This Is 一个 新的 TOken");
|
||
$this->readerWithCn = new StringReader(" 中文 这是 Is 一个 新的 TOken");
|
||
}
|
||
|
||
public function testGetNextChar()
|
||
{
|
||
$this->reader->reset();
|
||
$this->assertEquals('A', $this->reader->getNextChar(), "不匹配");
|
||
$this->assertEquals(1, $this->reader->getCurrentPosition(), "CurPos与预计不符");
|
||
$this->readerWithCn->reset();
|
||
$this->assertEquals('中', $this->readerWithCn->getNextChar(), "不匹配");
|
||
$this->assertEquals(1, $this->readerWithCn->getCurrentPosition(), "CurPos与预计不符");
|
||
}
|
||
|
||
public function testGetCurrentToken()
|
||
{
|
||
$this->reader->reset();
|
||
$this->assertEquals('Ahhh', $this->reader->getCurrentToken(), "不匹配");
|
||
$this->assertEquals(6, $this->reader->getNextPosition(), "NextPos与预计不符");
|
||
$this->readerWithCn->reset();
|
||
$this->assertEquals('中文', $this->readerWithCn->getCurrentToken(), "不匹配");
|
||
$this->assertEquals(4, $this->readerWithCn->getNextPosition(), "NextPos与预计不符");
|
||
}
|
||
|
||
/**
|
||
* 移动至下一个Token
|
||
* @author Jerry Yan <792602257@qq.com>
|
||
* @date 2020/12/18 14:16
|
||
* @depends testGetNextChar
|
||
* @depends testGetCurrentToken
|
||
*/
|
||
public function testMoveToNextToken()
|
||
{
|
||
$this->reader->reset();
|
||
$this->reader->moveToNextToken();
|
||
$this->assertEquals('This', $this->reader->getCurrentToken(), "不匹配");
|
||
$this->assertEquals(7, $this->reader->getCurrentPosition(), "CurPos与预计不符");
|
||
$this->readerWithCn->reset();
|
||
$this->readerWithCn->moveToNextToken();
|
||
$this->assertEquals('这是', $this->readerWithCn->getCurrentToken(), "不匹配");
|
||
$this->assertEquals(5, $this->readerWithCn->getCurrentPosition(), "CurPos与预计不符");
|
||
}
|
||
|
||
/**
|
||
* 获取下一个Token,重复调用均为同一结果
|
||
* @author Jerry Yan <792602257@qq.com>
|
||
* @date 2020/12/18 18:49
|
||
*/
|
||
public function testGetNextToken()
|
||
{
|
||
$this->reader->reset();
|
||
$curPos = $this->readerWithCn->getCurrentPosition();
|
||
$string = $this->reader->getNextToken();
|
||
$this->assertEquals($string, $this->reader->getNextToken(), "不匹配");
|
||
$this->assertEquals($this->reader->getNextToken(), $this->reader->getNextToken(), "不匹配");
|
||
$this->assertEquals($curPos, $this->reader->getCurrentPosition(), "CurPos不可以发生变化");
|
||
}
|
||
}
|