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
|
# typed: true
# frozen_string_literal: true
require "test_helper"
require_relative "support/expectations_test_runner"
class CodeActionsExpectationsTest < ExpectationsTestRunner
expectations_tests RubyLsp::Requests::CodeActions, "code_actions"
def run_expectations(source)
params = @__params&.any? ? @__params : default_args(source)
document = RubyLsp::RubyDocument.new(
source: source,
version: 1,
uri: URI("file:///fake"),
global_state: @global_state,
)
result = nil #: Array[LanguageServer::Protocol::Interface::CodeAction]?
stdout, _ = capture_io do
result = RubyLsp::Requests::CodeActions.new(
document,
params[:range],
params[:context],
).perform
end
assert_empty(stdout)
result
end
def assert_expectations(source, expected)
actual = run_expectations(source)
assert_equal(map_actions(json_expectations(expected)), JSON.parse(actual.to_json))
end
private
def default_args(source)
end_line = source.lines.count > 1 ? 1 : 0
end_character = source.empty? ? 0 : 1
{
range: {
start: { line: 0, character: 0 },
end: { line: end_line, character: end_character },
},
context: {
diagnostics: [],
},
}
end
def map_actions(expectation)
quickfixes = expectation
.select { |action| action["kind"] == "quickfix" }
.map { |action| code_action_for_diagnostic(action) }
refactors = expectation
.select { |action| action["kind"].start_with?("refactor") || action["kind"] == "" }
.map { |action| code_action_for_refactor(action) }
result = [*quickfixes, *refactors]
JSON.parse(result.to_json)
end
def code_action_for_diagnostic(diagnostic)
LanguageServer::Protocol::Interface::CodeAction.new(
title: diagnostic["title"],
kind: LanguageServer::Protocol::Constant::CodeActionKind::QUICK_FIX,
edit: LanguageServer::Protocol::Interface::WorkspaceEdit.new(
document_changes: [
LanguageServer::Protocol::Interface::TextDocumentEdit.new(
text_document: LanguageServer::Protocol::Interface::OptionalVersionedTextDocumentIdentifier.new(
uri: "file:///fake",
version: nil,
),
edits: diagnostic["edit"]["documentChanges"].first["edits"],
),
],
),
is_preferred: true,
)
end
def code_action_for_refactor(refactor)
LanguageServer::Protocol::Interface::CodeAction.new(
title: refactor["title"],
kind: refactor["kind"],
data: {
range: refactor.dig("data", "range"),
uri: refactor.dig("data", "uri"),
},
)
end
end
|