Class: RubyIndexer::ClassesAndModulesTest

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

Instance Method Summary collapse

Methods inherited from TestCase

#setup

Instance Method Details

#test_class_with_statementsObject



17
18
19
20
21
22
23
24
25
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 17

def test_class_with_statements
  index(<<~RUBY)
    class Foo
      def something; end
    end
  RUBY

  assert_entry("Foo", Index::Entry::Class, "/fake/path/foo.rb:0-0:2-3")
end

#test_colon_colon_classObject



27
28
29
30
31
32
33
34
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 27

def test_colon_colon_class
  index(<<~RUBY)
    class ::Foo
    end
  RUBY

  assert_entry("Foo", Index::Entry::Class, "/fake/path/foo.rb:0-0:1-3")
end

#test_colon_colon_class_inside_classObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 36

def test_colon_colon_class_inside_class
  index(<<~RUBY)
    class Bar
      class ::Foo
      end
    end
  RUBY

  assert_entry("Bar", Index::Entry::Class, "/fake/path/foo.rb:0-0:3-3")
  assert_entry("Foo", Index::Entry::Class, "/fake/path/foo.rb:1-2:2-5")
end

#test_colon_colon_moduleObject



85
86
87
88
89
90
91
92
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 85

def test_colon_colon_module
  index(<<~RUBY)
    module ::Foo
    end
  RUBY

  assert_entry("Foo", Index::Entry::Module, "/fake/path/foo.rb:0-0:1-3")
end

#test_comments_can_be_attached_to_a_classObject



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/classes_and_modules_test.rb', line 147

def test_comments_can_be_attached_to_a_class
  index(<<~RUBY)
    # This is method comment
    def foo; end
    # This is a Foo comment
    # This is another Foo comment
    class Foo
      # This should not be attached
    end

    # Ignore me

    # This Bar comment has 1 line padding

    class Bar; end
  RUBY

  foo_entry = @index["Foo"].first
  assert_equal("This is a Foo comment\nThis is another Foo comment", foo_entry.comments.join("\n"))

  bar_entry = @index["Bar"].first
  assert_equal("This Bar comment has 1 line padding", bar_entry.comments.join("\n"))
end

#test_comments_can_be_attached_to_a_namespaced_classObject



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

def test_comments_can_be_attached_to_a_namespaced_class
  index(<<~RUBY)
    # This is a Foo comment
    # This is another Foo comment
    class Foo
      # This is a Bar comment
      class Bar; end
    end
  RUBY

  foo_entry = @index["Foo"].first
  assert_equal("This is a Foo comment\nThis is another Foo comment", foo_entry.comments.join("\n"))

  bar_entry = @index["Foo::Bar"].first
  assert_equal("This is a Bar comment", bar_entry.comments.join("\n"))
end

#test_comments_can_be_attached_to_a_reopened_classObject



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

def test_comments_can_be_attached_to_a_reopened_class
  index(<<~RUBY)
    # This is a Foo comment
    class Foo; end

    # This is another Foo comment
    class Foo; end
  RUBY

  first_foo_entry = @index["Foo"][0]
  assert_equal("This is a Foo comment", first_foo_entry.comments.join("\n"))

  second_foo_entry = @index["Foo"][1]
  assert_equal("This is another Foo comment", second_foo_entry.comments.join("\n"))
end

#test_comments_removes_the_leading_pound_and_spaceObject



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

def test_comments_removes_the_leading_pound_and_space
  index(<<~RUBY)
    # This is a Foo comment
    class Foo; end

    #This is a Bar comment
    class Bar; end
  RUBY

  first_foo_entry = @index["Foo"][0]
  assert_equal("This is a Foo comment", first_foo_entry.comments.join("\n"))

  second_foo_entry = @index["Bar"][0]
  assert_equal("This is a Bar comment", second_foo_entry.comments.join("\n"))
end

#test_deleting_from_index_based_on_file_pathObject



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 134

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

  assert_entry("Foo", Index::Entry::Class, "/fake/path/foo.rb:0-0:1-3")

  @index.delete(IndexablePath.new(nil, "/fake/path/foo.rb"))
  refute_entry("Foo")
  assert_empty(@index.instance_variable_get(:@files_to_entries))
end

#test_dynamically_namespaced_classObject



57
58
59
60
61
62
63
64
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 57

def test_dynamically_namespaced_class
  index(<<~RUBY)
    class self::Bar
    end
  RUBY

  refute_entry("self::Bar")
end

#test_dynamically_namespaced_moduleObject



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

def test_dynamically_namespaced_module
  index(<<~RUBY)
    module self::Bar
    end
  RUBY

  refute_entry("self::Bar")
end

#test_empty_statements_classObject



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

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

  assert_entry("Foo", Index::Entry::Class, "/fake/path/foo.rb:0-0:1-3")
end

#test_empty_statements_moduleObject



66
67
68
69
70
71
72
73
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 66

def test_empty_statements_module
  index(<<~RUBY)
    module Foo
    end
  RUBY

  assert_entry("Foo", Index::Entry::Module, "/fake/path/foo.rb:0-0:1-3")
end

#test_module_with_statementsObject



75
76
77
78
79
80
81
82
83
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 75

def test_module_with_statements
  index(<<~RUBY)
    module Foo
      def something; end
    end
  RUBY

  assert_entry("Foo", Index::Entry::Module, "/fake/path/foo.rb:0-0:2-3")
end

#test_namespaced_classObject



48
49
50
51
52
53
54
55
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 48

def test_namespaced_class
  index(<<~RUBY)
    class Foo::Bar
    end
  RUBY

  assert_entry("Foo::Bar", Index::Entry::Class, "/fake/path/foo.rb:0-0:1-3")
end

#test_namespaced_moduleObject



94
95
96
97
98
99
100
101
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 94

def test_namespaced_module
  index(<<~RUBY)
    module Foo::Bar
    end
  RUBY

  assert_entry("Foo::Bar", Index::Entry::Module, "/fake/path/foo.rb:0-0:1-3")
end

#test_nested_modules_and_classesObject



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/classes_and_modules_test.rb', line 112

def test_nested_modules_and_classes
  index(<<~RUBY)
    module Foo
      class Bar
      end

      module Baz
        class Qux
          class Something
          end
        end
      end
    end
  RUBY

  assert_entry("Foo", Index::Entry::Module, "/fake/path/foo.rb:0-0:10-3")
  assert_entry("Foo::Bar", Index::Entry::Class, "/fake/path/foo.rb:1-2:2-5")
  assert_entry("Foo::Baz", Index::Entry::Module, "/fake/path/foo.rb:4-2:9-5")
  assert_entry("Foo::Baz::Qux", Index::Entry::Class, "/fake/path/foo.rb:5-4:8-7")
  assert_entry("Foo::Baz::Qux::Something", Index::Entry::Class, "/fake/path/foo.rb:6-6:7-9")
end

#test_private_class_and_module_indexingObject



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

def test_private_class_and_module_indexing
  index(<<~RUBY)
    class A
      class B; end
      private_constant(:B)

      module C; end
      private_constant("C")

      class D; end
    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