blob: 2cf0767cdb2f856749859e23658f9879b1a5601a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
#!/usr/bin/env ruby
require File.dirname(__FILE__)+'/test_helper'
if RUBY_VERSION < "1.9"
exit
end
class MessagePackTestEncoding < Test::Unit::TestCase
def self.it(name, &block)
define_method("test_#{name}", &block)
end
it "US-ASCII" do
check_unpack "abc".force_encoding("US-ASCII")
end
it "UTF-8 ascii" do
check_unpack "abc".force_encoding("UTF-8")
end
it "UTF-8 mbstr" do
check_unpack "\xE3\x81\x82".force_encoding("UTF-8")
end
it "UTF-8 invalid" do
check_unpack "\xD0".force_encoding("UTF-8")
end
it "ASCII-8BIT" do
check_unpack "\xD0".force_encoding("ASCII-8BIT")
end
it "EUC-JP" do
x = "\xA4\xA2".force_encoding("EUC-JP")
check_unpack(x)
end
it "EUC-JP invalid" do
begin
"\xD0".force_encoding("EUC-JP").to_msgpack
assert(false)
rescue Encoding::InvalidByteSequenceError
assert(true)
end
end
private
def check_unpack(str)
if str.encoding.to_s == "ASCII-8BIT"
should_str = str.dup.force_encoding("UTF-8")
else
should_str = str.encode("UTF-8")
end
raw = str.to_msgpack
r = MessagePack.unpack(str.to_msgpack)
assert_equal(r.encoding.to_s, "UTF-8")
assert_equal(r, should_str.force_encoding("UTF-8"))
if str.valid_encoding?
sym = str.to_sym
r = MessagePack.unpack(sym.to_msgpack)
assert_equal(r.encoding.to_s, "UTF-8")
assert_equal(r, should_str.force_encoding("UTF-8"))
end
end
end
|