ruby-changes:3311
From: ko1@a...
Date: 31 Dec 2007 02:31:34 +0900
Subject: [ruby-changes:3311] akr - Ruby:r14804 (trunk): add more tests for sub, gsub, sub!, gsub!.
akr 2007-12-31 02:30:54 +0900 (Mon, 31 Dec 2007) New Revision: 14804 Modified files: trunk/test/ruby/test_m17n.rb Log: add more tests for sub, gsub, sub!, gsub!. http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/test/ruby/test_m17n.rb?r1=14804&r2=14803 Index: test/ruby/test_m17n.rb =================================================================== --- test/ruby/test_m17n.rb (revision 14803) +++ test/ruby/test_m17n.rb (revision 14804) @@ -1917,4 +1917,106 @@ assert_equal(Encoding::ASCII_8BIT, v.encoding) } end + + def test_str_sub + combination(STRINGS, STRINGS, STRINGS) {|s1, s2, s3| + if !s2.valid_encoding? + assert_raise(RegexpError) { Regexp.new(Regexp.escape(s2)) } + next + end + r2 = Regexp.new(Regexp.escape(s2)) + [ + [ + "#{encdump s1}.sub(Regexp.new(#{encdump s2}), #{encdump s3})", + lambda { s1.sub(r2, s3) } + ], + [ + "#{encdump s1}.sub(Regexp.new(#{encdump s2}), #{encdump s3})", + lambda { s1.sub(r2) { s3 } } + ], + [ + "#{encdump s1}.gsub(Regexp.new(#{encdump s2}), #{encdump s3})", + lambda { s1.gsub(r2, s3) } + ], + [ + "#{encdump s1}.gsub(Regexp.new(#{encdump s2}), #{encdump s3})", + lambda { s1.gsub(r2) { s3 } } + ] + ].each {|desc, doit| + if !str_enc_compatible?(s1, s2) + assert_raise(ArgumentError, desc) { doit.call } + next + end + if !s1.include?(s2) + assert_equal(s1, doit.call) + next + end + if !str_enc_compatible?(s1, s3) + assert_raise(ArgumentError, desc) { doit.call } + next + end + t = nil + assert_nothing_raised(desc) { + t = doit.call + } + if s2 == s3 + assert_equal(s1, t, desc) + else + assert_not_equal(s1, t, desc) + end + } + } + end + + def test_str_sub! + combination(STRINGS, STRINGS, STRINGS) {|s1, s2, s3| + if !s2.valid_encoding? + assert_raise(RegexpError) { Regexp.new(Regexp.escape(s2)) } + next + end + r2 = Regexp.new(Regexp.escape(s2)) + [ + [ + "t=#{encdump s1}.dup;t.sub!(Regexp.new(#{encdump s2}), #{encdump s3})", + lambda { t=s1.dup; [t, t.sub!(r2, s3)] } + ], + [ + "t=#{encdump s1}.dup;t.sub!(Regexp.new(#{encdump s2}), #{encdump s3})", + lambda { t=s1.dup; [t, t.sub!(r2) { s3 }] } + ], + [ + "t=#{encdump s1}.dup;t.gsub!(Regexp.new(#{encdump s2}), #{encdump s3})", + lambda { t=s1.dup; [t, t.gsub!(r2, s3)] } + ], + [ + "t=#{encdump s1}.dup;t.gsub!(Regexp.new(#{encdump s2}), #{encdump s3})", + lambda { t=s1.dup; [t, t.gsub!(r2) { s3 }] } + ] + ].each {|desc, doit| + if !str_enc_compatible?(s1, s2) + assert_raise(ArgumentError, desc) { doit.call } + next + end + if !s1.include?(s2) + assert_equal([s1, nil], doit.call) + next + end + if !str_enc_compatible?(s1, s3) + assert_raise(ArgumentError, desc) { doit.call } + next + end + t = ret = nil + assert_nothing_raised(desc) { + t, ret = doit.call + } + assert(ret) + if s2 == s3 + assert_equal(s1, t, desc) + else + assert_not_equal(s1, t, desc) + end + } + } + end + end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml