PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms 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
Possible License(s): GPL-3.0, JSON
  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. ###############################################################################
  7. require 'test/unit'
  8. class TC_String_GsubBang_InstanceMethod < Test::Unit::TestCase
  9. def setup
  10. @basic = 'hello123'
  11. @path = '/foo/bar'
  12. end
  13. def test_gsub_basic
  14. assert_respond_to(@basic, :gsub!)
  15. assert_nothing_raised{ @basic.gsub!(/\w/, '*') }
  16. assert_equal(nil, @basic.gsub!(/\w/, '*'))
  17. end
  18. def test_gsub_regex
  19. assert_equal('hexxo123', @basic.gsub!(/l/, 'x'))
  20. assert_equal('hexxo123', @basic)
  21. assert_equal('h*xx*123', @basic.gsub!(/[aeiou]/, '*'))
  22. assert_equal('h*xx*123', @basic)
  23. assert_equal(nil, @basic.gsub!(/[pqrs]/, ''))
  24. assert_equal('h*xx*123', @basic)
  25. assert_nil(@basic.gsub!(/y/, '*'))
  26. assert_equal('h*xx*123', @basic)
  27. assert_equal('h*xx*453', @basic.gsub!(/\d\d/, '45'))
  28. assert_equal('h*xx*453', @basic)
  29. end
  30. def test_gsub_string
  31. assert_equal('/poo/bar', @path.gsub!('f', 'p'))
  32. assert_equal('/poo/bar', @path)
  33. assert_equal('/poo\car', @path.gsub!('/b', '\c'))
  34. assert_equal('/poo\car', @path)
  35. assert_equal('=blah=car', @path.gsub!("/poo\\", '=blah='))
  36. assert_equal('=blah=car', @path)
  37. assert_nil(@path.gsub!("xxx", "yyy"))
  38. assert_equal('=blah=car', @path)
  39. end
  40. def test_gsub_with_backreferences
  41. assert_equal('h-e-ll-o-123', @basic.gsub!(/([aeiou])/, '-\1-'))
  42. assert_equal('h-e-ll-o-123', @basic)
  43. assert_equal('h--ll--123', @basic.gsub!(/[aeiou]/, '\1'))
  44. assert_equal('h--ll--123', @basic)
  45. end
  46. def test_gsub_with_block
  47. assert_equal('hello123xxx', @basic.gsub!(/\d+/){ |m| m += 'xxx' })
  48. assert_equal('hello123xxx', @basic)
  49. assert_nil(@basic.gsub!(/y/){ |m| m += 'xxx' })
  50. assert_equal('hello123xxx', @basic)
  51. assert_equal('hello777xxx', @basic.gsub!(/\d/, '7'){ |m| m += 'xxx' })
  52. assert_equal('hello777xxx', @basic)
  53. end
  54. def test_gsub_with_tainted_replacement
  55. str = 'x'
  56. assert_equal(false, @basic.tainted?)
  57. assert_nothing_raised{ @basic.gsub!('l', str) }
  58. assert_equal(false, @basic.tainted?)
  59. @basic = 'hello123' # reset
  60. str.taint
  61. assert_nothing_raised{ @basic.gsub!('l', str) }
  62. assert_equal(true, @basic.tainted?)
  63. end
  64. def test_gsub_edge_cases
  65. assert_equal('xhxexlxlxox1x2x3x', @basic.gsub!(//, 'x'))
  66. assert_equal('worldworld', @basic.gsub!(/.*/, 'world'))
  67. end
  68. def test_gsub_expected_errors
  69. assert_raise(TypeError){ @basic.gsub!(1, 2) }
  70. assert_raise(ArgumentError){ @basic.gsub!(1, 2, 3) }
  71. assert_raise(ArgumentError){ @basic.gsub! }
  72. assert_raise(ArgumentError){ @basic.gsub!{ } }
  73. end
  74. def teardown
  75. @basic = nil
  76. @path = nil
  77. end
  78. end