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 121
|
# typed: true
# frozen_string_literal: true
require "test_helper"
require_relative "support/expectations_test_runner"
class DocumentLinkExpectationsTest < ExpectationsTestRunner
expectations_tests RubyLsp::Requests::DocumentLink, "document_link"
def assert_expectations(source, expected)
source = substitute_syntax_tree_version(source)
actual = run_expectations(source) #: as Array[LanguageServer::Protocol::Interface::DocumentLink]
assert_equal(map_expectations(json_expectations(expected)), JSON.parse(actual.to_json))
end
def map_expectations(expectations)
expectations.each do |expectation|
expectation["target"] = substitute(expectation["target"])
expectation["tooltip"] = substitute(expectation["tooltip"])
end
end
def run_expectations(source)
uri = URI("file://#{@_path}")
document = RubyLsp::RubyDocument.new(source: source, version: 1, uri: uri, global_state: @global_state)
dispatcher = Prism::Dispatcher.new
parse_result = document.parse_result
listener = RubyLsp::Requests::DocumentLink.new(uri, parse_result.comments, dispatcher)
dispatcher.dispatch(document.ast)
listener.perform
end
def test_magic_source_links_on_unsaved_files
source = <<~RUBY
# source://erb/#1
def bar
end
RUBY
with_server(source) do |server, uri|
server.process_message(
id: 1,
method: "textDocument/documentLink",
params: { textDocument: { uri: uri } },
)
server.pop_response
assert_empty(server.pop_response.response)
end
end
def test_magic_source_links_with_invalid_uris
source = <<~RUBY
# source://some_file /#123
def bar
end
RUBY
with_server(source) do |server, uri|
server.process_message(
id: 1,
method: "textDocument/documentLink",
params: { textDocument: { uri: uri } },
)
server.pop_response
assert_empty(server.pop_response.response)
end
end
def test_package_url_links
source = <<~RUBY
# pkg:gem/erb#:99
def bar
end
RUBY
with_server(source) do |server, uri|
server.process_message(
id: 1,
method: "textDocument/documentLink",
params: { textDocument: { uri: uri } },
)
server.pop_response
assert_empty(server.pop_response.response)
end
end
def test_package_url_links_with_invalid_uris
source = <<~RUBY
# pkg:gem/rubocop$1.78.0#:99
def bar
end
RUBY
with_server(source) do |server, uri|
server.process_message(
id: 1,
method: "textDocument/documentLink",
params: { textDocument: { uri: uri } },
)
server.pop_response
assert_empty(server.pop_response.response)
end
end
private
def substitute(original)
substitute_syntax_tree_version(original)
.sub("BUNDLER_PATH", Bundler.bundle_path.to_s)
.sub("RUBY_ROOT", RbConfig::CONFIG["rubylibdir"])
end
def substitute_syntax_tree_version(original)
original.sub("SYNTAX_TREE_VERSION", Gem::Specification.find_by_name("syntax_tree").version.to_s)
end
end
|