rtext 0.9.0 → 0.9.1

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