Skip to content

Commit e6fa804

Browse files
committed
test: verify program output
1 parent 5d26e5e commit e6fa804

File tree

7 files changed

+303
-5
lines changed

7 files changed

+303
-5
lines changed

test/fixtures/addition_output.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3

test/fixtures/args_sum_output.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
6

test/fixtures/args_sum_trace.json

Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
[
2+
{
3+
"Type": {
4+
"kind": 7,
5+
"lang_type": "Integer",
6+
"specific_info": {
7+
"kind": "None"
8+
}
9+
}
10+
},
11+
{
12+
"Type": {
13+
"kind": 9,
14+
"lang_type": "String",
15+
"specific_info": {
16+
"kind": "None"
17+
}
18+
}
19+
},
20+
{
21+
"Type": {
22+
"kind": 12,
23+
"lang_type": "Bool",
24+
"specific_info": {
25+
"kind": "None"
26+
}
27+
}
28+
},
29+
{
30+
"Type": {
31+
"kind": 9,
32+
"lang_type": "Symbol",
33+
"specific_info": {
34+
"kind": "None"
35+
}
36+
}
37+
},
38+
{
39+
"Type": {
40+
"kind": 24,
41+
"lang_type": "No type",
42+
"specific_info": {
43+
"kind": "None"
44+
}
45+
}
46+
},
47+
{
48+
"Path": ""
49+
},
50+
{
51+
"Function": {
52+
"path_id": 0,
53+
"line": 1,
54+
"name": "<top-level>"
55+
}
56+
},
57+
{
58+
"Call": {
59+
"function_id": 0,
60+
"args": [
61+
62+
]
63+
}
64+
},
65+
{
66+
"Path": "test/programs/args_sum.rb"
67+
},
68+
{
69+
"Step": {
70+
"path_id": 1,
71+
"line": 3
72+
}
73+
},
74+
{
75+
"Step": {
76+
"path_id": 1,
77+
"line": 7
78+
}
79+
},
80+
{
81+
"Type": {
82+
"kind": 0,
83+
"lang_type": "Array",
84+
"specific_info": {
85+
"kind": "None"
86+
}
87+
}
88+
},
89+
{
90+
"Type": {
91+
"kind": 16,
92+
"lang_type": "Object",
93+
"specific_info": {
94+
"kind": "None"
95+
}
96+
}
97+
},
98+
{
99+
"VariableName": "self"
100+
},
101+
{
102+
"Value": {
103+
"variable_id": 0,
104+
"value": {
105+
"kind": "Raw",
106+
"type_id": 6,
107+
"r": "main"
108+
}
109+
}
110+
},
111+
{
112+
"VariableName": "args"
113+
},
114+
{
115+
"Value": {
116+
"variable_id": 1,
117+
"value": {
118+
"kind": "Sequence",
119+
"type_id": 5,
120+
"elements": [
121+
{
122+
"kind": "String",
123+
"type_id": 1,
124+
"text": "1"
125+
},
126+
{
127+
"kind": "String",
128+
"type_id": 1,
129+
"text": "2"
130+
},
131+
{
132+
"kind": "String",
133+
"type_id": 1,
134+
"text": "3"
135+
}
136+
],
137+
"is_slice": false
138+
}
139+
}
140+
},
141+
{
142+
"Step": {
143+
"path_id": 1,
144+
"line": 3
145+
}
146+
},
147+
{
148+
"Function": {
149+
"path_id": 1,
150+
"line": 3,
151+
"name": "sum_args"
152+
}
153+
},
154+
{
155+
"Call": {
156+
"function_id": 1,
157+
"args": [
158+
{
159+
"variable_id": 0,
160+
"value": {
161+
"kind": "Raw",
162+
"type_id": 6,
163+
"r": "main"
164+
}
165+
},
166+
{
167+
"variable_id": 1,
168+
"value": {
169+
"kind": "Sequence",
170+
"type_id": 5,
171+
"elements": [
172+
{
173+
"kind": "String",
174+
"type_id": 1,
175+
"text": "1"
176+
},
177+
{
178+
"kind": "String",
179+
"type_id": 1,
180+
"text": "2"
181+
},
182+
{
183+
"kind": "String",
184+
"type_id": 1,
185+
"text": "3"
186+
}
187+
],
188+
"is_slice": false
189+
}
190+
}
191+
]
192+
}
193+
},
194+
{
195+
"Step": {
196+
"path_id": 1,
197+
"line": 4
198+
}
199+
},
200+
{
201+
"Value": {
202+
"variable_id": 1,
203+
"value": {
204+
"kind": "Sequence",
205+
"type_id": 5,
206+
"elements": [
207+
{
208+
"kind": "String",
209+
"type_id": 1,
210+
"text": "1"
211+
},
212+
{
213+
"kind": "String",
214+
"type_id": 1,
215+
"text": "2"
216+
},
217+
{
218+
"kind": "String",
219+
"type_id": 1,
220+
"text": "3"
221+
}
222+
],
223+
"is_slice": false
224+
}
225+
}
226+
},
227+
{
228+
"Step": {
229+
"path_id": 1,
230+
"line": 5
231+
}
232+
},
233+
{
234+
"VariableName": "<return_value>"
235+
},
236+
{
237+
"Value": {
238+
"variable_id": 2,
239+
"value": {
240+
"kind": "Int",
241+
"type_id": 0,
242+
"i": 6
243+
}
244+
}
245+
},
246+
{
247+
"Return": {
248+
"return_value": {
249+
"kind": "Int",
250+
"type_id": 0,
251+
"i": 6
252+
}
253+
}
254+
},
255+
{
256+
"Step": {
257+
"path_id": 1,
258+
"line": 7
259+
}
260+
},
261+
{
262+
"Event": {
263+
"kind": 0,
264+
"content": "6",
265+
"metadata": ""
266+
}
267+
}
268+
]

test/fixtures/array_sum_output.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
6
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"(3, 4)"

test/programs/args_sum.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env ruby
2+
3+
def sum_args(args)
4+
args.map(&:to_i).reduce(0, :+)
5+
end
6+
7+
puts sum_args(ARGV)
8+

test/test_tracer.rb

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
11
require 'minitest/autorun'
22
require 'json'
33
require 'fileutils'
4+
require 'open3'
45

56
class TraceTest < Minitest::Test
67
TMP_DIR = File.expand_path('tmp', __dir__)
78
FIXTURE_DIR = File.expand_path('fixtures', __dir__)
9+
PROGRAM_ARGS = {
10+
'args_sum' => %w[1 2 3]
11+
}
812

913
def setup
1014
FileUtils.mkdir_p(TMP_DIR)
1115
end
1216

13-
def run_trace(program_name)
17+
def run_trace(program_name, *args)
1418
base = File.basename(program_name, '.rb')
1519
Dir.chdir(File.expand_path('..', __dir__)) do
1620
program = File.join('test', 'programs', program_name)
1721
out_dir = File.join('test', 'tmp', base)
1822
FileUtils.mkdir_p(out_dir)
19-
system('ruby', 'gems/pure-ruby-tracer/lib/trace.rb', '--out-dir', out_dir, program)
20-
raise "trace failed" unless $?.success?
21-
JSON.parse(File.read(File.join(out_dir, 'trace.json')))
23+
stdout, stderr, status = Open3.capture3('ruby', 'gems/pure-ruby-tracer/lib/trace.rb', '--out-dir', out_dir, program, *args)
24+
raise "trace failed: #{stderr}" unless status.success?
25+
trace = JSON.parse(File.read(File.join(out_dir, 'trace.json')))
26+
program_out = stdout.lines.reject { |l| l.start_with?('call ') || l.start_with?('return') }.join
27+
[trace, program_out]
2228
end
2329
end
2430

@@ -28,10 +34,22 @@ def expected_trace(program_name)
2834
JSON.parse(File.read(fixture))
2935
end
3036

37+
def expected_output(program_name)
38+
base = File.basename(program_name, '.rb')
39+
fixture = File.join(FIXTURE_DIR, "#{base}_output.txt")
40+
File.read(fixture)
41+
end
42+
43+
def program_args(base)
44+
PROGRAM_ARGS.fetch(base, [])
45+
end
46+
3147
Dir.glob(File.join(FIXTURE_DIR, '*_trace.json')).each do |fixture|
3248
base = File.basename(fixture, '_trace.json')
3349
define_method("test_#{base}") do
34-
assert_equal expected_trace("#{base}.rb"), run_trace("#{base}.rb")
50+
trace, out = run_trace("#{base}.rb", *program_args(base))
51+
assert_equal expected_trace("#{base}.rb"), trace
52+
assert_equal expected_output("#{base}.rb"), out
3553
end
3654
end
3755
end

0 commit comments

Comments
 (0)