PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/test/externals/ruby_test/test/core/String/instance/tc_gsub_bang.rb

https://bitbucket.org/nicksieger/jruby
Ruby | 99 lines | 72 code | 21 blank | 6 comment | 0 complexity | 80903f1b6d85304efc0f2d299c4328f9 MD5 | raw file
 1###############################################################################
 2# tc_gsub_bang.rb
 3#
 4# Test case for the String#gsub! instance methods. The test case for the
 5# String#gsub instance method can be found in the tc_gsub.rb file.
 6###############################################################################
 7require 'test/unit'
 8
 9class TC_String_GsubBang_InstanceMethod < Test::Unit::TestCase
10   def setup
11      @basic = 'hello123'
12      @path  = '/foo/bar'
13   end
14
15   def test_gsub_basic
16      assert_respond_to(@basic, :gsub!)
17      assert_nothing_raised{ @basic.gsub!(/\w/, '*') }
18      assert_equal(nil, @basic.gsub!(/\w/, '*'))
19   end
20
21   def test_gsub_regex
22      assert_equal('hexxo123', @basic.gsub!(/l/, 'x'))
23      assert_equal('hexxo123', @basic)
24
25      assert_equal('h*xx*123', @basic.gsub!(/[aeiou]/, '*'))
26      assert_equal('h*xx*123', @basic)
27
28      assert_equal(nil, @basic.gsub!(/[pqrs]/, ''))
29      assert_equal('h*xx*123', @basic)
30
31      assert_nil(@basic.gsub!(/y/, '*'))
32      assert_equal('h*xx*123', @basic)
33
34      assert_equal('h*xx*453', @basic.gsub!(/\d\d/, '45'))
35      assert_equal('h*xx*453', @basic)
36   end
37
38   def test_gsub_string
39      assert_equal('/poo/bar', @path.gsub!('f', 'p'))
40      assert_equal('/poo/bar', @path)
41
42      assert_equal('/poo\car', @path.gsub!('/b', '\c'))
43      assert_equal('/poo\car', @path)
44
45      assert_equal('=blah=car', @path.gsub!("/poo\\", '=blah='))
46      assert_equal('=blah=car', @path)
47
48      assert_nil(@path.gsub!("xxx", "yyy"))
49      assert_equal('=blah=car', @path)
50   end
51
52   def test_gsub_with_backreferences
53      assert_equal('h-e-ll-o-123', @basic.gsub!(/([aeiou])/, '-\1-'))
54      assert_equal('h-e-ll-o-123', @basic)
55
56      assert_equal('h--ll--123', @basic.gsub!(/[aeiou]/, '\1'))
57      assert_equal('h--ll--123', @basic)
58   end
59
60   def test_gsub_with_block
61      assert_equal('hello123xxx', @basic.gsub!(/\d+/){ |m| m += 'xxx' })
62      assert_equal('hello123xxx', @basic)
63
64      assert_nil(@basic.gsub!(/y/){ |m| m += 'xxx' })
65      assert_equal('hello123xxx', @basic)
66
67      assert_equal('hello777xxx', @basic.gsub!(/\d/, '7'){ |m| m += 'xxx' })
68      assert_equal('hello777xxx', @basic)
69   end
70
71   def test_gsub_with_tainted_replacement
72      str = 'x'
73      assert_equal(false, @basic.tainted?)
74      assert_nothing_raised{ @basic.gsub!('l', str) }
75      assert_equal(false, @basic.tainted?)
76      
77      @basic = 'hello123' # reset
78      str.taint
79      assert_nothing_raised{ @basic.gsub!('l', str) }
80      assert_equal(true, @basic.tainted?)
81   end
82
83   def test_gsub_edge_cases
84      assert_equal('xhxexlxlxox1x2x3x', @basic.gsub!(//, 'x'))
85      assert_equal('worldworld', @basic.gsub!(/.*/, 'world'))
86   end
87
88   def test_gsub_expected_errors
89      assert_raise(TypeError){ @basic.gsub!(1, 2) }
90      assert_raise(ArgumentError){ @basic.gsub!(1, 2, 3) }
91      assert_raise(ArgumentError){ @basic.gsub! }
92      assert_raise(ArgumentError){ @basic.gsub!{ } }
93   end
94
95   def teardown
96      @basic = nil
97      @path  = nil
98   end
99end