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

Avoid polluting String class globally in a test case

This commit is contained in:
Akira Matsuda 2014-08-16 07:51:13 +09:00
parent de2fab5f86
commit 5643cace2d

View file

@ -3,13 +3,6 @@ require 'abstract_unit'
require 'multibyte_test_helpers'
require 'active_support/core_ext/string/multibyte'
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 < ActiveSupport::TestCase
include MultibyteTestHelpers
@ -24,6 +17,8 @@ class MultibyteCharsTest < ActiveSupport::TestCase
end
def test_should_allow_method_calls_to_string
@chars.wrapped_string.singleton_class.class_eval { def __method_for_multibyte_testing; 'result'; end }
assert_nothing_raised do
@chars.__method_for_multibyte_testing
end
@ -33,16 +28,22 @@ class MultibyteCharsTest < ActiveSupport::TestCase
end
def test_forwarded_method_calls_should_return_new_chars_instance
@chars.wrapped_string.singleton_class.class_eval { def __method_for_multibyte_testing; 'result'; end }
assert_kind_of @proxy_class, @chars.__method_for_multibyte_testing
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_when_result_is_not_nil
@chars.wrapped_string.singleton_class.class_eval { def __method_for_multibyte_testing!; 'result'; end }
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
@chars.wrapped_string.singleton_class.class_eval { def __method_for_multibyte_testing_that_returns_nil!; end }
assert_nil @chars.__method_for_multibyte_testing_that_returns_nil!
end
@ -54,7 +55,11 @@ class MultibyteCharsTest < ActiveSupport::TestCase
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
str = ''
str.singleton_class.class_eval { def __method_for_multibyte_testing_with_integer_result; 1; end }
@chars.wrapped_string.singleton_class.class_eval { def __method_for_multibyte_testing_with_integer_result; 1; end }
assert_equal str.__method_for_multibyte_testing_with_integer_result, @chars.__method_for_multibyte_testing_with_integer_result
end
def test_should_concatenate