11require 'minitest/autorun'
22require 'json'
33require 'fileutils'
4+ require 'open3'
45
56class 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
3755end
0 commit comments