PageRenderTime 211ms CodeModel.GetById 29ms 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
Possible License(s): GPL-3.0, JSON
  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. ###############################################################################
  7. require 'test/unit'
  8. class TC_String_Gsub_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_kind_of(String, @basic.gsub(/\w/, '*'))
  17. end
  18. def test_gsub_regex
  19. assert_equal('hexxo123', @basic.gsub(/l/, 'x'))
  20. assert_equal('h*ll*123', @basic.gsub(/[aeiou]/, '*'))
  21. assert_equal('hll123', @basic.gsub(/[aeiou]/, ''))
  22. assert_equal('hello123', @basic.gsub(/x/, '*'))
  23. assert_equal('hello444', @basic.gsub(/\d/, '4'))
  24. end
  25. def test_gsub_string
  26. assert_equal('\foo\bar', @path.gsub('/', '\\'))
  27. assert_equal('/foo\car', @path.gsub('/b', '\c'))
  28. assert_equal('=blah=bar', @path.gsub('/foo/', '=blah='))
  29. end
  30. def test_gsub_with_backreferences
  31. assert_equal('h-e-ll-o-123', @basic.gsub(/([aeiou])/, '-\1-'))
  32. assert_equal('hll123', @basic.gsub(/[aeiou]/, '\1'))
  33. end
  34. def test_gsub_with_block
  35. assert_equal('hello123xxx', @basic.gsub(/\d+/){ |m| m += 'xxx' })
  36. assert_equal('hello123', @basic.gsub(/x/){ |m| m += 'xxx' })
  37. assert_equal('hello777', @basic.gsub(/\d/, '7'){ |m| m += 'x' }) #ignored
  38. end
  39. def test_gsub_with_tainted_replacement
  40. str = 'x'
  41. assert_equal(false, @basic.tainted?)
  42. assert_equal(false, @basic.gsub('l', str).tainted?)
  43. str.taint
  44. assert_equal(true, @basic.gsub!('l', str).tainted?)
  45. end
  46. def test_gsub_edge_cases
  47. assert_equal('xhxexlxlxox1x2x3x', @basic.gsub(//, 'x'))
  48. assert_equal('worldworld', @basic.gsub(/.*/, 'world'))
  49. end
  50. def test_gsub_expected_errors
  51. assert_raise(TypeError){ @basic.gsub(1, 2) }
  52. assert_raise(ArgumentError){ @basic.gsub(1, 2, 3) }
  53. assert_raise(ArgumentError){ @basic.gsub }
  54. assert_raise(ArgumentError){ @basic.gsub{ } }
  55. end
  56. def teardown
  57. @basic = nil
  58. @path = nil
  59. end
  60. end