PageRenderTime 50ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/rails/activesupport/test/multibyte_conformance.rb

https://github.com/bricooke/my-biz-expenses
Ruby | 141 lines | 107 code | 15 blank | 19 comment | 4 complexity | dbaedd1c38b223b05a9aae7b68dc7392 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, BSD-3-Clause
  1. require File.dirname(__FILE__) + '/abstract_unit'
  2. require 'open-uri'
  3. $KCODE = 'UTF8'
  4. UNIDATA_URL = "http://www.unicode.org/Public/#{ActiveSupport::Multibyte::UNICODE_VERSION}/ucd"
  5. UNIDATA_FILE = '/NormalizationTest.txt'
  6. CACHE_DIR = File.dirname(__FILE__) + '/cache'
  7. class Downloader
  8. def self.download(from, to)
  9. unless File.exist?(to)
  10. $stderr.puts "Downloading #{from} to #{to}"
  11. unless File.exists?(File.dirname(to))
  12. system "mkdir -p #{File.dirname(to)}"
  13. end
  14. open(from) do |source|
  15. File.open(to, 'w') do |target|
  16. source.each_line do |l|
  17. target.write l
  18. end
  19. end
  20. end
  21. end
  22. end
  23. end
  24. class String
  25. # Unicode Inspect returns the codepoints of the string in hex
  26. def ui
  27. "#{self} " + ("[%s]" % unpack("U*").map{|cp| cp.to_s(16) }.join(' '))
  28. end unless ''.respond_to?(:ui)
  29. end
  30. Dir.mkdir(CACHE_DIR) unless File.exists?(CACHE_DIR)
  31. Downloader.download(UNIDATA_URL + UNIDATA_FILE, CACHE_DIR + UNIDATA_FILE)
  32. module ConformanceTest
  33. def test_normalizations_C
  34. each_line_of_norm_tests do |*cols|
  35. col1, col2, col3, col4, col5, comment = *cols
  36. # CONFORMANCE:
  37. # 1. The following invariants must be true for all conformant implementations
  38. #
  39. # NFC
  40. # c2 == NFC(c1) == NFC(c2) == NFC(c3)
  41. assert_equal col2.ui, @handler.normalize(col1, :c).ui, "Form C - Col 2 has to be NFC(1) - #{comment}"
  42. assert_equal col2.ui, @handler.normalize(col2, :c).ui, "Form C - Col 2 has to be NFC(2) - #{comment}"
  43. assert_equal col2.ui, @handler.normalize(col3, :c).ui, "Form C - Col 2 has to be NFC(3) - #{comment}"
  44. #
  45. # c4 == NFC(c4) == NFC(c5)
  46. assert_equal col4.ui, @handler.normalize(col4, :c).ui, "Form C - Col 4 has to be C(4) - #{comment}"
  47. assert_equal col4.ui, @handler.normalize(col5, :c).ui, "Form C - Col 4 has to be C(5) - #{comment}"
  48. end
  49. end
  50. def test_normalizations_D
  51. each_line_of_norm_tests do |*cols|
  52. col1, col2, col3, col4, col5, comment = *cols
  53. #
  54. # NFD
  55. # c3 == NFD(c1) == NFD(c2) == NFD(c3)
  56. assert_equal col3.ui, @handler.normalize(col1, :d).ui, "Form D - Col 3 has to be NFD(1) - #{comment}"
  57. assert_equal col3.ui, @handler.normalize(col2, :d).ui, "Form D - Col 3 has to be NFD(2) - #{comment}"
  58. assert_equal col3.ui, @handler.normalize(col3, :d).ui, "Form D - Col 3 has to be NFD(3) - #{comment}"
  59. # c5 == NFD(c4) == NFD(c5)
  60. assert_equal col5.ui, @handler.normalize(col4, :d).ui, "Form D - Col 5 has to be NFD(4) - #{comment}"
  61. assert_equal col5.ui, @handler.normalize(col5, :d).ui, "Form D - Col 5 has to be NFD(5) - #{comment}"
  62. end
  63. end
  64. def test_normalizations_KC
  65. each_line_of_norm_tests do | *cols |
  66. col1, col2, col3, col4, col5, comment = *cols
  67. #
  68. # NFKC
  69. # c4 == NFKC(c1) == NFKC(c2) == NFKC(c3) == NFKC(c4) == NFKC(c5)
  70. assert_equal col4.ui, @handler.normalize(col1, :kc).ui, "Form D - Col 4 has to be NFKC(1) - #{comment}"
  71. assert_equal col4.ui, @handler.normalize(col2, :kc).ui, "Form D - Col 4 has to be NFKC(2) - #{comment}"
  72. assert_equal col4.ui, @handler.normalize(col3, :kc).ui, "Form D - Col 4 has to be NFKC(3) - #{comment}"
  73. assert_equal col4.ui, @handler.normalize(col4, :kc).ui, "Form D - Col 4 has to be NFKC(4) - #{comment}"
  74. assert_equal col4.ui, @handler.normalize(col5, :kc).ui, "Form D - Col 4 has to be NFKC(5) - #{comment}"
  75. end
  76. end
  77. def test_normalizations_KD
  78. each_line_of_norm_tests do | *cols |
  79. col1, col2, col3, col4, col5, comment = *cols
  80. #
  81. # NFKD
  82. # c5 == NFKD(c1) == NFKD(c2) == NFKD(c3) == NFKD(c4) == NFKD(c5)
  83. assert_equal col5.ui, @handler.normalize(col1, :kd).ui, "Form KD - Col 5 has to be NFKD(1) - #{comment}"
  84. assert_equal col5.ui, @handler.normalize(col2, :kd).ui, "Form KD - Col 5 has to be NFKD(2) - #{comment}"
  85. assert_equal col5.ui, @handler.normalize(col3, :kd).ui, "Form KD - Col 5 has to be NFKD(3) - #{comment}"
  86. assert_equal col5.ui, @handler.normalize(col4, :kd).ui, "Form KD - Col 5 has to be NFKD(4) - #{comment}"
  87. assert_equal col5.ui, @handler.normalize(col5, :kd).ui, "Form KD - Col 5 has to be NFKD(5) - #{comment}"
  88. end
  89. end
  90. protected
  91. def each_line_of_norm_tests(&block)
  92. lines = 0
  93. max_test_lines = 0 # Don't limit below 38, because that's the header of the testfile
  94. File.open(File.dirname(__FILE__) + '/cache' + UNIDATA_FILE, 'r') do | f |
  95. until f.eof? || (max_test_lines > 38 and lines > max_test_lines)
  96. lines += 1
  97. line = f.gets.chomp!
  98. next if (line.empty? || line =~ /^\#/)
  99. cols, comment = line.split("#")
  100. cols = cols.split(";").map{|e| e.strip}.reject{|e| e.empty? }
  101. next unless cols.length == 5
  102. # codepoints are in hex in the test suite, pack wants them as integers
  103. cols.map!{|c| c.split.map{|codepoint| codepoint.to_i(16)}.pack("U*") }
  104. cols << comment
  105. yield(*cols)
  106. end
  107. end
  108. end
  109. end
  110. begin
  111. require_library_or_gem('utf8proc_native')
  112. require 'active_record/multibyte/handlers/utf8_handler_proc'
  113. class ConformanceTestProc < Test::Unit::TestCase
  114. include ConformanceTest
  115. def setup
  116. @handler = ::ActiveSupport::Multibyte::Handlers::UTF8HandlerProc
  117. end
  118. end
  119. rescue LoadError
  120. end
  121. class ConformanceTestPure < Test::Unit::TestCase
  122. include ConformanceTest
  123. def setup
  124. @handler = ::ActiveSupport::Multibyte::Handlers::UTF8Handler
  125. end
  126. end