@@ -12,14 +12,17 @@ class ParserGrammarTest extends TestCase {
1212 public function run (PHPUnit_Framework_TestResult $ result = null ) : PHPUnit_Framework_TestResult {
1313 if (!isset ($ GLOBALS ["GIT_CHECKOUT_PARSER " ])) {
1414 $ GLOBALS ["GIT_CHECKOUT_PARSER " ] = true ;
15- exec ("git -C " . dirname (self ::FILE_PATTERN ) . " checkout *.php.tree " );
15+ exec ("git -C " . dirname (self ::FILE_PATTERN ) . " checkout *.php.tree *.php.diag " );
1616 }
1717
1818 $ result ->addListener (new class () extends PHPUnit_Framework_BaseTestListener {
1919 function addFailure (PHPUnit_Framework_Test $ test , PHPUnit_Framework_AssertionFailedError $ e , $ time ) {
2020 if (isset ($ test ->expectedTokensFile ) && isset ($ test ->tokens )) {
2121 file_put_contents ($ test ->expectedTokensFile , str_replace ("\r\n" , "\n" , $ test ->tokens ));
2222 }
23+ if (isset ($ test ->expectedDiagnosticsFile ) && isset ($ test ->diagnostics )) {
24+ file_put_contents ($ test ->expectedDiagnosticsFile , str_replace ("\r\n" , "\n" , $ test ->diagnostics ));
25+ }
2326 parent ::addFailure ($ test , $ e , $ time );
2427 }
2528 });
@@ -31,25 +34,40 @@ function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFai
3134 /**
3235 * @dataProvider treeProvider
3336 */
34- public function testOutputTreeClassificationAndLength ($ testCaseFile , $ expectedTokensFile ) {
37+ public function testOutputTreeClassificationAndLength ($ testCaseFile , $ expectedTokensFile, $ expectedDiagnosticsFile ) {
3538 $ this ->expectedTokensFile = $ expectedTokensFile ;
39+ $ this ->expectedDiagnosticsFile = $ expectedDiagnosticsFile ;
3640
3741 $ fileContents = file_get_contents ($ testCaseFile );
3842 if (!file_exists ($ expectedTokensFile )) {
3943 file_put_contents ($ expectedTokensFile , $ fileContents );
4044 exec ("git add " . $ expectedTokensFile );
4145 }
4246
43- $ expectedTokens = str_replace ("\r\n" , "\n" , file_get_contents ($ expectedTokensFile ));
47+ if (!file_exists ($ expectedDiagnosticsFile )) {
48+ file_put_contents ($ expectedDiagnosticsFile , $ fileContents );
49+ exec ("git add " . $ expectedDiagnosticsFile );
50+ }
51+
4452 $ parser = new \Microsoft \PhpParser \Parser ();
53+ $ sourceFileNode = $ parser ->parseSourceFile ($ fileContents );
54+
55+ $ expectedTokens = str_replace ("\r\n" , "\n" , file_get_contents ($ expectedTokensFile ));
56+ $ expectedDiagnostics = str_replace ("\r\n" , "\n" , file_get_contents ($ expectedDiagnosticsFile ));
57+
4558 $ GLOBALS ["SHORT_TOKEN_SERIALIZE " ] = true ;
46- $ tokens = str_replace ("\r\n" , "\n" , json_encode ($ parser ->parseSourceFile ($ fileContents ), JSON_PRETTY_PRINT ));
59+ $ tokens = str_replace ("\r\n" , "\n" , json_encode ($ sourceFileNode , JSON_PRETTY_PRINT ));
60+ $ diagnostics = str_replace ("\r\n" , "\n" , json_encode (\Microsoft \PhpParser \DiagnosticsProvider::getDiagnostics ($ sourceFileNode ), JSON_PRETTY_PRINT ));
4761 $ GLOBALS ["SHORT_TOKEN_SERIALIZE " ] = false ;
62+
4863 $ this ->tokens = $ tokens ;
64+ $ this ->diagnostics = $ diagnostics ;
4965
50- $ outputStr = "input doc: \r\n$ fileContents \r\n\r\ninput: $ testCaseFile \r\nexpected: $ expectedTokensFile " ;
51-
52- $ this ->assertEquals ($ expectedTokens , $ tokens , $ outputStr );
66+ $ tokensOutputStr = "input doc: \r\n$ fileContents \r\n\r\ninput: $ testCaseFile \r\nexpected: $ expectedTokensFile " ;
67+ $ diagnosticsOutputStr = "input doc: \r\n$ fileContents \r\n\r\ninput: $ testCaseFile \r\nexpected: $ expectedDiagnosticsFile " ;
68+
69+ $ this ->assertEquals ($ expectedTokens , $ tokens , $ tokensOutputStr );
70+ $ this ->assertEquals ($ expectedDiagnostics , $ diagnostics , $ diagnosticsOutputStr );
5371 }
5472
5573 const FILE_PATTERN = __DIR__ . "/cases/parser/* " ;
@@ -63,7 +81,7 @@ public function treeProvider() {
6381 if (in_array (basename ($ testCase ), $ skipped )) {
6482 continue ;
6583 }
66- $ testProviderArray [basename ($ testCase )] = [$ testCase , $ testCase . ".tree " ];
84+ $ testProviderArray [basename ($ testCase )] = [$ testCase , $ testCase . ".tree " , $ testCase . " .diag " ];
6785 }
6886
6987 return $ testProviderArray ;
0 commit comments