diff --git a/test/fixtures/addition_output.txt b/test/fixtures/addition_output.txt new file mode 100644 index 0000000..00750ed --- /dev/null +++ b/test/fixtures/addition_output.txt @@ -0,0 +1 @@ +3 diff --git a/test/fixtures/args_sum_output.txt b/test/fixtures/args_sum_output.txt new file mode 100644 index 0000000..1e8b314 --- /dev/null +++ b/test/fixtures/args_sum_output.txt @@ -0,0 +1 @@ +6 diff --git a/test/fixtures/args_sum_trace.json b/test/fixtures/args_sum_trace.json new file mode 100644 index 0000000..c295992 --- /dev/null +++ b/test/fixtures/args_sum_trace.json @@ -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": "" + } + }, + { + "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": "" + }, + { + "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": "" + } + } +] diff --git a/test/fixtures/array_sum_output.txt b/test/fixtures/array_sum_output.txt new file mode 100644 index 0000000..1e8b314 --- /dev/null +++ b/test/fixtures/array_sum_output.txt @@ -0,0 +1 @@ +6 diff --git a/test/fixtures/point_representation_output.txt b/test/fixtures/point_representation_output.txt new file mode 100644 index 0000000..c3bda07 --- /dev/null +++ b/test/fixtures/point_representation_output.txt @@ -0,0 +1 @@ +"(3, 4)" diff --git a/test/programs/args_sum.rb b/test/programs/args_sum.rb new file mode 100644 index 0000000..16f9e6a --- /dev/null +++ b/test/programs/args_sum.rb @@ -0,0 +1,8 @@ +#!/usr/bin/env ruby + +def sum_args(args) + args.map(&:to_i).reduce(0, :+) +end + +puts sum_args(ARGV) + diff --git a/test/test_tracer.rb b/test/test_tracer.rb index 6b15259..54396fc 100644 --- a/test/test_tracer.rb +++ b/test/test_tracer.rb @@ -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 @@ -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