ruby-changes:41780
From: naruse <ko1@a...>
Date: Wed, 17 Feb 2016 19:15:04 +0900 (JST)
Subject: [ruby-changes:41780] naruse:r53854 (trunk): Additional fix and tests for r53851
naruse 2016-02-17 19:15:28 +0900 (Wed, 17 Feb 2016) New Revision: 53854 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=53854 Log: Additional fix and tests for r53851 Modified files: trunk/string.c trunk/test/ruby/test_string.rb Index: string.c =================================================================== --- string.c (revision 53853) +++ string.c (revision 53854) @@ -1383,6 +1383,7 @@ rb_str_init(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/string.c#L1383 vcapa = kwargs[1]; if (vcapa != Qundef && !NIL_P(vcapa)) { long capa = NUM2LONG(vcapa); + str_discard(str); if (capa < STR_BUF_MIN_SIZE) { capa = STR_BUF_MIN_SIZE; } @@ -1405,6 +1406,7 @@ rb_str_init(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/string.c#L1406 } else if (n == 1) { StringValue(orig); + str_discard(str); str_replace(str, orig); } if (enc != Qundef && !NIL_P(enc)) { @@ -1414,9 +1416,7 @@ rb_str_init(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/string.c#L1416 } else if (n == 1) { StringValue(orig); - if (OBJ_FROZEN(str)) { - rb_error_frozen_object(str); - } + str_discard(str); str_replace(str, orig); } return str; Index: test/ruby/test_string.rb =================================================================== --- test/ruby/test_string.rb (revision 53853) +++ test/ruby/test_string.rb (revision 53854) @@ -59,6 +59,17 @@ class TestString < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_string.rb#L59 assert_equal(Encoding::EUC_JP, S("", capacity: 1000, encoding: "euc-jp").encoding) end + def test_initialize + str = S("").freeze + assert_equal("", str.__send__(:initialize)) + assert_raise(RuntimeError){ str.__send__(:initialize, 'abc') } + assert_raise(RuntimeError){ str.__send__(:initialize, capacity: 1000) } + assert_raise(RuntimeError){ str.__send__(:initialize, 'abc', capacity: 1000) } + assert_raise(RuntimeError){ str.__send__(:initialize, encoding: 'euc-jp') } + assert_raise(RuntimeError){ str.__send__(:initialize, 'abc', encoding: 'euc-jp') } + assert_raise(RuntimeError){ str.__send__(:initialize, 'abc', capacity: 1000, encoding: 'euc-jp') } + end + def test_AREF # '[]' assert_equal("A", S("AooBar")[0]) assert_equal("B", S("FooBaB")[-1]) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/