Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test helper methods from Rails add-on #2991

Merged
merged 4 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
andyw8 marked this conversation as resolved.
Show resolved Hide resolved

# 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
Loading