2017-07-09 08:06:36 -04:00
|
|
|
|
# frozen_string_literal: true
|
2013-11-13 17:16:55 -05:00
|
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
|
require "abstract_unit"
|
|
|
|
|
require "multibyte_test_helpers"
|
2013-11-13 17:16:55 -05:00
|
|
|
|
|
2016-01-24 09:22:14 -05:00
|
|
|
|
class MultibyteGraphemeBreakConformanceTest < ActiveSupport::TestCase
|
2016-07-04 07:11:44 -04:00
|
|
|
|
include MultibyteTestHelpers
|
2013-11-13 17:16:55 -05:00
|
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
|
UNIDATA_FILE = "/auxiliary/GraphemeBreakTest.txt"
|
2016-07-04 07:36:30 -04:00
|
|
|
|
RUN_P = begin
|
|
|
|
|
Downloader.download(UNIDATA_URL + UNIDATA_FILE, CACHE_DIR + UNIDATA_FILE)
|
|
|
|
|
rescue
|
|
|
|
|
end
|
2013-11-13 17:16:55 -05:00
|
|
|
|
|
|
|
|
|
def setup
|
2016-07-04 07:36:30 -04:00
|
|
|
|
skip "Unable to download test data" unless RUN_P
|
2013-11-13 17:16:55 -05:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_breaks
|
|
|
|
|
each_line_of_break_tests do |*cols|
|
|
|
|
|
*clusters, comment = *cols
|
|
|
|
|
packed = ActiveSupport::Multibyte::Unicode.pack_graphemes(clusters)
|
|
|
|
|
assert_equal clusters, ActiveSupport::Multibyte::Unicode.unpack_graphemes(packed), comment
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2016-12-23 06:31:57 -05:00
|
|
|
|
private
|
2013-11-13 17:16:55 -05:00
|
|
|
|
def each_line_of_break_tests(&block)
|
|
|
|
|
lines = 0
|
|
|
|
|
max_test_lines = 0 # Don't limit below 21, because that's the header of the testfile
|
2016-08-06 12:03:25 -04:00
|
|
|
|
File.open(File.join(CACHE_DIR, UNIDATA_FILE), "r") do | f |
|
2016-09-01 17:41:49 -04:00
|
|
|
|
until f.eof? || (max_test_lines > 21 && lines > max_test_lines)
|
2013-11-13 17:16:55 -05:00
|
|
|
|
lines += 1
|
|
|
|
|
line = f.gets.chomp!
|
2016-08-06 12:03:25 -04:00
|
|
|
|
next if line.empty? || line.start_with?("#")
|
2013-11-13 17:16:55 -05:00
|
|
|
|
|
|
|
|
|
cols, comment = line.split("#")
|
|
|
|
|
# Cluster breaks are represented by ÷
|
2016-08-16 03:30:11 -04:00
|
|
|
|
clusters = cols.split("÷").map { |e| e.strip }.reject { |e| e.empty? }
|
2013-11-13 17:16:55 -05:00
|
|
|
|
clusters = clusters.map do |cluster|
|
|
|
|
|
# Codepoints within each cluster are separated by ×
|
2016-08-16 03:30:11 -04:00
|
|
|
|
codepoints = cluster.split("×").map { |e| e.strip }.reject { |e| e.empty? }
|
2013-11-13 17:16:55 -05:00
|
|
|
|
# codepoints are in hex in the test suite, pack wants them as integers
|
2016-08-16 03:30:11 -04:00
|
|
|
|
codepoints.map { |codepoint| codepoint.to_i(16) }
|
2013-11-13 17:16:55 -05:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# The tests contain a solitary U+D800 <Non Private Use High
|
|
|
|
|
# Surrogate, First> character, which Ruby does not allow to stand
|
|
|
|
|
# alone in a UTF-8 string. So we'll just skip it.
|
|
|
|
|
next if clusters.flatten.include?(0xd800)
|
|
|
|
|
|
|
|
|
|
clusters << comment.strip
|
|
|
|
|
|
|
|
|
|
yield(*clusters)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|