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



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

def test_aliasing_namespaces
  index(<<~RUBY)
    module A
      module B
        module C
        end
      end

      ALIAS = B
    end

    module Other
      ONE_MORE = A::ALIAS
    end
  RUBY

  unresolve_entry = @index["A::ALIAS"].first
  assert_instance_of(Index::Entry::UnresolvedAlias, unresolve_entry)
  assert_equal(["A"], unresolve_entry.nesting)
  assert_equal("B", unresolve_entry.target)

  resolved_entry = @index.resolve("ALIAS", ["A"]).first
  assert_instance_of(Index::Entry::Alias, resolved_entry)
  assert_equal("A::B", resolved_entry.target)

  resolved_entry = @index.resolve("ALIAS::C", ["A"]).first
  assert_instance_of(Index::Entry::Module, resolved_entry)
  assert_equal("A::B::C", resolved_entry.name)

  unresolve_entry = @index["Other::ONE_MORE"].first
  assert_instance_of(Index::Entry::UnresolvedAlias, unresolve_entry)
  assert_equal(["Other"], unresolve_entry.nesting)
  assert_equal("A::ALIAS", unresolve_entry.target)

  resolved_entry = @index.resolve("Other::ONE_MORE::C", []).first
  assert_instance_of(Index::Entry::Module, resolved_entry)
end

#test_comments_for_constantsObject



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

def test_comments_for_constants
  index(<<~RUBY)
    # FOO comment
    FOO = 1

    class A
      # A::FOO comment
      FOO = 1

      # ::BAR comment
      ::BAR = 1
    end

    # A::BAZ comment
    A::BAZ = 1
  RUBY

  foo_comment = @index["FOO"].first.comments.join("\n")
  assert_equal("FOO comment", foo_comment)

  a_foo_comment = @index["A::FOO"].first.comments.join("\n")
  assert_equal("A::FOO comment", a_foo_comment)

  bar_comment = @index["BAR"].first.comments.join("\n")
  assert_equal("::BAR comment", bar_comment)

  a_baz_comment = @index["A::BAZ"].first.comments.join("\n")
  assert_equal("A::BAZ comment", a_baz_comment)
end

#test_constant_or_writesObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ruby_indexer/test/constant_test.rb', line 21

def test_constant_or_writes
  index(<<~RUBY)
    FOO ||= 1

    class ::Bar
      FOO ||= 2
    end
  RUBY

  assert_entry("FOO", Index::Entry::Constant, "/fake/path/foo.rb:0-0:0-9")
  assert_entry("Bar::FOO", Index::Entry::Constant, "/fake/path/foo.rb:3-2:3-11")
end

#test_constant_path_or_writesObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ruby_indexer/test/constant_test.rb', line 54

def test_constant_path_or_writes
  index(<<~RUBY)
    class A
      FOO ||= 1
      ::BAR ||= 1
    end

    A::BAZ ||= 1
  RUBY

  assert_entry("A::FOO", Index::Entry::Constant, "/fake/path/foo.rb:1-2:1-11")
  assert_entry("BAR", Index::Entry::Constant, "/fake/path/foo.rb:2-2:2-13")
  assert_entry("A::BAZ", Index::Entry::Constant, "/fake/path/foo.rb:5-0:5-12")
end

#test_constant_path_writesObject



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

def test_constant_path_writes
  index(<<~RUBY)
    class A
      FOO = 1
      ::BAR = 1

      module B
        FOO = 1
      end
    end

    A::BAZ = 1
  RUBY

  assert_entry("A::FOO", Index::Entry::Constant, "/fake/path/foo.rb:1-2:1-9")
  assert_entry("BAR", Index::Entry::Constant, "/fake/path/foo.rb:2-2:2-11")
  assert_entry("A::B::FOO", Index::Entry::Constant, "/fake/path/foo.rb:5-4:5-11")
  assert_entry("A::BAZ", Index::Entry::Constant, "/fake/path/foo.rb:9-0:9-10")
end

#test_constant_writesObject



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

def test_constant_writes
  index(<<~RUBY)
    FOO = 1

    class ::Bar
      FOO = 2
    end
  RUBY

  assert_entry("FOO", Index::Entry::Constant, "/fake/path/foo.rb:0-0:0-7")
  assert_entry("Bar::FOO", Index::Entry::Constant, "/fake/path/foo.rb:3-2:3-9")
end

#test_indexing_constant_aliasesObject



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

def test_indexing_constant_aliases
  index(<<~RUBY)
    module A
      module B
        module C
        end
      end

      FIRST = B::C
    end

    SECOND = A::FIRST
  RUBY

  unresolve_entry = @index["A::FIRST"].first
  assert_instance_of(Index::Entry::UnresolvedAlias, unresolve_entry)
  assert_equal(["A"], unresolve_entry.nesting)
  assert_equal("B::C", unresolve_entry.target)

  resolved_entry = @index.resolve("A::FIRST", []).first
  assert_instance_of(Index::Entry::Alias, resolved_entry)
  assert_equal("A::B::C", resolved_entry.target)
end

#test_indexing_same_line_constant_aliasesObject



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

def test_indexing_same_line_constant_aliases
  index(<<~RUBY)
    module A
      B = C = 1
      D = E ||= 1
      F = G::H &&= 1
      I::J = K::L = M = 1
    end
  RUBY

  # B and C
  unresolve_entry = @index["A::B"].first
  assert_instance_of(Index::Entry::UnresolvedAlias, unresolve_entry)
  assert_equal(["A"], unresolve_entry.nesting)
  assert_equal("C", unresolve_entry.target)

  resolved_entry = @index.resolve("A::B", []).first
  assert_instance_of(Index::Entry::Alias, resolved_entry)
  assert_equal("A::C", resolved_entry.target)

  constant = @index["A::C"].first
  assert_instance_of(Index::Entry::Constant, constant)

  # D and E
  unresolve_entry = @index["A::D"].first
  assert_instance_of(Index::Entry::UnresolvedAlias, unresolve_entry)
  assert_equal(["A"], unresolve_entry.nesting)
  assert_equal("E", unresolve_entry.target)

  resolved_entry = @index.resolve("A::D", []).first
  assert_instance_of(Index::Entry::Alias, resolved_entry)
  assert_equal("A::E", resolved_entry.target)

  # F and G::H
  unresolve_entry = @index["A::F"].first
  assert_instance_of(Index::Entry::UnresolvedAlias, unresolve_entry)
  assert_equal(["A"], unresolve_entry.nesting)
  assert_equal("G::H", unresolve_entry.target)

  resolved_entry = @index.resolve("A::F", []).first
  assert_instance_of(Index::Entry::Alias, resolved_entry)
  assert_equal("A::G::H", resolved_entry.target)

  # I::J, K::L and M
  unresolve_entry = @index["A::I::J"].first
  assert_instance_of(Index::Entry::UnresolvedAlias, unresolve_entry)
  assert_equal(["A"], unresolve_entry.nesting)
  assert_equal("K::L", unresolve_entry.target)

  resolved_entry = @index.resolve("A::I::J", []).first
  assert_instance_of(Index::Entry::Alias, resolved_entry)
  assert_equal("A::K::L", resolved_entry.target)

  # When we are resolving A::I::J, we invoke `resolve("K::L", ["A"])`, which recursively resolves A::K::L too.
  # Therefore, both A::I::J and A::K::L point to A::M by the end of the previous resolve invocation
  resolved_entry = @index["A::K::L"].first
  assert_instance_of(Index::Entry::Alias, resolved_entry)
  assert_equal("A::M", resolved_entry.target)

  constant = @index["A::M"].first
  assert_instance_of(Index::Entry::Constant, constant)
end

#test_marking_constants_as_private_reopening_namespacesObject



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

def test_marking_constants_as_private_reopening_namespaces
  index(<<~RUBY)
    module A
      module B
        CONST_A = 1
        private_constant(:CONST_A)

        CONST_B = 2
        CONST_C = 3
      end

      module B
        private_constant(:CONST_B)
      end
    end

    module A
      module B
        private_constant(:CONST_C)
      end
    end
  RUBY

  a_const = @index["A::B::CONST_A"].first
  assert_equal(:private, a_const.visibility)

  b_const = @index["A::B::CONST_B"].first
  assert_equal(:private, b_const.visibility)

  c_const = @index["A::B::CONST_C"].first
  assert_equal(:private, c_const.visibility)
end

#test_marking_constants_as_private_with_receiverObject



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

def test_marking_constants_as_private_with_receiver
  index(<<~RUBY)
    module A
      module B
        CONST_A = 1
        CONST_B = 2
      end

      B.private_constant(:CONST_A)
    end

    A::B.private_constant(:CONST_B)
  RUBY

  a_const = @index["A::B::CONST_A"].first
  assert_equal(:private, a_const.visibility)

  b_const = @index["A::B::CONST_B"].first
  assert_equal(:private, b_const.visibility)
end

#test_private_constant_indexingObject



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

def test_private_constant_indexing
  index(<<~RUBY)
    class A
      B = 1
      private_constant(:B)

      C = 2
      private_constant("C")

      D = 1
    end
  RUBY

  b_const = @index["A::B"].first
  assert_equal(:private, b_const.visibility)

  c_const = @index["A::C"].first
  assert_equal(:private, c_const.visibility)

  d_const = @index["A::D"].first
  assert_equal(:public, d_const.visibility)
end

#test_variable_path_constants_are_ignoredObject



99
100
101
102
103
104
105
106
# File 'lib/ruby_indexer/test/constant_test.rb', line 99

def test_variable_path_constants_are_ignored
  index(<<~RUBY)
    var::FOO = 1
    self.class::FOO = 1
  RUBY

  assert_no_entry
end