rtext 0.8.0 → 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,118 +1,116 @@
1
- $:.unshift File.join(File.dirname(__FILE__),"..","lib")
2
-
3
- require 'test/unit'
4
- require 'rtext/message_helper'
5
-
6
- class MessageHelperTest < Test::Unit::TestCase
7
- include RText::MessageHelper
8
-
9
- def test_serialize
10
- str = serialize_message({"key" => 1})
11
- assert_equal '9{"key":1}', str
12
- str = serialize_message({"key" => true})
13
- assert_equal '12{"key":true}', str
14
- str = serialize_message({"key" => "value"})
15
- assert_equal '15{"key":"value"}', str
16
- str = serialize_message({"key" => {"nested" => "value"}})
17
- assert_equal '26{"key":{"nested":"value"}}', str
18
- str = serialize_message({"key" => ["value1", "value2"]})
19
- assert_equal '27{"key":["value1","value2"]}', str
20
-
21
- # a iso-8859-1 ''
22
- str = serialize_message({"key" => "umlaut\xe4".force_encoding("binary")})
23
- assert_equal '19{"key":"umlaut%e4"}', str
24
- str = serialize_message({"key" => "umlaut\xe4".force_encoding("iso-8859-1")})
25
- assert_equal '19{"key":"umlaut%e4"}', str
26
- str = serialize_message({"key" => "umlaut\xe4".force_encoding("cp850")})
27
- assert_equal '19{"key":"umlaut%e4"}', str
28
- str = serialize_message({"key" => "umlaut\xe4".force_encoding("utf-8")})
29
- assert_equal '19{"key":"umlaut%e4"}', str
30
-
31
- # a utf-8 ''
32
- str = serialize_message({"key" => "umlaut\xc3\xa4".force_encoding("binary")})
33
- assert_equal '22{"key":"umlaut%c3%a4"}', str
34
- str = serialize_message({"key" => "umlaut\xc3\xa4".force_encoding("iso-8859-1")})
35
- assert_equal '22{"key":"umlaut%c3%a4"}', str
36
- str = serialize_message({"key" => "umlaut\xc3\xa4".force_encoding("cp850")})
37
- assert_equal '22{"key":"umlaut%c3%a4"}', str
38
- str = serialize_message({"key" => "umlaut\xc3\xa4".force_encoding("utf-8")})
39
- assert_equal '22{"key":"umlaut%c3%a4"}', str
40
-
41
- # %
42
- str = serialize_message({"key" => "%"})
43
- assert_equal '13{"key":"%25"}', str
44
- end
45
-
46
- def test_extract
47
- # specified length too short
48
- assert_raise JSON::ParserError do
49
- extract_message('8{"key":1}')
50
- end
51
- # specified length too long
52
- assert_raise JSON::ParserError do
53
- extract_message('10{"key":1}x')
54
- end
55
- # message data shorter than length specifie, waits for more data
56
- assert_nothing_raised do
57
- extract_message('10{"key":1}')
58
- end
59
-
60
- obj = extract_message('9{"key":1}')
61
- assert_equal({"key" => 1}, obj)
62
- obj = extract_message('12{"key":true}')
63
- assert_equal({"key" => true}, obj)
64
- obj = extract_message('15{"key":"value"}')
65
- assert_equal({"key" => "value"}, obj)
66
- obj = extract_message('26{"key":{"nested":"value"}}')
67
- assert_equal({"key" => {"nested" => "value"}}, obj)
68
- obj = extract_message('27{"key":["value1","value2"]}')
69
- assert_equal({"key" => ["value1", "value2"]}, obj)
70
-
71
- # a iso-8859-1 '�'
72
- obj = extract_message('19{"key":"umlaut%e4"}'.force_encoding("binary"))
73
- assert_equal "ASCII-8BIT", obj["key"].encoding.name
74
- assert_equal "umlaut\xe4".force_encoding("ascii-8bit"), obj["key"]
75
- obj = extract_message('19{"key":"umlaut%e4"}'.force_encoding("utf-8"))
76
- assert_equal "ASCII-8BIT", obj["key"].encoding.name
77
- assert_equal "umlaut\xe4".force_encoding("ascii-8bit"), obj["key"]
78
-
79
- # a utf-8 '�'
80
- obj = extract_message('22{"key":"umlaut%c3%a4"}'.force_encoding("binary"))
81
- assert_equal "ASCII-8BIT", obj["key"].encoding.name
82
- assert_equal "umlaut\xc3\xa4".force_encoding("ascii-8bit"), obj["key"]
83
- obj = extract_message('22{"key":"umlaut%c3%a4"}'.force_encoding("utf-8"))
84
- assert_equal "ASCII-8BIT", obj["key"].encoding.name
85
- assert_equal "umlaut\xc3\xa4".force_encoding("ascii-8bit"), obj["key"]
86
-
87
- # %
88
- obj = extract_message('13{"key":"%25"}')
89
- assert_equal "ASCII-8BIT", obj["key"].encoding.name
90
- assert_equal "%", obj["key"]
91
-
92
- # invalid escape sequence
93
- obj = extract_message('11{"key":"%"}')
94
- assert_equal "ASCII-8BIT", obj["key"].encoding.name
95
- assert_equal "%", obj["key"]
96
- obj = extract_message('13{"key":"%xx"}')
97
- assert_equal "ASCII-8BIT", obj["key"].encoding.name
98
- assert_equal "%xx", obj["key"]
99
- obj = extract_message('14{"key":"%%25"}')
100
- assert_equal "ASCII-8BIT", obj["key"].encoding.name
101
- assert_equal "%%", obj["key"]
102
-
103
- # invalid characters (protocol should use 7-bit ascii only)
104
- obj = extract_message(%Q(14{"key":"\xe4345"}).force_encoding("binary"))
105
- assert_equal "ASCII-8BIT", obj["key"].encoding.name
106
- assert_equal "?345", obj["key"]
107
- end
108
-
109
- def test_roundtrip
110
- value = (0..255).collect{|b| b.chr("binary")}.join
111
- str = serialize_message({"key" => value})
112
- obj = extract_message(str)
113
- assert_equal "ASCII-8BIT", obj["key"].encoding.name
114
- assert_equal (0..255).collect{|i| i}, obj["key"].bytes.to_a
115
- end
116
-
117
- end
118
-
1
+ $:.unshift File.join(File.dirname(__FILE__),"..","lib")
2
+
3
+ require 'minitest/autorun'
4
+ require 'rtext/message_helper'
5
+
6
+ class MessageHelperTest < MiniTest::Test
7
+ include RText::MessageHelper
8
+
9
+ def test_serialize
10
+ str = serialize_message({"key" => 1})
11
+ assert_equal '9{"key":1}', str
12
+ str = serialize_message({"key" => true})
13
+ assert_equal '12{"key":true}', str
14
+ str = serialize_message({"key" => "value"})
15
+ assert_equal '15{"key":"value"}', str
16
+ str = serialize_message({"key" => {"nested" => "value"}})
17
+ assert_equal '26{"key":{"nested":"value"}}', str
18
+ str = serialize_message({"key" => ["value1", "value2"]})
19
+ assert_equal '27{"key":["value1","value2"]}', str
20
+
21
+ # a iso-8859-1 'ä'
22
+ str = serialize_message({"key" => "umlaut\xe4".force_encoding("binary")})
23
+ assert_equal '19{"key":"umlaut%e4"}', str
24
+ str = serialize_message({"key" => "umlaut\xe4".force_encoding("iso-8859-1")})
25
+ assert_equal '19{"key":"umlaut%e4"}', str
26
+ str = serialize_message({"key" => "umlaut\xe4".force_encoding("cp850")})
27
+ assert_equal '19{"key":"umlaut%e4"}', str
28
+ str = serialize_message({"key" => "umlaut\xe4".force_encoding("utf-8")})
29
+ assert_equal '19{"key":"umlaut%e4"}', str
30
+
31
+ # a utf-8 'ä'
32
+ str = serialize_message({"key" => "umlaut\xc3\xa4".force_encoding("binary")})
33
+ assert_equal '22{"key":"umlaut%c3%a4"}', str
34
+ str = serialize_message({"key" => "umlaut\xc3\xa4".force_encoding("iso-8859-1")})
35
+ assert_equal '22{"key":"umlaut%c3%a4"}', str
36
+ str = serialize_message({"key" => "umlaut\xc3\xa4".force_encoding("cp850")})
37
+ assert_equal '22{"key":"umlaut%c3%a4"}', str
38
+ str = serialize_message({"key" => "umlaut\xc3\xa4".force_encoding("utf-8")})
39
+ assert_equal '22{"key":"umlaut%c3%a4"}', str
40
+
41
+ # %
42
+ str = serialize_message({"key" => "%"})
43
+ assert_equal '13{"key":"%25"}', str
44
+ end
45
+
46
+ def test_extract
47
+ # specified length too short
48
+ assert_raises JSON::ParserError do
49
+ extract_message('8{"key":1}')
50
+ end
51
+ # specified length too long
52
+ assert_raises JSON::ParserError do
53
+ extract_message('10{"key":1}x')
54
+ end
55
+ # message data shorter than length specifie, waits for more data
56
+ extract_message('10{"key":1}')
57
+
58
+ obj = extract_message('9{"key":1}')
59
+ assert_equal({"key" => 1}, obj)
60
+ obj = extract_message('12{"key":true}')
61
+ assert_equal({"key" => true}, obj)
62
+ obj = extract_message('15{"key":"value"}')
63
+ assert_equal({"key" => "value"}, obj)
64
+ obj = extract_message('26{"key":{"nested":"value"}}')
65
+ assert_equal({"key" => {"nested" => "value"}}, obj)
66
+ obj = extract_message('27{"key":["value1","value2"]}')
67
+ assert_equal({"key" => ["value1", "value2"]}, obj)
68
+
69
+ # a iso-8859-1 'ä'
70
+ obj = extract_message('19{"key":"umlaut%e4"}'.force_encoding("binary"))
71
+ assert_equal "ASCII-8BIT", obj["key"].encoding.name
72
+ assert_equal "umlaut\xe4".force_encoding("ascii-8bit"), obj["key"]
73
+ obj = extract_message('19{"key":"umlaut%e4"}'.force_encoding("utf-8"))
74
+ assert_equal "ASCII-8BIT", obj["key"].encoding.name
75
+ assert_equal "umlaut\xe4".force_encoding("ascii-8bit"), obj["key"]
76
+
77
+ # a utf-8 'ä'
78
+ obj = extract_message('22{"key":"umlaut%c3%a4"}'.force_encoding("binary"))
79
+ assert_equal "ASCII-8BIT", obj["key"].encoding.name
80
+ assert_equal "umlaut\xc3\xa4".force_encoding("ascii-8bit"), obj["key"]
81
+ obj = extract_message('22{"key":"umlaut%c3%a4"}'.force_encoding("utf-8"))
82
+ assert_equal "ASCII-8BIT", obj["key"].encoding.name
83
+ assert_equal "umlaut\xc3\xa4".force_encoding("ascii-8bit"), obj["key"]
84
+
85
+ # %
86
+ obj = extract_message('13{"key":"%25"}')
87
+ assert_equal "ASCII-8BIT", obj["key"].encoding.name
88
+ assert_equal "%", obj["key"]
89
+
90
+ # invalid escape sequence
91
+ obj = extract_message('11{"key":"%"}')
92
+ assert_equal "ASCII-8BIT", obj["key"].encoding.name
93
+ assert_equal "%", obj["key"]
94
+ obj = extract_message('13{"key":"%xx"}')
95
+ assert_equal "ASCII-8BIT", obj["key"].encoding.name
96
+ assert_equal "%xx", obj["key"]
97
+ obj = extract_message('14{"key":"%%25"}')
98
+ assert_equal "ASCII-8BIT", obj["key"].encoding.name
99
+ assert_equal "%%", obj["key"]
100
+
101
+ # invalid characters (protocol should use 7-bit ascii only)
102
+ obj = extract_message(%Q(14{"key":"\xe4345"}).force_encoding("binary"))
103
+ assert_equal "ASCII-8BIT", obj["key"].encoding.name
104
+ assert_equal "?345", obj["key"]
105
+ end
106
+
107
+ def test_roundtrip
108
+ value = (0..255).collect{|b| b.chr("binary")}.join
109
+ str = serialize_message({"key" => value})
110
+ obj = extract_message(str)
111
+ assert_equal "ASCII-8BIT", obj["key"].encoding.name
112
+ assert_equal (0..255).collect{|i| i}, obj["key"].bytes.to_a
113
+ end
114
+
115
+ end
116
+