Move modeline handling into CodeTestAbstract

This commit is contained in:
Nikita Popov 2015-06-13 15:10:46 +02:00
parent d18dcc0c7f
commit f372a4c4ab
3 changed files with 27 additions and 24 deletions

View File

@ -31,13 +31,28 @@ abstract class CodeTestAbstract extends \PHPUnit_Framework_TestCase
// multiple sections possible with always two forming a pair
foreach (array_chunk($parts, 2) as $chunk) {
$tests[] = array($name, $chunk[0], $chunk[1]);
list($expected, $mode) = $this->extractMode($this->canonicalize($chunk[1]));
$tests[] = array($name, $chunk[0], $expected, $mode);
}
}
return $tests;
}
private function extractMode($expected) {
$firstNewLine = strpos($expected, "\n");
if (false === $firstNewLine) {
return [$expected, null];
}
$firstLine = substr($expected, 0, $firstNewLine);
if (0 !== strpos($firstLine, '!!')) {
return [$expected, null];
}
return [substr($expected, $firstNewLine + 1), substr($firstLine, 2)];
}
protected function canonicalize($str) {
// trim from both sides
$str = trim($str);

View File

@ -11,7 +11,7 @@ class ParserTest extends CodeTestAbstract
/**
* @dataProvider provideTestParse
*/
public function testParse($name, $code, $expected) {
public function testParse($name, $code, $expected, $mode) {
$lexer = new Lexer\Emulative(array('usedAttributes' => array(
'startLine', 'endLine', 'startFilePos', 'endFilePos'
)));
@ -22,29 +22,17 @@ class ParserTest extends CodeTestAbstract
'throwOnError' => false,
));
$expected = $this->canonicalize($expected);
$mode = null;
$firstNewLine = strpos($expected, "\n");
if (false !== $firstNewLine) {
$firstLine = substr($expected, 0, $firstNewLine);
if (substr($firstLine, 0, 2) === '!!') {
$mode = substr($firstLine, 2);
$expected = substr($expected, $firstNewLine + 1);
}
}
if ($mode === 'php5') {
$output5 = $this->getParseOutput($parser5, $code);
$this->assertSame($this->canonicalize($expected), $output5, $name);
$this->assertSame($expected, $output5, $name);
} else if ($mode === 'php7') {
$output7 = $this->getParseOutput($parser7, $code);
$this->assertSame($this->canonicalize($expected), $output7, $name);
$this->assertSame($expected, $output7, $name);
} else {
$output5 = $this->getParseOutput($parser5, $code);
$this->assertSame($this->canonicalize($expected), $output5, $name);
$this->assertSame($expected, $output5, $name);
$output7 = $this->getParseOutput($parser7, $code);
$this->assertSame($this->canonicalize($expected), $output7, $name);
$this->assertSame($expected, $output7, $name);
}
}

View File

@ -11,13 +11,13 @@ require_once __DIR__ . '/CodeTestAbstract.php';
class PrettyPrinterTest extends CodeTestAbstract
{
protected function doTestPrettyPrintMethod($method, $name, $code, $dump) {
protected function doTestPrettyPrintMethod($method, $name, $code, $expected) {
$parser = new Parser(new Lexer\Emulative);
$prettyPrinter = new Standard;
$stmts = $parser->parse($code);
$this->assertSame(
$this->canonicalize($dump),
$expected,
$this->canonicalize($prettyPrinter->$method($stmts)),
$name
);
@ -27,16 +27,16 @@ class PrettyPrinterTest extends CodeTestAbstract
* @dataProvider provideTestPrettyPrint
* @covers PhpParser\PrettyPrinter\Standard<extended>
*/
public function testPrettyPrint($name, $code, $dump) {
$this->doTestPrettyPrintMethod('prettyPrint', $name, $code, $dump);
public function testPrettyPrint($name, $code, $expected) {
$this->doTestPrettyPrintMethod('prettyPrint', $name, $code, $expected);
}
/**
* @dataProvider provideTestPrettyPrintFile
* @covers PhpParser\PrettyPrinter\Standard<extended>
*/
public function testPrettyPrintFile($name, $code, $dump) {
$this->doTestPrettyPrintMethod('prettyPrintFile', $name, $code, $dump);
public function testPrettyPrintFile($name, $code, $expected) {
$this->doTestPrettyPrintMethod('prettyPrintFile', $name, $code, $expected);
}
public function provideTestPrettyPrint() {