Skip to content

Commit

Permalink
Non-string results from forwarded methods should be returned vertabim.
Browse files Browse the repository at this point in the history
  • Loading branch information
Manfred committed Sep 21, 2008
1 parent b8eec5a commit 809af7f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
5 changes: 3 additions & 2 deletions activesupport/lib/active_support/multibyte/chars.rb
Expand Up @@ -106,10 +106,11 @@ def method_missing(method, *args, &block)
@wrapped_string.__send__(method, *args, &block) @wrapped_string.__send__(method, *args, &block)
self self
else else
chars(@wrapped_string.__send__(method, *args, &block)) result = @wrapped_string.__send__(method, *args, &block)
result.kind_of?(String) ? chars(result) : result
end end
end end

# Returns +true+ if _obj_ responds to the given method. Private methods are included in the search # Returns +true+ if _obj_ responds to the given method. Private methods are included in the search
# only if the optional second parameter evaluates to +true+. # only if the optional second parameter evaluates to +true+.
def respond_to?(method, include_private=false) def respond_to?(method, include_private=false)
Expand Down
19 changes: 12 additions & 7 deletions activesupport/test/multibyte_chars_test.rb
Expand Up @@ -21,8 +21,9 @@ def assert_equal_codepoints(expected, actual, message=nil)
end end


class String class String
def __string_for_multibyte_testing; self; end def __method_for_multibyte_testing_with_integer_result; 1; end
def __string_for_multibyte_testing!; self; end def __method_for_multibyte_testing; 'result'; end
def __method_for_multibyte_testing!; 'result'; end
end end


class MultibyteCharsTest < Test::Unit::TestCase class MultibyteCharsTest < Test::Unit::TestCase
Expand All @@ -40,27 +41,31 @@ def test_wraps_the_original_string


def test_should_allow_method_calls_to_string def test_should_allow_method_calls_to_string
assert_nothing_raised do assert_nothing_raised do
@chars.__string_for_multibyte_testing @chars.__method_for_multibyte_testing
end end
assert_raises NoMethodError do assert_raises NoMethodError do
@chars.__unknown_method @chars.__unknown_method
end end
end end


def test_forwarded_method_calls_should_return_new_chars_instance def test_forwarded_method_calls_should_return_new_chars_instance
assert @chars.__string_for_multibyte_testing.kind_of?(@proxy_class) assert @chars.__method_for_multibyte_testing.kind_of?(@proxy_class)
assert_not_equal @chars.object_id, @chars.__string_for_multibyte_testing.object_id assert_not_equal @chars.object_id, @chars.__method_for_multibyte_testing.object_id
end end


def test_forwarded_bang_method_calls_should_return_the_original_chars_instance def test_forwarded_bang_method_calls_should_return_the_original_chars_instance
assert @chars.__string_for_multibyte_testing!.kind_of?(@proxy_class) assert @chars.__method_for_multibyte_testing!.kind_of?(@proxy_class)
assert_equal @chars.object_id, @chars.__string_for_multibyte_testing!.object_id assert_equal @chars.object_id, @chars.__method_for_multibyte_testing!.object_id
end end


def test_methods_are_forwarded_to_wrapped_string_for_byte_strings def test_methods_are_forwarded_to_wrapped_string_for_byte_strings
assert_equal BYTE_STRING.class, BYTE_STRING.mb_chars.class assert_equal BYTE_STRING.class, BYTE_STRING.mb_chars.class
end end


def test_forwarded_method_with_non_string_result_should_be_returned_vertabim
assert_equal ''.__method_for_multibyte_testing_with_integer_result, @chars.__method_for_multibyte_testing_with_integer_result
end

def test_should_concatenate def test_should_concatenate
assert_equal 'ab', 'a'.mb_chars + 'b' assert_equal 'ab', 'a'.mb_chars + 'b'
assert_equal 'ab', 'a' + 'b'.mb_chars assert_equal 'ab', 'a' + 'b'.mb_chars
Expand Down

0 comments on commit 809af7f

Please sign in to comment.