From 021172208885be0c137a9d5f352f862479044e7a Mon Sep 17 00:00:00 2001 From: Manfred Stienstra Date: Sun, 21 Sep 2008 17:22:55 +0200 Subject: Move with_kcode helper to abstract_unit. Add tests for multibyte string extensions. --- activesupport/test/abstract_unit.rb | 15 +++++++- activesupport/test/core_ext/string_ext_test.rb | 53 +++++++++++++++++++++++--- activesupport/test/json/encoding_test.rb | 12 ------ 3 files changed, 61 insertions(+), 19 deletions(-) diff --git a/activesupport/test/abstract_unit.rb b/activesupport/test/abstract_unit.rb index f39f264ae1..a698beaa0e 100644 --- a/activesupport/test/abstract_unit.rb +++ b/activesupport/test/abstract_unit.rb @@ -27,4 +27,17 @@ unless defined? uses_mocha end # Show backtraces for deprecated behavior for quicker cleanup. -ActiveSupport::Deprecation.debug = true \ No newline at end of file +ActiveSupport::Deprecation.debug = true + +def with_kcode(code) + if RUBY_VERSION < '1.9' + begin + old_kcode, $KCODE = $KCODE, code + yield + ensure + $KCODE = old_kcode + end + else + yield + end +end \ No newline at end of file diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb index c0decf2c3f..fe5ce276c5 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -191,13 +191,12 @@ class StringInflectionsTest < Test::Unit::TestCase if RUBY_VERSION < '1.9' def test_each_char_with_utf8_string_when_kcode_is_utf8 - old_kcode, $KCODE = $KCODE, 'UTF8' - '€2.99'.each_char do |char| - assert_not_equal 1, char.length - break + with_kcode('UTF8') do + '€2.99'.each_char do |char| + assert_not_equal 1, char.length + break + end end - ensure - $KCODE = old_kcode end end end @@ -206,4 +205,46 @@ class StringBehaviourTest < Test::Unit::TestCase def test_acts_like_string assert 'Bambi'.acts_like_string? end +end + +class CoreExtStringMultibyteTest < Test::Unit::TestCase + UNICODE_STRING = 'こにちわ' + ASCII_STRING = 'ohayo' + BYTE_STRING = "\270\236\010\210\245" + + def test_core_ext_adds_mb_chars + assert UNICODE_STRING.respond_to?(:mb_chars) + end + + def test_string_should_recognize_utf8_strings + assert UNICODE_STRING.is_utf8? + assert ASCII_STRING.is_utf8? + assert !BYTE_STRING.is_utf8? + end + + if RUBY_VERSION < '1.8.7' + def test_core_ext_adds_chars + assert UNICODE_STRING.respond_to?(:chars) + end + end + + if RUBY_VERSION < '1.9' + def test_mb_chars_returns_self_when_kcode_not_set + with_kcode('none') do + assert UNICODE_STRING.mb_chars.kind_of?(String) + end + end + + def test_mb_chars_returns_an_instance_of_the_chars_proxy_when_kcode_utf8 + with_kcode('UTF8') do + assert UNICODE_STRING.mb_chars.kind_of?(ActiveSupport::Multibyte.proxy_class) + end + end + end + + if RUBY_VERSION >= '1.9' + def test_mb_chars_returns_string + assert UNICODE_STRING.mb_chars.kind_of?(String) + end + end end \ No newline at end of file diff --git a/activesupport/test/json/encoding_test.rb b/activesupport/test/json/encoding_test.rb index 38bb8f3e79..497f028369 100644 --- a/activesupport/test/json/encoding_test.rb +++ b/activesupport/test/json/encoding_test.rb @@ -101,18 +101,6 @@ class TestJSONEncoding < Test::Unit::TestCase end protected - def with_kcode(code) - if RUBY_VERSION < '1.9' - begin - old_kcode, $KCODE = $KCODE, 'UTF8' - yield - ensure - $KCODE = old_kcode - end - else - yield - end - end def object_keys(json_object) json_object[1..-2].scan(/([^{}:,\s]+):/).flatten.sort -- cgit v1.2.3