Debian Codegraph @

tests/language/test_ast.py

raw
1 from copy import copy, deepcopy
2 import weakref
3
4 from graphql.language import Location, Node, Source, Token, TokenKind
5 from graphql.pyutils import inspect
6
7
8 class SampleTestNode(Node):
9 __slots__ = "alpha", "beta"
10
11 alpha: int
12 beta: int
13
14
15 def describe_token_class():
16 def initializes():
17 token = Token(
18 kind=TokenKind.NAME,
19 start=11,
20 end=12,
21 line=1,
22 column=3,
23 value="n",
24 )
25 assert token.kind == TokenKind.NAME
26 assert token.start == 11
27 assert token.end == 12
28 assert token.line == 1
29 assert token.column == 3
30 assert token.prev is None
31 assert token.value == "n"
32
33 def can_stringify():
34 token = Token(TokenKind.NAME, 1, 2, 1, 2, value="test")
35 assert str(token) == "Name 'test'"
36 assert token.desc == str(token)
37
38 def has_representation_with_line_and_column():
39 token = Token(TokenKind.NAME, 1, 2, 1, 2, value="test")
40 assert repr(token) == "<Token Name 'test' 1:2>"
41 assert inspect(token) == repr(token)
42
43 def can_check_equality():
44 token1 = Token(TokenKind.NAME, 1, 2, 1, 2, value="test")
45 token2 = Token(TokenKind.NAME, 1, 2, 1, 2, value="test")
46 assert token2 == token1
47 assert not token2 != token1
48 token3 = Token(TokenKind.NAME, 1, 2, 1, 2, value="text")
49 assert token3 != token1
50 token4 = Token(TokenKind.NAME, 1, 4, 1, 2, value="test")
51 assert token4 != token1
52 token5 = Token(TokenKind.NAME, 1, 2, 1, 4, value="test")
53 assert token5 != token1
54
55 def can_compare_with_string():
56 token = Token(TokenKind.NAME, 1, 2, 1, 2, value="test")
57 assert token == "Name 'test'"
58 assert token != "Name 'foo'"
59
60 def does_not_equal_incompatible_object():
61 token = Token(TokenKind.NAME, 1, 2, 1, 2, value="test")
62 assert token != {"Name": "test"}
63
64 def can_hash():
65 token1 = Token(TokenKind.NAME, 1, 2, 1, 2, value="hash")
66 token2 = Token(TokenKind.NAME, 1, 2, 1, 2, value="hash")
67 assert token2 == token1
68 assert hash(token2) == hash(token1)
69 token3 = Token(TokenKind.NAME, 1, 2, 1, 2, value="bash")
70 assert token3 != token1
71 assert hash(token3) != hash(token1)
72
73 def can_copy():
74 token1 = Token(TokenKind.NAME, 1, 2, 1, 2, value="copy")
75 token2 = copy(token1)
76 assert token2 == token1
77 assert token2 is not token1
78
79
80 def describe_location_class():
81 token1 = Token(TokenKind.NAME, 1, 2, 1, 2)
82 token2 = Token(TokenKind.NAME, 2, 3, 1, 3)
83 source = Source("source")
84
85 def initializes():
86 loc = Location(token1, token2, source)
87 assert loc.start == token1.start
88 assert loc.end == token2.end
89 assert loc.start_token is token1
90 assert loc.end_token is token2
91 assert loc.source is source
92
93 def can_stringify_with_start_and_end():
94 loc = Location(token1, token2, source)
95 assert str(loc) == "1:3"
96
97 def has_representation_with_start_and_end():
98 loc = Location(token1, token2, source)
99 assert repr(loc) == "<Location 1:3>"
100 assert inspect(loc) == repr(loc)
101
102 def can_check_equality():
103 loc1 = Location(token1, token2, source)
104 loc2 = Location(token1, token2, source)
105 assert loc2 == loc1
106 loc3 = Location(token1, token1, source)
107 assert loc3 != loc1
108 loc4 = Location(token2, token2, source)
109 assert loc4 != loc1
110 assert loc4 != loc3
111
112 def can_check_equality_with_tuple_or_list():
113 loc = Location(token1, token2, source)
114 assert loc == (1, 3)
115 assert loc == [1, 3]
116 assert not loc != (1, 3)
117 assert not loc != [1, 3]
118 assert loc != (1, 2)
119 assert loc != [2, 3]
120
121 def does_not_equal_incompatible_object():
122 loc = Location(token1, token2, source)
123 assert not loc == (1, 2, 3)
124 assert loc != (1, 2, 3)
125 assert not loc == {1: 2}
126 assert loc != {1: 2}
127
128 def can_hash():
129 loc1 = Location(token1, token2, source)
130 loc2 = Location(token1, token2, source)
131 assert loc2 == loc1
132 assert hash(loc2) == hash(loc1)
133 loc3 = Location(token1, token1, source)
134 assert loc3 != loc1
135 assert hash(loc3) != hash(loc1)
136 loc4 = Location(token2, token2, source)
137 assert loc4 != loc1
138 assert hash(loc4) != hash(loc1)
139 assert hash(loc4) != hash(loc3)
140
141
142 def describe_node_class():
143 def initializes_with_keywords():
144 node = SampleTestNode(alpha=1, beta=2, loc=0)
145 assert node.alpha == 1
146 assert node.beta == 2
147 assert node.loc == 0
148 node = SampleTestNode(alpha=1, loc=None)
149 assert node.loc is None
150 assert node.alpha == 1
151 assert node.beta is None
152 node = SampleTestNode(alpha=1, beta=2, gamma=3)
153 assert node.alpha == 1
154 assert node.beta == 2
155 assert not hasattr(node, "gamma")
156
157 def has_representation_with_loc():
158 node = SampleTestNode(alpha=1, beta=2)
159 assert repr(node) == "SampleTestNode"
160 node = SampleTestNode(alpha=1, beta=2, loc=3)
161 assert repr(node) == "SampleTestNode at 3"
162
163 def can_check_equality():
164 node = SampleTestNode(alpha=1, beta=2)
165 node2 = SampleTestNode(alpha=1, beta=2)
166 assert node2 == node
167 assert not node2 != node
168 node2 = SampleTestNode(alpha=1, beta=1)
169 assert node2 != node
170 node3 = Node(alpha=1, beta=2)
171 assert node3 != node
172
173 def can_hash():
174 node = SampleTestNode(alpha=1, beta=2)
175 node2 = SampleTestNode(alpha=1, beta=2)
176 assert node == node2
177 assert node2 is not node
178 assert hash(node2) == hash(node)
179 node3 = SampleTestNode(alpha=1, beta=3)
180 assert node3 != node
181 assert hash(node3) != hash(node)
182
183 def caches_are_hashed():
184 node = SampleTestNode(alpha=1)
185 assert not hasattr(node, "_hash")
186 hash1 = hash(node)
187 assert hasattr(node, "_hash")
188 assert hash1 == getattr(node, "_hash")
189 node.alpha = 2
190 assert not hasattr(node, "_hash")
191 hash2 = hash(node)
192 assert hash2 != hash1
193 assert hasattr(node, "_hash")
194 assert hash2 == getattr(node, "_hash")
195
196 def can_create_weak_reference():
197 node = SampleTestNode(alpha=1, beta=2)
198 ref = weakref.ref(node)
199 assert ref() is node
200
201 def can_create_custom_attribute():
202 node = SampleTestNode(alpha=1, beta=2)
203 node.gamma = 3
204 assert node.gamma == 3 # type: ignore
205
206 def can_create_shallow_copy():
207 node = SampleTestNode(alpha=1, beta=2)
208 node2 = copy(node)
209 assert node2 is not node
210 assert node2 == node
211
212 def shallow_copy_is_really_shallow():
213 node = SampleTestNode(alpha=1, beta=2)
214 node2 = SampleTestNode(alpha=node, beta=node)
215 node3 = copy(node2)
216 assert node3 is not node2
217 assert node3 == node2
218 assert node3.alpha is node2.alpha
219 assert node3.beta is node2.beta
220
221 def can_create_deep_copy():
222 alpha = SampleTestNode(alpha=1, beta=2)
223 beta = SampleTestNode(alpha=3, beta=4)
224 node = SampleTestNode(alpha=alpha, beta=beta)
225 node2 = deepcopy(node)
226 assert node2 is not node
227 assert node2 == node
228 assert node2.alpha == alpha
229 assert node2.alpha is not alpha
230 assert node2.alpha == alpha
231 assert node2.beta is not beta
232
233 def provides_snake_cased_kind_as_class_attribute():
234 assert SampleTestNode.kind == "sample_test"
235
236 def provides_proper_kind_if_class_does_not_end_with_node():
237 class Foo(Node):
238 pass
239
240 assert Foo.kind == "foo"
241
242 def provides_keys_as_class_attribute():
243 assert SampleTestNode.keys == ("loc", "alpha", "beta")
244
245 def can_can_convert_to_dict():
246 node = SampleTestNode(alpha=1, beta=2)
247 res = node.to_dict()
248 assert node.to_dict(locations=True) == res
249 assert res == {"kind": "sample_test", "alpha": 1, "beta": 2}
250 assert list(res) == ["kind", "alpha", "beta"]
251
252 def can_can_convert_to_dict_with_locations():
253 token = Token(
254 kind=TokenKind.NAME,
255 start=1,
256 end=3,
257 line=1,
258 column=1,
259 value="foo",
260 )
261 loc = Location(token, token, Source("foo"))
262 node = SampleTestNode(alpha=1, beta=2, loc=loc)
263 res = node.to_dict(locations=True)
264 assert res == {
265 "kind": "sample_test",
266 "alpha": 1,
267 "beta": 2,
268 "loc": {"start": 1, "end": 3},
269 }
270 assert list(res) == ["kind", "alpha", "beta", "loc"]
271 assert list(res["loc"]) == ["start", "end"]