2007-12-21 06:21:20 -05:00
|
|
|
require 'abstract_unit'
|
2008-09-21 11:37:38 -04:00
|
|
|
require 'multibyte_test_helpers'
|
|
|
|
|
2008-09-21 11:21:30 -04:00
|
|
|
require 'fileutils'
|
2006-10-03 19:45:32 -04:00
|
|
|
require 'open-uri'
|
2008-09-21 11:21:30 -04:00
|
|
|
require 'tmpdir'
|
2006-10-03 19:45:32 -04:00
|
|
|
|
|
|
|
class Downloader
|
|
|
|
def self.download(from, to)
|
|
|
|
unless File.exist?(to)
|
2007-12-10 00:54:46 -05:00
|
|
|
unless File.exist?(File.dirname(to))
|
2006-10-03 19:45:32 -04:00
|
|
|
system "mkdir -p #{File.dirname(to)}"
|
|
|
|
end
|
|
|
|
open(from) do |source|
|
|
|
|
File.open(to, 'w') do |target|
|
|
|
|
source.each_line do |l|
|
|
|
|
target.write l
|
|
|
|
end
|
|
|
|
end
|
2014-04-25 18:35:22 -04:00
|
|
|
end
|
|
|
|
end
|
2014-07-17 18:36:42 -04:00
|
|
|
true
|
2006-10-03 19:45:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-05 20:12:46 -05:00
|
|
|
class MultibyteConformanceTest < ActiveSupport::TestCase
|
2008-09-21 11:37:38 -04:00
|
|
|
include MultibyteTestHelpers
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-05-10 09:46:37 -04:00
|
|
|
UNIDATA_URL = "http://www.unicode.org/Public/#{ActiveSupport::Multibyte::Unicode::UNICODE_VERSION}/ucd"
|
2008-09-21 11:21:30 -04:00
|
|
|
UNIDATA_FILE = '/NormalizationTest.txt'
|
|
|
|
CACHE_DIR = File.join(Dir.tmpdir, 'cache')
|
2014-07-17 18:36:42 -04:00
|
|
|
FileUtils.mkdir_p(CACHE_DIR)
|
|
|
|
RUN_P = begin
|
|
|
|
Downloader.download(UNIDATA_URL + UNIDATA_FILE, CACHE_DIR + UNIDATA_FILE)
|
|
|
|
rescue
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2008-09-21 11:21:30 -04:00
|
|
|
def setup
|
|
|
|
@proxy = ActiveSupport::Multibyte::Chars
|
2014-07-17 18:36:42 -04:00
|
|
|
skip "Unable to download test data" unless RUN_P
|
2008-09-21 11:21:30 -04:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2006-10-03 19:45:32 -04:00
|
|
|
def test_normalizations_C
|
|
|
|
each_line_of_norm_tests do |*cols|
|
|
|
|
col1, col2, col3, col4, col5, comment = *cols
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2006-10-03 19:45:32 -04:00
|
|
|
# CONFORMANCE:
|
|
|
|
# 1. The following invariants must be true for all conformant implementations
|
|
|
|
#
|
|
|
|
# NFC
|
|
|
|
# c2 == NFC(c1) == NFC(c2) == NFC(c3)
|
2008-09-21 11:21:30 -04:00
|
|
|
assert_equal_codepoints col2, @proxy.new(col1).normalize(:c), "Form C - Col 2 has to be NFC(1) - #{comment}"
|
|
|
|
assert_equal_codepoints col2, @proxy.new(col2).normalize(:c), "Form C - Col 2 has to be NFC(2) - #{comment}"
|
|
|
|
assert_equal_codepoints col2, @proxy.new(col3).normalize(:c), "Form C - Col 2 has to be NFC(3) - #{comment}"
|
2006-10-03 19:45:32 -04:00
|
|
|
#
|
|
|
|
# c4 == NFC(c4) == NFC(c5)
|
2008-09-21 11:21:30 -04:00
|
|
|
assert_equal_codepoints col4, @proxy.new(col4).normalize(:c), "Form C - Col 4 has to be C(4) - #{comment}"
|
|
|
|
assert_equal_codepoints col4, @proxy.new(col5).normalize(:c), "Form C - Col 4 has to be C(5) - #{comment}"
|
2006-10-03 19:45:32 -04:00
|
|
|
end
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2006-10-03 19:45:32 -04:00
|
|
|
def test_normalizations_D
|
|
|
|
each_line_of_norm_tests do |*cols|
|
|
|
|
col1, col2, col3, col4, col5, comment = *cols
|
|
|
|
#
|
|
|
|
# NFD
|
|
|
|
# c3 == NFD(c1) == NFD(c2) == NFD(c3)
|
2008-09-21 11:21:30 -04:00
|
|
|
assert_equal_codepoints col3, @proxy.new(col1).normalize(:d), "Form D - Col 3 has to be NFD(1) - #{comment}"
|
|
|
|
assert_equal_codepoints col3, @proxy.new(col2).normalize(:d), "Form D - Col 3 has to be NFD(2) - #{comment}"
|
|
|
|
assert_equal_codepoints col3, @proxy.new(col3).normalize(:d), "Form D - Col 3 has to be NFD(3) - #{comment}"
|
2006-10-03 19:45:32 -04:00
|
|
|
# c5 == NFD(c4) == NFD(c5)
|
2008-09-21 11:21:30 -04:00
|
|
|
assert_equal_codepoints col5, @proxy.new(col4).normalize(:d), "Form D - Col 5 has to be NFD(4) - #{comment}"
|
|
|
|
assert_equal_codepoints col5, @proxy.new(col5).normalize(:d), "Form D - Col 5 has to be NFD(5) - #{comment}"
|
2006-10-03 19:45:32 -04:00
|
|
|
end
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2006-10-03 19:45:32 -04:00
|
|
|
def test_normalizations_KC
|
|
|
|
each_line_of_norm_tests do | *cols |
|
2010-08-14 01:13:00 -04:00
|
|
|
col1, col2, col3, col4, col5, comment = *cols
|
2006-10-03 19:45:32 -04:00
|
|
|
#
|
|
|
|
# NFKC
|
|
|
|
# c4 == NFKC(c1) == NFKC(c2) == NFKC(c3) == NFKC(c4) == NFKC(c5)
|
2008-09-21 11:21:30 -04:00
|
|
|
assert_equal_codepoints col4, @proxy.new(col1).normalize(:kc), "Form D - Col 4 has to be NFKC(1) - #{comment}"
|
|
|
|
assert_equal_codepoints col4, @proxy.new(col2).normalize(:kc), "Form D - Col 4 has to be NFKC(2) - #{comment}"
|
|
|
|
assert_equal_codepoints col4, @proxy.new(col3).normalize(:kc), "Form D - Col 4 has to be NFKC(3) - #{comment}"
|
|
|
|
assert_equal_codepoints col4, @proxy.new(col4).normalize(:kc), "Form D - Col 4 has to be NFKC(4) - #{comment}"
|
|
|
|
assert_equal_codepoints col4, @proxy.new(col5).normalize(:kc), "Form D - Col 4 has to be NFKC(5) - #{comment}"
|
2006-10-03 19:45:32 -04:00
|
|
|
end
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2006-10-03 19:45:32 -04:00
|
|
|
def test_normalizations_KD
|
|
|
|
each_line_of_norm_tests do | *cols |
|
2010-08-14 01:13:00 -04:00
|
|
|
col1, col2, col3, col4, col5, comment = *cols
|
2006-10-03 19:45:32 -04:00
|
|
|
#
|
|
|
|
# NFKD
|
|
|
|
# c5 == NFKD(c1) == NFKD(c2) == NFKD(c3) == NFKD(c4) == NFKD(c5)
|
2008-09-21 11:21:30 -04:00
|
|
|
assert_equal_codepoints col5, @proxy.new(col1).normalize(:kd), "Form KD - Col 5 has to be NFKD(1) - #{comment}"
|
|
|
|
assert_equal_codepoints col5, @proxy.new(col2).normalize(:kd), "Form KD - Col 5 has to be NFKD(2) - #{comment}"
|
|
|
|
assert_equal_codepoints col5, @proxy.new(col3).normalize(:kd), "Form KD - Col 5 has to be NFKD(3) - #{comment}"
|
|
|
|
assert_equal_codepoints col5, @proxy.new(col4).normalize(:kd), "Form KD - Col 5 has to be NFKD(4) - #{comment}"
|
|
|
|
assert_equal_codepoints col5, @proxy.new(col5).normalize(:kd), "Form KD - Col 5 has to be NFKD(5) - #{comment}"
|
2006-10-03 19:45:32 -04:00
|
|
|
end
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2006-10-03 19:45:32 -04:00
|
|
|
protected
|
|
|
|
def each_line_of_norm_tests(&block)
|
|
|
|
lines = 0
|
|
|
|
max_test_lines = 0 # Don't limit below 38, because that's the header of the testfile
|
2008-09-21 11:21:30 -04:00
|
|
|
File.open(File.join(CACHE_DIR, UNIDATA_FILE), 'r') do | f |
|
2006-10-03 19:45:32 -04:00
|
|
|
until f.eof? || (max_test_lines > 38 and lines > max_test_lines)
|
|
|
|
lines += 1
|
|
|
|
line = f.gets.chomp!
|
2010-08-14 01:13:00 -04:00
|
|
|
next if (line.empty? || line =~ /^\#/)
|
|
|
|
|
2006-10-03 19:45:32 -04:00
|
|
|
cols, comment = line.split("#")
|
2014-10-27 12:28:53 -04:00
|
|
|
cols = cols.split(";").map(&:strip).reject(&:empty?)
|
2006-10-03 19:45:32 -04:00
|
|
|
next unless cols.length == 5
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2006-10-03 19:45:32 -04:00
|
|
|
# codepoints are in hex in the test suite, pack wants them as integers
|
|
|
|
cols.map!{|c| c.split.map{|codepoint| codepoint.to_i(16)}.pack("U*") }
|
|
|
|
cols << comment
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2006-10-03 19:45:32 -04:00
|
|
|
yield(*cols)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2008-09-21 11:21:30 -04:00
|
|
|
def inspect_codepoints(str)
|
|
|
|
str.to_s.unpack("U*").map{|cp| cp.to_s(16) }.join(' ')
|
2006-10-03 19:45:32 -04:00
|
|
|
end
|
2014-07-04 14:26:19 -04:00
|
|
|
end
|