~ubuntu-branches/ubuntu/vivid/phabricator/vivid-proposed

« back to all changes in this revision

Viewing changes to libphutil/src/parser/xhpast/__tests__/PHPASTParserTestCase.php

  • Committer: Package Import Robot
  • Author(s): Richard Sellam
  • Date: 2014-10-23 20:49:26 UTC
  • mfrom: (0.2.1) (0.1.1)
  • Revision ID: package-import@ubuntu.com-20141023204926-vq80u1op4df44azb
Tags: 0~git20141023-1
Initial release (closes: #703046)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
final class PHPASTParserTestCase extends PhutilTestCase {
 
4
 
 
5
  public function testParser() {
 
6
    if (!xhpast_is_available()) {
 
7
      $this->assertSkipped(pht('xhpast is not built or not up to date.'));
 
8
    }
 
9
 
 
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));
 
14
      }
 
15
    }
 
16
  }
 
17
 
 
18
  private function executeParserTest($name, $data) {
 
19
    $data = explode("\n", $data, 2);
 
20
    if (count($data) !== 2) {
 
21
      throw new Exception(
 
22
        pht('Expected multiple lines in parser test file "%s".', $name));
 
23
    }
 
24
 
 
25
    $head = head($data);
 
26
    $body = last($data);
 
27
 
 
28
    if (!preg_match('/^#/', $head)) {
 
29
      throw new Exception(
 
30
        pht(
 
31
          'Expected first line of parser test file "%s" to begin with "#" '.
 
32
          'and specify test options.',
 
33
          $name));
 
34
    }
 
35
 
 
36
    $head = preg_replace('/^#\s*/', '', $head);
 
37
 
 
38
    $options_parser = new PhutilSimpleOptions();
 
39
    $options = $options_parser->parse($head);
 
40
 
 
41
    $type = null;
 
42
    foreach ($options as $key => $value) {
 
43
      switch ($key) {
 
44
        case 'pass':
 
45
        case 'fail-syntax':
 
46
        case 'fail-parse':
 
47
          if ($type !== null) {
 
48
            throw new Exception(
 
49
              pht(
 
50
                'Test file "%s" unexpectedly specifies multiple expected ',
 
51
                'test outcomes.',
 
52
                $name));
 
53
          }
 
54
          $type = $key;
 
55
          break;
 
56
        case 'comment':
 
57
          // Human readable comment providing test case information.
 
58
          break;
 
59
        case 'rtrim':
 
60
          // Allows construction of tests which rely on EOF without newlines.
 
61
          $body = rtrim($body);
 
62
          break;
 
63
        default:
 
64
          throw new Exception(
 
65
            pht(
 
66
              'Test file "%s" has unknown option "%s" in its options '.
 
67
              'string.',
 
68
              $name,
 
69
              $key));
 
70
      }
 
71
    }
 
72
 
 
73
    if ($type === null) {
 
74
      throw new Exception(
 
75
        pht(
 
76
          'Test file "%s" does not specify a test result (like "pass") in '.
 
77
          'its options string.',
 
78
          $name));
 
79
    }
 
80
 
 
81
    $future = xhpast_get_parser_future($body);
 
82
    list($err, $stdout, $stderr) = $future->resolve();
 
83
 
 
84
    switch ($type) {
 
85
      case 'pass':
 
86
      case 'fail-parse':
 
87
        $this->assertEqual(0, $err, pht('Exit code for "%s".', $name));
 
88
 
 
89
        $expect_name = preg_replace('/\.test$/', '.expect', $name);
 
90
 
 
91
        $dir = dirname(__FILE__).'/data/';
 
92
        $expect = Filesystem::readFile($dir.$expect_name);
 
93
 
 
94
        $expect = json_decode($expect, true);
 
95
        if (!is_array($expect)) {
 
96
          throw new Exception(
 
97
            pht(
 
98
              'Test ".expect" file "%s" for test "%s" is not valid JSON.',
 
99
              $expect_name,
 
100
              $name));
 
101
        }
 
102
 
 
103
        $stdout = json_decode($stdout, true);
 
104
        if (!is_array($stdout)) {
 
105
          throw new Exception(
 
106
            pht(
 
107
              'Output for test file "%s" is not valid JSON.',
 
108
              $name));
 
109
        }
 
110
 
 
111
        $json = new PhutilJSON();
 
112
 
 
113
        $expect_nice = $json->encodeFormatted($expect);
 
114
        $stdout_nice = $json->encodeFormatted($stdout);
 
115
 
 
116
        if ($type == 'pass') {
 
117
          $this->assertEqual(
 
118
            $expect_nice,
 
119
            $stdout_nice,
 
120
            pht('Parser output for "%s".', $name));
 
121
        } else {
 
122
          $this->assertFalse(
 
123
            ($expect_nice == $stdout_nice),
 
124
            pht('Expected parser to parse "%s" incorrectly.', $name));
 
125
        }
 
126
        break;
 
127
      case 'fail-syntax':
 
128
        $this->assertEqual(1, $err, pht('Exit code for "%s".', $name));
 
129
        $this->assertTrue(
 
130
          (bool)preg_match('/syntax error/', $stderr),
 
131
          pht('Expect "syntax error" in stderr or "%s".', $name));
 
132
        break;
 
133
    }
 
134
  }
 
135
 
 
136
}