原始输出类及测试

This commit is contained in:
Jerry Yan 2021-02-19 11:18:28 +08:00
parent 273f6ba2e8
commit e738695057
4 changed files with 70 additions and 21 deletions

View File

@ -1,21 +0,0 @@
<?php
/**
* @filename CnTextOutput.php
* @author Jerry Yan <792602257@qq.com>
* @date 2020/12/17 15:33
*/
namespace JerryYan\DSL\Output;
/**
* 又以中文形式重新输出一遍
* @package JerryYan\DSL\Output
* @author Jerry Yan <792602257@qq.com>
* @date 2020/12/17 16:12
*/
class CnTextOutput extends OutputInterface
{
}

View File

@ -9,7 +9,15 @@
namespace JerryYan\DSL\Output;
use JerryYan\DSL\Token\TokenInterface;
abstract class OutputInterface
{
protected $tokenChain;
public function __construct(TokenInterface $tokenChain)
{
$this->tokenChain = $tokenChain->getFirstToken();
}
abstract public function output();
}

29
src/Output/RawOutput.php Normal file
View File

@ -0,0 +1,29 @@
<?php
/**
* @filename CnTextOutput.php
* @author Jerry Yan <792602257@qq.com>
* @date 2020/12/17 15:33
*/
namespace JerryYan\DSL\Output;
/**
* 又以中文形式重新输出一遍
* @package JerryYan\DSL\Output
* @author Jerry Yan <792602257@qq.com>
* @date 2020/12/17 16:12
*/
class RawOutput extends OutputInterface
{
public function output(): string
{
$output = [];
$token = $this->tokenChain;
do {
array_push($output, $token->getRawString());
} while ($token = $token->getNextToken());
return implode(' ', $output);
}
}

View File

@ -0,0 +1,33 @@
<?php
/**
* @filename RawOutputTest.php
* @author Jerry Yan <792602257@qq.com>
* @date 2021/2/19 11:10
*/
namespace JerryYan\DSL\Test\Output;
use JerryYan\DSL\Grammar\DefaultGrammar;
use JerryYan\DSL\Output\RawOutput;
use JerryYan\DSL\Reader\StringReader;
use JerryYan\DSL\Tokenizer\Tokenizer;
use PHPUnit\Framework\TestCase;
class RawOutputTest extends TestCase
{
private $text = "当 另外那个 与 另外一个 不相等时 或者 那个 和 这个 等于 -0.5 的时候";
private $output;
protected function setUp(): void
{
$tokenizer = new Tokenizer(new DefaultGrammar());
$reader = new StringReader($this->text);
$token = $tokenizer->tokenize($reader);
$this->output = new RawOutput($token);
}
public function testOutput()
{
$this->assertEquals($this->text, $this->output->output(), '输出与预期不一致');
}
}