mirror of
https://github.com/phabelio/PHP-Parser.git
synced 2024-12-12 09:09:41 +01:00
3701e02d32
Now the lexer is injected only once when creating the parser. Instead of $parser = new PHPParser_Parser; $parser->parse(new PHPParser_Lexer($code)); $parser->parse(new PHPParser_Lexer($code2)); you write: $parser = new PHPParser_Parser(new PHPParser_Lexer); $parser->parse($code); $parser->parse($code2);
89 lines
2.5 KiB
PHP
89 lines
2.5 KiB
PHP
<?php
|
|
|
|
class PHPParser_Tests_codeTest extends PHPUnit_Framework_TestCase
|
|
{
|
|
/**
|
|
* @dataProvider provideTestCode
|
|
*/
|
|
public function testCode($name, $code, $dump) {
|
|
$parser = new PHPParser_Parser(new PHPParser_Lexer_Emulative);
|
|
$dumper = new PHPParser_NodeDumper;
|
|
|
|
$stmts = $parser->parse($code);
|
|
$this->assertEquals(
|
|
$this->canonicalize($dump),
|
|
$this->canonicalize($dumper->dump($stmts)),
|
|
$name
|
|
);
|
|
}
|
|
|
|
public function provideTestCode() {
|
|
return $this->getTests('test');
|
|
}
|
|
|
|
/**
|
|
* @dataProvider provideTestCodeFail
|
|
*/
|
|
public function testCodeFail($name, $code, $msg) {
|
|
$parser = new PHPParser_Parser(new PHPParser_Lexer_Emulative);
|
|
|
|
try {
|
|
$parser->parse($code);
|
|
|
|
$this->fail(sprintf('"%s": Expected PHPParser_Error', $name));
|
|
} catch (PHPParser_Error $e) {
|
|
$this->assertEquals($msg, $e->getMessage(), $name);
|
|
}
|
|
}
|
|
|
|
public function provideTestCodeFail() {
|
|
return $this->getTests('test-fail');
|
|
}
|
|
|
|
protected function getTests($ext) {
|
|
$it = new RecursiveDirectoryIterator(dirname(__FILE__) . '/../../code');
|
|
$it = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::LEAVES_ONLY);
|
|
$it = new RegexIterator($it, '(\.' . preg_quote($ext) . '$)');
|
|
|
|
$tests = array();
|
|
foreach ($it as $file) {
|
|
// read file
|
|
$fileContents = file_get_contents($file);
|
|
|
|
// evaluate @@{expr}@@ expressions
|
|
$fileContents = preg_replace_callback(
|
|
'/@@\{(.*?)\}@@/',
|
|
array($this, 'evalCallback'),
|
|
$fileContents
|
|
);
|
|
|
|
// parse sections
|
|
$parts = array_map('trim', explode('-----', $fileContents));
|
|
|
|
// first part is the name
|
|
$name = array_shift($parts);
|
|
|
|
// multiple sections possible with always two forming a pair
|
|
foreach (array_chunk($parts, 2) as $chunk) {
|
|
$tests[] = array($name, $chunk[0], $chunk[1]);
|
|
}
|
|
}
|
|
|
|
return $tests;
|
|
}
|
|
|
|
protected function canonicalize($str) {
|
|
// trim from both sides
|
|
$str = trim($str);
|
|
|
|
// normalize EOL to \n
|
|
$str = str_replace(array("\r\n", "\r"), "\n", $str);
|
|
|
|
// trim right side of all lines
|
|
return implode("\n", array_map('rtrim', explode("\n", $str)));
|
|
}
|
|
|
|
protected function evalCallback($matches) {
|
|
return eval('return ' . $matches[1] . ';');
|
|
}
|
|
} |