Skip to content

Commit df2b76f

Browse files
committed
Add tests for trace inspector and record inspector
1 parent c71538d commit df2b76f

File tree

4 files changed

+289
-0
lines changed

4 files changed

+289
-0
lines changed

lib/debug/dap_custom/recordInspector.rb

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def custom_dap_request_rdbgRecordInspector(req)
2020
tid = req.dig('arguments', 'threadId')
2121
count = req.dig('arguments', 'count')
2222
if tc = find_waiting_tc(tid)
23+
@ui.respond req, {}
2324
tc << [:step, :in, count]
2425
else
2526
fail_response req
@@ -28,6 +29,7 @@ def custom_dap_request_rdbgRecordInspector(req)
2829
tid = req.dig('arguments', 'threadId')
2930
count = req.dig('arguments', 'count')
3031
if tc = find_waiting_tc(tid)
32+
@ui.respond req, {}
3133
tc << [:step, :back, count]
3234
else
3335
fail_response req
+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../support/protocol_test_case'
4+
5+
module DEBUGGER__
6+
class RdbgRecordInspectorTest < ProtocolTestCase
7+
PROGRAM = <<~RUBY
8+
1| module Foo
9+
2| class Bar
10+
3| def self.a
11+
4| "hello"
12+
5| end
13+
6| end
14+
7| Bar.a
15+
8| bar = Bar.new
16+
9| end
17+
RUBY
18+
19+
def test_defaut_setting
20+
DEBUGGER__::INITIALIZE_DAP_MSGS[1][:arguments][:rdbgExtension] = ["recordInspector"]
21+
run_protocol_scenario(PROGRAM, cdp: false) do
22+
req_rdbgRecordInspector_enable
23+
req_add_breakpoint 5
24+
req_continue
25+
assert_rdbgRecordInspector_collect_result(
26+
[
27+
{
28+
name: "<module:Foo>",
29+
location: {
30+
line: 2,
31+
}
32+
},
33+
{
34+
name: "<class:Bar>",
35+
location: {
36+
line: 3,
37+
}
38+
}
39+
]
40+
)
41+
req_rdbgRecordInspector_step_back 4
42+
assert_line_num 2
43+
req_rdbgRecordInspector_step 1
44+
assert_line_num 3
45+
req_terminate_debuggee
46+
end
47+
ensure
48+
DEBUGGER__::INITIALIZE_DAP_MSGS[1][:arguments].delete :rdbgExtension
49+
end
50+
51+
def test_restart_trace
52+
DEBUGGER__::INITIALIZE_DAP_MSGS[1][:arguments][:rdbgExtension] = ["recordInspector"]
53+
run_protocol_scenario(PROGRAM, cdp: false) do
54+
req_rdbgRecordInspector_enable
55+
req_rdbgRecordInspector_disable
56+
req_rdbgRecordInspector_enable
57+
req_add_breakpoint 5
58+
req_continue
59+
assert_rdbgRecordInspector_collect_result(
60+
[
61+
{
62+
name: "<module:Foo>",
63+
location: {
64+
line: 2,
65+
}
66+
},
67+
{
68+
name: "<class:Bar>",
69+
location: {
70+
line: 3,
71+
}
72+
}
73+
]
74+
)
75+
req_rdbgRecordInspector_step_back 4
76+
assert_line_num 2
77+
req_rdbgRecordInspector_step 1
78+
assert_line_num 3
79+
req_terminate_debuggee
80+
end
81+
ensure
82+
DEBUGGER__::INITIALIZE_DAP_MSGS[1][:arguments].delete :rdbgExtension
83+
end
84+
end
85+
end
+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../support/protocol_test_case'
4+
5+
module DEBUGGER__
6+
class RdbgTraceInspectorTest < ProtocolTestCase
7+
PROGRAM = <<~RUBY
8+
1| def foo
9+
2| 'bar'
10+
3| end
11+
4| foo
12+
5| foo
13+
RUBY
14+
15+
def test_defaut_setting
16+
DEBUGGER__::INITIALIZE_DAP_MSGS[1][:arguments][:rdbgExtension] = ["traceInspector"]
17+
run_protocol_scenario(PROGRAM, cdp: false) do
18+
req_rdbgTraceInspector_enable
19+
req_add_breakpoint 5
20+
req_continue
21+
assert_rdbgTraceInspector_collect_result(
22+
[
23+
{
24+
returnValue: "\"bar\"",
25+
name: 'Object#foo',
26+
location: {
27+
line: 3,
28+
}
29+
},
30+
{
31+
name: 'Object#foo',
32+
location: {
33+
line: 1,
34+
}
35+
},
36+
{
37+
location: {
38+
line: 4,
39+
}
40+
},
41+
]
42+
)
43+
req_terminate_debuggee
44+
end
45+
ensure
46+
DEBUGGER__::INITIALIZE_DAP_MSGS[1][:arguments].delete :rdbgExtension
47+
end
48+
49+
def test_call_event
50+
DEBUGGER__::INITIALIZE_DAP_MSGS[1][:arguments][:rdbgExtension] = ["traceInspector"]
51+
run_protocol_scenario(PROGRAM, cdp: false) do
52+
req_rdbgTraceInspector_enable(events: ['call'])
53+
req_add_breakpoint 5
54+
req_continue
55+
assert_rdbgTraceInspector_collect_result(
56+
[
57+
{
58+
name: 'Object#foo',
59+
location: {
60+
line: 1,
61+
}
62+
},
63+
]
64+
)
65+
req_terminate_debuggee
66+
end
67+
ensure
68+
DEBUGGER__::INITIALIZE_DAP_MSGS[1][:arguments].delete :rdbgExtension
69+
end
70+
71+
def test_return_event
72+
DEBUGGER__::INITIALIZE_DAP_MSGS[1][:arguments][:rdbgExtension] = ["traceInspector"]
73+
run_protocol_scenario(PROGRAM, cdp: false) do
74+
req_rdbgTraceInspector_enable(events: ['return'])
75+
req_add_breakpoint 5
76+
req_continue
77+
assert_rdbgTraceInspector_collect_result(
78+
[
79+
{
80+
returnValue: "\"bar\"",
81+
name: 'Object#foo',
82+
location: {
83+
line: 3,
84+
}
85+
},
86+
]
87+
)
88+
req_terminate_debuggee
89+
end
90+
ensure
91+
DEBUGGER__::INITIALIZE_DAP_MSGS[1][:arguments].delete :rdbgExtension
92+
end
93+
94+
def test_line_event
95+
DEBUGGER__::INITIALIZE_DAP_MSGS[1][:arguments][:rdbgExtension] = ["traceInspector"]
96+
run_protocol_scenario(PROGRAM, cdp: false) do
97+
req_rdbgTraceInspector_enable(events: ['line'])
98+
req_add_breakpoint 5
99+
req_continue
100+
assert_rdbgTraceInspector_collect_result(
101+
[
102+
{
103+
location: {
104+
line: 4,
105+
}
106+
},
107+
]
108+
)
109+
req_terminate_debuggee
110+
end
111+
ensure
112+
DEBUGGER__::INITIALIZE_DAP_MSGS[1][:arguments].delete :rdbgExtension
113+
end
114+
115+
def test_restart_trace
116+
DEBUGGER__::INITIALIZE_DAP_MSGS[1][:arguments][:rdbgExtension] = ["traceInspector"]
117+
run_protocol_scenario(PROGRAM, cdp: false) do
118+
req_rdbgTraceInspector_enable
119+
req_rdbgTraceInspector_disable
120+
req_rdbgTraceInspector_enable(events: ['line'])
121+
req_add_breakpoint 5
122+
req_continue
123+
assert_rdbgTraceInspector_collect_result(
124+
[
125+
{
126+
location: {
127+
line: 4,
128+
}
129+
},
130+
]
131+
)
132+
req_terminate_debuggee
133+
end
134+
ensure
135+
DEBUGGER__::INITIALIZE_DAP_MSGS[1][:arguments].delete :rdbgExtension
136+
end
137+
end
138+
end

test/support/protocol_test_case.rb

+64
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,22 @@ def gather_variables(frame_idx: 0, type: "locals")
219219
end
220220
end
221221

222+
def req_rdbgTraceInspector_enable events: ['line', 'call', 'return']
223+
send_custom_dap_request 'rdbgTraceInspector', command: 'enable', events: events
224+
end
225+
226+
def req_rdbgTraceInspector_disable
227+
send_custom_dap_request 'rdbgTraceInspector', command: 'disable'
228+
end
229+
230+
def req_rdbgRecordInspector_enable
231+
send_custom_dap_request 'rdbgRecordInspector', command: 'enable'
232+
end
233+
234+
def req_rdbgRecordInspector_disable
235+
send_custom_dap_request 'rdbgRecordInspector', command: 'disable'
236+
end
237+
222238
def assert_locals_result expected, frame_idx: 0
223239
case get_target_ui
224240
when 'vscode'
@@ -301,8 +317,51 @@ def assert_watch_result expected, expression, frame_idx: 0
301317
end
302318
end
303319

320+
def req_rdbgRecordInspector_step_back count
321+
send_custom_dap_request 'rdbgRecordInspector', command: 'stepBack', threadId: 1, count: count
322+
end
323+
324+
def req_rdbgRecordInspector_step count
325+
send_custom_dap_request 'rdbgRecordInspector', command: 'step', threadId: 1, count: count
326+
end
327+
328+
def assert_rdbgRecordInspector_collect_result expected
329+
res = send_custom_dap_request 'rdbgRecordInspector', command: 'collect', threadId: 1
330+
assert_collect_result(res, expected) do |exp, log|
331+
check_hash_values(exp, log, :name) &&
332+
check_hash_values(exp[:location], log[:location], :line)
333+
end
334+
end
335+
336+
def assert_rdbgTraceInspector_collect_result expected
337+
res = send_custom_dap_request 'rdbgTraceInspector', command: 'collect'
338+
assert_collect_result(res, expected) do |exp, log|
339+
check_hash_values(exp, log, :returnValue) &&
340+
check_hash_values(exp, log, :name) &&
341+
check_hash_values(exp[:location], log[:location], :line)
342+
end
343+
end
344+
304345
# Not API
305346

347+
def assert_collect_result res, expected
348+
logs = res.dig(:body, :logs)
349+
expected.each{|exp|
350+
matched = logs.find {|log|
351+
yield exp, log
352+
}
353+
if matched.nil?
354+
msg = create_protocol_message "Expected to include\n`#{JSON.pretty_generate exp}`\nIn\n`#{JSON.pretty_generate logs}`\n"
355+
flunk(msg)
356+
end
357+
}
358+
end
359+
360+
def check_hash_values hash_a, hash_b, key
361+
hash_a.has_key?(key) == hash_b.has_key?(key) &&
362+
hash_a[key] == hash_b[key]
363+
end
364+
306365
def attach_to_cdp_server_
307366
body = get_request HOST, @remote_info.port, '/json'
308367
Timeout.timeout(TIMEOUT_SEC) do
@@ -558,6 +617,11 @@ def send_dap_request command, **kw
558617
res
559618
end
560619

620+
def send_custom_dap_request command, **kw
621+
send_request command, **kw
622+
return find_crt_dap_response
623+
end
624+
561625
def send_cdp_request command, **kw
562626
send_request command, **kw
563627
res = find_crt_cdp_response

0 commit comments

Comments
 (0)