-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathnode_test.rb
252 lines (202 loc) · 8.98 KB
/
node_test.rb
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# typed: ignore
# frozen_string_literal: true
require "test_helper"
require "parser_test_helper"
require "parser"
module Packwerk
class NodeTest < ActiveSupport::TestCase
test ".class_or_module_name returns the name of a class being defined with the class keyword" do
node = parse("class My::Class; end")
assert_equal "My::Class", Node.class_or_module_name(node)
end
test ".class_or_module_name returns the name of a module being defined with the module keyword" do
node = parse("module My::Module; end")
assert_equal "My::Module", Node.class_or_module_name(node)
end
test ".constant_name returns the name of a constant being referenced" do
node = parse("My::Constant")
assert_equal "My::Constant", Node.constant_name(node)
end
test ".constant_name returns the name of a constant being assigned to" do
node = parse("My::Constant = 42")
assert_equal "My::Constant", Node.constant_name(node)
end
test ".constant_name raises a TypeError for dynamically namespaced constants" do
node = parse("self.class::HEADERS")
assert_raises(Node::TypeError) { Node.constant_name(node) }
end
test ".constant_name preserves the name of a fully-qualified constant" do
node = parse("::My::Constant")
assert_equal "::My::Constant", Node.constant_name(node)
end
test ".each_child with a block iterates over all child nodes" do
node = parse("My::Constant = 6 * 7")
children = []
Node.each_child(node) do |child|
children << child
end
assert_equal [parse("My"), parse("6 * 7")], children
end
test ".each_child without a block returns an enumerator" do
node = parse("My::Constant = 6 * 7")
children = Node.each_child(node)
assert_instance_of Enumerator, children
assert_equal [parse("My"), parse("6 * 7")], children.entries
end
test "#enclosing_namespace_path should return empty path for const node" do
node = parse("Order")
path = Node.enclosing_namespace_path(node, ancestors: [])
assert_equal [], path
end
test "#enclosing_namespace_path should return correct path for simple class definition" do
parent = parse("class Order; end")
node = Node.each_child(parent).entries[0]
path = Node.enclosing_namespace_path(node, ancestors: [parent])
assert_equal ["Order"], path
end
test "#enclosing_namespace_path should skip child class name when finding path for parent class" do
grandparent = parse("module Sales; class Order < Base; end; end")
parent = Node.each_child(grandparent).entries[1] # module node; second child is the body of the module
node = Node.each_child(parent).entries[1] # class node; second child is parent
path = Node.enclosing_namespace_path(node, ancestors: [parent, grandparent])
assert_equal ["Sales"], path
end
test "#enclosing_namespace_path should return correct path for nested and compact class definition" do
grandparent = parse("module Foo::Bar; class Sales::Order; end; end")
parent = Node.each_child(grandparent).entries[1] # module node; second child is the body of the module
node = Node.each_child(parent).entries[0] # class node; first child is constant
path = Node.enclosing_namespace_path(node, ancestors: [parent, grandparent])
assert_equal ["Foo::Bar", "Sales::Order"], path
end
test ".literal_value returns the value of a string node" do
node = parse("'Hello'")
assert_equal "Hello", Node.literal_value(node)
end
test ".literal_value returns the value of a symbol node" do
node = parse(":world")
assert_equal :world, Node.literal_value(node)
end
test ".location returns a source location" do
node = parse("HELLO = 'World'")
assert_kind_of Node::Location, Node.location(node)
end
test ".method_arguments returns the arguments of a method call" do
node = parse("a.b(:c, 'd', E)")
assert_equal [parse(":c"), parse("'d'"), parse("E")], Node.method_arguments(node)
end
test ".method_name returns the name of a method call" do
node = parse("a.b(:c, 'd', E)")
assert_equal :b, Node.method_name(node)
end
test ".module_name_from_definition returns the name of the class being defined" do
[
["class MyClass; end", "MyClass"],
["class ::MyClass; end", "::MyClass"],
["class My::Class; end", "My::Class"],
["My::Class = Class.new", "My::Class"],
["My::Class = Class.new do end", "My::Class"],
].each do |class_definition, name|
node = parse(class_definition)
assert_equal name, Node.module_name_from_definition(node)
end
end
test ".module_name_from_definition returns the name of the module being defined" do
[
["module MyModule; end", "MyModule"],
["module ::MyModule; end", "::MyModule"],
["module My::Module; end", "My::Module"],
["My::Module = Module.new", "My::Module"],
["My::Module = Module.new do end", "My::Module"],
].each do |module_definition, name|
node = parse(module_definition)
assert_equal name, Node.module_name_from_definition(node)
end
end
test ".module_name_from_definition returns nil if no class or module is being defined" do
[
"'Hello'",
"MyConstant",
"MyObject = Object.new",
"MyFile = File.new do end",
"-> x { x * 2 }",
"Class.new",
"Class.new do end",
].each do |module_definition|
node = parse(module_definition)
assert_nil Node.module_name_from_definition(node)
end
end
test ".name_location returns a source location for a constant" do
node = parse("HELLO")
assert_kind_of Node::Location, Node.name_location(node)
end
test ".name_location returns a source location for a constant assignment" do
node = parse("HELLO = 'World'")
assert_kind_of Node::Location, Node.name_location(node)
end
test ".name_location returns nil for a method call" do
node = parse("has_many :hellos")
assert_nil Node.name_location(node)
end
test ".parent_class returns the constant referring to the parent class in a class being defined with the class keyword" do
node = parse("class B < A; end")
assert_equal parse("A"), Node.parent_class(node)
end
test ".parent_module_name returns the name of a constant’s enclosing module" do
grandparent = parse("module A; class B; C; end end")
parent = Node.each_child(grandparent).entries[1] # "class B; C; end"
assert_equal "A::B", Node.parent_module_name(ancestors: [parent, grandparent])
end
test ".parent_module_name returns Object if the constant has no enclosing module" do
assert_equal "Object", Node.parent_module_name(ancestors: [])
end
test ".parent_module_name supports constant assignment" do
grandparent = parse("module A; B = Class.new do C end end")
parent = Node.each_child(grandparent).entries[1] # "B = Class.new do C end"
assert_equal "A::B", Node.parent_module_name(ancestors: [parent, grandparent])
end
test ".parent_module_name supports class_eval with no receiver" do
grandparent = parse("module A; class_eval do C; end end")
parent = Node.each_child(grandparent).entries[1] # "class_eval do C; end"
assert_equal "A", Node.parent_module_name(ancestors: [parent, grandparent])
end
test ".parent_module_name supports class_eval with an explicit receiver" do
grandparent = parse("module A; B.class_eval do C; end end")
parent = Node.each_child(grandparent).entries[1] # "B.class_eval do C; end"
assert_equal "A::B", Node.parent_module_name(ancestors: [parent, grandparent])
end
test ".class? can identify a class node" do
assert Node.class?(parse("class Fruit; end"))
end
test ".constant? can identify a constant node" do
assert Node.constant?(parse("Oranges"))
end
test ".constant_assignment? can identify a constant assignment node" do
assert Node.constant_assignment?(parse("Apples = 13"))
end
test ".hash? can identify a hash node" do
assert Node.hash?(parse("{ pears: 3, bananas: 6 }"))
end
test ".method_call? can identify a method call node" do
assert Node.method_call?(parse("quantity(bananas)"))
end
test ".string? can identify a string node" do
assert Node.string?(parse("'cashew apple'"))
end
test ".symbol? can identify a symbol node" do
assert Node.symbol?(parse(":papaya"))
end
test ".value_from_hash looks up the node for a key in a hash" do
hash_node = parse("{ apples: 13, oranges: 27 }")
assert_equal parse("13"), Node.value_from_hash(hash_node, :apples)
end
test ".value_from_hash returns nil if a key isn't found in a hash" do
hash_node = parse("{ apples: 13, oranges: 27 }")
assert_nil Node.value_from_hash(hash_node, :pears)
end
private
def parse(string)
ParserTestHelper.parse(string)
end
end
end