* @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 $readerWithCrLf; protected $reader; protected $things; protected $thingsWithCrLf; protected function setUp(): void { $this->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' => [], ]; $this->thingsWithCrLf = [ '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], ]; $this->reader = new StringReader($this->things['original']); $this->readerWithCrLf = new StringReader($this->thingsWithCrLf['original']); } public function testGetNextChar() { $this->reader->reset(); $this->assertEquals(mb_substr(trim($this->things['original']), 0, 1), $this->reader->getNextChar(), "不匹配"); $this->assertEquals($this->things['positions'][0], $this->reader->getCurrentPosition(), "CurPos与预计不符"); $this->readerWithCrLf->reset(); $this->assertEquals(mb_substr(trim($this->thingsWithCrLf['original']), 0, 1), $this->readerWithCrLf->getNextChar(), "不匹配"); $this->assertEquals($this->thingsWithCrLf['positions'][0], $this->readerWithCrLf->getCurrentPosition(), "CurPos与预计不符"); } public function testGetCurrentToken() { $this->reader->reset(); $this->assertEquals($this->things['positions'][0], $this->reader->getCurrentPosition(), "CurPos与预计不符"); $this->assertEquals($this->things['tokens'][0], $this->reader->getCurrentToken(), "不匹配"); $this->readerWithCrLf->reset(); $this->assertEquals($this->thingsWithCrLf['positions'][0], $this->readerWithCrLf->getCurrentPosition(), "CurPos与预计不符"); $this->assertEquals($this->thingsWithCrLf['tokens'][0], $this->readerWithCrLf->getCurrentToken(), "不匹配"); } /** * 移动至下一个Token * @author Jerry Yan <792602257@qq.com> * @date 2020/12/18 14:16 * @depends testGetNextChar * @depends testGetCurrentToken * @depends testGetNextToken */ public function testMoveToNextToken() { $this->reader->reset(); foreach ($this->things['nextTokens'] as $key=> $nextToken){ $oldCurToken = $this->reader->getCurrentToken(); $oldNextPos = $this->reader->getNextPosition(); $oldNextToken = $this->reader->getNextToken(); $this->assertEquals($nextToken, $oldNextToken, "不匹配"); $this->assertEquals($this->things['positions'][$key], $this->reader->getCurrentPosition(), "CurPos与预计不符"); $this->assertEquals($this->things['lines'][$key], $this->reader->getCurrentLine(), "CurLine与预计不符"); $this->assertEquals($this->things['linePositions'][$key], $this->reader->getCurrentLinePosition(), "CLPos与预计不符"); $hasNext = $this->reader->moveToNextToken(); if ($hasNext) { $this->assertNotEquals($oldCurToken, $this->reader->getCurrentToken(), "CurToken与旧CurToken相同"); $this->assertNotEquals($oldNextPos, $this->reader->getNextPosition(), "NextPos与旧NextPos相同"); $this->assertNotEquals($this->reader->getNextPosition(), $this->reader->getCurrentPosition(), "CurPos与NextPos相同"); } } // Cr/LF Support $this->readerWithCrLf->reset(); foreach ($this->thingsWithCrLf['nextTokens'] as $key=> $nextToken){ $oldCurToken = $this->readerWithCrLf->getCurrentToken(); $oldNextPos = $this->readerWithCrLf->getNextPosition(); $oldNextToken = $this->readerWithCrLf->getNextToken(); $this->assertEquals($nextToken, $oldNextToken, "不匹配"); $this->assertEquals($this->thingsWithCrLf['positions'][$key], $this->readerWithCrLf->getCurrentPosition(), "CurPos与预计不符"); $this->assertEquals($this->thingsWithCrLf['lines'][$key], $this->readerWithCrLf->getCurrentLine(), "CurLine与预计不符"); $this->assertEquals($this->thingsWithCrLf['linePositions'][$key], $this->readerWithCrLf->getCurrentLinePosition(), "CLPos与预计不符"); $hasNext = $this->readerWithCrLf->moveToNextToken(); if ($hasNext) { $this->assertNotEquals($oldCurToken, $this->readerWithCrLf->getCurrentToken(), "CurToken与旧CurToken相同"); $this->assertNotEquals($oldNextPos, $this->readerWithCrLf->getNextPosition(), "NextPos与旧NextPos相同"); $this->assertNotEquals($this->readerWithCrLf->getNextPosition(), $this->readerWithCrLf->getCurrentPosition(), "CurPos与NextPos相同"); } } } /** * 获取下一个Token,重复调用均为同一结果 * @author Jerry Yan <792602257@qq.com> * @date 2020/12/18 18:49 */ public function testGetNextToken() { $this->reader->reset(); $nextPos = $this->reader->getNextPosition(); $string = $this->reader->getNextToken(); $this->assertEquals($string, $this->reader->getNextToken(), "不匹配"); $this->assertEquals($this->reader->getNextToken(), $this->reader->getNextToken(), "不匹配"); $this->assertEquals($nextPos, $this->reader->getNextPosition(), "NextPos不可以发生变化"); } public function testSkipCurrentLine() { $this->readerWithCrLf->resetCursor(); foreach ($this->thingsWithCrLf['moveToNextLines'] as $key){ $this->readerWithCrLf->skipCurrentLine(); $this->assertEquals($this->thingsWithCrLf['lines'][$key], $this->readerWithCrLf->getCurrentLine(), "行号不匹配"); $this->assertEquals($this->thingsWithCrLf['linePositions'][$key], $this->readerWithCrLf->getCurrentLinePosition(), "CLPos不匹配"); $this->assertEquals($this->thingsWithCrLf['tokens'][$key], $this->readerWithCrLf->getCurrentToken(), "Token不匹配"); $this->assertEquals($this->thingsWithCrLf['positions'][$key], $this->readerWithCrLf->getCurrentPosition(), "CurPos不匹配"); $this->assertEquals($this->thingsWithCrLf['nextTokens'][$key], $this->readerWithCrLf->getNextToken(), "NextToken不匹配"); } } public function testResetCursor() { $this->readerWithCrLf->moveToNextToken(); $curPos = $this->readerWithCrLf->getCurrentPosition(); $nextPos = $this->readerWithCrLf->getNextPosition(); $curLine = $this->readerWithCrLf->getCurrentLine(); $string = $this->readerWithCrLf->getCurrentToken(); $this->readerWithCrLf->resetCursor(); $this->assertNotEquals($curPos, $this->readerWithCrLf->getCurrentPosition(), "CurPos未发生变化"); $this->assertNotEquals($nextPos, $this->readerWithCrLf->getNextPosition(), "NextPos未发生变化"); $this->assertNotEquals($curLine, $this->readerWithCrLf->getCurrentLine(), "CurLine未发生变化"); $this->assertNotEquals($string, $this->readerWithCrLf->getCurrentToken(), "CurToken未发生变化"); } }