提交 f90a08c1 编写于 作者: K Kevin McPhillips

Raise ArgumentError if attempting to transliterate anything that is not a string

上级 c8e92f17
......@@ -57,6 +57,8 @@ module Inflector
# transliterate('Jürgen')
# # => "Juergen"
def transliterate(string, replacement = "?".freeze)
raise ArgumentError, "Can only transliterate strings. Received #{string.class.name}" unless string.is_a?(String)
I18n.transliterate(ActiveSupport::Multibyte::Unicode.normalize(
ActiveSupport::Multibyte::Unicode.tidy_bytes(string), :c),
replacement: replacement)
......
......@@ -31,4 +31,22 @@ def test_transliterate_should_work_with_custom_i18n_rules_and_uncomposed_utf8
def test_transliterate_should_allow_a_custom_replacement_char
assert_equal "a*b", ActiveSupport::Inflector.transliterate("a索b", "*")
end
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
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册