Skip to content

Add assert_threads_result helper and threads test #607

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

Merged
merged 4 commits into from
Apr 17, 2022
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
17 changes: 17 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<main>'/,
/\.rb:\d:in `block in foo'/
]
)
```

## To Update README

Expand Down
33 changes: 33 additions & 0 deletions test/protocol/threads_test.rb
Original file line number Diff line number Diff line change
@@ -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 `<main>'/,
/\.rb:\d:in `block in foo'/
]
)

req_terminate_debuggee
end
end
end
end

23 changes: 23 additions & 0 deletions test/support/protocol_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

@ono-max ono-max Apr 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use assert_match method in this case, could you tell me the reason why you implemented so?

Copy link
Member Author

@st0012 st0012 Apr 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you think we can use it here? Remember, the order of threads info is not guaranteed. So we don't know which one will match.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that's right. My bad 🙏

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries 😉
Do other changes look good to you?

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use assert_empty?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to use assert_equal because it provides better highlighting in the failure messages:

截圖 2022-04-17 10 25 01

assert_empty

截圖 2022-04-17 10 25 15

I guess it's because assert_empty doesn't care about individual items in the array, but assert_equal does. So it provides a more detailed information.

end
end

def assert_hover_result expected, expression: nil, frame_idx: 0
case ENV['RUBY_DEBUG_TEST_UI']
when 'vscode'
Expand Down