1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/test/ruby/test_rubyvm.rb
Yusuke Endoh 1eac38c609 test/ruby/test_rubyvm.rb: prevent "assigned but unused variable" warnings
http://rubyci.s3.amazonaws.com/centos7/ruby-master/log/20211025T093004Z.log.html.gz
```
[ 4896/21159] TestRubyVM#test_keep_script_lines(none):3: warning: assigned but unused variable - b
(none):6: warning: assigned but unused variable - c
(none):1: warning: assigned but unused variable - a
<compiled>:3: warning: assigned but unused variable - b
(none):3: warning: assigned but unused variable - b
(none):6: warning: assigned but unused variable - c
(none):1: warning: assigned but unused variable - a
<compiled>:3: warning: assigned but unused variable - b
```
2021-10-25 20:45:05 +09:00

71 lines
1.5 KiB
Ruby

# frozen_string_literal: false
require 'test/unit'
class TestRubyVM < Test::Unit::TestCase
def test_stat
assert_kind_of Hash, RubyVM.stat
assert_kind_of Integer, RubyVM.stat[:global_constant_state]
RubyVM.stat(stat = {})
assert_not_empty stat
assert_equal stat[:global_constant_state], RubyVM.stat(:global_constant_state)
end
def test_stat_unknown
assert_raise(ArgumentError){ RubyVM.stat(:unknown) }
assert_raise_with_message(ArgumentError, /\u{30eb 30d3 30fc}/) {RubyVM.stat(:"\u{30eb 30d3 30fc}")}
end
def parse_and_compile
script = <<~RUBY
_a = 1
def foo
_b = 2
end
1.times{
_c = 3
}
RUBY
ast = RubyVM::AbstractSyntaxTree.parse(script)
iseq = RubyVM::InstructionSequence.compile(script)
[ast, iseq]
end
def test_keep_script_lines
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
}
assert lines.frozen?
# 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
end