3
final class PHPASTParserTestCase extends PhutilTestCase {
5
public function testParser() {
6
if (!xhpast_is_available()) {
7
$this->assertSkipped(pht('xhpast is not built or not up to date.'));
10
$dir = dirname(__FILE__).'/data/';
11
foreach (Filesystem::listDirectory($dir) as $file) {
12
if (preg_match('/\.test$/', $file)) {
13
$this->executeParserTest($file, Filesystem::readFile($dir.$file));
18
private function executeParserTest($name, $data) {
19
$data = explode("\n", $data, 2);
20
if (count($data) !== 2) {
22
pht('Expected multiple lines in parser test file "%s".', $name));
28
if (!preg_match('/^#/', $head)) {
31
'Expected first line of parser test file "%s" to begin with "#" '.
32
'and specify test options.',
36
$head = preg_replace('/^#\s*/', '', $head);
38
$options_parser = new PhutilSimpleOptions();
39
$options = $options_parser->parse($head);
42
foreach ($options as $key => $value) {
50
'Test file "%s" unexpectedly specifies multiple expected ',
57
// Human readable comment providing test case information.
60
// Allows construction of tests which rely on EOF without newlines.
66
'Test file "%s" has unknown option "%s" in its options '.
76
'Test file "%s" does not specify a test result (like "pass") in '.
77
'its options string.',
81
$future = xhpast_get_parser_future($body);
82
list($err, $stdout, $stderr) = $future->resolve();
87
$this->assertEqual(0, $err, pht('Exit code for "%s".', $name));
89
$expect_name = preg_replace('/\.test$/', '.expect', $name);
91
$dir = dirname(__FILE__).'/data/';
92
$expect = Filesystem::readFile($dir.$expect_name);
94
$expect = json_decode($expect, true);
95
if (!is_array($expect)) {
98
'Test ".expect" file "%s" for test "%s" is not valid JSON.',
103
$stdout = json_decode($stdout, true);
104
if (!is_array($stdout)) {
107
'Output for test file "%s" is not valid JSON.',
111
$json = new PhutilJSON();
113
$expect_nice = $json->encodeFormatted($expect);
114
$stdout_nice = $json->encodeFormatted($stdout);
116
if ($type == 'pass') {
120
pht('Parser output for "%s".', $name));
123
($expect_nice == $stdout_nice),
124
pht('Expected parser to parse "%s" incorrectly.', $name));
128
$this->assertEqual(1, $err, pht('Exit code for "%s".', $name));
130
(bool)preg_match('/syntax error/', $stderr),
131
pht('Expect "syntax error" in stderr or "%s".', $name));