diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 26121b2db..b9fa7f1a3 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -372,6 +372,23 @@ An variable entry looks like this: `{ name: "bar", value: "nil", type: "NilClass Please note that both `value` and `type` need to be strings. +- assert_threads_result(expected) + +Passes if both conditions are true: + +1. The number of expected patterns matches the number of threads. +2. Every pattern matches a thread name. Notice that the order of threads info is not guaranteed. + +Example: + +``` +assert_threads_result( + [ + /\.rb:\d:in `
'/, + /\.rb:\d:in `block in foo'/ + ] +) +``` ## To Update README diff --git a/test/protocol/threads_test.rb b/test/protocol/threads_test.rb new file mode 100644 index 000000000..32ba1bdc1 --- /dev/null +++ b/test/protocol/threads_test.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +require_relative '../support/test_case' + +module DEBUGGER__ + class ThreadsTest < TestCase + PROGRAM = <<~RUBY + 1| def foo + 2| Thread.new { sleep 30 } + 3| end + 4| + 5| foo + 6| sleep 0.1 # make sure the thread stops + 7| binding.b + RUBY + + def test_reponse_returns_correct_threads_info + run_protocol_scenario PROGRAM, cdp: false do + req_continue + + assert_threads_result( + [ + /\.rb:\d:in `
'/, + /\.rb:\d:in `block in foo'/ + ] + ) + + req_terminate_debuggee + end + end + end +end + diff --git a/test/support/protocol_utils.rb b/test/support/protocol_utils.rb index ec0cb948c..54076a46e 100644 --- a/test/support/protocol_utils.rb +++ b/test/support/protocol_utils.rb @@ -230,6 +230,29 @@ def assert_locals_result expected, frame_idx: 0 end end + def assert_threads_result(expected_names) + case ENV['RUBY_DEBUG_TEST_UI'] + when 'vscode' + res = send_dap_request 'threads' + failure_msg = FailureMessage.new{create_protocol_message "result:\n#{JSON.pretty_generate res}."} + + threads = res.dig(:body, :threads) + + assert_equal expected_names.count, threads.count, failure_msg + + thread_names = threads.map { |t| t[:name] } + + expected_names.each do |expected| + thread_names.reject! do |name| + name.match?(expected) + end + end + + failure_msg = FailureMessage.new{create_protocol_message "result:\n#{JSON.pretty_generate res}.\nExpect all thread names to be matched. Unmatched threads:"} + assert_equal [], thread_names, failure_msg + end + end + def assert_hover_result expected, expression: nil, frame_idx: 0 case ENV['RUBY_DEBUG_TEST_UI'] when 'vscode'