2016-08-06 12:03:25 -04:00
|
|
|
require "abstract_unit"
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
2010-04-12 11:44:25 -04:00
|
|
|
end
|