优化测试,文件读取测试

This commit is contained in:
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;
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)
{
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;
}
/**
* 移动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至下一行
* @param int|null $newPos 下一行位置,不提供则手动检测

View File

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

View File

@ -16,6 +16,9 @@ use JerryYan\DSL\Token\TokenFake;
use JerryYan\DSL\Token\TokenLogicAnd;
use JerryYan\DSL\Token\TokenLogicEqual;
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\TokenLogicOr;
use JerryYan\DSL\Token\TokenNumber;
@ -29,8 +32,11 @@ class DefaultFactory extends FactoryInterface
Token::CURRY => TokenCurry::class,
Token::LOGIC_AND => TokenLogicAnd::class,
Token::LOGIC_OR => TokenLogicOr::class,
Token::LOGIC_NOT => TokenLogicNot::class,
Token::LOGIC_EQUAL => TokenLogicEqual::class,
Token::LOGIC_NOT_EQUAL => TokenLogicNotEqual::class,
Token::LOGIC_GREATER => TokenLogicGreater::class,
Token::LOGIC_LESS => TokenLogicLess::class,
Token::LOGIC_FAKE => TokenLogicFake::class,
Token::VARIABLE => TokenVariable::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 = [
'不成立'
];
}