forked from austinbyers/esprima-ast-visitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
visitor_test.py
97 lines (71 loc) · 2.99 KB
/
visitor_test.py
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
"""Test the AST traversal."""
import gzip
import json
import unittest
import visitor
class TestTraverse(unittest.TestCase):
"""Verify that traversals progress in the correct order and terminate.
Test cases are drawn from a variety of very large real-world JS examples,
including popular libraries and code served by the Alexa top 10.
"""
def _test_ast_file(self, path: str) -> None:
"""Traverse the AST specified in the given test file."""
with gzip.open(path, 'rt') as ast_file:
ast_string = ast_file.read()
# The expected traversal is given by the layout of the JSON file.
expected_types = []
for line in ast_string.split('\n'):
words = line.strip().split()
if words[0] == '"type":':
expected_types.append(words[1].strip(',').strip('"'))
# Traverse the AST, keeping track of node types.
node = visitor.Program(json.loads(ast_string))
found_types = [n.type for n in node.traverse()]
self.assertEqual(expected_types, found_types)
# Dump the node back to a dict and make sure it parses again the same way.
reparsed = visitor.Program(node.dict())
newfound_types = [n.type for n in reparsed.traverse()]
self.assertEqual(expected_types, newfound_types)
# pylint: disable=missing-docstring
def test_amazon(self):
self._test_ast_file('test_ast/amazon.ast.gz')
def test_baidu(self):
self._test_ast_file('test_ast/baidu.ast.gz')
def test_facebook(self):
self._test_ast_file('test_ast/facebook.ast.gz')
def test_google(self):
self._test_ast_file('test_ast/google.ast.gz')
def test_handlebars(self):
self._test_ast_file('test_ast/handlebars.ast.gz')
def test_jquery(self):
self._test_ast_file('test_ast/jquery.ast.gz')
def test_jquery_ui(self):
self._test_ast_file('test_ast/jquery-ui.ast.gz')
def test_qq(self):
self._test_ast_file('test_ast/qq.ast.gz')
def test_sugar(self):
self._test_ast_file('test_ast/sugar.ast.gz')
def test_twitter(self):
self._test_ast_file('test_ast/twitter.ast.gz')
def test_wikipedia(self):
self._test_ast_file('test_ast/wikipedia.ast.gz')
def test_yahoo(self):
self._test_ast_file('test_ast/yahoo.ast.gz')
def test_remaining(self):
"""Test any node types which weren't already covered."""
data = {
'type': 'WithStatement',
'object': None,
'body': {
'type': 'DebuggerStatement'
}
}
node = visitor.objectify(data)
self.assertEqual(
['WithStatement', 'DebuggerStatement'], [n.type for n in node.traverse()])
def test_unexpected_node_type(self):
"""Verify traversal failure for an unknown node type."""
with self.assertRaises(visitor.UnknownNodeTypeError):
visitor.objectify({'type': 'FakeNodeType'})
if __name__ == '__main__':
unittest.main()