1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

scoped constant op-assignment

* node.h (NODE_OP_CDECL), compile.c (iseq_compile_each),
  parse.y (stmt, arg): allow scoped constant op-assignment.
  [ruby-core:40154] [Bug #5449]


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38585 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2012-12-24 15:59:31 +00:00
parent d2fa4d7118
commit 7ea675732a
6 changed files with 173 additions and 10 deletions

View file

@ -209,6 +209,70 @@ eom
assert_equal(expected, actual)
end
def assert_constant_reassignment_nested(preset, op, expected, err = [], bug = '[Bug #5449]')
[
["p ", ""], # no-pop
["", "p Foo::Bar"], # pop
].each do |p1, p2|
src = <<-EOM.gsub(/^\s*\n/, '')
class Foo
#{"Bar = " + preset if preset}
end
#{p1}Foo::Bar #{op}= 42
#{p2}
EOM
msg = "\# #{bug}\n#{src}"
assert_valid_syntax(src, caller_locations(1, 1)[0].path, msg)
assert_in_out_err([], src, expected, err, msg)
end
end
def test_constant_reassignment_nested
already = /already initialized constant Foo::Bar/
uninitialized = /uninitialized constant Foo::Bar/
assert_constant_reassignment_nested(nil, "||", %w[42])
assert_constant_reassignment_nested("false", "||", %w[42], already)
assert_constant_reassignment_nested("true", "||", %w[true])
assert_constant_reassignment_nested(nil, "&&", [], uninitialized)
assert_constant_reassignment_nested("false", "&&", %w[false])
assert_constant_reassignment_nested("true", "&&", %w[42], already)
assert_constant_reassignment_nested(nil, "+", [], uninitialized)
assert_constant_reassignment_nested("false", "+", [], /undefined method/)
assert_constant_reassignment_nested("11", "+", %w[53], already)
end
def assert_constant_reassignment_toplevel(preset, op, expected, err = [], bug = '[Bug #5449]')
[
["p ", ""], # no-pop
["", "p ::Bar"], # pop
].each do |p1, p2|
src = <<-EOM.gsub(/^\s*\n/, '')
#{"Bar = " + preset if preset}
class Foo
#{p1}::Bar #{op}= 42
#{p2}
end
EOM
msg = "\# #{bug}\n#{src}"
assert_valid_syntax(src, caller_locations(1, 1)[0].path, msg)
assert_in_out_err([], src, expected, err, msg)
end
end
def test_constant_reassignment_toplevel
already = /already initialized constant Bar/
uninitialized = /uninitialized constant Bar/
assert_constant_reassignment_toplevel(nil, "||", %w[42])
assert_constant_reassignment_toplevel("false", "||", %w[42], already)
assert_constant_reassignment_toplevel("true", "||", %w[true])
assert_constant_reassignment_toplevel(nil, "&&", [], uninitialized)
assert_constant_reassignment_toplevel("false", "&&", %w[false])
assert_constant_reassignment_toplevel("true", "&&", %w[42], already)
assert_constant_reassignment_toplevel(nil, "+", [], uninitialized)
assert_constant_reassignment_toplevel("false", "+", [], /undefined method/)
assert_constant_reassignment_toplevel("11", "+", %w[53], already)
end
private
def not_label(x) @result = x; @not_label ||= nil end