PageRenderTime 46ms CodeModel.GetById 36ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/nicksieger/jruby
Ruby | 71 lines | 61 code | 4 blank | 6 comment | 0 complexity | 079ffcb80727c67572292883895ecb1f MD5 | raw file
 1###############################################################################
 2# tc_gsub.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_bang.rb file.
 6###############################################################################
 7require 'test/unit'
 8
 9class TC_String_Gsub_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_kind_of(String, @basic.gsub(/\w/, '*'))
19   end
20
21   def test_gsub_regex
22      assert_equal('hexxo123', @basic.gsub(/l/, 'x'))
23      assert_equal('h*ll*123', @basic.gsub(/[aeiou]/, '*'))
24      assert_equal('hll123', @basic.gsub(/[aeiou]/, ''))
25      assert_equal('hello123', @basic.gsub(/x/, '*'))
26      assert_equal('hello444', @basic.gsub(/\d/, '4'))
27   end
28
29   def test_gsub_string
30      assert_equal('\foo\bar', @path.gsub('/', '\\'))
31      assert_equal('/foo\car', @path.gsub('/b', '\c'))
32      assert_equal('=blah=bar', @path.gsub('/foo/', '=blah='))
33   end
34
35   def test_gsub_with_backreferences
36      assert_equal('h-e-ll-o-123', @basic.gsub(/([aeiou])/, '-\1-'))
37      assert_equal('hll123', @basic.gsub(/[aeiou]/, '\1'))
38   end
39
40   def test_gsub_with_block
41      assert_equal('hello123xxx', @basic.gsub(/\d+/){ |m| m += 'xxx' })
42      assert_equal('hello123', @basic.gsub(/x/){ |m| m += 'xxx' })
43      assert_equal('hello777', @basic.gsub(/\d/, '7'){ |m| m += 'x' }) #ignored
44   end
45
46   def test_gsub_with_tainted_replacement
47      str = 'x'
48      assert_equal(false, @basic.tainted?)
49      assert_equal(false, @basic.gsub('l', str).tainted?)
50      
51      str.taint
52      assert_equal(true, @basic.gsub!('l', str).tainted?)
53   end
54
55   def test_gsub_edge_cases
56      assert_equal('xhxexlxlxox1x2x3x', @basic.gsub(//, 'x'))
57      assert_equal('worldworld', @basic.gsub(/.*/, 'world'))
58   end
59
60   def test_gsub_expected_errors
61      assert_raise(TypeError){ @basic.gsub(1, 2) }
62      assert_raise(ArgumentError){ @basic.gsub(1, 2, 3) }
63      assert_raise(ArgumentError){ @basic.gsub }
64      assert_raise(ArgumentError){ @basic.gsub{ } }
65   end
66
67   def teardown
68      @basic = nil
69      @path  = nil
70   end
71end