2011-11-26 16:09:39 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class PHPParser_Tests_codeTest extends PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @dataProvider provideTestCode
|
|
|
|
*/
|
2011-11-26 17:10:18 +01:00
|
|
|
public function testCode($name, $code, $dump) {
|
|
|
|
$parser = new PHPParser_Parser;
|
|
|
|
$dumper = new PHPParser_NodeDumper;
|
2011-11-26 16:09:39 +01:00
|
|
|
|
|
|
|
$stmts = $parser->parse(new PHPParser_Lexer($code));
|
2011-11-26 17:10:18 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
$this->canonicalize($dump),
|
|
|
|
$this->canonicalize($dumper->dump($stmts)),
|
|
|
|
$name
|
|
|
|
);
|
2011-11-26 16:09:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideTestCode() {
|
2011-11-27 12:53:48 +01:00
|
|
|
return $this->getTests('test');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideTestCodeFail
|
|
|
|
*/
|
|
|
|
public function testCodeFail($name, $code, $msg) {
|
|
|
|
$parser = new PHPParser_Parser;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$parser->parse(new PHPParser_Lexer($code));
|
|
|
|
|
|
|
|
$this->fail(sprintf('"%s": Expected PHPParser_Error', $name));
|
|
|
|
} catch (PHPParser_Error $e) {
|
|
|
|
$this->assertEquals($msg, $e->getMessage(), $name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideTestCodeFail() {
|
2011-11-27 21:43:27 +01:00
|
|
|
return $this->getTests('test-fail');
|
2011-11-27 12:53:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function getTests($ext) {
|
2011-11-26 16:09:39 +01:00
|
|
|
$it = new RecursiveDirectoryIterator(dirname(__FILE__) . '/../../code');
|
|
|
|
$it = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::LEAVES_ONLY);
|
2011-11-27 11:20:35 +01:00
|
|
|
|
2011-11-27 12:53:48 +01:00
|
|
|
$ext = preg_quote($ext, '~');
|
2011-11-27 11:20:35 +01:00
|
|
|
if (version_compare(PHP_VERSION, '5.4.0RC1', '>=')) {
|
2011-11-27 12:53:48 +01:00
|
|
|
$it = new RegexIterator($it, '~\.' . $ext . '(-5\.4)?$~');
|
2011-11-27 11:20:35 +01:00
|
|
|
} else {
|
2011-11-27 12:53:48 +01:00
|
|
|
$it = new RegexIterator($it, '~\.' . $ext . '$~');
|
2011-11-27 11:20:35 +01:00
|
|
|
}
|
2011-11-26 16:09:39 +01:00
|
|
|
|
2011-11-27 21:43:27 +01:00
|
|
|
$tests = array();
|
2011-11-26 16:09:39 +01:00
|
|
|
foreach ($it as $file) {
|
2011-11-27 11:20:35 +01:00
|
|
|
// read file
|
2011-11-26 16:09:39 +01:00
|
|
|
$fileContents = file_get_contents($file);
|
|
|
|
|
|
|
|
// evaluate @@{expr}@@ expressions
|
|
|
|
$fileContents = preg_replace('/@@\{(.*?)\}@@/e', '$1', $fileContents);
|
|
|
|
|
2011-11-27 11:20:35 +01:00
|
|
|
// parse sections
|
2011-11-27 21:43:27 +01:00
|
|
|
$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]);
|
|
|
|
}
|
2011-11-26 16:09:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $tests;
|
|
|
|
}
|
2011-11-26 17:10:18 +01:00
|
|
|
|
|
|
|
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)));
|
|
|
|
}
|
2011-11-26 16:09:39 +01:00
|
|
|
}
|