Skip to content

Commit 32c63d9

Browse files
committed
move the test to test/ruby/test_transcode.rb and change the test to look like what @tenderlove and @nobu suggested.
1 parent 81aa7b2 commit 32c63d9

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

test/ruby/test_encoding.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,6 @@ def test_encoding
1717
end
1818
end
1919

20-
def test_encoding_of_ascii_originating_from_binary
21-
binary_string = "\x82foo"
22-
ascii_string = binary_string.bytes.to_a.pack('c*')[1..-1]
23-
assert_equal "foo", ascii_string
24-
assert_equal "ASCII-8BIT", ascii_string.encoding.name
25-
utf8_string = ascii_string.encode("UTF-8")
26-
assert_equal "foo", utf8_string
27-
assert_equal "UTF-8", utf8_string.encoding.name
28-
puts "End"
29-
end
30-
3120
def test_enc_names
3221
aliases = Encoding.aliases
3322
aliases.each do |a, en|

test/ruby/test_transcode.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,30 @@ def check_both_ways2(str1, enc1, str2, enc2)
6060
assert_equal(str2.force_encoding(enc2), str1.encode(enc2, enc1))
6161
end
6262

63+
def test_encoding_of_ascii_originating_from_binary
64+
binary_string = [0x82, 0x66, 0x6f, 0x6f]
65+
class << binary_string
66+
# create a copy on write substring that contains
67+
# just the ascii characters (i.e. foo), in JRuby
68+
# the underlying string have the same buffer backing
69+
# it up, but the offset of the string will be 1 instead
70+
# of 0.
71+
def make_cow_substring
72+
pack('C4').slice(1, 3)
73+
end
74+
end
75+
76+
ascii_string = binary_string.make_cow_substring
77+
assert_equal("foo", ascii_string)
78+
assert_equal(Encoding::ASCII_8BIT, ascii_string.encoding)
79+
utf8_string = nil
80+
assert_nothing_raised("JRUBY-6764") do
81+
utf8_string = ascii_string.encode(Encoding::UTF_8)
82+
end
83+
assert_equal("foo", utf8_string)
84+
assert_equal(Encoding::UTF_8, utf8_string.encoding)
85+
end
86+
6387
def test_encodings
6488
check_both_ways("\u307E\u3064\u3082\u3068 \u3086\u304D\u3072\u308D",
6589
"\x82\xdc\x82\xc2\x82\xe0\x82\xc6 \x82\xe4\x82\xab\x82\xd0\x82\xeb", 'shift_jis') # まつもと ゆきひろ

0 commit comments

Comments
 (0)