beefcake-spanx 0.3.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ module Beefcake
2
+ VERSION = "0.3.4.1"
3
+ end
@@ -0,0 +1,114 @@
1
+ require 'beefcake/buffer'
2
+
3
+ class BufferDecodeTest < Test::Unit::TestCase
4
+
5
+ B = Beefcake::Buffer
6
+
7
+ def setup
8
+ @buf = B.new
9
+ end
10
+
11
+ def test_read_info
12
+ @buf.append_info(1, 2)
13
+ assert_equal [1, 2], @buf.read_info
14
+
15
+ @buf.append_info(2, 5)
16
+ assert_equal [2, 5], @buf.read_info
17
+ end
18
+
19
+ def test_read_string
20
+ @buf.append_string("testing")
21
+ assert_equal "testing", @buf.read_string
22
+ end
23
+
24
+ def test_read_fixed32
25
+ @buf.append_fixed32(123)
26
+ assert_equal 123, @buf.read_fixed32
27
+ end
28
+
29
+ def test_read_fixed64
30
+ @buf.append_fixed64(456)
31
+ assert_equal 456, @buf.read_fixed64
32
+ end
33
+
34
+ def test_read_uint32
35
+ @buf.append_uint32(1)
36
+ assert_equal 1, @buf.read_uint32
37
+ end
38
+
39
+ def test_read_int32
40
+ @buf.append_int32(999)
41
+ assert_equal 999, @buf.read_int32
42
+
43
+ @buf.append_int32(-999)
44
+ assert_equal -999, @buf.read_int32
45
+ end
46
+
47
+ def test_read_int64
48
+ @buf.append_int64(999)
49
+ assert_equal 999, @buf.read_int64
50
+
51
+ @buf.append_int64(-999)
52
+ assert_equal -999, @buf.read_int64
53
+ end
54
+
55
+ def test_read_uint64
56
+ @buf.append_uint64(1)
57
+ assert_equal 1, @buf.read_uint64
58
+ end
59
+
60
+ def test_read_float
61
+ @buf.append_float(0.5)
62
+ assert_equal 0.5, @buf.read_float
63
+ end
64
+
65
+ def test_read_double
66
+ @buf.append_double(Math::PI)
67
+ assert_equal Math::PI, @buf.read_double
68
+ end
69
+
70
+ def test_read_bool
71
+ @buf.append_bool(true)
72
+ assert_equal true, @buf.read_bool
73
+
74
+ @buf.append_bool(false)
75
+ assert_equal false, @buf.read_bool
76
+ end
77
+
78
+ def test_read_sint32
79
+ @buf.append_sint32(B::MinInt32)
80
+ assert_equal B::MinInt32, @buf.read_sint32
81
+
82
+ @buf.buf = ""
83
+ @buf.append_sint32(B::MaxInt32)
84
+ assert_equal B::MaxInt32, @buf.read_sint32
85
+ end
86
+
87
+ def test_read_sfixed32
88
+ @buf.append_sfixed32(B::MinInt32)
89
+ assert_equal B::MinInt32, @buf.read_sfixed32
90
+
91
+ @buf.buf = ""
92
+ @buf.append_sfixed32(B::MaxInt32)
93
+ assert_equal B::MaxInt32, @buf.read_sfixed32
94
+ end
95
+
96
+ def test_read_sint64
97
+ @buf.append_sint64(B::MinInt64)
98
+ assert_equal B::MinInt64, @buf.read_sint64
99
+
100
+ @buf.buf = ""
101
+ @buf.append_sint64(B::MaxInt64)
102
+ assert_equal B::MaxInt64, @buf.read_sint64
103
+ end
104
+
105
+ def test_read_sfixed64
106
+ @buf.append_sfixed64(B::MinInt64)
107
+ assert_equal B::MinInt64, @buf.read_sfixed64
108
+
109
+ @buf.buf = ""
110
+ @buf.append_sfixed64(B::MaxInt64)
111
+ assert_equal B::MaxInt64, @buf.read_sfixed64
112
+ end
113
+
114
+ end
@@ -0,0 +1,222 @@
1
+ require 'beefcake/buffer/encode'
2
+
3
+ class BufferEncodeTest < Test::Unit::TestCase
4
+
5
+ B = Beefcake::Buffer
6
+
7
+ def setup
8
+ @buf = B.new
9
+ end
10
+
11
+ def test_append_info
12
+ @buf.append_info(1, 0)
13
+ assert_equal "\010", @buf.to_s
14
+
15
+ @buf.buf = ""
16
+ @buf.append_info(2, 1)
17
+ assert_equal "\021", @buf.to_s
18
+ end
19
+
20
+ def test_append_string
21
+ @buf.append_string("testing")
22
+ assert_equal "\007testing", @buf.to_s
23
+ end
24
+
25
+ def test_append_fixed32
26
+ @buf.append_fixed32(1)
27
+ assert_equal "\001\0\0\0", @buf.to_s
28
+
29
+ @buf.buf = ""
30
+ @buf.append_fixed32(B::MinUint32)
31
+ assert_equal "\0\0\0\0", @buf.to_s
32
+
33
+ @buf.buf = ""
34
+ @buf.append_fixed32(B::MaxUint32)
35
+ assert_equal "\377\377\377\377", @buf.to_s
36
+
37
+ assert_raises B::OutOfRangeError do
38
+ @buf.append_fixed32(B::MinUint32 - 1)
39
+ end
40
+
41
+ assert_raises B::OutOfRangeError do
42
+ @buf.append_fixed32(B::MaxUint32 + 1)
43
+ end
44
+ end
45
+
46
+ def test_append_fixed64
47
+ @buf.append_fixed64(1)
48
+ assert_equal "\001\0\0\0\0\0\0\0", @buf.to_s
49
+
50
+ @buf.buf = ""
51
+ @buf.append_fixed64(B::MinUint64)
52
+ assert_equal "\000\0\0\0\0\0\0\0", @buf.to_s
53
+
54
+ @buf.buf = ""
55
+ @buf.append_fixed64(B::MaxUint64)
56
+ assert_equal "\377\377\377\377\377\377\377\377", @buf.to_s
57
+
58
+ assert_raises B::OutOfRangeError do
59
+ @buf.append_fixed64(B::MinUint64 - 1)
60
+ end
61
+
62
+ assert_raises B::OutOfRangeError do
63
+ @buf.append_fixed64(B::MaxUint64 + 1)
64
+ end
65
+ end
66
+
67
+ def test_append_uint32
68
+ @buf.append_uint32(1)
69
+ assert_equal "\001", @buf.to_s
70
+
71
+ @buf.buf = ""
72
+ @buf.append_uint32(B::MinUint32)
73
+ assert_equal "\000", @buf.to_s
74
+
75
+ @buf.buf = ""
76
+ @buf.append_uint32(B::MaxUint32)
77
+ assert_equal "\377\377\377\377\017", @buf.to_s
78
+
79
+ assert_raises B::OutOfRangeError do
80
+ @buf.append_uint32(B::MinUint32 - 1)
81
+ end
82
+
83
+ assert_raises B::OutOfRangeError do
84
+ @buf.append_uint32(B::MaxUint32 + 1)
85
+ end
86
+ end
87
+
88
+ def test_append_int32
89
+ @buf.append_int32(1)
90
+ assert_equal "\001", @buf.to_s
91
+
92
+ @buf.buf = ""
93
+ @buf.append_int32(-1)
94
+ assert_equal "\377\377\377\377\377\377\377\377\377\001", @buf.to_s
95
+
96
+ @buf.buf = ""
97
+ @buf.append_int32(B::MinInt32)
98
+ assert_equal "\200\200\200\200\370\377\377\377\377\001", @buf.to_s
99
+
100
+ @buf.buf = ""
101
+ @buf.append_int32(B::MaxInt32)
102
+ assert_equal "\377\377\377\377\007", @buf.to_s
103
+
104
+ assert_raises B::OutOfRangeError do
105
+ @buf.append_int32(B::MinInt32 - 1)
106
+ end
107
+
108
+ assert_raises B::OutOfRangeError do
109
+ @buf.append_int32(B::MaxInt32 + 1)
110
+ end
111
+ end
112
+
113
+ def test_append_int64
114
+ @buf.append_int64(1)
115
+ assert_equal "\001", @buf.to_s
116
+
117
+ @buf.buf = ""
118
+ @buf.append_int64(-1)
119
+ assert_equal "\377\377\377\377\377\377\377\377\377\001", @buf.to_s
120
+
121
+ @buf.buf = ""
122
+ @buf.append_int64(B::MinInt64)
123
+ assert_equal "\200\200\200\200\200\200\200\200\200\001", @buf.to_s
124
+
125
+ @buf.buf = ""
126
+ @buf.append_int64(B::MaxInt64)
127
+ assert_equal "\377\377\377\377\377\377\377\377\177", @buf.to_s
128
+
129
+ assert_raises B::OutOfRangeError do
130
+ @buf.append_int64(B::MinInt64 - 1)
131
+ end
132
+
133
+ assert_raises B::OutOfRangeError do
134
+ @buf.append_int64(B::MaxInt64 + 1)
135
+ end
136
+ end
137
+
138
+ def test_append_uint64
139
+ @buf.append_uint64(1)
140
+ assert_equal "\001", @buf.to_s
141
+
142
+ @buf.buf = ""
143
+ @buf.append_uint64(B::MinUint64)
144
+ assert_equal "\000", @buf.to_s
145
+
146
+ @buf.buf = ""
147
+ @buf.append_uint64(B::MaxUint64)
148
+ assert_equal "\377\377\377\377\377\377\377\377\377\001", @buf.to_s
149
+
150
+ assert_raises B::OutOfRangeError do
151
+ @buf.append_uint64(B::MinUint64 - 1)
152
+ end
153
+
154
+ assert_raises B::OutOfRangeError do
155
+ @buf.append_uint64(B::MaxUint64 + 1)
156
+ end
157
+ end
158
+
159
+ def test_append_float
160
+ @buf.append_float(3.14)
161
+ assert_equal "\303\365H@", @buf.to_s
162
+ end
163
+
164
+ def test_append_double
165
+ @buf.buf = ""
166
+ @buf.append_float(Math::PI)
167
+ assert_equal "\333\017I@", @buf.to_s
168
+ end
169
+
170
+ def test_append_bool
171
+ @buf.append_bool(true)
172
+ @buf.append_bool(false)
173
+ assert_equal "\001\000", @buf.to_s
174
+ end
175
+
176
+ def test_append_sint32
177
+ @buf.append_sint32(-2)
178
+ assert_equal "\003", @buf.to_s
179
+
180
+ @buf.buf = ""
181
+ @buf.append_sint32(B::MaxInt32)
182
+ assert_equal "\376\377\377\377\017", @buf.to_s
183
+
184
+ @buf.buf = ""
185
+ @buf.append_sint32(B::MinInt32)
186
+ assert_equal "\377\377\377\377\017", @buf.to_s
187
+ end
188
+
189
+ def test_append_sfixed32
190
+ @buf.append_sfixed32(-2)
191
+ assert_equal "\003\000\000\000", @buf.to_s
192
+ end
193
+
194
+ def test_append_sint64
195
+ @buf.append_sint64(-2)
196
+ assert_equal "\003", @buf.to_s
197
+
198
+ @buf.buf = ""
199
+ @buf.append_sint64(B::MaxInt64)
200
+ assert_equal "\376\377\377\377\377\377\377\377\377\001", @buf.to_s
201
+
202
+ @buf.buf = ""
203
+ @buf.append_sint64(B::MinInt64)
204
+ assert_equal "\377\377\377\377\377\377\377\377\377\001", @buf.to_s
205
+ end
206
+
207
+ def test_append_sfixed64
208
+ @buf.append_sfixed64(B::MaxInt64)
209
+ assert_equal "\376\377\377\377\377\377\377\377", @buf.to_s
210
+ end
211
+
212
+ def test_append_string
213
+ @buf.append_string("testing")
214
+ assert_equal "\007testing", @buf.to_s
215
+ end
216
+
217
+ def test_append_bytes
218
+ @buf.append_bytes("testing")
219
+ assert_equal "\007testing", @buf.to_s
220
+ end
221
+
222
+ end
@@ -0,0 +1,44 @@
1
+ require 'beefcake/buffer'
2
+
3
+ class BufferTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @buf = Beefcake::Buffer.new
7
+ end
8
+
9
+ def test_simple
10
+ assert_equal "", @buf.to_s
11
+
12
+ @buf << "asdf"
13
+ assert_equal "asdf", @buf.to_s
14
+ assert_equal "asdf", @buf.to_str
15
+
16
+ @buf.buf = ""
17
+ assert_equal "", @buf.to_s
18
+ end
19
+
20
+ def test_wire_for
21
+ assert_equal 0, Beefcake::Buffer.wire_for(:int32)
22
+ assert_equal 0, Beefcake::Buffer.wire_for(:uint32)
23
+ assert_equal 0, Beefcake::Buffer.wire_for(:sint32)
24
+ assert_equal 0, Beefcake::Buffer.wire_for(:int64)
25
+ assert_equal 0, Beefcake::Buffer.wire_for(:uint64)
26
+ assert_equal 0, Beefcake::Buffer.wire_for(:sint64)
27
+
28
+ assert_equal 1, Beefcake::Buffer.wire_for(:fixed64)
29
+ assert_equal 1, Beefcake::Buffer.wire_for(:sfixed64)
30
+ assert_equal 1, Beefcake::Buffer.wire_for(:double)
31
+
32
+ assert_equal 2, Beefcake::Buffer.wire_for(:string)
33
+ assert_equal 2, Beefcake::Buffer.wire_for(:bytes)
34
+
35
+ assert_equal 5, Beefcake::Buffer.wire_for(:fixed32)
36
+ assert_equal 5, Beefcake::Buffer.wire_for(:sfixed32)
37
+ assert_equal 5, Beefcake::Buffer.wire_for(:float)
38
+
39
+ assert_raises Beefcake::Buffer::UnknownType do
40
+ Beefcake::Buffer.wire_for(:asdf)
41
+ end
42
+ end
43
+
44
+ end
@@ -0,0 +1,51 @@
1
+ require 'beefcake/generator'
2
+
3
+ class GeneratorTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ # Load up the generator request for the addressbook.proto example
7
+ dat = File.dirname(__FILE__) + "/../dat/code_generator_request.dat"
8
+ mock_request = File.read(dat)
9
+ @req = CodeGeneratorRequest.decode(mock_request)
10
+ end
11
+
12
+ if "".respond_to?(:encoding)
13
+ def test_request_has_filenames_as_binary
14
+ @req.proto_file.each do |file|
15
+ assert_equal Encoding.find("ASCII-8BIT"), file.name.encoding
16
+ end
17
+ end
18
+ end
19
+
20
+ def test_generate_empty_namespace
21
+ @res = Beefcake::Generator.compile([], @req)
22
+ assert_equal(CodeGeneratorResponse, @res.class)
23
+ end
24
+
25
+ def test_generate_top_namespace
26
+ @res = Beefcake::Generator.compile(["Top"], @req)
27
+ assert_equal(CodeGeneratorResponse, @res.class)
28
+ assert_match(/module Top/, @res.file.first.content)
29
+ end
30
+
31
+ def test_generate_two_level_namespace
32
+ @res = Beefcake::Generator.compile(["Top", "Bottom"], @req)
33
+ assert_equal(CodeGeneratorResponse, @res.class)
34
+ assert_match(/module Top\s*\n\s*module Bottom/m, @res.file.first.content)
35
+ end
36
+
37
+ # Covers the regression of encoding a CodeGeneratorResponse under 1.9.2-p136 raising
38
+ # Encoding::CompatibilityError: incompatible character encodings: ASCII-8BIT and US-ASCII
39
+ def test_encode_decode_generated_response
40
+ @res = Beefcake::Generator.compile([], @req)
41
+ assert_nothing_raised { @res.encode }
42
+ end
43
+
44
+ def test_encode_decode_generated_response
45
+ @res = Beefcake::Generator.compile([], @req)
46
+ assert_equal(CodeGeneratorResponse, @res.class)
47
+
48
+ round_trip = CodeGeneratorResponse.decode(@res.encode)
49
+ assert_equal round_trip, @res
50
+ end
51
+ end
@@ -0,0 +1,371 @@
1
+ require 'beefcake'
2
+
3
+ class NumericsMessage
4
+ include Beefcake::Message
5
+
6
+ required :int32, :int32, 1
7
+ required :uint32, :uint32, 2
8
+ required :sint32, :sint32, 3
9
+ required :fixed32, :fixed32, 4
10
+ required :sfixed32, :sfixed32, 5
11
+
12
+ required :int64, :int64, 6
13
+ required :uint64, :uint64, 7
14
+ required :sint64, :sint64, 8
15
+ required :fixed64, :fixed64, 9
16
+ required :sfixed64, :sfixed64, 10
17
+ end
18
+
19
+
20
+ class LendelsMessage
21
+ include Beefcake::Message
22
+
23
+ required :string, :string, 1
24
+ required :bytes, :bytes, 2
25
+ end
26
+
27
+
28
+ class SimpleMessage
29
+ include Beefcake::Message
30
+
31
+ optional :a, :int32, 1
32
+ optional :b, :string, 2
33
+ end
34
+
35
+
36
+ class CompositeMessage
37
+ include Beefcake::Message
38
+
39
+ required :encodable, SimpleMessage, 1
40
+ end
41
+
42
+
43
+ class EnumsMessage
44
+ include Beefcake::Message
45
+
46
+ module X
47
+ A = 1
48
+ end
49
+
50
+ required :a, X, 1
51
+ end
52
+
53
+
54
+ class EnumsDefaultMessage
55
+ include Beefcake::Message
56
+
57
+ module X
58
+ A = 1
59
+ B = 2
60
+ end
61
+
62
+ optional :a, X, 1, :default => X::B
63
+ end
64
+
65
+
66
+ class RepeatedMessage
67
+ include Beefcake::Message
68
+
69
+ repeated :a, :int32, 1
70
+ end
71
+
72
+
73
+ class PackedRepeatedMessage
74
+ include Beefcake::Message
75
+
76
+ repeated :a, :int32, 1, :packed => true
77
+ end
78
+
79
+ class RepeatedNestedMessage
80
+ include Beefcake::Message
81
+
82
+ repeated :simple, SimpleMessage, 1
83
+ end
84
+
85
+ class BoolMessage
86
+ include Beefcake::Message
87
+
88
+ required :bool, :bool, 1
89
+ end
90
+
91
+ class LargeFieldNumberMessage
92
+ include Beefcake::Message
93
+
94
+ required :field_1, :string, 1
95
+ required :field_2, :string, 100
96
+ end
97
+
98
+ class MessageTest < Test::Unit::TestCase
99
+ B = Beefcake::Buffer
100
+
101
+ ## Encoding
102
+ def test_encode_numerics
103
+ buf = Beefcake::Buffer.new
104
+
105
+ buf.append(:int32, B::MaxInt32, 1)
106
+ buf.append(:uint32, B::MaxUint32, 2)
107
+ buf.append(:sint32, B::MinInt32, 3)
108
+ buf.append(:fixed32, B::MaxInt32, 4)
109
+ buf.append(:sfixed32, B::MinInt32, 5)
110
+ buf.append(:int64, B::MaxInt64, 6)
111
+ buf.append(:uint64, B::MaxUint64, 7)
112
+ buf.append(:sint64, B::MinInt64, 8)
113
+ buf.append(:fixed64, B::MaxInt64, 9)
114
+ buf.append(:sfixed64, B::MinInt64, 10)
115
+
116
+ msg = NumericsMessage.new({
117
+ :int32 => B::MaxInt32,
118
+ :uint32 => B::MaxUint32,
119
+ :sint32 => B::MinInt32,
120
+ :fixed32 => B::MaxInt32,
121
+ :sfixed32 => B::MinInt32,
122
+
123
+ :int64 => B::MaxInt64,
124
+ :uint64 => B::MaxUint64,
125
+ :sint64 => B::MinInt64,
126
+ :fixed64 => B::MaxInt64,
127
+ :sfixed64 => B::MinInt64
128
+ })
129
+
130
+ assert_equal buf.to_s, msg.encode.to_s
131
+ end
132
+
133
+ def test_encode_strings
134
+ buf = Beefcake::Buffer.new
135
+
136
+ buf.append(:string, "test\ning", 1)
137
+ buf.append(:bytes, "unixisawesome", 2)
138
+
139
+ msg = LendelsMessage.new({
140
+ :string => "test\ning",
141
+ :bytes => "unixisawesome"
142
+ })
143
+
144
+ assert_equal buf.to_s, msg.encode.to_s
145
+ end
146
+
147
+ def test_encode_string_composite
148
+ buf1 = Beefcake::Buffer.new
149
+ buf1.append(:int32, 123, 1)
150
+
151
+ buf2 = Beefcake::Buffer.new
152
+ buf2.append(:string, buf1, 1)
153
+
154
+ msg = CompositeMessage.new(
155
+ :encodable => SimpleMessage.new(:a => 123)
156
+ )
157
+
158
+ assert_equal buf2.to_s, msg.encode.to_s
159
+ end
160
+
161
+ def test_encode_to_string
162
+ msg = SimpleMessage.new :a => 123
163
+ str = ""
164
+ msg.encode(str)
165
+ assert_equal "\b{", str
166
+ end
167
+
168
+ def test_encode_enum
169
+ buf = Beefcake::Buffer.new
170
+ buf.append(:int32, 2, 1)
171
+
172
+ msg = EnumsMessage.new :a => EnumsMessage::X::A
173
+ assert_equal "\b\001", msg.encode.to_s
174
+ end
175
+
176
+ def test_encode_invalid_enum_value
177
+ assert_raises Beefcake::Message::InvalidValueError do
178
+ EnumsMessage.new(:a => 99).encode
179
+ end
180
+ end
181
+
182
+ def test_encode_unset_required_field
183
+ assert_raises Beefcake::Message::RequiredFieldNotSetError do
184
+ NumericsMessage.new.encode
185
+ end
186
+ end
187
+
188
+ def test_decode_required_bool
189
+ msg = BoolMessage.new :bool => false
190
+ enc = msg.encode
191
+ dec = BoolMessage.decode(enc)
192
+ assert_equal false, dec.bool
193
+ end
194
+
195
+ def test_encode_repeated_field
196
+ buf = Beefcake::Buffer.new
197
+
198
+ buf.append(:int32, 1, 1)
199
+ buf.append(:int32, 2, 1)
200
+ buf.append(:int32, 3, 1)
201
+ buf.append(:int32, 4, 1)
202
+ buf.append(:int32, 5, 1)
203
+
204
+ msg = RepeatedMessage.new :a => [1, 2, 3, 4, 5]
205
+
206
+ assert_equal buf.to_s, msg.encode.to_s
207
+ end
208
+
209
+ def test_encode_packed_repeated_field
210
+ buf = Beefcake::Buffer.new
211
+
212
+ # Varint
213
+ buf.append_info(1, 0)
214
+
215
+ # Give size in bytes
216
+ buf.append_uint64 5
217
+
218
+ # Values
219
+ buf.append_int32 1
220
+ buf.append_int32 2
221
+ buf.append_int32 3
222
+ buf.append_int32 4
223
+ buf.append_int32 5
224
+
225
+ msg = PackedRepeatedMessage.new :a => [1, 2, 3, 4, 5]
226
+
227
+ assert_equal buf.to_s, msg.encode.to_s
228
+ end
229
+
230
+ def test_encode_large_field_number
231
+ buf = Beefcake::Buffer.new
232
+ buf.append(:string, "abc", 1)
233
+ buf.append(:string, "123", 100)
234
+
235
+ msg = LargeFieldNumberMessage.new
236
+ msg.field_1 = "abc"
237
+ msg.field_2 = "123"
238
+
239
+ assert_equal buf.to_s, msg.encode.to_s
240
+ end
241
+
242
+ ## Decoding
243
+ def test_decode_numerics
244
+ msg = NumericsMessage.new({
245
+ :int32 => B::MaxInt32,
246
+ :uint32 => B::MaxUint32,
247
+ :sint32 => B::MinInt32,
248
+ :fixed32 => B::MaxInt32,
249
+ :sfixed32 => B::MinInt32,
250
+
251
+ :int64 => B::MaxInt64,
252
+ :uint64 => B::MaxUint64,
253
+ :sint64 => B::MinInt64,
254
+ :fixed64 => B::MaxInt64,
255
+ :sfixed64 => B::MinInt64
256
+ })
257
+
258
+ got = NumericsMessage.decode(msg.encode)
259
+
260
+ msg.fields.values.each do |fld|
261
+ assert_equal msg[fld.name], got[fld.name], fld.name
262
+ end
263
+ end
264
+
265
+ def test_wire_does_not_match_decoded_info
266
+ buf = Beefcake::Buffer.new
267
+ buf.append(:string, "testing", 1)
268
+
269
+ assert_raises Beefcake::Message::WrongTypeError do
270
+ SimpleMessage.decode(buf)
271
+ end
272
+ end
273
+
274
+ def test_decode_unknown_field_number
275
+ buf = Beefcake::Buffer.new
276
+ buf.append(:string, "testing", 2)
277
+
278
+ msg = SimpleMessage.decode(buf)
279
+
280
+ assert_equal nil, msg.a
281
+ end
282
+
283
+ def test_decode_repeated_field
284
+ msg = RepeatedMessage.new :a => [1, 2, 3, 4, 5]
285
+ got = RepeatedMessage.decode(msg.encode)
286
+
287
+ assert_equal msg.a, got.a
288
+ end
289
+
290
+ def test_decode_packed_repeated_field
291
+ msg = PackedRepeatedMessage.new :a => [1, 2, 3, 4, 5]
292
+ got = PackedRepeatedMessage.decode(msg.encode)
293
+
294
+ assert_equal msg.a, got.a
295
+ end
296
+
297
+ def test_decode_merge
298
+ a = SimpleMessage.new :a => 1
299
+ b = SimpleMessage.new :a => 2
300
+
301
+ x = SimpleMessage.decode(a.encode)
302
+ SimpleMessage.decode(b.encode, x)
303
+
304
+ assert_equal b.a, x.a
305
+ end
306
+
307
+ def test_decode_default
308
+ got = EnumsDefaultMessage.decode("")
309
+ assert_equal EnumsDefaultMessage.fields[1].opts[:default], got.a
310
+ end
311
+
312
+ def test_decode_unset_required_fields
313
+ assert_raises Beefcake::Message::RequiredFieldNotSetError do
314
+ NumericsMessage.decode("")
315
+ end
316
+ end
317
+
318
+ def test_decode_enum
319
+ msg = EnumsMessage.new(:a => 1).encode
320
+ got = EnumsMessage.decode(msg)
321
+ assert_equal 1, got.a
322
+ end
323
+
324
+ def test_decode_repeated_nested
325
+ simple = [
326
+ SimpleMessage.new(:a => 1),
327
+ SimpleMessage.new(:b => "hello")
328
+ ]
329
+ msg = RepeatedNestedMessage.new(:simple => simple).encode
330
+ got = RepeatedNestedMessage.decode(msg)
331
+ assert_equal 2, got.simple.size
332
+ assert_equal 1, got.simple[0].a
333
+ assert_equal "hello", got.simple[1].b
334
+ end
335
+
336
+ def test_equality
337
+ a = SimpleMessage.new :a => 1
338
+ b = SimpleMessage.new :a => 1
339
+ assert_equal a, b
340
+ c = SimpleMessage.new :a => 2
341
+ assert_not_equal b, c
342
+ end
343
+
344
+ def test_inspect
345
+ msg = SimpleMessage.new :a => 1
346
+ assert_equal "<SimpleMessage a: 1>", msg.inspect
347
+ msg.b = "testing"
348
+ assert_equal "<SimpleMessage a: 1, b: \"testing\">", msg.inspect
349
+ msg.a = nil
350
+ assert_equal "<SimpleMessage b: \"testing\">", msg.inspect
351
+ end
352
+
353
+ def test_inspect_enums
354
+ msg = EnumsMessage.new :a => 1
355
+ assert_equal "<EnumsMessage a: A(1)>", msg.inspect
356
+ msg.a = 2
357
+ assert_equal "<EnumsMessage a: -NA-(2)>", msg.inspect
358
+ end
359
+
360
+ def test_inspect_nested_types
361
+ msg = CompositeMessage.new(:encodable => SimpleMessage.new(:a => 1))
362
+ assert_equal "<CompositeMessage encodable: <SimpleMessage a: 1>>", msg.inspect
363
+ end
364
+
365
+ def test_to_hash
366
+ msg = SimpleMessage.new :a => 1
367
+ exp = { :a => 1 }
368
+ assert_equal(exp, msg.to_hash)
369
+ end
370
+
371
+ end