forked from microsoft/tolerant-php-parser
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathParserGrammarTest.php
More file actions
127 lines (104 loc) · 4.94 KB
/
ParserGrammarTest.php
File metadata and controls
127 lines (104 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
use Microsoft\PhpParser\DiagnosticsProvider;
use PHPUnit\Framework\Test;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestResult;
use PHPUnit\Framework\TestListener;
use PHPUnit\Framework\TestListenerDefaultImplementation;
use PHPUnit\Framework\AssertionFailedError;
class ParserGrammarTest extends TestCase {
private $expectedTokensFile;
private $expectedDiagnosticsFile;
private $tokens;
private $diagnostics;
/**
* @dataProvider treeProvider
*/
public function testOutputTreeClassificationAndLength($testCaseFile, $expectedTokensFile, $expectedDiagnosticsFile) {
$fileContents = file_get_contents($testCaseFile);
$parser = new \Microsoft\PhpParser\Parser();
$sourceFileNode = $parser->parseSourceFile($fileContents);
$GLOBALS["SHORT_TOKEN_SERIALIZE"] = true;
$tokens = str_replace("\r\n", "\n", json_encode($sourceFileNode, JSON_PRETTY_PRINT));
$diagnostics = str_replace("\r\n", "\n", json_encode(\Microsoft\PhpParser\DiagnosticsProvider::getDiagnostics($sourceFileNode), JSON_PRETTY_PRINT));
$GLOBALS["SHORT_TOKEN_SERIALIZE"] = false;
$skip = false;
if (!file_exists($expectedTokensFile)) {
file_put_contents($expectedTokensFile, $tokens);
$skip = true;
} else {
$expectedTokens = trim(str_replace("\r\n", "\n", file_get_contents($expectedTokensFile)));
}
if (!file_exists($expectedDiagnosticsFile)) {
file_put_contents($expectedDiagnosticsFile, $diagnostics);
$skip = true;
} else {
$expectedDiagnostics = trim(str_replace("\r\n", "\n", file_get_contents($expectedDiagnosticsFile)));
}
if ($skip) {
self::markTestSkipped('Snapshot generated');
}
$tokensOutputStr = "input doc:\r\n$fileContents\r\n\r\ninput: $testCaseFile\r\nexpected: $expectedTokensFile";
$diagnosticsOutputStr = "input doc:\r\n$fileContents\r\n\r\ninput: $testCaseFile\r\nexpected: $expectedDiagnosticsFile";
$this->assertEquals($expectedTokens, $tokens, $tokensOutputStr);
$this->assertEquals($expectedDiagnostics, $diagnostics, $diagnosticsOutputStr);
}
const FILE_PATTERN = __DIR__ . "/cases/parser/*";
const PHP74_FILE_PATTERN = __DIR__ . "/cases/parser74/*";
const PHP80_FILE_PATTERN = __DIR__ . "/cases/parser80/*";
const PHP81_FILE_PATTERN = __DIR__ . "/cases/parser81/*";
const PHP84_FILE_PATTERN = __DIR__ . "/cases/parser84/*";
const PATTERNS_FOR_MINIMUM_PHP_VERSION = [
[70400, self::PHP74_FILE_PATTERN],
[80000, self::PHP80_FILE_PATTERN],
[80100, self::PHP81_FILE_PATTERN],
[80400, self::PHP84_FILE_PATTERN],
];
public function treeProvider() {
$testCases = glob(self::FILE_PATTERN . ".php");
$skipped = json_decode(file_get_contents(__DIR__ . "/skipped.json"));
$testProviderArray = [];
foreach ($testCases as $testCase) {
if (in_array(basename($testCase), $skipped)) {
continue;
}
$testProviderArray[basename($testCase)] = [$testCase, $testCase . ".tree", $testCase . ".diag"];
}
foreach (self::PATTERNS_FOR_MINIMUM_PHP_VERSION as [$minVersionId, $filePattern]) {
if (PHP_VERSION_ID < $minVersionId) {
continue;
}
$testCases = glob($filePattern . ".php");
foreach ($testCases as $testCase) {
$testProviderArray[basename($testCase)] = [$testCase, $testCase . ".tree", $testCase . ".diag"];
}
}
return $testProviderArray;
}
/**
* @dataProvider outTreeProvider
*/
public function testSpecOutputTreeClassificationAndLength($testCaseFile, $expectedTreeFile) {
$parser = new \Microsoft\PhpParser\Parser();
$sourceFile = $parser->parseSourceFile(file_get_contents($testCaseFile));
$tokens = str_replace("\r\n", "\n", json_encode($sourceFile, JSON_PRETTY_PRINT));
file_put_contents($expectedTreeFile, $tokens);
$this->assertSame([], DiagnosticsProvider::getDiagnostics($sourceFile));
}
public function outTreeProvider() {
$testCases = glob(__DIR__ . "/cases/php-langspec/**/*.php");
$skipped = json_decode(file_get_contents(__DIR__ . "/skipped.json"));
$testProviderArray = [];
foreach ($testCases as $case) {
if (in_array(basename($case), $skipped)) {
continue;
}
$testProviderArray[basename($case)] = [$case, $case . ".tree"];
}
return $testProviderArray;
}
}