Skip to content

Commit 8aa8c0a

Browse files
authored
Merge pull request microsoft#198 from MadHed/diag-validation
Add diagnostics test
2 parents fbac533 + 8378884 commit 8aa8c0a

File tree

639 files changed

+2787
-9
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

639 files changed

+2787
-9
lines changed

src/DiagnosticsProvider.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ public static function checkDiagnostics($node) {
7171
}
7272
}
7373
elseif ($node instanceof Node\Statement\NamespaceUseDeclaration) {
74-
if (\count($node->useClauses->children) > 1) {
74+
if (
75+
$node->useClauses != null
76+
&& \count($node->useClauses->children) > 1
77+
) {
7578
foreach ($node->useClauses->children as $useClause) {
7679
if($useClause instanceof Node\NamespaceUseClause && !is_null($useClause->openBrace)) {
7780
return new Diagnostic(

tests/ParserGrammarTest.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"kind": 0,
4+
"message": "'{' expected.",
5+
"start": 57,
6+
"length": 0
7+
},
8+
{
9+
"kind": 0,
10+
"message": "'}' expected.",
11+
"start": 59,
12+
"length": 0
13+
}
14+
]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"kind": 0,
4+
"message": "'{' expected.",
5+
"start": 20,
6+
"length": 0
7+
},
8+
{
9+
"kind": 0,
10+
"message": "'}' expected.",
11+
"start": 21,
12+
"length": 0
13+
}
14+
]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[
2+
{
3+
"kind": 0,
4+
"message": "Unexpected 'public'",
5+
"start": 29,
6+
"length": 6
7+
}
8+
]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[
2+
{
3+
"kind": 0,
4+
"message": "Unexpected 'abstract'",
5+
"start": 29,
6+
"length": 8
7+
}
8+
]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

0 commit comments

Comments
 (0)