Skip to content

Commit

Permalink
Extract TestUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
andyw8 committed Dec 17, 2024
1 parent 44f8c29 commit c8a0d1b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 31 deletions.
39 changes: 39 additions & 0 deletions lib/ruby_lsp/ruby_lsp_rails/test_utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# typed: strict
# frozen_string_literal: true

module RubyLsp
module Rails
module TestUtils
extend T::Sig

sig { params(server: RubyLsp::Server).returns(RubyLsp::Result) }
def pop_result(server)
result = server.pop_response
result = server.pop_response until result.is_a?(RubyLsp::Result) || result.is_a?(RubyLsp::Error)

refute_instance_of(
RubyLsp::Error,
result,
-> { "Failed to execute request #{T.cast(result, RubyLsp::Error).message}" },
)
T.cast(result, RubyLsp::Result)
end

def pop_log_notification(message_queue, type)
log = message_queue.pop
return log if log.params.type == type

log = message_queue.pop until log.params.type == type
log
end

def pop_message(outgoing_queue, &block)
message = outgoing_queue.pop
return message if block.call(message)

message = outgoing_queue.pop until block.call(message)
message
end
end
end
end
34 changes: 3 additions & 31 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require "ruby_lsp/internal"
require "ruby_lsp/test_helper"
require "ruby_lsp/ruby_lsp_rails/addon"
require "ruby_lsp/ruby_lsp_rails/test_utils"

if defined?(DEBUGGER__)
DEBUGGER__::CONFIG[:skip_path] =
Expand All @@ -28,40 +29,11 @@

module ActiveSupport
class TestCase
extend T::Sig
include RubyLsp::TestHelper

def dummy_root
File.expand_path("#{__dir__}/dummy")
end

sig { params(server: RubyLsp::Server).returns(RubyLsp::Result) }
def pop_result(server)
result = server.pop_response
result = server.pop_response until result.is_a?(RubyLsp::Result) || result.is_a?(RubyLsp::Error)

refute_instance_of(
RubyLsp::Error,
result,
-> { "Failed to execute request #{T.cast(result, RubyLsp::Error).message}" },
)
T.cast(result, RubyLsp::Result)
end

def pop_log_notification(message_queue, type)
log = message_queue.pop
return log if log.params.type == type

log = message_queue.pop until log.params.type == type
log
end

def pop_message(outgoing_queue, &block)
message = outgoing_queue.pop
return message if block.call(message)

message = outgoing_queue.pop until block.call(message)
message
end
include RubyLsp::TestHelper
include RubyLsp::Rails::TestUtils
end
end

0 comments on commit c8a0d1b

Please sign in to comment.