1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #27704 from kmcphillips/transliterate-exception

Raise ArgumentError if attempting to transliterate nil
This commit is contained in:
Rafael França 2017-01-16 15:32:49 -05:00 committed by GitHub
commit dc01a40eac
3 changed files with 25 additions and 0 deletions

View file

@ -1,3 +1,8 @@
* Changed `ActiveSupport::Inflector#transliterate` to raise `ArgumentError` when it receives
anything except a string.
*Kevin McPhillips*
* Fixed bugs that `StringInquirer#respond_to_missing?` and
`ArrayInquirer#respond_to_missing?` do not fallback to `super`.

View file

@ -57,6 +57,8 @@ module ActiveSupport
# 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)

View file

@ -31,4 +31,22 @@ class TransliterateTest < ActiveSupport::TestCase
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