Class: RubyIndexer::InstanceVariableTest

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

Instance Method Summary collapse

Methods inherited from TestCase

#setup, #teardown

Instance Method Details

#test_class_instance_variable_commentsObject



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/ruby_indexer/test/instance_variables_test.rb', line 240

def test_class_instance_variable_comments
  index("      class Foo\n        # Documentation for @a\n        @a = \"Hello\" #: String\n        @b = \"World\" # trailing comment\n        @c = \"!\"\n      end\n    end\n  RUBY\n\n  assert_entry(\"@a\", Entry::InstanceVariable, \"/fake/path/foo.rb:2-4:2-6\")\n  entry = @index[\"@a\"]&.first #: as Entry::InstanceVariable\n  assert_equal(\"Documentation for @a\", entry.comments)\n\n  assert_entry(\"@b\", Entry::InstanceVariable, \"/fake/path/foo.rb:3-4:3-6\")\n  entry = @index[\"@b\"]&.first #: as Entry::InstanceVariable\n  assert_empty(entry.comments)\n\n  assert_entry(\"@c\", Entry::InstanceVariable, \"/fake/path/foo.rb:4-4:4-6\")\n  entry = @index[\"@c\"]&.first #: as Entry::InstanceVariable\n  assert_empty(entry.comments)\nend\n")

#test_class_instance_variablesObject



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

def test_class_instance_variables
  index("    module Foo\n      class Bar\n        @a = 123\n\n        class << self\n          def hello\n            @b = 123\n          end\n\n          @c = 123\n        end\n      end\n    end\n  RUBY\n\n  assert_entry(\"@a\", Entry::InstanceVariable, \"/fake/path/foo.rb:2-4:2-6\")\n\n  entry = @index[\"@a\"]&.first #: as Entry::InstanceVariable\n  owner = entry.owner\n  assert_instance_of(Entry::SingletonClass, owner)\n  assert_equal(\"Foo::Bar::<Class:Bar>\", owner&.name)\n\n  assert_entry(\"@b\", Entry::InstanceVariable, \"/fake/path/foo.rb:6-8:6-10\")\n\n  entry = @index[\"@b\"]&.first #: as Entry::InstanceVariable\n  owner = entry.owner\n  assert_instance_of(Entry::SingletonClass, owner)\n  assert_equal(\"Foo::Bar::<Class:Bar>\", owner&.name)\n\n  assert_entry(\"@c\", Entry::InstanceVariable, \"/fake/path/foo.rb:9-6:9-8\")\n\n  entry = @index[\"@c\"]&.first #: as Entry::InstanceVariable\n  owner = entry.owner\n  assert_instance_of(Entry::SingletonClass, owner)\n  assert_equal(\"Foo::Bar::<Class:Bar>::<Class:<Class:Bar>>\", owner&.name)\nend\n")

#test_class_instance_variables_inside_self_methodObject



188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/ruby_indexer/test/instance_variables_test.rb', line 188

def test_class_instance_variables_inside_self_method
  index("    class Foo\n      def self.bar\n        @a = 123\n      end\n    end\n  RUBY\n\n  entry = @index[\"@a\"]&.first #: as Entry::InstanceVariable\n  owner = entry.owner\n  assert_instance_of(Entry::SingletonClass, owner)\n  assert_equal(\"Foo::<Class:Foo>\", owner&.name)\nend\n")

#test_empty_name_instance_variablesObject



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/ruby_indexer/test/instance_variables_test.rb', line 126

def test_empty_name_instance_variables
  index("    module Foo\n      class Bar\n        def initialize\n          @ = 123\n        end\n      end\n    end\n  RUBY\n\n  refute_entry(\"@\")\nend\n")

#test_instance_variable_and_writeObject



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

def test_instance_variable_and_write
  index("    module Foo\n      class Bar\n        def initialize\n          # Hello\n          @a &&= value\n        end\n      end\n    end\n  RUBY\n\n  assert_entry(\"@a\", Entry::InstanceVariable, \"/fake/path/foo.rb:4-6:4-8\")\n\n  entry = @index[\"@a\"]&.first #: as Entry::InstanceVariable\n  owner = entry.owner\n  assert_instance_of(Entry::Class, owner)\n  assert_equal(\"Foo::Bar\", owner&.name)\nend\n")

#test_instance_variable_inside_dynamic_method_declarationObject



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/ruby_indexer/test/instance_variables_test.rb', line 203

def test_instance_variable_inside_dynamic_method_declaration
  index("    class Foo\n      def something.bar\n        @a = 123\n      end\n    end\n  RUBY\n\n  # If the surrounding method is being defined on any dynamic value that isn't `self`, then we attribute the\n  # instance variable to the wrong owner since there's no way to understand that statically\n  entry = @index[\"@a\"]&.first #: as Entry::InstanceVariable\n  owner = entry.owner\n  assert_instance_of(Entry::Class, owner)\n  assert_equal(\"Foo\", owner&.name)\nend\n")

#test_instance_variable_operator_writeObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ruby_indexer/test/instance_variables_test.rb', line 60

def test_instance_variable_operator_write
  index("    module Foo\n      class Bar\n        def initialize\n          # Hello\n          @a += value\n        end\n      end\n    end\n  RUBY\n\n  assert_entry(\"@a\", Entry::InstanceVariable, \"/fake/path/foo.rb:4-6:4-8\")\n\n  entry = @index[\"@a\"]&.first #: as Entry::InstanceVariable\n  owner = entry.owner\n  assert_instance_of(Entry::Class, owner)\n  assert_equal(\"Foo::Bar\", owner&.name)\nend\n")

#test_instance_variable_or_writeObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ruby_indexer/test/instance_variables_test.rb', line 80

def test_instance_variable_or_write
  index("    module Foo\n      class Bar\n        def initialize\n          # Hello\n          @a ||= value\n        end\n      end\n    end\n  RUBY\n\n  assert_entry(\"@a\", Entry::InstanceVariable, \"/fake/path/foo.rb:4-6:4-8\")\n\n  entry = @index[\"@a\"]&.first #: as Entry::InstanceVariable\n  owner = entry.owner\n  assert_instance_of(Entry::Class, owner)\n  assert_equal(\"Foo::Bar\", owner&.name)\nend\n")

#test_instance_variable_targetObject



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

def test_instance_variable_target
  index("    module Foo\n      class Bar\n        def initialize\n          # Hello\n          @a, @b = [1, 2]\n        end\n      end\n    end\n  RUBY\n\n  assert_entry(\"@a\", Entry::InstanceVariable, \"/fake/path/foo.rb:4-6:4-8\")\n  assert_entry(\"@b\", Entry::InstanceVariable, \"/fake/path/foo.rb:4-10:4-12\")\n\n  entry = @index[\"@a\"]&.first #: as Entry::InstanceVariable\n  owner = entry.owner\n  assert_instance_of(Entry::Class, owner)\n  assert_equal(\"Foo::Bar\", owner&.name)\n\n  entry = @index[\"@b\"]&.first #: as Entry::InstanceVariable\n  owner = entry.owner\n  assert_instance_of(Entry::Class, owner)\n  assert_equal(\"Foo::Bar\", owner&.name)\nend\n")

#test_instance_variable_with_multibyte_charactersObject



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

def test_instance_variable_with_multibyte_characters
  index("    class Foo\n      def initialize\n        @\u3042 = 1\n      end\n    end\n  RUBY\n\n  assert_entry(\"@\u3042\", Entry::InstanceVariable, \"/fake/path/foo.rb:2-4:2-6\")\nend\n")

#test_instance_variable_writeObject



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

def test_instance_variable_write
  index("    module Foo\n      class Bar\n        def initialize\n          # Hello\n          @a = 1\n        end\n      end\n    end\n  RUBY\n\n  assert_entry(\"@a\", Entry::InstanceVariable, \"/fake/path/foo.rb:4-6:4-8\")\n\n  entry = @index[\"@a\"]&.first #: as Entry::InstanceVariable\n  owner = entry.owner\n  assert_instance_of(Entry::Class, owner)\n  assert_equal(\"Foo::Bar\", owner&.name)\nend\n")

#test_module_function_does_not_impact_instance_variablesObject



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/ruby_indexer/test/instance_variables_test.rb', line 220

def test_module_function_does_not_impact_instance_variables
  # One possible way of implementing `module_function` would be to push a fake singleton class to the stack, so that
  # methods are inserted into it. However, that would be incorrect because it would then bind instance variables to
  # the wrong type. This test is here to prevent that from happening.
  index("    module Foo\n      module_function\n\n      def something; end\n\n      @a = 123\n    end\n  RUBY\n\n  entry = @index[\"@a\"]&.first #: as Entry::InstanceVariable\n  owner = entry.owner\n  assert_instance_of(Entry::SingletonClass, owner)\n  assert_equal(\"Foo::<Class:Foo>\", owner&.name)\nend\n")

#test_top_level_instance_variablesObject



179
180
181
182
183
184
185
186
# File 'lib/ruby_indexer/test/instance_variables_test.rb', line 179

def test_top_level_instance_variables
  index("    @a = 123\n  RUBY\n\n  entry = @index[\"@a\"]&.first #: as Entry::InstanceVariable\n  assert_nil(entry.owner)\nend\n")