2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2018-09-29 20:50:43 -04:00
|
|
|
require_relative "abstract_unit"
|
2016-08-06 12:03:25 -04:00
|
|
|
require "active_support/inflector/transliterate"
|
2010-04-12 11:44:25 -04:00
|
|
|
|
2012-01-05 20:12:46 -05:00
|
|
|
class TransliterateTest < ActiveSupport::TestCase
|
2010-04-12 11:44:25 -04:00
|
|
|
def test_transliterate_should_not_change_ascii_chars
|
|
|
|
(0..127).each do |byte|
|
|
|
|
char = [byte].pack("U")
|
|
|
|
assert_equal char, ActiveSupport::Inflector.transliterate(char)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-14 10:12:07 -04:00
|
|
|
def test_transliterate_should_approximate_ascii
|
2014-08-25 12:33:31 -04:00
|
|
|
# create string with range of Unicode's western characters with
|
2010-04-14 10:12:07 -04:00
|
|
|
# diacritics, excluding the division and multiplication signs which for
|
|
|
|
# some reason or other are floating in the middle of all the letters.
|
2016-08-16 03:30:11 -04:00
|
|
|
string = (0xC0..0x17E).to_a.reject { |c| [0xD7, 0xF7].include?(c) }.pack("U*")
|
2010-04-14 10:12:07 -04:00
|
|
|
string.each_char do |char|
|
2013-03-17 14:49:57 -04:00
|
|
|
assert_match %r{^[a-zA-Z']*$}, ActiveSupport::Inflector.transliterate(char)
|
2010-04-12 11:44:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-14 10:12:07 -04:00
|
|
|
def test_transliterate_should_work_with_custom_i18n_rules_and_uncomposed_utf8
|
|
|
|
char = [117, 776].pack("U*") # "ü" as ASCII "u" plus COMBINING DIAERESIS
|
2016-08-16 03:30:11 -04:00
|
|
|
I18n.backend.store_translations(:de, i18n: { transliterate: { rule: { "ü" => "ue" } } })
|
2013-07-25 15:21:52 -04:00
|
|
|
default_locale, I18n.locale = I18n.locale, :de
|
2010-04-14 10:12:07 -04:00
|
|
|
assert_equal "ue", ActiveSupport::Inflector.transliterate(char)
|
2013-07-25 15:21:52 -04:00
|
|
|
ensure
|
|
|
|
I18n.locale = default_locale
|
2010-04-14 10:12:07 -04:00
|
|
|
end
|
|
|
|
|
2019-03-11 15:09:58 -04:00
|
|
|
def test_transliterate_respects_the_locale_argument
|
|
|
|
char = [117, 776].pack("U*") # "ü" as ASCII "u" plus COMBINING DIAERESIS
|
|
|
|
I18n.backend.store_translations(:de, i18n: { transliterate: { rule: { "ü" => "ue" } } })
|
|
|
|
assert_equal "ue", ActiveSupport::Inflector.transliterate(char, locale: :de)
|
|
|
|
end
|
|
|
|
|
2010-04-14 10:12:07 -04:00
|
|
|
def test_transliterate_should_allow_a_custom_replacement_char
|
|
|
|
assert_equal "a*b", ActiveSupport::Inflector.transliterate("a索b", "*")
|
|
|
|
end
|
2017-01-16 13:49:44 -05:00
|
|
|
|
|
|
|
def test_transliterate_handles_empty_string
|
|
|
|
assert_equal "", ActiveSupport::Inflector.transliterate("")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_transliterate_handles_nil
|
|
|
|
exception = assert_raises ArgumentError do
|
|
|
|
ActiveSupport::Inflector.transliterate(nil)
|
|
|
|
end
|
|
|
|
assert_equal "Can only transliterate strings. Received NilClass", exception.message
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_transliterate_handles_unknown_object
|
|
|
|
exception = assert_raises ArgumentError do
|
|
|
|
ActiveSupport::Inflector.transliterate(Object.new)
|
|
|
|
end
|
|
|
|
assert_equal "Can only transliterate strings. Received Object", exception.message
|
|
|
|
end
|
2019-07-17 14:12:41 -04:00
|
|
|
|
2019-07-19 15:16:23 -04:00
|
|
|
def test_transliterate_handles_strings_with_valid_utf8_encodings
|
2019-07-31 12:11:31 -04:00
|
|
|
string = String.new("A", encoding: Encoding::UTF_8).freeze
|
2019-07-19 15:16:23 -04:00
|
|
|
assert_equal "A", ActiveSupport::Inflector.transliterate(string)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_transliterate_handles_strings_with_valid_us_ascii_encodings
|
2019-07-31 12:11:31 -04:00
|
|
|
string = String.new("A", encoding: Encoding::US_ASCII).freeze
|
2019-07-25 14:51:42 -04:00
|
|
|
transcoded = ActiveSupport::Inflector.transliterate(string)
|
|
|
|
assert_equal "A", transcoded
|
|
|
|
assert_equal Encoding::US_ASCII, transcoded.encoding
|
2019-07-19 15:16:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_transliterate_handles_strings_with_valid_gb18030_encodings
|
2019-07-31 12:11:31 -04:00
|
|
|
string = String.new("A", encoding: Encoding::GB18030).freeze
|
2019-07-26 11:02:40 -04:00
|
|
|
transcoded = ActiveSupport::Inflector.transliterate(string)
|
|
|
|
assert_equal "A", transcoded
|
|
|
|
assert_equal Encoding::GB18030, transcoded.encoding
|
2019-07-19 15:16:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_transliterate_handles_strings_with_incompatible_encodings
|
|
|
|
incompatible_encodings = Encoding.list - [
|
|
|
|
Encoding::UTF_8,
|
|
|
|
Encoding::US_ASCII,
|
2019-07-25 14:51:42 -04:00
|
|
|
Encoding::GB18030
|
2019-07-19 15:16:23 -04:00
|
|
|
]
|
|
|
|
incompatible_encodings.each do |encoding|
|
2019-07-31 12:11:31 -04:00
|
|
|
string = String.new("", encoding: encoding).freeze
|
2019-07-25 14:51:42 -04:00
|
|
|
exception = assert_raises ArgumentError do
|
2019-07-19 15:16:23 -04:00
|
|
|
ActiveSupport::Inflector.transliterate(string)
|
|
|
|
end
|
2020-02-04 16:46:55 -05:00
|
|
|
assert_equal "Cannot transliterate strings with #{encoding} encoding", exception.message
|
2019-07-19 15:16:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_transliterate_handles_strings_with_invalid_utf8_bytes
|
2019-07-31 12:11:31 -04:00
|
|
|
string = String.new("\255", encoding: Encoding::UTF_8).freeze
|
2019-07-19 15:16:23 -04:00
|
|
|
assert_equal "?", ActiveSupport::Inflector.transliterate(string)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_transliterate_handles_strings_with_invalid_us_ascii_bytes
|
2019-07-31 12:11:31 -04:00
|
|
|
string = String.new("\255", encoding: Encoding::US_ASCII).freeze
|
2019-07-25 14:51:42 -04:00
|
|
|
assert_equal "?", ActiveSupport::Inflector.transliterate(string)
|
2019-07-19 15:16:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_transliterate_handles_strings_with_invalid_gb18030_bytes
|
2019-07-31 12:11:31 -04:00
|
|
|
string = String.new("\255", encoding: Encoding::GB18030).freeze
|
2019-07-26 11:02:40 -04:00
|
|
|
assert_equal "?", ActiveSupport::Inflector.transliterate(string)
|
2019-07-19 15:16:23 -04:00
|
|
|
end
|
2010-04-12 11:44:25 -04:00
|
|
|
end
|