Module: RubyLsp::Refactor::TestHelper

Defined in:
lib/ruby_lsp/test_helper.rb

Overview

TestHelper provides lightweight integration scaffolding for testing ruby-lsp-refactor listeners without spinning up a full LSP server.

Usage in a Minitest test class:

class MyTest < Minitest::Test
  include RubyLsp::Refactor::TestHelper

  def test_something
    actions = code_actions_for(source, line: 0)
    assert_includes actions.map(&:title), "Convert to post-conditional"
  end
end

Instance Method Summary collapse

Instance Method Details

#code_actions_for(source, line: 0, char: 0) ⇒ Array<Interface::CodeAction>

Parses source, runs the full listener pipeline via Addon.refactor_actions_for, and returns the resulting code actions. This exercises exactly the same path that runs inside the real LSP server.

Parameters:

  • source (String)

    Ruby source code to analyse.

  • line (Integer) (defaults to: 0)

    Zero-based line the cursor is on.

  • char (Integer) (defaults to: 0)

    Zero-based character offset (column).

Returns:

  • (Array<Interface::CodeAction>)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ruby_lsp/test_helper.rb', line 30

def code_actions_for(source, line: 0, char: 0)
  uri          = URI::Generic.from_path(path: "/test/fixture.rb")
  global_state = RubyLsp::GlobalState.new
  document     = RubyLsp::RubyDocument.new(
    source: source,
    version: 1,
    uri: uri,
    global_state: global_state
  )

  # LSP range hash — same shape the real server passes to CodeActions.
  range = {
    start: { line: line, character: char },
    end: { line: line, character: char }
  }

  RubyLsp::Refactor::Addon.refactor_actions_for(document, range)
end