|
| 1 | +require 'test/unit' |
| 2 | + |
| 3 | +class TestIseqLoad < Test::Unit::TestCase |
| 4 | + require '-test-/iseq_load/iseq_load' |
| 5 | + ISeq = RubyVM::InstructionSequence |
| 6 | + |
| 7 | + def test_bug8543 |
| 8 | + assert_iseq_roundtrip <<-'end;' |
| 9 | + puts "tralivali" |
| 10 | + def funct(a, b) |
| 11 | + a**b |
| 12 | + end |
| 13 | + 3.times { |i| puts "Hello, world#{funct(2,i)}!" } |
| 14 | + end; |
| 15 | + end |
| 16 | + |
| 17 | + def test_case_when |
| 18 | + assert_iseq_roundtrip <<-'end;' |
| 19 | + def user_mask(target) |
| 20 | + target.each_char.inject(0) do |mask, chr| |
| 21 | + case chr |
| 22 | + when "u" |
| 23 | + mask | 04700 |
| 24 | + when "g" |
| 25 | + mask | 02070 |
| 26 | + when "o" |
| 27 | + mask | 01007 |
| 28 | + when "a" |
| 29 | + mask | 07777 |
| 30 | + else |
| 31 | + raise ArgumentError, "invalid `who' symbol in file mode: #{chr}" |
| 32 | + end |
| 33 | + end |
| 34 | + end |
| 35 | + end; |
| 36 | + end |
| 37 | + |
| 38 | + def test_splatsplat |
| 39 | + assert_iseq_roundtrip('def splatsplat(**); end') |
| 40 | + end |
| 41 | + |
| 42 | + def test_hidden |
| 43 | + assert_iseq_roundtrip('def x(a, (b, *c), d: false); end') |
| 44 | + end |
| 45 | + |
| 46 | + def assert_iseq_roundtrip(src) |
| 47 | + a = ISeq.compile(src).to_a |
| 48 | + b = ISeq.iseq_load(a).to_a |
| 49 | + warn diff(a, b) if a != b |
| 50 | + assert_equal a, b |
| 51 | + assert_equal a, ISeq.iseq_load(b).to_a |
| 52 | + end |
| 53 | + |
| 54 | + def test_next_in_block_in_block |
| 55 | + skip "failing due to stack_max mismatch" |
| 56 | + assert_iseq_roundtrip <<-'end;' |
| 57 | + 3.times { 3.times { next } } |
| 58 | + end; |
| 59 | + end |
| 60 | + |
| 61 | + def test_break_ensure |
| 62 | + skip "failing due to exception entry sp mismatch" |
| 63 | + assert_iseq_roundtrip <<-'end;' |
| 64 | + def m |
| 65 | + bad = true |
| 66 | + while true |
| 67 | + begin |
| 68 | + break |
| 69 | + ensure |
| 70 | + bad = false |
| 71 | + end |
| 72 | + end |
| 73 | + end |
| 74 | + end; |
| 75 | + end |
| 76 | + |
| 77 | + # FIXME: still failing |
| 78 | + def test_require_integration |
| 79 | + skip "iseq loader require integration tests still failing" |
| 80 | + f = File.expand_path(__FILE__) |
| 81 | + # $(top_srcdir)/test/ruby/test_....rb |
| 82 | + 3.times { f = File.dirname(f) } |
| 83 | + Dir[File.join(f, 'ruby', '*.rb')].each do |f| |
| 84 | + iseq = ISeq.compile_file(f) |
| 85 | + orig = iseq.to_a.freeze |
| 86 | + |
| 87 | + loaded = ISeq.iseq_load(orig).to_a |
| 88 | + if loaded != orig |
| 89 | + warn f |
| 90 | + warn diff(orig, loaded) |
| 91 | + end |
| 92 | + #assert_equal orig, loaded |
| 93 | + end |
| 94 | + end |
| 95 | +end |
0 commit comments