Skip to content

Commit

Permalink
Add test helper methods from Rails add-on (#2991)
Browse files Browse the repository at this point in the history
* Add test helper methods from Rails add-on
  • Loading branch information
andyw8 authored Dec 19, 2024
1 parent 4604785 commit 934729f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Sorbet/StrictSigil:
- "lib/ruby_indexer/lib/ruby_indexer/prefix_tree.rb"
- "lib/ruby_lsp/load_sorbet.rb"
- "lib/ruby_lsp/scripts/compose_bundle.rb"
- "lib/ruby_lsp/test_helper.rb"

Layout/ClassStructure:
Enabled: true
Expand Down
35 changes: 34 additions & 1 deletion lib/ruby_lsp/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
# typed: strict
# typed: true
# frozen_string_literal: true

# NOTE: This module is intended to be used by addons for writing their own tests, so keep that in mind if changing.

module RubyLsp
module TestHelper
class TestError < StandardError; end

extend T::Sig
extend T::Helpers

requires_ancestor { Kernel }

sig do
type_parameters(:T)
Expand Down Expand Up @@ -52,5 +57,33 @@ def with_server(source = nil, uri = Kernel.URI("file:///fake.rb"), stub_no_typec
server.run_shutdown
end
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)

if result.is_a?(RubyLsp::Error)
raise TestError, "Failed to execute request #{result.message}"
else
result
end
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

0 comments on commit 934729f

Please sign in to comment.