Class: RubyIndexer::RBSIndexerTest
- Defined in:
- lib/ruby_indexer/test/rbs_indexer_test.rb
Instance Method Summary collapse
- #test_attaches_correct_owner_to_singleton_methods ⇒ Object
- #test_index_core_classes ⇒ Object
- #test_index_core_constants ⇒ Object
- #test_index_core_modules ⇒ Object
- #test_index_methods ⇒ Object
- #test_location_and_name_location_are_the_same ⇒ Object
- #test_parse_simple_rbs ⇒ Object
- #test_rbs_anonymous_block_parameter ⇒ Object
- #test_rbs_method_with_optional_keywords ⇒ Object
- #test_rbs_method_with_optional_parameter ⇒ Object
- #test_rbs_method_with_optional_positionals ⇒ Object
- #test_rbs_method_with_required_and_optional_parameters ⇒ Object
- #test_rbs_method_with_required_keywords ⇒ Object
- #test_rbs_method_with_required_positionals ⇒ Object
- #test_rbs_method_with_rest_keywords ⇒ Object
- #test_rbs_method_with_rest_positionals ⇒ Object
- #test_rbs_method_with_trailing_positionals ⇒ Object
- #test_rbs_method_with_unnamed_required_positionals ⇒ Object
- #test_signature_alias ⇒ Object
Methods inherited from TestCase
Instance Method Details
#test_attaches_correct_owner_to_singleton_methods ⇒ Object
79 80 81 82 83 84 85 86 |
# File 'lib/ruby_indexer/test/rbs_indexer_test.rb', line 79 def test_attaches_correct_owner_to_singleton_methods entries = @index["basename"] refute_nil(entries) owner = entries.first.owner assert_instance_of(Entry::SingletonClass, owner) assert_equal("File::<Class:File>", owner.name) end |
#test_index_core_classes ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/ruby_indexer/test/rbs_indexer_test.rb', line 8 def test_index_core_classes entries = @index["Array"] refute_nil(entries) # Array is a class but also an instance method on Kernel assert_equal(2, entries.length) entry = entries.find { |entry| entry.is_a?(RubyIndexer::Entry::Class) } assert_match(%r{/gems/rbs-.*/core/array.rbs}, entry.file_path) assert_equal("array.rbs", entry.file_name) assert_equal("Object", entry.parent_class) assert_equal(1, entry.mixin_operations.length) enumerable_include = entry.mixin_operations.first assert_equal("Enumerable", enumerable_include.module_name) # Using fixed positions would be fragile, so let's just check some basics. assert_operator(entry.location.start_line, :>, 0) assert_operator(entry.location.end_line, :>, entry.location.start_line) assert_equal(0, entry.location.start_column) assert_operator(entry.location.end_column, :>, 0) end |
#test_index_core_constants ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/ruby_indexer/test/rbs_indexer_test.rb', line 43 def test_index_core_constants entries = @index["RUBY_VERSION"] refute_nil(entries) assert_equal(1, entries.length) # Complex::I is defined as `Complex::I = ...` entries = @index["Complex::I"] refute_nil(entries) assert_equal(1, entries.length) # Encoding::US_ASCII is defined as # ``` # module Encoding # US_ASCII = ... # ... # ```` entries = @index["Encoding::US_ASCII"] refute_nil(entries) assert_equal(1, entries.length) end |
#test_index_core_modules ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/ruby_indexer/test/rbs_indexer_test.rb', line 28 def test_index_core_modules entries = @index["Kernel"] refute_nil(entries) assert_equal(1, entries.length) entry = entries.first assert_match(%r{/gems/rbs-.*/core/kernel.rbs}, entry.file_path) assert_equal("kernel.rbs", entry.file_name) # Using fixed positions would be fragile, so let's just check some basics. assert_operator(entry.location.start_line, :>, 0) assert_operator(entry.location.end_line, :>, entry.location.start_line) assert_equal(0, entry.location.start_column) assert_operator(entry.location.end_column, :>, 0) end |
#test_index_methods ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/ruby_indexer/test/rbs_indexer_test.rb', line 64 def test_index_methods entries = @index["initialize"] refute_nil(entries) entry = entries.find { |entry| entry.owner.name == "Array" } assert_match(%r{/gems/rbs-.*/core/array.rbs}, entry.file_path) assert_equal("array.rbs", entry.file_name) assert_equal(Entry::Visibility::PUBLIC, entry.visibility) # Using fixed positions would be fragile, so let's just check some basics. assert_operator(entry.location.start_line, :>, 0) assert_operator(entry.location.end_line, :>, entry.location.start_line) assert_equal(2, entry.location.start_column) assert_operator(entry.location.end_column, :>, 0) end |
#test_location_and_name_location_are_the_same ⇒ Object
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/ruby_indexer/test/rbs_indexer_test.rb', line 88 def test_location_and_name_location_are_the_same # NOTE: RBS does not store the name location for classes, modules or methods. This behaviour is not exactly what # we would like, but for now we assign the same location to both entries = @index["Array"] refute_nil(entries) entry = entries.find { |entry| entry.is_a?(Entry::Class) } assert_same(entry.location, entry.name_location) end |
#test_parse_simple_rbs ⇒ Object
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/ruby_indexer/test/rbs_indexer_test.rb', line 311 def test_parse_simple_rbs rbs = " class File\n def self?.open: (String name, ?String mode, ?Integer perm) -> IO?\n | [T] (String name, ?String mode, ?Integer perm) { (IO) -> T } -> T\n end\n RBS\n signatures = parse_rbs_methods(rbs, \"open\")\n assert_equal(2, signatures.length)\n parameters = signatures[0].parameters\n assert_equal([:name, :mode, :perm], parameters.map(&:name))\n assert_kind_of(Entry::RequiredParameter, parameters[0])\n assert_kind_of(Entry::OptionalParameter, parameters[1])\n assert_kind_of(Entry::OptionalParameter, parameters[2])\n\n parameters = signatures[1].parameters\n assert_equal([:name, :mode, :perm, :\"<anonymous block>\"], parameters.map(&:name))\n assert_kind_of(Entry::RequiredParameter, parameters[0])\n assert_kind_of(Entry::OptionalParameter, parameters[1])\n assert_kind_of(Entry::OptionalParameter, parameters[2])\n assert_kind_of(Entry::BlockParameter, parameters[3])\nend\n" |
#test_rbs_anonymous_block_parameter ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/ruby_indexer/test/rbs_indexer_test.rb', line 184 def test_rbs_anonymous_block_parameter entries = @index["open"] entry = entries.find { |entry| entry.owner.name == "File::<Class:File>" } assert_equal(2, entry.signatures.length) # (::String name, ?::String mode, ?::Integer perm) -> ::IO? # | [T] (::String name, ?::String mode, ?::Integer perm) { (::IO) -> T } -> T parameters = entry.signatures[0].parameters assert_equal([:file_name, :mode, :perm], parameters.map(&:name)) assert_kind_of(Entry::RequiredParameter, parameters[0]) assert_kind_of(Entry::OptionalParameter, parameters[1]) assert_kind_of(Entry::OptionalParameter, parameters[2]) parameters = entry.signatures[1].parameters assert_equal([:file_name, :mode, :perm, :"<anonymous block>"], parameters.map(&:name)) assert_kind_of(Entry::RequiredParameter, parameters[0]) assert_kind_of(Entry::OptionalParameter, parameters[1]) assert_kind_of(Entry::OptionalParameter, parameters[2]) assert_kind_of(Entry::BlockParameter, parameters[3]) end |
#test_rbs_method_with_optional_keywords ⇒ Object
245 246 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 |
# File 'lib/ruby_indexer/test/rbs_indexer_test.rb', line 245 def test_rbs_method_with_optional_keywords entries = @index["step"] entry = entries.find { |entry| entry.owner.name == "Numeric" } signatures = entry.signatures assert_equal(4, signatures.length) # (?::Numeric limit, ?::Numeric step) { (::Numeric) -> void } -> self # | (?::Numeric limit, ?::Numeric step) -> ::Enumerator[::Numeric, self] # | (?by: ::Numeric, ?to: ::Numeric) { (::Numeric) -> void } -> self # | (?by: ::Numeric, ?to: ::Numeric) -> ::Enumerator[::Numeric, self] parameters = signatures[0].parameters assert_equal([:limit, :step, :"<anonymous block>"], parameters.map(&:name)) assert_kind_of(Entry::OptionalParameter, parameters[0]) assert_kind_of(Entry::OptionalParameter, parameters[1]) assert_kind_of(Entry::BlockParameter, parameters[2]) parameters = signatures[1].parameters assert_equal([:limit, :step], parameters.map(&:name)) assert_kind_of(Entry::OptionalParameter, parameters[0]) assert_kind_of(Entry::OptionalParameter, parameters[1]) parameters = signatures[2].parameters assert_equal([:by, :to, :"<anonymous block>"], parameters.map(&:name)) assert_kind_of(Entry::OptionalKeywordParameter, parameters[0]) assert_kind_of(Entry::OptionalKeywordParameter, parameters[1]) assert_kind_of(Entry::BlockParameter, parameters[2]) parameters = signatures[3].parameters assert_equal([:by, :to], parameters.map(&:name)) assert_kind_of(Entry::OptionalKeywordParameter, parameters[0]) assert_kind_of(Entry::OptionalKeywordParameter, parameters[1]) end |
#test_rbs_method_with_optional_parameter ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/ruby_indexer/test/rbs_indexer_test.rb', line 139 def test_rbs_method_with_optional_parameter entries = @index["chomp"] assert_equal(1, entries.length) entry = entries.first signatures = entry.signatures assert_equal(1, signatures.length) first_signature = signatures.first # (?::string? separator) -> ::String assert_equal(1, first_signature.parameters.length) assert_kind_of(Entry::OptionalParameter, first_signature.parameters[0]) assert_equal(:separator, first_signature.parameters[0].name) end |
#test_rbs_method_with_optional_positionals ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/ruby_indexer/test/rbs_indexer_test.rb', line 126 def test_rbs_method_with_optional_positionals entries = @index["polar"] entry = entries.find { |entry| entry.owner.name == "Complex::<Class:Complex>" } # def self.polar: (Numeric, ?Numeric) -> Complex parameters = entry.signatures[0].parameters assert_equal([:arg0, :arg1], parameters.map(&:name)) assert_kind_of(Entry::RequiredParameter, parameters[0]) assert_kind_of(Entry::OptionalParameter, parameters[1]) end |
#test_rbs_method_with_required_and_optional_parameters ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/ruby_indexer/test/rbs_indexer_test.rb', line 156 def test_rbs_method_with_required_and_optional_parameters entries = @index["gsub"] assert_equal(1, entries.length) entry = entries.first signatures = entry.signatures assert_equal(3, signatures.length) # (::Regexp | ::string pattern, ::string | ::hash[::String, ::_ToS] replacement) -> ::String # | (::Regexp | ::string pattern) -> ::Enumerator[::String, ::String] # | (::Regexp | ::string pattern) { (::String match) -> ::_ToS } -> ::String parameters = signatures[0].parameters assert_equal([:pattern, :replacement], parameters.map(&:name)) assert_kind_of(Entry::RequiredParameter, parameters[0]) assert_kind_of(Entry::RequiredParameter, parameters[1]) parameters = signatures[1].parameters assert_equal([:pattern], parameters.map(&:name)) assert_kind_of(Entry::RequiredParameter, parameters[0]) parameters = signatures[2].parameters assert_equal([:pattern, :"<anonymous block>"], parameters.map(&:name)) assert_kind_of(Entry::RequiredParameter, parameters[0]) assert_kind_of(Entry::BlockParameter, parameters[1]) end |
#test_rbs_method_with_required_keywords ⇒ Object
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/ruby_indexer/test/rbs_indexer_test.rb', line 280 def test_rbs_method_with_required_keywords # There are no methods in Core that have required keyword arguments, # so we test against RBS directly rbs = " class File\n def foo: (a: ::Numeric sz, b: ::Numeric) -> void\n end\n RBS\n signatures = parse_rbs_methods(rbs, \"foo\")\n parameters = signatures[0].parameters\n assert_equal([:a, :b], parameters.map(&:name))\n assert_kind_of(Entry::KeywordParameter, parameters[0])\n assert_kind_of(Entry::KeywordParameter, parameters[1])\nend\n" |
#test_rbs_method_with_required_positionals ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/ruby_indexer/test/rbs_indexer_test.rb', line 99 def test_rbs_method_with_required_positionals entries = @index["crypt"] assert_equal(1, entries.length) entry = entries.first signatures = entry.signatures assert_equal(1, signatures.length) first_signature = signatures.first # (::string salt_str) -> ::String assert_equal(1, first_signature.parameters.length) assert_kind_of(Entry::RequiredParameter, first_signature.parameters[0]) assert_equal(:salt_str, first_signature.parameters[0].name) end |
#test_rbs_method_with_rest_keywords ⇒ Object
296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/ruby_indexer/test/rbs_indexer_test.rb', line 296 def test_rbs_method_with_rest_keywords entries = @index["method_missing"] entry = entries.find { |entry| entry.owner.name == "BasicObject" } signatures = entry.signatures assert_equal(1, signatures.length) # (Symbol, *untyped, **untyped) ?{ (*untyped, **untyped) -> untyped } -> untyped parameters = signatures[0].parameters assert_equal([:arg0, :"<anonymous splat>", :"<anonymous keyword splat>"], parameters.map(&:name)) assert_kind_of(Entry::RequiredParameter, parameters[0]) assert_kind_of(Entry::RestParameter, parameters[1]) assert_kind_of(Entry::KeywordRestParameter, parameters[2]) end |
#test_rbs_method_with_rest_positionals ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/ruby_indexer/test/rbs_indexer_test.rb', line 207 def test_rbs_method_with_rest_positionals entries = @index["count"] entry = entries.find { |entry| entry.owner.name == "String" } parameters = entry.signatures.first.parameters assert_equal(1, entry.signatures.length) # (::String::selector selector_0, *::String::selector more_selectors) -> ::Integer assert_equal([:selector_0, :more_selectors], parameters.map(&:name)) assert_kind_of(RubyIndexer::Entry::RequiredParameter, parameters[0]) assert_kind_of(RubyIndexer::Entry::RestParameter, parameters[1]) end |
#test_rbs_method_with_trailing_positionals ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/ruby_indexer/test/rbs_indexer_test.rb', line 221 def test_rbs_method_with_trailing_positionals entries = @index["select"] # https://ruby-doc.org/3.3.3/IO.html#method-c-select entry = entries.find { |entry| entry.owner.name == "IO::<Class:IO>" } signatures = entry.signatures assert_equal(2, signatures.length) # def self.select: [X, Y, Z] (::Array[X & io]? read_array, ?::Array[Y & io]? write_array, ?::Array[Z & io]? error_array) -> [ Array[X], Array[Y], Array[Z] ] # rubocop:disable Layout/LineLength # | [X, Y, Z] (::Array[X & io]? read_array, ?::Array[Y & io]? write_array, ?::Array[Z & io]? error_array, Time::_Timeout? timeout) -> [ Array[X], Array[Y], Array[Z] ]? # rubocop:disable Layout/LineLength parameters = signatures[0].parameters assert_equal([:read_array, :write_array, :error_array], parameters.map(&:name)) assert_kind_of(Entry::RequiredParameter, parameters[0]) assert_kind_of(Entry::OptionalParameter, parameters[1]) assert_kind_of(Entry::OptionalParameter, parameters[2]) parameters = signatures[1].parameters assert_equal([:read_array, :write_array, :error_array, :timeout], parameters.map(&:name)) assert_kind_of(Entry::RequiredParameter, parameters[0]) assert_kind_of(Entry::OptionalParameter, parameters[1]) assert_kind_of(Entry::OptionalParameter, parameters[2]) assert_kind_of(Entry::OptionalParameter, parameters[3]) end |
#test_rbs_method_with_unnamed_required_positionals ⇒ Object
116 117 118 119 120 121 122 123 124 |
# File 'lib/ruby_indexer/test/rbs_indexer_test.rb', line 116 def test_rbs_method_with_unnamed_required_positionals entries = @index["try_convert"] entry = entries.find { |entry| entry.owner.name == "Array::<Class:Array>" } parameters = entry.signatures[0].parameters assert_equal([:arg0], parameters.map(&:name)) assert_kind_of(Entry::RequiredParameter, parameters[0]) end |
#test_signature_alias ⇒ Object
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/ruby_indexer/test/rbs_indexer_test.rb', line 334 def test_signature_alias # In RBS, an alias means that two methods have the same signature. # It does not mean the same thing as a Ruby alias. any_entries = @index["any?"] assert_equal(["Array", "Enumerable", "Hash"], any_entries.map { _1.owner.name }) entry = any_entries.find { |entry| entry.owner.name == "Array" } assert_kind_of(RubyIndexer::Entry::UnresolvedMethodAlias, entry) assert_equal("any?", entry.name) assert_equal("all?", entry.old_name) assert_equal("Array", entry.owner.name) assert(entry.file_path.end_with?("core/array.rbs")) assert_includes(entry.comments[0], "Returns `true` if any element of `self` meets a given criterion.") end |