Permalink
Browse files

Make return value from bang methods match Ruby docs

The docs for the String class indicate that methods like `rstrip!` and
others should return nil when they do not have an effect on the string.
  • Loading branch information...
norman committed Jan 5, 2012
1 parent 4387f97 commit c4b522d3c89208b554780e9c49747d07dbe3a4e5
Showing with 12 additions and 6 deletions.
  1. +2 −2 activesupport/lib/active_support/multibyte/chars.rb
  2. +10 −4 activesupport/test/multibyte_chars_test.rb
@@ -47,8 +47,8 @@ def initialize(string)
# Forward all undefined methods to the wrapped string.
def method_missing(method, *args, &block)
if method.to_s =~ /!$/
- @wrapped_string.__send__(method, *args, &block)
- self
+ result = @wrapped_string.__send__(method, *args, &block)
+ self if result
else
result = @wrapped_string.__send__(method, *args, &block)
result.kind_of?(String) ? chars(result) : result
@@ -7,6 +7,7 @@ class String
def __method_for_multibyte_testing_with_integer_result; 1; end
def __method_for_multibyte_testing; 'result'; end
def __method_for_multibyte_testing!; 'result'; end
+ def __method_for_multibyte_testing_that_returns_nil!; end
end
class MultibyteCharsTest < Test::Unit::TestCase
@@ -36,11 +37,15 @@ def test_forwarded_method_calls_should_return_new_chars_instance
assert_not_equal @chars.object_id, @chars.__method_for_multibyte_testing.object_id
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_when_result_is_not_nil
assert_kind_of @proxy_class, @chars.__method_for_multibyte_testing!
assert_equal @chars.object_id, @chars.__method_for_multibyte_testing!.object_id
end
+ def test_forwarded_bang_method_calls_should_return_nil_when_result_is_nil
+ assert_nil @chars.__method_for_multibyte_testing_that_returns_nil!
+ end
+
def test_methods_are_forwarded_to_wrapped_string_for_byte_strings
assert_equal BYTE_STRING.class, BYTE_STRING.mb_chars.class
end
@@ -117,10 +122,11 @@ def test_indexed_insert_accepts_fixnums
assert_equal 'こに わ', @chars
end
- %w{capitalize downcase lstrip reverse rstrip strip upcase}.each do |method|
+ %w{capitalize downcase lstrip reverse rstrip upcase}.each do |method|
class_eval(<<-EOTESTS)
- def test_#{method}_bang_should_return_self
- assert_equal @chars.object_id, @chars.send("#{method}!").object_id
+ def test_#{method}_bang_should_return_self_when_modifying_wrapped_string
+ chars = ' él piDió Un bUen café '
+ assert_equal chars.object_id, chars.send("#{method}!").object_id
end
def test_#{method}_bang_should_change_wrapped_string

0 comments on commit c4b522d

Please sign in to comment.