2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2013-12-07 23:05:59 -05:00
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
class TestRubyVM < Test::Unit::TestCase
|
|
|
|
def test_stat
|
|
|
|
assert_kind_of Hash, RubyVM.stat
|
2022-03-31 11:04:25 -04:00
|
|
|
assert_kind_of Integer, RubyVM.stat[:class_serial]
|
2013-12-07 23:05:59 -05:00
|
|
|
|
|
|
|
RubyVM.stat(stat = {})
|
|
|
|
assert_not_empty stat
|
2022-03-31 11:04:25 -04:00
|
|
|
assert_equal stat[:class_serial], RubyVM.stat(:class_serial)
|
2013-12-07 23:05:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_stat_unknown
|
|
|
|
assert_raise(ArgumentError){ RubyVM.stat(:unknown) }
|
2014-03-30 22:34:40 -04:00
|
|
|
assert_raise_with_message(ArgumentError, /\u{30eb 30d3 30fc}/) {RubyVM.stat(:"\u{30eb 30d3 30fc}")}
|
2013-12-07 23:05:59 -05:00
|
|
|
end
|
2021-09-30 03:58:46 -04:00
|
|
|
|
|
|
|
def parse_and_compile
|
|
|
|
script = <<~RUBY
|
2021-10-25 07:45:05 -04:00
|
|
|
_a = 1
|
2021-09-30 03:58:46 -04:00
|
|
|
def foo
|
2021-10-25 07:45:05 -04:00
|
|
|
_b = 2
|
2021-09-30 03:58:46 -04:00
|
|
|
end
|
|
|
|
1.times{
|
2021-10-25 07:45:05 -04:00
|
|
|
_c = 3
|
2021-09-30 03:58:46 -04:00
|
|
|
}
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
ast = RubyVM::AbstractSyntaxTree.parse(script)
|
|
|
|
iseq = RubyVM::InstructionSequence.compile(script)
|
|
|
|
|
|
|
|
[ast, iseq]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_keep_script_lines
|
2022-01-30 13:27:18 -05:00
|
|
|
pend if ENV['RUBY_ISEQ_DUMP_DEBUG'] # TODO
|
|
|
|
|
2021-09-30 03:58:46 -04:00
|
|
|
prev_conf = RubyVM.keep_script_lines
|
|
|
|
|
|
|
|
# keep
|
|
|
|
RubyVM.keep_script_lines = true
|
|
|
|
|
|
|
|
ast, iseq = *parse_and_compile
|
|
|
|
|
|
|
|
lines = ast.script_lines
|
|
|
|
assert_equal Array, lines.class
|
|
|
|
|
|
|
|
lines = iseq.script_lines
|
|
|
|
assert_equal Array, lines.class
|
|
|
|
iseq.each_child{|child|
|
|
|
|
assert_equal lines, child.script_lines
|
|
|
|
}
|
2021-09-30 04:30:04 -04:00
|
|
|
assert lines.frozen?
|
2021-09-30 03:58:46 -04:00
|
|
|
|
|
|
|
# don't keep
|
|
|
|
RubyVM.keep_script_lines = false
|
|
|
|
|
|
|
|
ast, iseq = *parse_and_compile
|
|
|
|
|
|
|
|
lines = ast.script_lines
|
|
|
|
assert_equal nil, lines
|
|
|
|
|
|
|
|
lines = iseq.script_lines
|
|
|
|
assert_equal nil, lines
|
|
|
|
iseq.each_child{|child|
|
|
|
|
assert_equal lines, child.script_lines
|
|
|
|
}
|
|
|
|
|
|
|
|
ensure
|
|
|
|
RubyVM.keep_script_lines = prev_conf
|
|
|
|
end
|
2013-12-07 23:05:59 -05:00
|
|
|
end
|