Class: RubyIndexer::MethodTest

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

Instance Method Summary collapse

Methods inherited from TestCase

#setup

Instance Method Details

#test_ignores_attributes_invoked_on_constantObject



278
279
280
281
282
283
284
285
286
287
# File 'lib/ruby_indexer/test/method_test.rb', line 278

def test_ignores_attributes_invoked_on_constant
  index(<<~RUBY)
    class Foo
    end

    Foo.attr_reader :bar
  RUBY

  assert_no_entry("bar")
end

#test_keeps_track_of_attributesObject



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/ruby_indexer/test/method_test.rb', line 259

def test_keeps_track_of_attributes
  index(<<~RUBY)
    class Foo
      # Hello there
      attr_reader :bar, :other
      attr_writer :baz
      attr_accessor :qux
    end
  RUBY

  assert_entry("bar", Entry::Accessor, "/fake/path/foo.rb:2-15:2-18")
  assert_equal("Hello there", @index["bar"].first.comments.join("\n"))
  assert_entry("other", Entry::Accessor, "/fake/path/foo.rb:2-21:2-26")
  assert_equal("Hello there", @index["other"].first.comments.join("\n"))
  assert_entry("baz=", Entry::Accessor, "/fake/path/foo.rb:3-15:3-18")
  assert_entry("qux", Entry::Accessor, "/fake/path/foo.rb:4-17:4-20")
  assert_entry("qux=", Entry::Accessor, "/fake/path/foo.rb:4-17:4-20")
end

#test_keeps_track_of_method_ownerObject



245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/ruby_indexer/test/method_test.rb', line 245

def test_keeps_track_of_method_owner
  index(<<~RUBY)
    class Foo
      def bar
      end
    end
  RUBY

  entry = T.must(@index["bar"].first)
  owner_name = T.must(entry.owner).name

  assert_equal("Foo", owner_name)
end

#test_method_with_anonymous_rest_parametersObject



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/ruby_indexer/test/method_test.rb', line 212

def test_method_with_anonymous_rest_parameters
  index(<<~RUBY)
    class Foo
      def bar(*, **)
      end
    end
  RUBY

  assert_entry("bar", Entry::InstanceMethod, "/fake/path/foo.rb:1-2:2-5")
  entry = T.must(@index["bar"].first)
  assert_equal(2, entry.parameters.length)
  first, second = entry.parameters

  assert_equal(Entry::RestParameter::DEFAULT_NAME, first.name)
  assert_instance_of(Entry::RestParameter, first)

  assert_equal(Entry::KeywordRestParameter::DEFAULT_NAME, second.name)
  assert_instance_of(Entry::KeywordRestParameter, second)
end

#test_method_with_block_parametersObject



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/method_test.rb', line 188

def test_method_with_block_parameters
  index(<<~RUBY)
    class Foo
      def bar(&block)
      end

      def baz(&)
      end
    end
  RUBY

  entry = T.must(@index["bar"].first)
  param = entry.parameters.first
  assert_equal(:block, param.name)
  assert_instance_of(Entry::BlockParameter, param)

  entry = T.must(@index["baz"].first)
  assert_equal(1, entry.parameters.length)

  param = entry.parameters.first
  assert_equal(Entry::BlockParameter::DEFAULT_NAME, param.name)
  assert_instance_of(Entry::BlockParameter, param)
end

#test_method_with_destructed_parametersObject



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

def test_method_with_destructed_parameters
  index(<<~RUBY)
    class Foo
      def bar((a, (b, )))
      end
    end
  RUBY

  assert_entry("bar", Entry::InstanceMethod, "/fake/path/foo.rb:1-2:2-5")
  entry = T.must(@index["bar"].first)
  assert_equal(1, entry.parameters.length)
  parameter = entry.parameters.first
  assert_equal(:"(a, (b, ))", parameter.name)
  assert_instance_of(Entry::RequiredParameter, parameter)
end

#test_method_with_destructured_rest_parametersObject



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

def test_method_with_destructured_rest_parameters
  index(<<~RUBY)
    class Foo
      def bar((a, *b))
      end
    end
  RUBY

  assert_entry("bar", Entry::InstanceMethod, "/fake/path/foo.rb:1-2:2-5")
  entry = T.must(@index["bar"].first)
  assert_equal(1, entry.parameters.length)
  param = entry.parameters.first

  assert_equal(:"(a, *b)", param.name)
  assert_instance_of(Entry::RequiredParameter, param)
end

#test_method_with_forbidden_keyword_splat_parameterObject



232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/ruby_indexer/test/method_test.rb', line 232

def test_method_with_forbidden_keyword_splat_parameter
  index(<<~RUBY)
    class Foo
      def bar(**nil)
      end
    end
  RUBY

  assert_entry("bar", Entry::InstanceMethod, "/fake/path/foo.rb:1-2:2-5")
  entry = T.must(@index["bar"].first)
  assert_empty(entry.parameters)
end

#test_method_with_keyword_parametersObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ruby_indexer/test/method_test.rb', line 89

def test_method_with_keyword_parameters
  index(<<~RUBY)
    class Foo
      def bar(a:, b: 123)
      end
    end
  RUBY

  assert_entry("bar", Entry::InstanceMethod, "/fake/path/foo.rb:1-2:2-5")
  entry = T.must(@index["bar"].first)
  assert_equal(2, entry.parameters.length)
  a, b = entry.parameters

  assert_equal(:a, a.name)
  assert_instance_of(Entry::KeywordParameter, a)

  assert_equal(:b, b.name)
  assert_instance_of(Entry::OptionalKeywordParameter, b)
end

#test_method_with_no_parametersObject



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

def test_method_with_no_parameters
  index(<<~RUBY)
    class Foo
      def bar
      end
    end
  RUBY

  assert_entry("bar", Entry::InstanceMethod, "/fake/path/foo.rb:1-2:2-5")
end

#test_method_with_optional_parametersObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ruby_indexer/test/method_test.rb', line 73

def test_method_with_optional_parameters
  index(<<~RUBY)
    class Foo
      def bar(a = 123)
      end
    end
  RUBY

  assert_entry("bar", Entry::InstanceMethod, "/fake/path/foo.rb:1-2:2-5")
  entry = T.must(@index["bar"].first)
  assert_equal(1, entry.parameters.length)
  parameter = entry.parameters.first
  assert_equal(:a, parameter.name)
  assert_instance_of(Entry::OptionalParameter, parameter)
end

#test_method_with_parametersObject



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

def test_method_with_parameters
  index(<<~RUBY)
    class Foo
      def bar(a)
      end
    end
  RUBY

  assert_entry("bar", Entry::InstanceMethod, "/fake/path/foo.rb:1-2:2-5")
  entry = T.must(@index["bar"].first)
  assert_equal(1, entry.parameters.length)
  parameter = entry.parameters.first
  assert_equal(:a, parameter.name)
  assert_instance_of(Entry::RequiredParameter, parameter)
end

#test_method_with_post_parametersObject



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

def test_method_with_post_parameters
  index(<<~RUBY)
    class Foo
      def bar(*a, b)
      end

      def baz(**a, b)
      end

      def qux(*a, (b, c))
    end
  RUBY

  assert_entry("bar", Entry::InstanceMethod, "/fake/path/foo.rb:1-2:2-5")
  entry = T.must(@index["bar"].first)
  assert_equal(2, entry.parameters.length)
  a, b = entry.parameters

  assert_equal(:a, a.name)
  assert_instance_of(Entry::RestParameter, a)

  assert_equal(:b, b.name)
  assert_instance_of(Entry::RequiredParameter, b)

  entry = T.must(@index["baz"].first)
  assert_equal(2, entry.parameters.length)
  a, b = entry.parameters

  assert_equal(:a, a.name)
  assert_instance_of(Entry::KeywordRestParameter, a)

  assert_equal(:b, b.name)
  assert_instance_of(Entry::RequiredParameter, b)

  entry = T.must(@index["qux"].first)
  assert_equal(2, entry.parameters.length)
  _a, second = entry.parameters

  assert_equal(:"(b, c)", second.name)
  assert_instance_of(Entry::RequiredParameter, second)
end

#test_method_with_rest_and_keyword_rest_parametersObject



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

def test_method_with_rest_and_keyword_rest_parameters
  index(<<~RUBY)
    class Foo
      def bar(*a, **b)
      end
    end
  RUBY

  assert_entry("bar", Entry::InstanceMethod, "/fake/path/foo.rb:1-2:2-5")
  entry = T.must(@index["bar"].first)
  assert_equal(2, entry.parameters.length)
  a, b = entry.parameters

  assert_equal(:a, a.name)
  assert_instance_of(Entry::RestParameter, a)

  assert_equal(:b, b.name)
  assert_instance_of(Entry::KeywordRestParameter, b)
end

#test_singleton_method_using_other_receiver_is_not_indexedObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/ruby_indexer/test/method_test.rb', line 30

def test_singleton_method_using_other_receiver_is_not_indexed
  index(<<~RUBY)
    class Foo
      def String.bar
      end
    end
  RUBY

  assert_no_entry("bar")
end

#test_singleton_method_using_self_receiverObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/ruby_indexer/test/method_test.rb', line 19

def test_singleton_method_using_self_receiver
  index(<<~RUBY)
    class Foo
      def self.bar
      end
    end
  RUBY

  assert_entry("bar", Entry::SingletonMethod, "/fake/path/foo.rb:1-2:2-5")
end