Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F15449820
D17819.id42847.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
4 KB
Referenced Files
None
Subscribers
None
D17819.id42847.diff
View Options
diff --git a/src/parser/xhpast/__tests__/PHPASTParserTestCase.php b/src/parser/xhpast/__tests__/PHPASTParserTestCase.php
--- a/src/parser/xhpast/__tests__/PHPASTParserTestCase.php
+++ b/src/parser/xhpast/__tests__/PHPASTParserTestCase.php
@@ -89,16 +89,6 @@
}
try {
- $expect = phutil_json_decode($expect);
- } catch (PhutilJSONParserException $ex) {
- throw new PhutilProxyException(
- pht(
- 'Expect data for test "%s" is not valid JSON.',
- $name),
- $ex);
- }
-
- try {
$stdout = phutil_json_decode($stdout);
} catch (PhutilJSONParserException $ex) {
throw new PhutilProxyException(
@@ -108,19 +98,16 @@
$ex);
}
- $json = new PhutilJSON();
-
- $expect_nice = $json->encodeFormatted($expect);
- $stdout_nice = $json->encodeFormatted($stdout);
+ $stdout_nice = $this->newReadableAST($stdout, $data);
if ($type == 'pass') {
$this->assertEqual(
- $expect_nice,
+ $expect,
$stdout_nice,
pht('Parser output for "%s".', $name));
} else {
$this->assertFalse(
- ($expect_nice == $stdout_nice),
+ ($expect == $stdout_nice),
pht('Expected parser to parse "%s" incorrectly.', $name));
}
break;
@@ -133,4 +120,128 @@
}
}
+ /**
+ * Build a human-readable, stable, relatively diff-able string representing
+ * an AST (both the node tree itself and the accompanying token stream) for
+ * use in unit tests.
+ */
+ private function newReadableAST(array $data, $source) {
+ $tree = new XHPASTTree($data['tree'], $data['stream'], $source);
+
+ $out = array();
+
+ $root = $tree->getRootNode();
+
+ $out[] = implode('', $this->newReadableTree($root, 0));
+
+ $out[] = str_repeat('-', 80)."\n";
+
+ $tokens = $tree->getRawTokenStream();
+ foreach ($tokens as $token) {
+ $value = $token->getValue();
+ $vector = $this->getEscapedValueVector($value);
+
+ $vector = array_merge(
+ array(
+ // At time of writing, the longest tokens are
+ // "T_CONSTANT_ENCAPSED_STRING" and "T_DOLLAR_OPEN_CURLY_BRACES".
+ str_pad($token->getTypeName(), 30),
+ ' ',
+ ),
+ $vector);
+
+ $out[] = $this->wrapVector($vector);
+ }
+
+ $out = implode('', $out);
+
+ return $out;
+ }
+
+ private function newReadableTree(AASTNode $node, $depth) {
+ $out = array();
+
+ try {
+ $type_name = $node->getTypeName();
+ } catch (Exception $ex) {
+ $type_name = sprintf('<INVALID TYPE "%s">', $node->getTypeID());
+ }
+
+ $out[] = str_repeat(' ', $depth).'+ '.$type_name."\n";
+
+ $children = $node->getChildren();
+ if ($children) {
+ foreach ($children as $child) {
+ foreach ($this->newReadableTree($child, $depth + 1) as $line) {
+ $out[] = $line;
+ }
+ }
+ } else {
+ $value = $node->getConcreteString();
+ $vector = $this->getEscapedValueVector($value);
+ $out[] = $this->wrapVector($vector, $depth + 1);
+ }
+
+ return $out;
+ }
+
+ private function getEscapedValueVector($value) {
+ if (!$value) {
+ return array('<null>');
+ }
+
+ $vector = phutil_utf8v_combined($value);
+
+ foreach ($vector as $key => $character) {
+ switch ($character) {
+ case '\\':
+ $vector[$key] = '\\\\';
+ break;
+ case "\r":
+ $vector[$key] = "\\r";
+ break;
+ case "\n":
+ $vector[$key] = "\\n";
+ break;
+ case ' ':
+ $vector[$key] = '_';
+ break;
+ case '_':
+ $vector[$key] = '\\_';
+ break;
+ default:
+ break;
+ }
+ }
+
+ return $vector;
+ }
+
+ private function wrapVector(array $vector, $indent = 0, $width = 80) {
+ $lines = array();
+ $prefix = str_repeat(' ', $indent);
+
+ $line = $prefix.'> ';
+ $len = strlen($line);
+ foreach ($vector as $character) {
+ // We're just wrapping based on bytes. This isn't the most correct
+ // wrapping for human language, but sufficient and stable for unit
+ // tests, which will rarely have long sequences of combining or
+ // multibyte characters.
+ $charlen = strlen($character);
+ if ($len + $charlen > $width) {
+ $lines[] = $line."\n";
+ $line = $prefix.'. ';
+ $len = strlen($line);
+ }
+
+ $line .= $character;
+ $len += $charlen;
+ }
+
+ $lines[] = $line."\n";
+
+ return implode('', $lines);
+ }
+
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Mar 29, 12:22 PM (1 w, 1 d ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
7385801
Default Alt Text
D17819.id42847.diff (4 KB)
Attached To
Mode
D17819: Make PHPAST parser tests stable and human-readable
Attached
Detach File
Event Timeline
Log In to Comment