Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

All methods which normally return a string now return a proxy instance.

  • Loading branch information...
commit 8abef4fd0df828e79be6b9fadd8f45c575ab817c 1 parent 520c3f3
@Manfred Manfred authored
View
3  activesupport/lib/active_support/multibyte/chars.rb
@@ -246,7 +246,6 @@ def []=(*args)
result[range] = self.class.u_unpack(replace_by)
@wrapped_string.replace(result.pack('U*'))
end
- self
end
# Works just like String#rjust, only integer specifies characters instead of bytes.
@@ -365,7 +364,7 @@ def downcase
# Example:
# 'über'.mb_chars.capitalize.to_s #=> "Über"
def capitalize
- (slice(0) || '').upcase + (slice(1..-1) || '').downcase
+ (slice(0) || chars('')).upcase + (slice(1..-1) || chars('')).downcase
end
# Returns the KC normalization of the string by default. NFKC is considered the best normalization form for
View
49 activesupport/test/multibyte_chars_test.rb
@@ -48,12 +48,12 @@ def test_should_allow_method_calls_to_string
end
def test_forwarded_method_calls_should_return_new_chars_instance
- assert @chars.__string_for_multibyte_testing.kind_of?(ActiveSupport::Multibyte::Chars)
+ assert @chars.__string_for_multibyte_testing.kind_of?(@proxy_class)
assert_not_equal @chars.object_id, @chars.__string_for_multibyte_testing.object_id
end
def test_forwarded_bang_method_calls_should_return_the_original_chars_instance
- assert @chars.__string_for_multibyte_testing!.kind_of?(ActiveSupport::Multibyte::Chars)
+ assert @chars.__string_for_multibyte_testing!.kind_of?(@proxy_class)
assert_equal @chars.object_id, @chars.__string_for_multibyte_testing!.object_id
end
@@ -88,10 +88,10 @@ def test_ascii_strings_are_treated_at_utf8_strings
end
def test_concatenate_should_return_proxy_instance
- assert(('a'.mb_chars + 'b').kind_of?(ActiveSupport::Multibyte::Chars))
- assert(('a'.mb_chars + 'b'.mb_chars).kind_of?(ActiveSupport::Multibyte::Chars))
- assert(('a'.mb_chars << 'b').kind_of?(ActiveSupport::Multibyte::Chars))
- assert(('a'.mb_chars << 'b'.mb_chars).kind_of?(ActiveSupport::Multibyte::Chars))
+ assert(('a'.mb_chars + 'b').kind_of?(@proxy_class))
+ assert(('a'.mb_chars + 'b'.mb_chars).kind_of?(@proxy_class))
+ assert(('a'.mb_chars << 'b').kind_of?(@proxy_class))
+ assert(('a'.mb_chars << 'b'.mb_chars).kind_of?(@proxy_class))
end
end
end
@@ -111,7 +111,7 @@ def setup
if RUBY_VERSION < '1.9'
def test_split_should_return_an_array_of_chars_instances
@chars.split(//).each do |character|
- assert character.kind_of?(ActiveSupport::Multibyte::Chars)
+ assert character.kind_of?(ActiveSupport::Multibyte.proxy_class)
end
end
@@ -150,6 +150,35 @@ def test_unicode_string_should_have_utf8_encoding
end
end
+ def test_identity
+ assert_equal @chars, @chars
+ assert @chars.eql?(@chars)
+ if RUBY_VERSION <= '1.9'
+ assert !@chars.eql?(UNICODE_STRING)
+ else
+ assert @chars.eql?(UNICODE_STRING)
+ end
+ end
+
+ def test_string_methods_are_chainable
+ assert chars('').insert(0, '').kind_of?(ActiveSupport::Multibyte.proxy_class)
+ assert chars('').rjust(1).kind_of?(ActiveSupport::Multibyte.proxy_class)
+ assert chars('').ljust(1).kind_of?(ActiveSupport::Multibyte.proxy_class)
+ assert chars('').center(1).kind_of?(ActiveSupport::Multibyte.proxy_class)
+ assert chars('').rstrip.kind_of?(ActiveSupport::Multibyte.proxy_class)
+ assert chars('').lstrip.kind_of?(ActiveSupport::Multibyte.proxy_class)
+ assert chars('').strip.kind_of?(ActiveSupport::Multibyte.proxy_class)
+ assert chars('').reverse.kind_of?(ActiveSupport::Multibyte.proxy_class)
+ assert chars(' ').slice(0).kind_of?(ActiveSupport::Multibyte.proxy_class)
+ assert chars('').upcase.kind_of?(ActiveSupport::Multibyte.proxy_class)
+ assert chars('').downcase.kind_of?(ActiveSupport::Multibyte.proxy_class)
+ assert chars('').capitalize.kind_of?(ActiveSupport::Multibyte.proxy_class)
+ assert chars('').normalize.kind_of?(ActiveSupport::Multibyte.proxy_class)
+ assert chars('').decompose.kind_of?(ActiveSupport::Multibyte.proxy_class)
+ assert chars('').compose.kind_of?(ActiveSupport::Multibyte.proxy_class)
+ assert chars('').tidy_bytes.kind_of?(ActiveSupport::Multibyte.proxy_class)
+ end
+
def test_should_be_equal_to_the_wrapped_string
assert_equal UNICODE_STRING, @chars
assert_equal @chars, UNICODE_STRING
@@ -160,6 +189,11 @@ def test_should_not_be_equal_to_an_other_string
assert_not_equal 'other', @chars
end
+ def test_sortability
+ words = %w(builder armor zebra).map(&:mb_chars).sort
+ assert_equal %w(armor builder zebra), words
+ end
+
def test_should_return_character_offset_for_regexp_matches
assert_nil(@chars =~ /wrong/u)
assert_equal 0, (@chars =~ //u)
@@ -181,6 +215,7 @@ def test_insert_should_be_destructive
end
def test_insert_throws_index_error
+ assert_raises(IndexError) { @chars.insert(-12, '')}
assert_raises(IndexError) { @chars.insert(12, '') }
end
Please sign in to comment.
Something went wrong with that request. Please try again.