Skip to content
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
1 change: 1 addition & 0 deletions test/fixtures/addition_output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3
1 change: 1 addition & 0 deletions test/fixtures/args_sum_output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6
268 changes: 268 additions & 0 deletions test/fixtures/args_sum_trace.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
[
{
"Type": {
"kind": 7,
"lang_type": "Integer",
"specific_info": {
"kind": "None"
}
}
},
{
"Type": {
"kind": 9,
"lang_type": "String",
"specific_info": {
"kind": "None"
}
}
},
{
"Type": {
"kind": 12,
"lang_type": "Bool",
"specific_info": {
"kind": "None"
}
}
},
{
"Type": {
"kind": 9,
"lang_type": "Symbol",
"specific_info": {
"kind": "None"
}
}
},
{
"Type": {
"kind": 24,
"lang_type": "No type",
"specific_info": {
"kind": "None"
}
}
},
{
"Path": ""
},
{
"Function": {
"path_id": 0,
"line": 1,
"name": "<top-level>"
}
},
{
"Call": {
"function_id": 0,
"args": [

]
}
},
{
"Path": "test/programs/args_sum.rb"
},
{
"Step": {
"path_id": 1,
"line": 3
}
},
{
"Step": {
"path_id": 1,
"line": 7
}
},
{
"Type": {
"kind": 0,
"lang_type": "Array",
"specific_info": {
"kind": "None"
}
}
},
{
"Type": {
"kind": 16,
"lang_type": "Object",
"specific_info": {
"kind": "None"
}
}
},
{
"VariableName": "self"
},
{
"Value": {
"variable_id": 0,
"value": {
"kind": "Raw",
"type_id": 6,
"r": "main"
}
}
},
{
"VariableName": "args"
},
{
"Value": {
"variable_id": 1,
"value": {
"kind": "Sequence",
"type_id": 5,
"elements": [
{
"kind": "String",
"type_id": 1,
"text": "1"
},
{
"kind": "String",
"type_id": 1,
"text": "2"
},
{
"kind": "String",
"type_id": 1,
"text": "3"
}
],
"is_slice": false
}
}
},
{
"Step": {
"path_id": 1,
"line": 3
}
},
{
"Function": {
"path_id": 1,
"line": 3,
"name": "sum_args"
}
},
{
"Call": {
"function_id": 1,
"args": [
{
"variable_id": 0,
"value": {
"kind": "Raw",
"type_id": 6,
"r": "main"
}
},
{
"variable_id": 1,
"value": {
"kind": "Sequence",
"type_id": 5,
"elements": [
{
"kind": "String",
"type_id": 1,
"text": "1"
},
{
"kind": "String",
"type_id": 1,
"text": "2"
},
{
"kind": "String",
"type_id": 1,
"text": "3"
}
],
"is_slice": false
}
}
]
}
},
{
"Step": {
"path_id": 1,
"line": 4
}
},
{
"Value": {
"variable_id": 1,
"value": {
"kind": "Sequence",
"type_id": 5,
"elements": [
{
"kind": "String",
"type_id": 1,
"text": "1"
},
{
"kind": "String",
"type_id": 1,
"text": "2"
},
{
"kind": "String",
"type_id": 1,
"text": "3"
}
],
"is_slice": false
}
}
},
{
"Step": {
"path_id": 1,
"line": 5
}
},
{
"VariableName": "<return_value>"
},
{
"Value": {
"variable_id": 2,
"value": {
"kind": "Int",
"type_id": 0,
"i": 6
}
}
},
{
"Return": {
"return_value": {
"kind": "Int",
"type_id": 0,
"i": 6
}
}
},
{
"Step": {
"path_id": 1,
"line": 7
}
},
{
"Event": {
"kind": 0,
"content": "6",
"metadata": ""
}
}
]
1 change: 1 addition & 0 deletions test/fixtures/array_sum_output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6
1 change: 1 addition & 0 deletions test/fixtures/point_representation_output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"(3, 4)"
8 changes: 8 additions & 0 deletions test/programs/args_sum.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env ruby

def sum_args(args)
args.map(&:to_i).reduce(0, :+)
end

puts sum_args(ARGV)

28 changes: 23 additions & 5 deletions test/test_tracer.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
require 'minitest/autorun'
require 'json'
require 'fileutils'
require 'open3'

class TraceTest < Minitest::Test
TMP_DIR = File.expand_path('tmp', __dir__)
FIXTURE_DIR = File.expand_path('fixtures', __dir__)
PROGRAM_ARGS = {
'args_sum' => %w[1 2 3]
}

def setup
FileUtils.mkdir_p(TMP_DIR)
end

def run_trace(program_name)
def run_trace(program_name, *args)
base = File.basename(program_name, '.rb')
Dir.chdir(File.expand_path('..', __dir__)) do
program = File.join('test', 'programs', program_name)
out_dir = File.join('test', 'tmp', base)
FileUtils.mkdir_p(out_dir)
system('ruby', 'gems/pure-ruby-tracer/lib/trace.rb', '--out-dir', out_dir, program)
raise "trace failed" unless $?.success?
JSON.parse(File.read(File.join(out_dir, 'trace.json')))
stdout, stderr, status = Open3.capture3('ruby', 'gems/pure-ruby-tracer/lib/trace.rb', '--out-dir', out_dir, program, *args)
raise "trace failed: #{stderr}" unless status.success?
trace = JSON.parse(File.read(File.join(out_dir, 'trace.json')))
program_out = stdout.lines.reject { |l| l.start_with?('call ') || l.start_with?('return') }.join
[trace, program_out]
end
end

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

def expected_output(program_name)
base = File.basename(program_name, '.rb')
fixture = File.join(FIXTURE_DIR, "#{base}_output.txt")
File.read(fixture)
end

def program_args(base)
PROGRAM_ARGS.fetch(base, [])
end

Dir.glob(File.join(FIXTURE_DIR, '*_trace.json')).each do |fixture|
base = File.basename(fixture, '_trace.json')
define_method("test_#{base}") do
assert_equal expected_trace("#{base}.rb"), run_trace("#{base}.rb")
trace, out = run_trace("#{base}.rb", *program_args(base))
assert_equal expected_trace("#{base}.rb"), trace
assert_equal expected_output("#{base}.rb"), out
end
end
end