test_xchar.rb 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/env ruby
  2. #--
  3. # Portions copyright 2004 by Jim Weirich (jim@weirichhouse.org).
  4. # Portions copyright 2005 by Sam Ruby (rubys@intertwingly.net).
  5. # All rights reserved.
  6. # Permission is granted for use, copying, modification, distribution,
  7. # and distribution of modified versions of this work as long as the
  8. # above copyright notice is included.
  9. #++
  10. #!/usr/bin/env ruby
  11. require 'test/unit'
  12. require 'builder/xchar'
  13. if String.method_defined?(:encode)
  14. class String
  15. ENCODING_BINARY = Encoding.find('BINARY')
  16. # shim method for testing purposes
  17. def to_xs(escape=true)
  18. raise NameError.new('to_xs') unless caller[0].index(__FILE__)
  19. result = Builder::XChar.encode(self)
  20. if escape
  21. result.gsub(/[^\u0000-\u007F]/) {|c| "&##{c.ord};"}
  22. else
  23. # really only useful for testing purposes
  24. result.force_encoding(ENCODING_BINARY)
  25. end
  26. end
  27. end
  28. end
  29. class TestXmlEscaping < Test::Unit::TestCase
  30. REPLACEMENT_CHAR = Builder::XChar::REPLACEMENT_CHAR.to_xs
  31. def test_ascii
  32. assert_equal 'abc', 'abc'.to_xs
  33. end
  34. def test_predefined
  35. assert_equal '&amp;', '&'.to_xs # ampersand
  36. assert_equal '&lt;', '<'.to_xs # left angle bracket
  37. assert_equal '&gt;', '>'.to_xs # right angle bracket
  38. end
  39. def test_invalid
  40. assert_equal REPLACEMENT_CHAR, "\x00".to_xs # null
  41. assert_equal REPLACEMENT_CHAR, "\x0C".to_xs # form feed
  42. assert_equal REPLACEMENT_CHAR, "\xEF\xBF\xBF".to_xs # U+FFFF
  43. end
  44. def test_iso_8859_1
  45. assert_equal '&#231;', "\xE7".to_xs # small c cedilla
  46. assert_equal '&#169;', "\xA9".to_xs # copyright symbol
  47. end
  48. def test_win_1252
  49. assert_equal '&#8217;', "\x92".to_xs # smart quote
  50. assert_equal '&#8364;', "\x80".to_xs # euro
  51. end
  52. def test_utf8
  53. assert_equal '&#8217;', "\xE2\x80\x99".to_xs # right single quote
  54. assert_equal '&#169;', "\xC2\xA9".to_xs # copy
  55. end
  56. def test_utf8_verbatim
  57. assert_equal "\xE2\x80\x99", "\xE2\x80\x99".to_xs(false) # right single quote
  58. assert_equal "\xC2\xA9", "\xC2\xA9".to_xs(false) # copy
  59. assert_equal "\xC2\xA9&amp;\xC2\xA9",
  60. "\xC2\xA9&\xC2\xA9".to_xs(false) # copy with ampersand
  61. end
  62. end