优化测试,文件读取测试

This commit is contained in:
Jerry Yan 2021-01-26 14:51:28 +08:00
parent 3adf4811c0
commit 31222da326
13 changed files with 257 additions and 89 deletions

View File

@ -0,0 +1,17 @@
<?php
/**
* @filename FileNotFoundException.php
* @author Jerry Yan <792602257@qq.com>
* @date 2021/1/26 13:52
*/
namespace JerryYan\DSL\Exception;
use Exception;
class FileNotFoundException extends Exception
{
}

View File

@ -9,10 +9,22 @@
namespace JerryYan\DSL\Reader; namespace JerryYan\DSL\Reader;
class FileReader /** extends ReaderInterface */ use JerryYan\DSL\Exception\FileNotFoundException;
class FileReader extends StringReader
{ {
/**
* FileReader constructor.
* @param string $fileName
* @throws FileNotFoundException
* @author Jerry Yan <792602257@qq.com>
* @date 2021/1/26 13:52
*/
public function __construct(string $fileName) public function __construct(string $fileName)
{ {
if (!file_exists($fileName)) throw new FileNotFoundException;
$content = file_get_contents($fileName);
parent::__construct($content);
} }
} }

View File

@ -133,6 +133,17 @@ abstract class ReaderInterface
$this->currentPosition += $charLength; $this->currentPosition += $charLength;
} }
/**
* 移动Cursor至上一字符
* @param int $charLength
* @author Jerry Yan <792602257@qq.com>
* @date 2020/12/19 15:05
*/
protected function moveCursorToPrevChar(int $charLength = 1): void
{
$this->currentPosition -= $charLength;
}
/** /**
* 移动Cursor至下一行 * 移动Cursor至下一行
* @param int|null $newPos 下一行位置,不提供则手动检测 * @param int|null $newPos 下一行位置,不提供则手动检测

View File

@ -79,18 +79,16 @@ class StringReader extends ReaderInterface
// 否则就结束(已经匹配完成) // 否则就结束(已经匹配完成)
break 2; break 2;
case "\r": case "\r":
if ($this->getNextChar($this->nextPosition) === "\n") {
// CRLF换行
if (empty($curToken)) { if (empty($curToken)) {
$this->moveCursorToNextChar(); // CRLF换行
} if ($this->getNextChar($this->nextPosition) === "\n") {
$this->nextPosition++; $this->nextPosition++;
} }
// CR换行
if (empty($curToken)) {
$this->moveCursorToNextLine(); $this->moveCursorToNextLine();
continue 2; continue 2;
} else { } else {
// 重置游标
$this->nextPosition--;
break 2; break 2;
} }
case "\n": case "\n":
@ -99,6 +97,7 @@ class StringReader extends ReaderInterface
$this->moveCursorToNextLine(); $this->moveCursorToNextLine();
continue 2; continue 2;
} else { } else {
$this->nextPosition--;
break 2; break 2;
} }
default: default:

View File

@ -16,6 +16,9 @@ use JerryYan\DSL\Token\TokenFake;
use JerryYan\DSL\Token\TokenLogicAnd; use JerryYan\DSL\Token\TokenLogicAnd;
use JerryYan\DSL\Token\TokenLogicEqual; use JerryYan\DSL\Token\TokenLogicEqual;
use JerryYan\DSL\Token\TokenLogicFake; use JerryYan\DSL\Token\TokenLogicFake;
use JerryYan\DSL\Token\TokenLogicGreater;
use JerryYan\DSL\Token\TokenLogicLess;
use JerryYan\DSL\Token\TokenLogicNot;
use JerryYan\DSL\Token\TokenLogicNotEqual; use JerryYan\DSL\Token\TokenLogicNotEqual;
use JerryYan\DSL\Token\TokenLogicOr; use JerryYan\DSL\Token\TokenLogicOr;
use JerryYan\DSL\Token\TokenNumber; use JerryYan\DSL\Token\TokenNumber;
@ -29,8 +32,11 @@ class DefaultFactory extends FactoryInterface
Token::CURRY => TokenCurry::class, Token::CURRY => TokenCurry::class,
Token::LOGIC_AND => TokenLogicAnd::class, Token::LOGIC_AND => TokenLogicAnd::class,
Token::LOGIC_OR => TokenLogicOr::class, Token::LOGIC_OR => TokenLogicOr::class,
Token::LOGIC_NOT => TokenLogicNot::class,
Token::LOGIC_EQUAL => TokenLogicEqual::class, Token::LOGIC_EQUAL => TokenLogicEqual::class,
Token::LOGIC_NOT_EQUAL => TokenLogicNotEqual::class, Token::LOGIC_NOT_EQUAL => TokenLogicNotEqual::class,
Token::LOGIC_GREATER => TokenLogicGreater::class,
Token::LOGIC_LESS => TokenLogicLess::class,
Token::LOGIC_FAKE => TokenLogicFake::class, Token::LOGIC_FAKE => TokenLogicFake::class,
Token::VARIABLE => TokenVariable::class, Token::VARIABLE => TokenVariable::class,
Token::NUMBER => TokenNumber::class, Token::NUMBER => TokenNumber::class,

View File

@ -0,0 +1,17 @@
<?php
/**
* @filename TokenComment.php
* @author Jerry Yan <792602257@qq.com>
* @date 2021/1/26 13:43
*/
namespace JerryYan\DSL\Token;
class TokenComment extends TokenInterface
{
public static $alias = [
'//'
];
}

View File

@ -0,0 +1,17 @@
<?php
/**
* @filename TokenCommentBlock.php
* @author Jerry Yan <792602257@qq.com>
* @date 2021/1/26 13:44
*/
namespace JerryYan\DSL\Token;
class TokenCommentBlock extends TokenInterface
{
public static $alias = [
'/*', '*/'
];
}

View File

@ -0,0 +1,15 @@
<?php
/**
* @filename TokenLogicGreater.php
* @author Jerry Yan <792602257@qq.com>
* @date 2021/1/26 13:48
*/
namespace JerryYan\DSL\Token;
class TokenLogicGreater extends TokenInterface
{
}

View File

@ -0,0 +1,15 @@
<?php
/**
* @filename TokenLogicLess.php
* @author Jerry Yan <792602257@qq.com>
* @date 2021/1/26 13:48
*/
namespace JerryYan\DSL\Token;
class TokenLogicLess extends TokenInterface
{
}

View File

@ -0,0 +1,17 @@
<?php
/**
* @filename TokenLogicNot.php
* @author Jerry Yan <792602257@qq.com>
* @date 2021/1/26 13:46
*/
namespace JerryYan\DSL\Token;
class TokenLogicNot extends TokenInterface
{
public static $alias = [
'不成立'
];
}

View File

@ -0,0 +1,45 @@
<?php
/**
* @filename FileReaderTest.php
* @author Jerry Yan <792602257@qq.com>
* @date 2021/1/26 13:52
*/
namespace JerryYan\DSL\Test\Reader;
use JerryYan\DSL\Exception\FileNotFoundException;
use JerryYan\DSL\Reader\FileReader;
use JerryYan\DSL\Reader\ReaderInterface;
class FileReaderTest extends StringReaderTest
{
protected $files = [
__DIR__ . DIRECTORY_SEPARATOR . 'test.jdsl'
];
/** @var ReaderInterface[] */
protected $readerArray = [];
protected $things = [
[
'original' => "当 这个 等于 那个 的时候 则\r\n对 用户表 中的 该用户 更新 字段 状态 为 禁用",
'tokens' => ["", "这个", "等于", "那个", "的时候", "", "", "用户表", "中的", "该用户", "更新", "字段", "状态", "", "禁用"],
'nextTokens' => ["这个", "等于", "那个", "的时候", "", "", "用户表", "中的", "该用户", "更新", "字段", "状态", "", "禁用", ""],
'positions' => [0, 2, 5, 8, 11, 15, 18, 20, 24, 27, 31, 34, 37, 40, 42, 45, 47],
'lines' => [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
'linePositions' => [0, 2, 5, 8, 11, 15, 0, 2, 6, 9, 13, 16, 19, 22, 24, 27, 29],
'moveToNextLines' => [6],
],
];
protected function setUp(): void
{
foreach ($this->files as $file) {
$this->readerArray[] = new FileReader($file);
}
}
public function testFileNotFoundException()
{
$this->expectException(FileNotFoundException::class);
new FileReader('Not Exist File');
}
}

View File

@ -7,19 +7,16 @@
namespace JerryYan\DSL\Test\Reader; namespace JerryYan\DSL\Test\Reader;
use JerryYan\DSL\Reader\ReaderInterface;
use JerryYan\DSL\Reader\StringReader; use JerryYan\DSL\Reader\StringReader;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
class StringReaderTest extends TestCase class StringReaderTest extends TestCase
{ {
protected $readerWithCrLf; /** @var ReaderInterface[] */
protected $reader; protected $readerArray = [];
protected $things; protected $things = [
protected $thingsWithCrLf; [
protected function setUp(): void
{
$this->things = [
'original' => " Ahhh This Is 一个 新的 TOken", 'original' => " Ahhh This Is 一个 新的 TOken",
'tokens' => ["Ahhh", "This", "Is", "一个", "新的", "TOken"], 'tokens' => ["Ahhh", "This", "Is", "一个", "新的", "TOken"],
'nextTokens' => ["This", "Is", "一个", "新的", "TOken", ""], 'nextTokens' => ["This", "Is", "一个", "新的", "TOken", ""],
@ -27,8 +24,8 @@ class StringReaderTest extends TestCase
'lines' => [1, 1, 1, 1, 1, 1], 'lines' => [1, 1, 1, 1, 1, 1],
'linePositions' => [1, 7, 12, 15, 18, 21], 'linePositions' => [1, 7, 12, 15, 18, 21],
'moveToNextLines' => [], 'moveToNextLines' => [],
]; ],
$this->thingsWithCrLf = [ [
'original' => " 中文 \r\n\r 这是 \r Is A \n\n 一个 新的 TOken", 'original' => " 中文 \r\n\r 这是 \r Is A \n\n 一个 新的 TOken",
'tokens' => ["中文", "这是", "Is", "A", "一个", "新的", "TOken"], 'tokens' => ["中文", "这是", "Is", "A", "一个", "新的", "TOken"],
'nextTokens' => ["这是", "Is", "A", "一个", "新的", "TOken", ""], 'nextTokens' => ["这是", "Is", "A", "一个", "新的", "TOken", ""],
@ -36,29 +33,34 @@ class StringReaderTest extends TestCase
'lines' => [1, 3, 4, 4, 6, 6, 6], 'lines' => [1, 3, 4, 4, 6, 6, 6],
'linePositions' => [1, 1, 1, 4, 1, 4, 7], 'linePositions' => [1, 1, 1, 4, 1, 4, 7],
'moveToNextLines' => [1, 2, 4], 'moveToNextLines' => [1, 2, 4],
]
]; ];
$this->reader = new StringReader($this->things['original']);
$this->readerWithCrLf = new StringReader($this->thingsWithCrLf['original']); protected function setUp(): void
{
foreach ($this->things as $thing) {
$this->readerArray[] = new StringReader($thing['original']);
}
} }
public function testGetNextChar() public function testGetNextChar()
{ {
$this->reader->reset(); foreach ($this->things as $index=>$thing) {
$this->assertEquals(mb_substr(trim($this->things['original']), 0, 1), $this->reader->getNextChar(), "不匹配"); $reader = $this->readerArray[$index];
$this->assertEquals($this->things['positions'][0], $this->reader->getCurrentPosition(), "CurPos与预计不符"); $reader->reset();
$this->readerWithCrLf->reset(); $this->assertEquals(mb_substr(trim($thing['original']), 0, 1), $reader->getNextChar(), "不匹配");
$this->assertEquals(mb_substr(trim($this->thingsWithCrLf['original']), 0, 1), $this->readerWithCrLf->getNextChar(), "不匹配"); $this->assertEquals($thing['positions'][0], $reader->getCurrentPosition(), "CurPos与预计不符");
$this->assertEquals($this->thingsWithCrLf['positions'][0], $this->readerWithCrLf->getCurrentPosition(), "CurPos与预计不符"); }
} }
public function testGetCurrentToken() public function testGetCurrentToken()
{ {
$this->reader->reset(); foreach ($this->things as $index=>$thing) {
$this->assertEquals($this->things['positions'][0], $this->reader->getCurrentPosition(), "CurPos与预计不符"); $reader = $this->readerArray[$index];
$this->assertEquals($this->things['tokens'][0], $this->reader->getCurrentToken(), "不匹配"); $reader->reset();
$this->readerWithCrLf->reset(); $this->assertEquals($thing['positions'][0], $reader->getCurrentPosition(), "CurPos与预计不符");
$this->assertEquals($this->thingsWithCrLf['positions'][0], $this->readerWithCrLf->getCurrentPosition(), "CurPos与预计不符"); $this->assertEquals($thing['tokens'][0], $reader->getCurrentToken(), "不匹配");
$this->assertEquals($this->thingsWithCrLf['tokens'][0], $this->readerWithCrLf->getCurrentToken(), "不匹配"); }
} }
/** /**
@ -71,38 +73,24 @@ class StringReaderTest extends TestCase
*/ */
public function testMoveToNextToken() public function testMoveToNextToken()
{ {
$this->reader->reset(); foreach ($this->things as $index=>$thing) {
foreach ($this->things['nextTokens'] as $key=> $nextToken){ $reader = $this->readerArray[$index];
$oldCurToken = $this->reader->getCurrentToken(); $reader->reset();
$oldNextPos = $this->reader->getNextPosition(); foreach ($thing['nextTokens'] as $key=> $nextToken){
$oldNextToken = $this->reader->getNextToken(); $oldCurToken = $reader->getCurrentToken();
$oldNextPos = $reader->getNextPosition();
$oldNextToken = $reader->getNextToken();
$this->assertEquals($nextToken, $oldNextToken, "不匹配"); $this->assertEquals($nextToken, $oldNextToken, "不匹配");
$this->assertEquals($this->things['positions'][$key], $this->reader->getCurrentPosition(), "CurPos与预计不符"); $this->assertEquals($thing['positions'][$key], $reader->getCurrentPosition(), "CurPos与预计不符");
$this->assertEquals($this->things['lines'][$key], $this->reader->getCurrentLine(), "CurLine与预计不符"); $this->assertEquals($thing['lines'][$key], $reader->getCurrentLine(), "CurLine与预计不符");
$this->assertEquals($this->things['linePositions'][$key], $this->reader->getCurrentLinePosition(), "CLPos与预计不符"); $this->assertEquals($thing['linePositions'][$key], $reader->getCurrentLinePosition(), "CLPos与预计不符");
$hasNext = $this->reader->moveToNextToken(); $hasNext = $reader->moveToNextToken();
if ($hasNext) { if ($hasNext) {
$this->assertNotEquals($oldCurToken, $this->reader->getCurrentToken(), "CurToken与旧CurToken相同"); $this->assertNotEquals($oldCurToken, $reader->getCurrentToken(), "CurToken与旧CurToken相同");
$this->assertNotEquals($oldNextPos, $this->reader->getNextPosition(), "NextPos与旧NextPos相同"); $this->assertNotEquals($oldNextPos, $reader->getNextPosition(), "NextPos与旧NextPos相同");
$this->assertNotEquals($this->reader->getNextPosition(), $this->reader->getCurrentPosition(), "CurPos与NextPos相同"); $this->assertNotEquals($reader->getNextPosition(), $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相同");
}
} }
} }
@ -113,38 +101,45 @@ class StringReaderTest extends TestCase
*/ */
public function testGetNextToken() public function testGetNextToken()
{ {
$this->reader->reset(); foreach ($this->things as $index=>$thing) {
$nextPos = $this->reader->getNextPosition(); $reader = $this->readerArray[$index];
$string = $this->reader->getNextToken(); $reader->reset();
$this->assertEquals($string, $this->reader->getNextToken(), "不匹配"); $nextPos = $reader->getNextPosition();
$this->assertEquals($this->reader->getNextToken(), $this->reader->getNextToken(), "不匹配"); $string = $reader->getNextToken();
$this->assertEquals($nextPos, $this->reader->getNextPosition(), "NextPos不可以发生变化"); $this->assertEquals($string, $reader->getNextToken(), "不匹配");
$this->assertEquals($reader->getNextToken(), $reader->getNextToken(), "不匹配");
$this->assertEquals($nextPos, $reader->getNextPosition(), "NextPos不可以发生变化");
}
} }
public function testSkipCurrentLine() public function testSkipCurrentLine()
{ {
$this->readerWithCrLf->resetCursor(); foreach ($this->things as $index=>$thing) {
foreach ($this->thingsWithCrLf['moveToNextLines'] as $key){ $reader = $this->readerArray[$index];
$this->readerWithCrLf->skipCurrentLine(); $reader->reset();
$this->assertEquals($this->thingsWithCrLf['lines'][$key], $this->readerWithCrLf->getCurrentLine(), "行号不匹配"); foreach ($thing['moveToNextLines'] as $key){
$this->assertEquals($this->thingsWithCrLf['linePositions'][$key], $this->readerWithCrLf->getCurrentLinePosition(), "CLPos不匹配"); $reader->skipCurrentLine();
$this->assertEquals($this->thingsWithCrLf['tokens'][$key], $this->readerWithCrLf->getCurrentToken(), "Token不匹配"); $this->assertEquals($thing['lines'][$key], $reader->getCurrentLine(), "行号不匹配");
$this->assertEquals($this->thingsWithCrLf['positions'][$key], $this->readerWithCrLf->getCurrentPosition(), "CurPos不匹配"); $this->assertEquals($thing['linePositions'][$key], $reader->getCurrentLinePosition(), "CLPos不匹配");
$this->assertEquals($this->thingsWithCrLf['nextTokens'][$key], $this->readerWithCrLf->getNextToken(), "NextToken不匹配"); $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() public function testResetCursor()
{ {
$this->readerWithCrLf->moveToNextToken(); foreach ($this->things as $index=>$thing) {
$curPos = $this->readerWithCrLf->getCurrentPosition(); $reader = $this->readerArray[$index];
$nextPos = $this->readerWithCrLf->getNextPosition(); $reader->moveToNextToken();
$curLine = $this->readerWithCrLf->getCurrentLine(); $curPos = $reader->getCurrentPosition();
$string = $this->readerWithCrLf->getCurrentToken(); $nextPos = $reader->getNextPosition();
$this->readerWithCrLf->resetCursor(); $string = $reader->getCurrentToken();
$this->assertNotEquals($curPos, $this->readerWithCrLf->getCurrentPosition(), "CurPos未发生变化"); $reader->resetCursor();
$this->assertNotEquals($nextPos, $this->readerWithCrLf->getNextPosition(), "NextPos未发生变化"); $this->assertNotEquals($curPos, $reader->getCurrentPosition(), "CurPos未发生变化");
$this->assertNotEquals($curLine, $this->readerWithCrLf->getCurrentLine(), "CurLine未发生变化"); $this->assertNotEquals($nextPos, $reader->getNextPosition(), "NextPos未发生变化");
$this->assertNotEquals($string, $this->readerWithCrLf->getCurrentToken(), "CurToken未发生变化"); $this->assertNotEquals($string, $reader->getCurrentToken(), "CurToken未发生变化");
}
} }
} }

2
tests/Reader/test.jdsl Normal file
View File

@ -0,0 +1,2 @@
当 这个 等于 那个 的时候 则
对 用户表 中的 该用户 更新 字段 状态 为 禁用