diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-12-24 15:59:31 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-12-24 15:59:31 +0000 |
commit | 7ea675732ac1dac72f07756498706678d8725719 (patch) | |
tree | e6a405332d045f861a5ec13e253bdcfc56ffbf5c /test | |
parent | d2fa4d7118b4b064cd84a7b1dbf37ae0ff6c1883 (diff) | |
download | ruby-7ea675732ac1dac72f07756498706678d8725719.tar.gz |
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
Diffstat (limited to 'test')
-rw-r--r-- | test/ruby/test_parse.rb | 4 | ||||
-rw-r--r-- | test/ruby/test_syntax.rb | 64 |
2 files changed, 67 insertions, 1 deletions
diff --git a/test/ruby/test_parse.rb b/test/ruby/test_parse.rb index 319af751d0..d6e42b12eb 100644 --- a/test/ruby/test_parse.rb +++ b/test/ruby/test_parse.rb @@ -173,10 +173,12 @@ class TestParse < Test::Unit::TestCase end c = Class.new - assert_raise(SyntaxError) do + assert_nothing_raised(SyntaxError) do eval <<-END, nil, __FILE__, __LINE__+1 + if false c::FOO &= 1 ::FOO &= 1 + end END end diff --git a/test/ruby/test_syntax.rb b/test/ruby/test_syntax.rb index b2d625cd2a..5af1ea48ba 100644 --- a/test/ruby/test_syntax.rb +++ b/test/ruby/test_syntax.rb @@ -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 |