| 1 |
require 'abstract_unit' |
|---|
| 2 |
require 'open-uri' |
|---|
| 3 |
|
|---|
| 4 |
if RUBY_VERSION < '1.9' |
|---|
| 5 |
|
|---|
| 6 |
$KCODE = 'UTF8' |
|---|
| 7 |
|
|---|
| 8 |
UNIDATA_URL = "http://www.unicode.org/Public/#{ActiveSupport::Multibyte::UNICODE_VERSION}/ucd" |
|---|
| 9 |
UNIDATA_FILE = '/NormalizationTest.txt' |
|---|
| 10 |
CACHE_DIR = File.dirname(__FILE__) + '/cache' |
|---|
| 11 |
|
|---|
| 12 |
class Downloader |
|---|
| 13 |
def self.download(from, to) |
|---|
| 14 |
unless File.exist?(to) |
|---|
| 15 |
$stderr.puts "Downloading #{from} to #{to}" |
|---|
| 16 |
unless File.exist?(File.dirname(to)) |
|---|
| 17 |
system "mkdir -p #{File.dirname(to)}" |
|---|
| 18 |
end |
|---|
| 19 |
open(from) do |source| |
|---|
| 20 |
File.open(to, 'w') do |target| |
|---|
| 21 |
source.each_line do |l| |
|---|
| 22 |
target.write l |
|---|
| 23 |
end |
|---|
| 24 |
end |
|---|
| 25 |
end |
|---|
| 26 |
end |
|---|
| 27 |
end |
|---|
| 28 |
end |
|---|
| 29 |
|
|---|
| 30 |
class String |
|---|
| 31 |
|
|---|
| 32 |
def ui |
|---|
| 33 |
"#{self} " + ("[%s]" % unpack("U*").map{|cp| cp.to_s(16) }.join(' ')) |
|---|
| 34 |
end unless ''.respond_to?(:ui) |
|---|
| 35 |
end |
|---|
| 36 |
|
|---|
| 37 |
Dir.mkdir(CACHE_DIR) unless File.exist?(CACHE_DIR) |
|---|
| 38 |
Downloader.download(UNIDATA_URL + UNIDATA_FILE, CACHE_DIR + UNIDATA_FILE) |
|---|
| 39 |
|
|---|
| 40 |
module ConformanceTest |
|---|
| 41 |
def test_normalizations_C |
|---|
| 42 |
each_line_of_norm_tests do |*cols| |
|---|
| 43 |
col1, col2, col3, col4, col5, comment = *cols |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
assert_equal col2.ui, @handler.normalize(col1, :c).ui, "Form C - Col 2 has to be NFC(1) - #{comment}" |
|---|
| 51 |
assert_equal col2.ui, @handler.normalize(col2, :c).ui, "Form C - Col 2 has to be NFC(2) - #{comment}" |
|---|
| 52 |
assert_equal col2.ui, @handler.normalize(col3, :c).ui, "Form C - Col 2 has to be NFC(3) - #{comment}" |
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
assert_equal col4.ui, @handler.normalize(col4, :c).ui, "Form C - Col 4 has to be C(4) - #{comment}" |
|---|
| 56 |
assert_equal col4.ui, @handler.normalize(col5, :c).ui, "Form C - Col 4 has to be C(5) - #{comment}" |
|---|
| 57 |
end |
|---|
| 58 |
end |
|---|
| 59 |
|
|---|
| 60 |
def test_normalizations_D |
|---|
| 61 |
each_line_of_norm_tests do |*cols| |
|---|
| 62 |
col1, col2, col3, col4, col5, comment = *cols |
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
assert_equal col3.ui, @handler.normalize(col1, :d).ui, "Form D - Col 3 has to be NFD(1) - #{comment}" |
|---|
| 67 |
assert_equal col3.ui, @handler.normalize(col2, :d).ui, "Form D - Col 3 has to be NFD(2) - #{comment}" |
|---|
| 68 |
assert_equal col3.ui, @handler.normalize(col3, :d).ui, "Form D - Col 3 has to be NFD(3) - #{comment}" |
|---|
| 69 |
|
|---|
| 70 |
assert_equal col5.ui, @handler.normalize(col4, :d).ui, "Form D - Col 5 has to be NFD(4) - #{comment}" |
|---|
| 71 |
assert_equal col5.ui, @handler.normalize(col5, :d).ui, "Form D - Col 5 has to be NFD(5) - #{comment}" |
|---|
| 72 |
end |
|---|
| 73 |
end |
|---|
| 74 |
|
|---|
| 75 |
def test_normalizations_KC |
|---|
| 76 |
each_line_of_norm_tests do | *cols | |
|---|
| 77 |
col1, col2, col3, col4, col5, comment = *cols |
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
assert_equal col4.ui, @handler.normalize(col1, :kc).ui, "Form D - Col 4 has to be NFKC(1) - #{comment}" |
|---|
| 82 |
assert_equal col4.ui, @handler.normalize(col2, :kc).ui, "Form D - Col 4 has to be NFKC(2) - #{comment}" |
|---|
| 83 |
assert_equal col4.ui, @handler.normalize(col3, :kc).ui, "Form D - Col 4 has to be NFKC(3) - #{comment}" |
|---|
| 84 |
assert_equal col4.ui, @handler.normalize(col4, :kc).ui, "Form D - Col 4 has to be NFKC(4) - #{comment}" |
|---|
| 85 |
assert_equal col4.ui, @handler.normalize(col5, :kc).ui, "Form D - Col 4 has to be NFKC(5) - #{comment}" |
|---|
| 86 |
end |
|---|
| 87 |
end |
|---|
| 88 |
|
|---|
| 89 |
def test_normalizations_KD |
|---|
| 90 |
each_line_of_norm_tests do | *cols | |
|---|
| 91 |
col1, col2, col3, col4, col5, comment = *cols |
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
assert_equal col5.ui, @handler.normalize(col1, :kd).ui, "Form KD - Col 5 has to be NFKD(1) - #{comment}" |
|---|
| 96 |
assert_equal col5.ui, @handler.normalize(col2, :kd).ui, "Form KD - Col 5 has to be NFKD(2) - #{comment}" |
|---|
| 97 |
assert_equal col5.ui, @handler.normalize(col3, :kd).ui, "Form KD - Col 5 has to be NFKD(3) - #{comment}" |
|---|
| 98 |
assert_equal col5.ui, @handler.normalize(col4, :kd).ui, "Form KD - Col 5 has to be NFKD(4) - #{comment}" |
|---|
| 99 |
assert_equal col5.ui, @handler.normalize(col5, :kd).ui, "Form KD - Col 5 has to be NFKD(5) - #{comment}" |
|---|
| 100 |
end |
|---|
| 101 |
end |
|---|
| 102 |
|
|---|
| 103 |
protected |
|---|
| 104 |
def each_line_of_norm_tests(&block) |
|---|
| 105 |
lines = 0 |
|---|
| 106 |
max_test_lines = 0 |
|---|
| 107 |
File.open(File.dirname(__FILE__) + '/cache' + UNIDATA_FILE, 'r') do | f | |
|---|
| 108 |
until f.eof? || (max_test_lines > 38 and lines > max_test_lines) |
|---|
| 109 |
lines += 1 |
|---|
| 110 |
line = f.gets.chomp! |
|---|
| 111 |
next if (line.empty? || line =~ /^\ |
|---|
| 112 |
|
|---|
| 113 |
cols, comment = line.split("#") |
|---|
| 114 |
cols = cols.split(";").map{|e| e.strip}.reject{|e| e.empty? } |
|---|
| 115 |
next unless cols.length == 5 |
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
cols.map!{|c| c.split.map{|codepoint| codepoint.to_i(16)}.pack("U*") } |
|---|
| 119 |
cols << comment |
|---|
| 120 |
|
|---|
| 121 |
yield(*cols) |
|---|
| 122 |
end |
|---|
| 123 |
end |
|---|
| 124 |
end |
|---|
| 125 |
end |
|---|
| 126 |
|
|---|
| 127 |
begin |
|---|
| 128 |
require_library_or_gem('utf8proc_native') |
|---|
| 129 |
require 'active_record/multibyte/handlers/utf8_handler_proc' |
|---|
| 130 |
class ConformanceTestProc < Test::Unit::TestCase |
|---|
| 131 |
include ConformanceTest |
|---|
| 132 |
def setup |
|---|
| 133 |
@handler = ::ActiveSupport::Multibyte::Handlers::UTF8HandlerProc |
|---|
| 134 |
end |
|---|
| 135 |
end |
|---|
| 136 |
rescue LoadError |
|---|
| 137 |
end |
|---|
| 138 |
|
|---|
| 139 |
class ConformanceTestPure < Test::Unit::TestCase |
|---|
| 140 |
include ConformanceTest |
|---|
| 141 |
def setup |
|---|
| 142 |
@handler = ::ActiveSupport::Multibyte::Handlers::UTF8Handler |
|---|
| 143 |
end |
|---|
| 144 |
end |
|---|
| 145 |
|
|---|
| 146 |
end |
|---|