Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Move with_kcode helper to abstract_unit. Add tests for multibyte stri…

…ng extensions.
  • Loading branch information...
commit 021172208885be0c137a9d5f352f862479044e7a 1 parent 042fd97
Manfred Stienstra Manfred authored
15 activesupport/test/abstract_unit.rb
View
@@ -27,4 +27,17 @@ def uses_mocha(test_name, &block)
end
# Show backtraces for deprecated behavior for quicker cleanup.
-ActiveSupport::Deprecation.debug = true
+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
53 activesupport/test/core_ext/string_ext_test.rb
View
@@ -191,13 +191,12 @@ def test_string_squish
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
12 activesupport/test/json/encoding_test.rb
View
@@ -101,18 +101,6 @@ def test_time_to_json_includes_local_offset
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
Please sign in to comment.
Something went wrong with that request. Please try again.