Class: RubyIndexer::ConstantTest

Inherits:
TestCase
  • Object
show all
Defined in:
lib/ruby_indexer/test/constant_test.rb

Instance Method Summary collapse

Methods inherited from TestCase

#setup

Instance Method Details

#test_aliasing_namespacesObject



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
# File 'lib/ruby_indexer/test/constant_test.rb', line 212

def test_aliasing_namespaces
  index("    module A\n      module B\n        module C\n        end\n      end\n\n      ALIAS = B\n    end\n\n    module Other\n      ONE_MORE = A::ALIAS\n    end\n  RUBY\n\n  unresolve_entry = @index[\"A::ALIAS\"].first\n  assert_instance_of(Entry::UnresolvedConstantAlias, unresolve_entry)\n  assert_equal([\"A\"], unresolve_entry.nesting)\n  assert_equal(\"B\", unresolve_entry.target)\n\n  resolved_entry = @index.resolve(\"ALIAS\", [\"A\"]).first\n  assert_instance_of(Entry::ConstantAlias, resolved_entry)\n  assert_equal(\"A::B\", resolved_entry.target)\n\n  resolved_entry = @index.resolve(\"ALIAS::C\", [\"A\"]).first\n  assert_instance_of(Entry::Module, resolved_entry)\n  assert_equal(\"A::B::C\", resolved_entry.name)\n\n  unresolve_entry = @index[\"Other::ONE_MORE\"].first\n  assert_instance_of(Entry::UnresolvedConstantAlias, unresolve_entry)\n  assert_equal([\"Other\"], unresolve_entry.nesting)\n  assert_equal(\"A::ALIAS\", unresolve_entry.target)\n\n  resolved_entry = @index.resolve(\"Other::ONE_MORE::C\", []).first\n  assert_instance_of(Entry::Module, resolved_entry)\nend\n")

#test_comments_for_constantsObject



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
# File 'lib/ruby_indexer/test/constant_test.rb', line 72

def test_comments_for_constants
  index("    # FOO comment\n    FOO = 1\n\n    class A\n      # A::FOO comment\n      FOO = 1\n\n      # ::BAR comment\n      ::BAR = 1\n    end\n\n    # A::BAZ comment\n    A::BAZ = 1\n  RUBY\n\n  foo_comment = @index[\"FOO\"].first.comments.join(\"\\n\")\n  assert_equal(\"FOO comment\", foo_comment)\n\n  a_foo_comment = @index[\"A::FOO\"].first.comments.join(\"\\n\")\n  assert_equal(\"A::FOO comment\", a_foo_comment)\n\n  bar_comment = @index[\"BAR\"].first.comments.join(\"\\n\")\n  assert_equal(\"::BAR comment\", bar_comment)\n\n  a_baz_comment = @index[\"A::BAZ\"].first.comments.join(\"\\n\")\n  assert_equal(\"A::BAZ comment\", a_baz_comment)\nend\n")

#test_constant_or_writesObject



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ruby_indexer/test/constant_test.rb', line 24

def test_constant_or_writes
  index("    FOO ||= 1\n\n    class ::Bar\n      FOO ||= 2\n    end\n  RUBY\n\n  assert_entry(\"FOO\", Entry::Constant, \"/fake/path/foo.rb:0-0:0-9\")\n  assert_entry(\"Bar::FOO\", Entry::Constant, \"/fake/path/foo.rb:3-2:3-11\")\nend\n")

#test_constant_path_or_writesObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ruby_indexer/test/constant_test.rb', line 57

def test_constant_path_or_writes
  index("    class A\n      FOO ||= 1\n      ::BAR ||= 1\n    end\n\n    A::BAZ ||= 1\n  RUBY\n\n  assert_entry(\"A::FOO\", Entry::Constant, \"/fake/path/foo.rb:1-2:1-11\")\n  assert_entry(\"BAR\", Entry::Constant, \"/fake/path/foo.rb:2-2:2-13\")\n  assert_entry(\"A::BAZ\", Entry::Constant, \"/fake/path/foo.rb:5-0:5-12\")\nend\n")

#test_constant_path_writesObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ruby_indexer/test/constant_test.rb', line 37

def test_constant_path_writes
  index("    class A\n      FOO = 1\n      ::BAR = 1\n\n      module B\n        FOO = 1\n      end\n    end\n\n    A::BAZ = 1\n  RUBY\n\n  assert_entry(\"A::FOO\", Entry::Constant, \"/fake/path/foo.rb:1-2:1-9\")\n  assert_entry(\"BAR\", Entry::Constant, \"/fake/path/foo.rb:2-2:2-11\")\n  assert_entry(\"A::B::FOO\", Entry::Constant, \"/fake/path/foo.rb:5-4:5-11\")\n  assert_entry(\"A::BAZ\", Entry::Constant, \"/fake/path/foo.rb:9-0:9-10\")\nend\n")

#test_constant_writesObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ruby_indexer/test/constant_test.rb', line 8

def test_constant_writes
  index("    FOO = 1\n\n    class ::Bar\n      FOO = 2\n    end\n\n    BAR = 3 if condition\n  RUBY\n\n  assert_entry(\"FOO\", Entry::Constant, \"/fake/path/foo.rb:0-0:0-7\")\n  assert_entry(\"Bar::FOO\", Entry::Constant, \"/fake/path/foo.rb:3-2:3-9\")\n  assert_entry(\"BAR\", Entry::Constant, \"/fake/path/foo.rb:6-0:6-7\")\nend\n")

#test_indexing_constant_aliasesObject



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/ruby_indexer/test/constant_test.rb', line 188

def test_indexing_constant_aliases
  index("    module A\n      module B\n        module C\n        end\n      end\n\n      FIRST = B::C\n    end\n\n    SECOND = A::FIRST\n  RUBY\n\n  unresolve_entry = @index[\"A::FIRST\"].first\n  assert_instance_of(Entry::UnresolvedConstantAlias, unresolve_entry)\n  assert_equal([\"A\"], unresolve_entry.nesting)\n  assert_equal(\"B::C\", unresolve_entry.target)\n\n  resolved_entry = @index.resolve(\"A::FIRST\", []).first\n  assert_instance_of(Entry::ConstantAlias, resolved_entry)\n  assert_equal(\"A::B::C\", resolved_entry.target)\nend\n")

#test_indexing_constant_targetsObject



331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/ruby_indexer/test/constant_test.rb', line 331

def test_indexing_constant_targets
  index("    module A\n      B, C = [1, Y]\n      D::E, F::G = [Z, 4]\n      H, I::J = [5, B]\n      K, L = C\n    end\n\n    module Real\n      Z = 1\n      Y = 2\n    end\n  RUBY\n\n  assert_entry(\"A::B\", Entry::Constant, \"/fake/path/foo.rb:1-2:1-3\")\n  assert_entry(\"A::C\", Entry::UnresolvedConstantAlias, \"/fake/path/foo.rb:1-5:1-6\")\n  assert_entry(\"A::D::E\", Entry::UnresolvedConstantAlias, \"/fake/path/foo.rb:2-2:2-6\")\n  assert_entry(\"A::F::G\", Entry::Constant, \"/fake/path/foo.rb:2-8:2-12\")\n  assert_entry(\"A::H\", Entry::Constant, \"/fake/path/foo.rb:3-2:3-3\")\n  assert_entry(\"A::I::J\", Entry::UnresolvedConstantAlias, \"/fake/path/foo.rb:3-5:3-9\")\n  assert_entry(\"A::K\", Entry::Constant, \"/fake/path/foo.rb:4-2:4-3\")\n  assert_entry(\"A::L\", Entry::Constant, \"/fake/path/foo.rb:4-5:4-6\")\nend\n")

#test_indexing_constant_targets_with_splatsObject



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/ruby_indexer/test/constant_test.rb', line 356

def test_indexing_constant_targets_with_splats
  index("    A, *, B = baz\n    C, = bar\n    (D, E) = baz\n    F, G = *baz, qux\n    H, I = [baz, *qux]\n    J, L = [*something, String]\n    M = [String]\n  RUBY\n\n  assert_entry(\"A\", Entry::Constant, \"/fake/path/foo.rb:0-0:0-1\")\n  assert_entry(\"B\", Entry::Constant, \"/fake/path/foo.rb:0-6:0-7\")\n  assert_entry(\"D\", Entry::Constant, \"/fake/path/foo.rb:2-1:2-2\")\n  assert_entry(\"E\", Entry::Constant, \"/fake/path/foo.rb:2-4:2-5\")\n  assert_entry(\"F\", Entry::Constant, \"/fake/path/foo.rb:3-0:3-1\")\n  assert_entry(\"G\", Entry::Constant, \"/fake/path/foo.rb:3-3:3-4\")\n  assert_entry(\"H\", Entry::Constant, \"/fake/path/foo.rb:4-0:4-1\")\n  assert_entry(\"I\", Entry::Constant, \"/fake/path/foo.rb:4-3:4-4\")\n  assert_entry(\"J\", Entry::Constant, \"/fake/path/foo.rb:5-0:5-1\")\n  assert_entry(\"L\", Entry::Constant, \"/fake/path/foo.rb:5-3:5-4\")\n  assert_entry(\"M\", Entry::Constant, \"/fake/path/foo.rb:6-0:6-12\")\nend\n")

#test_indexing_destructuring_an_arrayObject



380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/ruby_indexer/test/constant_test.rb', line 380

def test_indexing_destructuring_an_array
  index("    Baz = [1, 2]\n    Foo, Bar = Baz\n    This, That = foo, bar\n  RUBY\n\n  assert_entry(\"Baz\", Entry::Constant, \"/fake/path/foo.rb:0-0:0-12\")\n  assert_entry(\"Foo\", Entry::Constant, \"/fake/path/foo.rb:1-0:1-3\")\n  assert_entry(\"Bar\", Entry::Constant, \"/fake/path/foo.rb:1-5:1-8\")\n  assert_entry(\"This\", Entry::Constant, \"/fake/path/foo.rb:2-0:2-4\")\n  assert_entry(\"That\", Entry::Constant, \"/fake/path/foo.rb:2-6:2-10\")\nend\n")

#test_indexing_or_and_operator_nodesObject



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/ruby_indexer/test/constant_test.rb', line 313

def test_indexing_or_and_operator_nodes
  index("    A ||= 1\n    B &&= 2\n    C &= 3\n    D::E ||= 4\n    F::G &&= 5\n    H::I &= 6\n  RUBY\n\n  assert_entry(\"A\", Entry::Constant, \"/fake/path/foo.rb:0-0:0-7\")\n  assert_entry(\"B\", Entry::Constant, \"/fake/path/foo.rb:1-0:1-7\")\n  assert_entry(\"C\", Entry::Constant, \"/fake/path/foo.rb:2-0:2-6\")\n  assert_entry(\"D::E\", Entry::Constant, \"/fake/path/foo.rb:3-0:3-10\")\n  assert_entry(\"F::G\", Entry::Constant, \"/fake/path/foo.rb:4-0:4-10\")\n  assert_entry(\"H::I\", Entry::Constant, \"/fake/path/foo.rb:5-0:5-9\")\nend\n")

#test_indexing_same_line_constant_aliasesObject



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/ruby_indexer/test/constant_test.rb', line 250

def test_indexing_same_line_constant_aliases
  index("    module A\n      B = C = 1\n      D = E ||= 1\n      F = G::H &&= 1\n      I::J = K::L = M = 1\n    end\n  RUBY\n\n  # B and C\n  unresolve_entry = @index[\"A::B\"].first\n  assert_instance_of(Entry::UnresolvedConstantAlias, unresolve_entry)\n  assert_equal([\"A\"], unresolve_entry.nesting)\n  assert_equal(\"C\", unresolve_entry.target)\n\n  resolved_entry = @index.resolve(\"A::B\", []).first\n  assert_instance_of(Entry::ConstantAlias, resolved_entry)\n  assert_equal(\"A::C\", resolved_entry.target)\n\n  constant = @index[\"A::C\"].first\n  assert_instance_of(Entry::Constant, constant)\n\n  # D and E\n  unresolve_entry = @index[\"A::D\"].first\n  assert_instance_of(Entry::UnresolvedConstantAlias, unresolve_entry)\n  assert_equal([\"A\"], unresolve_entry.nesting)\n  assert_equal(\"E\", unresolve_entry.target)\n\n  resolved_entry = @index.resolve(\"A::D\", []).first\n  assert_instance_of(Entry::ConstantAlias, resolved_entry)\n  assert_equal(\"A::E\", resolved_entry.target)\n\n  # F and G::H\n  unresolve_entry = @index[\"A::F\"].first\n  assert_instance_of(Entry::UnresolvedConstantAlias, unresolve_entry)\n  assert_equal([\"A\"], unresolve_entry.nesting)\n  assert_equal(\"G::H\", unresolve_entry.target)\n\n  resolved_entry = @index.resolve(\"A::F\", []).first\n  assert_instance_of(Entry::ConstantAlias, resolved_entry)\n  assert_equal(\"A::G::H\", resolved_entry.target)\n\n  # I::J, K::L and M\n  unresolve_entry = @index[\"A::I::J\"].first\n  assert_instance_of(Entry::UnresolvedConstantAlias, unresolve_entry)\n  assert_equal([\"A\"], unresolve_entry.nesting)\n  assert_equal(\"K::L\", unresolve_entry.target)\n\n  resolved_entry = @index.resolve(\"A::I::J\", []).first\n  assert_instance_of(Entry::ConstantAlias, resolved_entry)\n  assert_equal(\"A::K::L\", resolved_entry.target)\n\n  # When we are resolving A::I::J, we invoke `resolve(\"K::L\", [\"A\"])`, which recursively resolves A::K::L too.\n  # Therefore, both A::I::J and A::K::L point to A::M by the end of the previous resolve invocation\n  resolved_entry = @index[\"A::K::L\"].first\n  assert_instance_of(Entry::ConstantAlias, resolved_entry)\n  assert_equal(\"A::M\", resolved_entry.target)\n\n  constant = @index[\"A::M\"].first\n  assert_instance_of(Entry::Constant, constant)\nend\n")

#test_marking_constants_as_private_reopening_namespacesObject



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
# File 'lib/ruby_indexer/test/constant_test.rb', line 134

def test_marking_constants_as_private_reopening_namespaces
  index("    module A\n      module B\n        CONST_A = 1\n        private_constant(:CONST_A)\n\n        CONST_B = 2\n        CONST_C = 3\n      end\n\n      module B\n        private_constant(:CONST_B)\n      end\n    end\n\n    module A\n      module B\n        private_constant(:CONST_C)\n      end\n    end\n  RUBY\n\n  a_const = @index[\"A::B::CONST_A\"].first\n  assert_equal(Entry::Visibility::PRIVATE, a_const.visibility)\n\n  b_const = @index[\"A::B::CONST_B\"].first\n  assert_equal(Entry::Visibility::PRIVATE, b_const.visibility)\n\n  c_const = @index[\"A::B::CONST_C\"].first\n  assert_equal(Entry::Visibility::PRIVATE, c_const.visibility)\nend\n")

#test_marking_constants_as_private_with_receiverObject



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/ruby_indexer/test/constant_test.rb', line 167

def test_marking_constants_as_private_with_receiver
  index("    module A\n      module B\n        CONST_A = 1\n        CONST_B = 2\n      end\n\n      B.private_constant(:CONST_A)\n    end\n\n    A::B.private_constant(:CONST_B)\n  RUBY\n\n  a_const = @index[\"A::B::CONST_A\"].first\n  assert_equal(Entry::Visibility::PRIVATE, a_const.visibility)\n\n  b_const = @index[\"A::B::CONST_B\"].first\n  assert_equal(Entry::Visibility::PRIVATE, b_const.visibility)\nend\n")

#test_private_constant_indexingObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/ruby_indexer/test/constant_test.rb', line 111

def test_private_constant_indexing
  index("    class A\n      B = 1\n      private_constant(:B)\n\n      C = 2\n      private_constant(\"C\")\n\n      D = 1\n    end\n  RUBY\n\n  b_const = @index[\"A::B\"].first\n  assert_equal(Entry::Visibility::PRIVATE, b_const.visibility)\n\n  c_const = @index[\"A::C\"].first\n  assert_equal(Entry::Visibility::PRIVATE, c_const.visibility)\n\n  d_const = @index[\"A::D\"].first\n  assert_equal(Entry::Visibility::PUBLIC, d_const.visibility)\nend\n")

#test_variable_path_constants_are_ignoredObject



102
103
104
105
106
107
108
109
# File 'lib/ruby_indexer/test/constant_test.rb', line 102

def test_variable_path_constants_are_ignored
  index("    var::FOO = 1\n    self.class::FOO = 1\n  RUBY\n\n  assert_no_indexed_entries\nend\n")