1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
# typed: true
# frozen_string_literal: true
require "test_helper"
class WorkspaceSymbolTest < Minitest::Test
def setup
@global_state = RubyLsp::GlobalState.new
@global_state.stubs(:has_type_checker).returns(false)
@index = @global_state.index
end
def test_returns_index_entries_based_on_query
@index.index_single(URI::Generic.from_path(path: "/fake.rb"), <<~RUBY)
class Foo; end
module Bar; end
CONSTANT = 1
RUBY
result = RubyLsp::Requests::WorkspaceSymbol.new(@global_state, "Foo").perform.first
assert_equal("Foo", result&.name)
assert_equal(RubyLsp::Constant::SymbolKind::CLASS, result&.kind)
result = RubyLsp::Requests::WorkspaceSymbol.new(@global_state, "Bar").perform.first
assert_equal("Bar", result&.name)
assert_equal(RubyLsp::Constant::SymbolKind::NAMESPACE, result&.kind)
result = RubyLsp::Requests::WorkspaceSymbol.new(@global_state, "CONST").perform.first
assert_equal("CONSTANT", result&.name)
assert_equal(RubyLsp::Constant::SymbolKind::CONSTANT, result&.kind)
end
def test_fuzzy_matches_symbols
@index.index_single(URI::Generic.from_path(path: "/fake.rb"), <<~RUBY)
class Foo; end
module Bar; end
CONSTANT = 1
RUBY
result = RubyLsp::Requests::WorkspaceSymbol.new(@global_state, "Floo").perform.first
assert_equal("Foo", result&.name)
assert_equal(RubyLsp::Constant::SymbolKind::CLASS, result&.kind)
result = RubyLsp::Requests::WorkspaceSymbol.new(@global_state, "Bear").perform.first
assert_equal("Bar", result&.name)
assert_equal(RubyLsp::Constant::SymbolKind::NAMESPACE, result&.kind)
result = RubyLsp::Requests::WorkspaceSymbol.new(@global_state, "CONF").perform.first
assert_equal("CONSTANT", result&.name)
assert_equal(RubyLsp::Constant::SymbolKind::CONSTANT, result&.kind)
end
def test_symbols_include_container_name
@index.index_single(URI::Generic.from_path(path: "/fake.rb"), <<~RUBY)
module Foo
class Bar; end
end
RUBY
result = RubyLsp::Requests::WorkspaceSymbol.new(@global_state, "Foo::Bar").perform.first
assert_equal("Foo::Bar", result&.name)
assert_equal(RubyLsp::Constant::SymbolKind::CLASS, result&.kind)
assert_equal("Foo", result&.container_name)
end
def test_does_not_include_symbols_from_dependencies
@index.index_file(URI::Generic.from_path(path: "#{RbConfig::CONFIG["rubylibdir"]}/pathname.rb"))
result = RubyLsp::Requests::WorkspaceSymbol.new(@global_state, "Pathname").perform
assert_empty(result)
end
def test_does_not_include_private_constants
@index.index_single(URI::Generic.from_path(path: "/fake.rb"), <<~RUBY)
class Foo
CONSTANT = 1
private_constant(:CONSTANT)
end
RUBY
result = RubyLsp::Requests::WorkspaceSymbol.new(@global_state, "Foo::CONSTANT").perform
assert_equal(1, result.length)
assert_equal("Foo", result.first&.name)
end
def test_returns_method_symbols
@index.index_single(URI::Generic.from_path(path: "/fake.rb"), <<~RUBY)
class Foo
attr_reader :baz
def initialize; end
def bar; end
end
RUBY
result = RubyLsp::Requests::WorkspaceSymbol.new(@global_state, "bar").perform.first
assert_equal("bar", result&.name)
assert_equal(RubyLsp::Constant::SymbolKind::METHOD, result&.kind)
result = RubyLsp::Requests::WorkspaceSymbol.new(@global_state, "initialize").perform.first
assert_equal("initialize", result&.name)
assert_equal(RubyLsp::Constant::SymbolKind::CONSTRUCTOR, result&.kind)
result = RubyLsp::Requests::WorkspaceSymbol.new(@global_state, "baz").perform.first
assert_equal("baz", result&.name)
assert_equal(RubyLsp::Constant::SymbolKind::PROPERTY, result&.kind)
end
def test_returns_symbols_from_unsaved_files
@index.index_single(URI("untitled:Untitled-1"), <<~RUBY)
class Foo; end
RUBY
result = RubyLsp::Requests::WorkspaceSymbol.new(@global_state, "Foo").perform.first
assert_equal("Foo", result&.name)
assert_equal(RubyLsp::Constant::SymbolKind::CLASS, result&.kind)
end
end
|