OLD | NEW |
| (Empty) |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 | |
5 import imp | |
6 import os.path | |
7 import sys | |
8 import unittest | |
9 | |
10 def _GetDirAbove(dirname): | |
11 """Returns the directory "above" this file containing |dirname| (which must | |
12 also be "above" this file).""" | |
13 path = os.path.abspath(__file__) | |
14 while True: | |
15 path, tail = os.path.split(path) | |
16 assert tail | |
17 if tail == dirname: | |
18 return path | |
19 | |
20 try: | |
21 imp.find_module("mojom") | |
22 except ImportError: | |
23 sys.path.append(os.path.join(_GetDirAbove("pylib"), "pylib")) | |
24 import mojom.parse.ast as ast | |
25 | |
26 | |
27 class _TestNode(ast.NodeBase): | |
28 """Node type for tests.""" | |
29 | |
30 def __init__(self, value, **kwargs): | |
31 super(_TestNode, self).__init__(**kwargs) | |
32 self.value = value | |
33 | |
34 def __eq__(self, other): | |
35 return super(_TestNode, self).__eq__(other) and self.value == other.value | |
36 | |
37 | |
38 class _TestNodeList(ast.NodeListBase): | |
39 """Node list type for tests.""" | |
40 | |
41 _list_item_type = _TestNode | |
42 | |
43 | |
44 class ASTTest(unittest.TestCase): | |
45 """Tests various AST classes.""" | |
46 | |
47 def testNodeBase(self): | |
48 # Test |__eq__()|; this is only used for testing, where we want to do | |
49 # comparison by value and ignore filenames/line numbers (for convenience). | |
50 node1 = ast.NodeBase(filename="hello.mojom", lineno=123) | |
51 node2 = ast.NodeBase() | |
52 self.assertEquals(node1, node2) | |
53 self.assertEquals(node2, node1) | |
54 | |
55 # Check that |__ne__()| just defers to |__eq__()| properly. | |
56 self.assertFalse(node1 != node2) | |
57 self.assertFalse(node2 != node1) | |
58 | |
59 # Check that |filename| and |lineno| are set properly (and are None by | |
60 # default). | |
61 self.assertEquals(node1.filename, "hello.mojom") | |
62 self.assertEquals(node1.lineno, 123) | |
63 self.assertIsNone(node2.filename) | |
64 self.assertIsNone(node2.lineno) | |
65 | |
66 # |NodeBase|'s |__eq__()| should compare types (and a subclass's |__eq__()| | |
67 # should first defer to its superclass's). | |
68 node3 = _TestNode(123) | |
69 self.assertNotEqual(node1, node3) | |
70 self.assertNotEqual(node3, node1) | |
71 # Also test |__eq__()| directly. | |
72 self.assertFalse(node1 == node3) | |
73 self.assertFalse(node3 == node1) | |
74 | |
75 node4 = _TestNode(123, filename="world.mojom", lineno=123) | |
76 self.assertEquals(node4, node3) | |
77 node5 = _TestNode(456) | |
78 self.assertNotEquals(node5, node4) | |
79 | |
80 def testNodeListBase(self): | |
81 node1 = _TestNode(1, filename="foo.mojom", lineno=1) | |
82 # Equal to, but not the same as, |node1|: | |
83 node1b = _TestNode(1, filename="foo.mojom", lineno=1) | |
84 node2 = _TestNode(2, filename="foo.mojom", lineno=2) | |
85 | |
86 nodelist1 = _TestNodeList() # Contains: (empty). | |
87 self.assertEquals(nodelist1, nodelist1) | |
88 self.assertEquals(nodelist1.items, []) | |
89 self.assertIsNone(nodelist1.filename) | |
90 self.assertIsNone(nodelist1.lineno) | |
91 | |
92 nodelist2 = _TestNodeList(node1) # Contains: 1. | |
93 self.assertEquals(nodelist2, nodelist2) | |
94 self.assertEquals(nodelist2.items, [node1]) | |
95 self.assertNotEqual(nodelist2, nodelist1) | |
96 self.assertEquals(nodelist2.filename, "foo.mojom") | |
97 self.assertEquals(nodelist2.lineno, 1) | |
98 | |
99 nodelist3 = _TestNodeList([node2]) # Contains: 2. | |
100 self.assertEquals(nodelist3.items, [node2]) | |
101 self.assertNotEqual(nodelist3, nodelist1) | |
102 self.assertNotEqual(nodelist3, nodelist2) | |
103 self.assertEquals(nodelist3.filename, "foo.mojom") | |
104 self.assertEquals(nodelist3.lineno, 2) | |
105 | |
106 nodelist1.Append(node1b) # Contains: 1. | |
107 self.assertEquals(nodelist1.items, [node1]) | |
108 self.assertEquals(nodelist1, nodelist2) | |
109 self.assertNotEqual(nodelist1, nodelist3) | |
110 self.assertEquals(nodelist1.filename, "foo.mojom") | |
111 self.assertEquals(nodelist1.lineno, 1) | |
112 | |
113 nodelist1.Append(node2) # Contains: 1, 2. | |
114 self.assertEquals(nodelist1.items, [node1, node2]) | |
115 self.assertNotEqual(nodelist1, nodelist2) | |
116 self.assertNotEqual(nodelist1, nodelist3) | |
117 self.assertEquals(nodelist1.lineno, 1) | |
118 | |
119 nodelist2.Append(node2) # Contains: 1, 2. | |
120 self.assertEquals(nodelist2.items, [node1, node2]) | |
121 self.assertEquals(nodelist2, nodelist1) | |
122 self.assertNotEqual(nodelist2, nodelist3) | |
123 self.assertEquals(nodelist2.lineno, 1) | |
124 | |
125 nodelist3.Insert(node1) # Contains: 1, 2. | |
126 self.assertEquals(nodelist3.items, [node1, node2]) | |
127 self.assertEquals(nodelist3, nodelist1) | |
128 self.assertEquals(nodelist3, nodelist2) | |
129 self.assertEquals(nodelist3.lineno, 1) | |
130 | |
131 # Test iteration: | |
132 i = 1 | |
133 for item in nodelist1: | |
134 self.assertEquals(item.value, i) | |
135 i += 1 | |
OLD | NEW |