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

Move with_kcode helper to abstract_unit. Add tests for multibyte string extensions.

This commit is contained in:
Manfred Stienstra 2008-09-21 17:22:55 +02:00
parent 042fd97127
commit 0211722088
3 changed files with 61 additions and 19 deletions

View file

@ -27,4 +27,17 @@ unless defined? uses_mocha
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

View file

@ -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

View file

@ -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