Skip to content
Browse files

ActiveSupport::Multibyte::Handlers::UTF8Handler should raise when a r…

…ange and an integer are passed in (just like the native implementation). Closes #7176 [Rich Collins]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6021 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 1bdb14b commit a44cee2549e04e6e586f0f9432d98bb2dd4e3625 @technoweenie technoweenie committed
View
8 activesupport/lib/active_support/multibyte/handlers/utf8_handler.rb
@@ -168,8 +168,12 @@ def reverse(str)
# Implements Unicode-aware slice with codepoints. Slicing on one point returns the codepoints for that
# character.
def slice(str, *args)
- if (args.size == 2 && args.first.is_a?(Range))
- raise TypeError, 'cannot convert Range into Integer' # Do as if we were native
+ if args.size > 2
+ raise ArgumentError, "wrong number of arguments (#{args.size} for 1)" # Do as if we were native
+ elsif (args.size == 2 && !(args.first.is_a?(Numeric) || args.first.is_a?(Regexp)))
+ raise TypeError, "cannot convert #{args.first.class} into Integer" # Do as if we were native
+ elsif (args.size == 2 && !args[1].is_a?(Numeric))
+ raise TypeError, "cannot convert #{args[1].class} into Integer" # Do as if we were native
elsif args[0].kind_of? Range
cps = u_unpack(str).slice(*args)
cps.nil? ? nil : cps.pack('U*')
View
3 activesupport/test/multibyte_handler_test.rb
@@ -162,6 +162,9 @@ def test_slice
assert_equal " Блå ffi ", @handler.slice(@string, 4..10), "Unicode characters have to be returned"
assert_equal "", @handler.slice(@string, 7..6), "Range is empty, should return an empty string"
assert_raise(ActiveSupport::Multibyte::Handlers::EncodingError) { @handler.slice(@bytestring, 2..3) }
+ assert_raise(TypeError, "With 2 args, should raise TypeError for non-Numeric or Regexp first argument") { @handler.slice(@string, 2..3, 1) }
+ assert_raise(TypeError, "With 2 args, should raise TypeError for non-Numeric or Regexp second argument") { @handler.slice(@string, 1, 2..3) }
+ assert_raise(ArgumentError, "Should raise ArgumentError when there are more than 2 args") { @handler.slice(@string, 1, 1, 1) }
end
def test_grapheme_cluster_length

0 comments on commit a44cee2

Please sign in to comment.
Something went wrong with that request. Please try again.