MyDSL/tests/Reader/StringReaderTest.php

146 lines
6.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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\ReaderInterface;
use JerryYan\DSL\Reader\StringReader;
use PHPUnit\Framework\TestCase;
class StringReaderTest extends TestCase
{
/** @var ReaderInterface[] */
protected $readerArray = [];
protected $things = [
[
'original' => " Ahhh This Is 一个 新的 TOken",
'tokens' => ["Ahhh", "This", "Is", "一个", "新的", "TOken"],
'nextTokens' => ["This", "Is", "一个", "新的", "TOken", ""],
'positions' => [1, 7, 12, 15, 18, 21],
'lines' => [1, 1, 1, 1, 1, 1],
'linePositions' => [1, 7, 12, 15, 18, 21],
'moveToNextLines' => [],
],
[
'original' => " 中文 \r\n\r 这是 \r Is A \n\n 一个 新的 TOken",
'tokens' => ["中文", "这是", "Is", "A", "一个", "新的", "TOken"],
'nextTokens' => ["这是", "Is", "A", "一个", "新的", "TOken", ""],
'positions' => [1, 8, 13, 16, 21, 24, 27],
'lines' => [1, 3, 4, 4, 6, 6, 6],
'linePositions' => [1, 1, 1, 4, 1, 4, 7],
'moveToNextLines' => [1, 2, 4],
]
];
protected function setUp(): void
{
foreach ($this->things as $thing) {
$this->readerArray[] = new StringReader($thing['original']);
}
}
public function testGetNextChar()
{
foreach ($this->things as $index=>$thing) {
$reader = $this->readerArray[$index];
$reader->reset();
$this->assertEquals(mb_substr(trim($thing['original']), 0, 1), $reader->getNextChar(), "不匹配");
$this->assertEquals($thing['positions'][0], $reader->getCurrentPosition(), "CurPos与预计不符");
}
}
public function testGetCurrentToken()
{
foreach ($this->things as $index=>$thing) {
$reader = $this->readerArray[$index];
$reader->reset();
$this->assertEquals($thing['positions'][0], $reader->getCurrentPosition(), "CurPos与预计不符");
$this->assertEquals($thing['tokens'][0], $reader->getCurrentToken(), "不匹配");
}
}
/**
* 移动至下一个Token
* @author Jerry Yan <792602257@qq.com>
* @date 2020/12/18 14:16
* @depends testGetNextChar
* @depends testGetCurrentToken
* @depends testGetNextToken
*/
public function testMoveToNextToken()
{
foreach ($this->things as $index=>$thing) {
$reader = $this->readerArray[$index];
$reader->reset();
foreach ($thing['nextTokens'] as $key=> $nextToken){
$oldCurToken = $reader->getCurrentToken();
$oldNextPos = $reader->getNextPosition();
$oldNextToken = $reader->getNextToken();
$this->assertEquals($nextToken, $oldNextToken, "不匹配");
$this->assertEquals($thing['positions'][$key], $reader->getCurrentPosition(), "CurPos与预计不符");
$this->assertEquals($thing['lines'][$key], $reader->getCurrentLine(), "CurLine与预计不符");
$this->assertEquals($thing['linePositions'][$key], $reader->getCurrentLinePosition(), "CLPos与预计不符");
$hasNext = $reader->moveToNextToken();
if ($hasNext) {
$this->assertNotEquals($oldCurToken, $reader->getCurrentToken(), "CurToken与旧CurToken相同");
$this->assertNotEquals($oldNextPos, $reader->getNextPosition(), "NextPos与旧NextPos相同");
$this->assertNotEquals($reader->getNextPosition(), $reader->getCurrentPosition(), "CurPos与NextPos相同");
}
}
}
}
/**
* 获取下一个Token重复调用均为同一结果
* @author Jerry Yan <792602257@qq.com>
* @date 2020/12/18 18:49
*/
public function testGetNextToken()
{
foreach ($this->things as $index=>$thing) {
$reader = $this->readerArray[$index];
$reader->reset();
$nextPos = $reader->getNextPosition();
$string = $reader->getNextToken();
$this->assertEquals($string, $reader->getNextToken(), "不匹配");
$this->assertEquals($reader->getNextToken(), $reader->getNextToken(), "不匹配");
$this->assertEquals($nextPos, $reader->getNextPosition(), "NextPos不可以发生变化");
}
}
public function testSkipCurrentLine()
{
foreach ($this->things as $index=>$thing) {
$reader = $this->readerArray[$index];
$reader->reset();
foreach ($thing['moveToNextLines'] as $key){
$reader->skipCurrentLine();
$this->assertEquals($thing['lines'][$key], $reader->getCurrentLine(), "行号不匹配");
$this->assertEquals($thing['linePositions'][$key], $reader->getCurrentLinePosition(), "CLPos不匹配");
$this->assertEquals($thing['tokens'][$key], $reader->getCurrentToken(), "Token不匹配");
$this->assertEquals($thing['positions'][$key], $reader->getCurrentPosition(), "CurPos不匹配");
$this->assertEquals($thing['nextTokens'][$key], $reader->getNextToken(), "NextToken不匹配");
}
}
}
public function testResetCursor()
{
foreach ($this->things as $index=>$thing) {
$reader = $this->readerArray[$index];
$reader->moveToNextToken();
$curPos = $reader->getCurrentPosition();
$nextPos = $reader->getNextPosition();
$string = $reader->getCurrentToken();
$reader->resetCursor();
$this->assertNotEquals($curPos, $reader->getCurrentPosition(), "CurPos未发生变化");
$this->assertNotEquals($nextPos, $reader->getNextPosition(), "NextPos未发生变化");
$this->assertNotEquals($string, $reader->getCurrentToken(), "CurToken未发生变化");
}
}
}