Skip to content

Commit 262486b

Browse files
vinistockegiurleo
authored andcommitted
Add test for global variable support in DAP and CDP
1 parent bd8678c commit 262486b

File tree

2 files changed

+79
-12
lines changed

2 files changed

+79
-12
lines changed

test/protocol/variables_test.rb

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "../support/protocol_test_case"
4+
5+
module DEBUGGER__
6+
class DAPVariablesTest < ProtocolTestCase
7+
PROGRAM = <<~RUBY
8+
1| $a = 1
9+
2| $b = 2
10+
3| $c = 3
11+
RUBY
12+
13+
def test_eval_evaluates_global_variables
14+
run_protocol_scenario PROGRAM, cdp: false do
15+
req_add_breakpoint 3
16+
req_continue
17+
18+
globals = gather_variables(type: "globals")
19+
20+
# User defined globals
21+
assert_includes(globals, { name: "$a", value: "1", type: "Integer" })
22+
assert_includes(globals, { name: "$b", value: "2", type: "Integer" })
23+
24+
# Ruby defined globals
25+
assert_includes(globals, { name: "$VERBOSE", value: "false", type: "FalseClass" })
26+
assert_includes(globals, { name: "$stdout", value: "#<IO:<STDOUT>>", type: "IO" })
27+
28+
req_terminate_debuggee
29+
end
30+
end
31+
end
32+
33+
class CDPVariablesTest < ProtocolTestCase
34+
PROGRAM = <<~RUBY
35+
1| $a = 1
36+
2| $b = 2
37+
3| $c = 3
38+
RUBY
39+
40+
def test_eval_evaluates_global_variables
41+
run_protocol_scenario PROGRAM, dap: false do
42+
req_add_breakpoint 3
43+
req_continue
44+
45+
globals = gather_variables(type: "global")
46+
47+
# User defined globals
48+
assert_includes(globals, { name: "$a", value: "1", type: "Number" })
49+
assert_includes(globals, { name: "$b", value: "2", type: "Number" })
50+
51+
# Ruby defined globals
52+
assert_includes(globals, { name: "$VERBOSE", value: "false", type: "Boolean" })
53+
assert_includes(globals, { name: "$stdout", value: "#<IO:<STDOUT>>", type: "Object" })
54+
55+
req_terminate_debuggee
56+
end
57+
end
58+
end
59+
end

test/support/protocol_test_case.rb

+20-12
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def req_terminate_debuggee
206206
close_reader
207207
end
208208

209-
def assert_locals_result expected, frame_idx: 0
209+
def gather_variables(frame_idx: 0, type: "locals")
210210
case get_target_ui
211211
when 'vscode'
212212
# get frameId
@@ -219,32 +219,40 @@ def assert_locals_result expected, frame_idx: 0
219219
# get variablesReference
220220
res = send_dap_request 'scopes', frameId: f_id
221221

222-
locals_scope = res.dig(:body, :scopes).find { |d| d[:presentationHint] == "locals" }
222+
locals_scope = res.dig(:body, :scopes).find { |d| d[:presentationHint] == type }
223223
locals_reference = locals_scope[:variablesReference]
224224

225225
# get variables
226226
res = send_dap_request 'variables', variablesReference: locals_reference
227-
228-
expected.each do |exp|
229-
if exp[:type] == "String"
230-
exp[:value] = exp[:value].inspect
231-
end
232-
end
233-
234-
actual_locals = res.dig(:body, :variables).map { |loc| { name: loc[:name], value: loc[:value], type: loc[:type] } }
227+
res.dig(:body, :variables).map { |loc| { name: loc[:name], value: loc[:value], type: loc[:type] } }
235228
when 'chrome'
236229
current_frame = @crt_frames.first
237-
locals_scope = current_frame[:scopeChain].find { |f| f[:type] == "local" }
230+
locals_scope = current_frame[:scopeChain].find { |f| f[:type] == type }
238231
object_id = locals_scope.dig(:object, :objectId)
239232

240233
res = send_cdp_request "Runtime.getProperties", objectId: object_id
241234

242-
actual_locals = res.dig(:result, :result).map do |loc|
235+
res.dig(:result, :result).map do |loc|
243236
type = loc.dig(:value, :className) || loc.dig(:value, :type).capitalize # TODO: sync this with get_ruby_type
244237

245238
{ name: loc[:name], value: loc.dig(:value, :description), type: type }
246239
end
247240
end
241+
end
242+
243+
def assert_locals_result expected, frame_idx: 0
244+
case get_target_ui
245+
when 'vscode'
246+
actual_locals = gather_dap_variables(frame_idx: frame_idx, type: "locals")
247+
248+
expected.each do |exp|
249+
if exp[:type] == "String"
250+
exp[:value] = exp[:value].inspect
251+
end
252+
end
253+
when 'chrome'
254+
actual_locals = gather_variables(type: "local")
255+
end
248256

249257
failure_msg = FailureMessage.new{create_protocol_message "result:\n#{JSON.pretty_generate res}"}
250258

0 commit comments

Comments
 (0)