prepor-beefcake 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,446 @@
1
+ require 'minitest/autorun'
2
+ require 'beefcake'
3
+
4
+ class NumericsMessage
5
+ include Beefcake::Message
6
+
7
+ required :int32, :int32, 1
8
+ required :uint32, :uint32, 2
9
+ required :sint32, :sint32, 3
10
+ required :fixed32, :fixed32, 4
11
+ required :sfixed32, :sfixed32, 5
12
+
13
+ required :int64, :int64, 6
14
+ required :uint64, :uint64, 7
15
+ required :sint64, :sint64, 8
16
+ required :fixed64, :fixed64, 9
17
+ required :sfixed64, :sfixed64, 10
18
+ end
19
+
20
+
21
+ class LendelsMessage
22
+ include Beefcake::Message
23
+
24
+ required :string, :string, 1
25
+ required :bytes, :bytes, 2
26
+ end
27
+
28
+
29
+ class SimpleMessage
30
+ include Beefcake::Message
31
+
32
+ optional :a, :int32, 1
33
+ optional :b, :string, 2
34
+ end
35
+
36
+
37
+ class CompositeMessage
38
+ include Beefcake::Message
39
+
40
+ required :encodable, SimpleMessage, 1
41
+ end
42
+
43
+
44
+ class EnumsMessage
45
+ include Beefcake::Message
46
+
47
+ module X
48
+ A = 1
49
+ end
50
+
51
+ required :a, X, 1
52
+ end
53
+
54
+
55
+ class EnumsDefaultMessage
56
+ include Beefcake::Message
57
+
58
+ module X
59
+ A = 1
60
+ B = 2
61
+ end
62
+
63
+ optional :a, X, 1, :default => X::B
64
+ end
65
+
66
+
67
+ class RepeatedMessage
68
+ include Beefcake::Message
69
+
70
+ repeated :a, :int32, 1
71
+ end
72
+
73
+
74
+ class PackedRepeatedMessage
75
+ include Beefcake::Message
76
+
77
+ repeated :a, :int32, 1, :packed => true
78
+ end
79
+
80
+ class RepeatedNestedMessage
81
+ include Beefcake::Message
82
+
83
+ repeated :simple, SimpleMessage, 1
84
+ end
85
+
86
+ class BoolMessage
87
+ include Beefcake::Message
88
+
89
+ required :bool, :bool, 1
90
+ end
91
+
92
+ class LargeFieldNumberMessage
93
+ include Beefcake::Message
94
+
95
+ required :field_1, :string, 1
96
+ required :field_2, :string, 100
97
+ end
98
+
99
+ class FieldsMessage
100
+ include Beefcake::Message
101
+
102
+ repeated :fields, :string, 1
103
+ end
104
+
105
+ class MessageTest < Minitest::Test
106
+ B = Beefcake::Buffer
107
+
108
+ ## Encoding
109
+ def test_encode_numerics
110
+ buf = Beefcake::Buffer.new
111
+
112
+ buf.append(:int32, B::MaxInt32, 1)
113
+ buf.append(:uint32, B::MaxUint32, 2)
114
+ buf.append(:sint32, B::MinInt32, 3)
115
+ buf.append(:fixed32, B::MaxInt32, 4)
116
+ buf.append(:sfixed32, B::MinInt32, 5)
117
+ buf.append(:int64, B::MaxInt64, 6)
118
+ buf.append(:uint64, B::MaxUint64, 7)
119
+ buf.append(:sint64, B::MinInt64, 8)
120
+ buf.append(:fixed64, B::MaxInt64, 9)
121
+ buf.append(:sfixed64, B::MinInt64, 10)
122
+
123
+ msg = NumericsMessage.new({
124
+ :int32 => B::MaxInt32,
125
+ :uint32 => B::MaxUint32,
126
+ :sint32 => B::MinInt32,
127
+ :fixed32 => B::MaxInt32,
128
+ :sfixed32 => B::MinInt32,
129
+
130
+ :int64 => B::MaxInt64,
131
+ :uint64 => B::MaxUint64,
132
+ :sint64 => B::MinInt64,
133
+ :fixed64 => B::MaxInt64,
134
+ :sfixed64 => B::MinInt64
135
+ })
136
+
137
+ assert_equal buf.to_s, msg.encode.to_s
138
+ end
139
+
140
+ def test_encode_strings
141
+ buf = Beefcake::Buffer.new
142
+
143
+ buf.append(:string, "test\ning", 1)
144
+ buf.append(:bytes, "unixisawesome", 2)
145
+
146
+ msg = LendelsMessage.new({
147
+ :string => "test\ning",
148
+ :bytes => "unixisawesome"
149
+ })
150
+
151
+ assert_equal buf.to_s, msg.encode.to_s
152
+ end
153
+
154
+ def test_encode_string_composite
155
+ buf1 = Beefcake::Buffer.new
156
+ buf1.append(:int32, 123, 1)
157
+
158
+ buf2 = Beefcake::Buffer.new
159
+ buf2.append(:string, buf1, 1)
160
+
161
+ msg = CompositeMessage.new(
162
+ :encodable => SimpleMessage.new(:a => 123)
163
+ )
164
+
165
+ assert_equal buf2.to_s, msg.encode.to_s
166
+ end
167
+
168
+ def test_encode_to_string
169
+ msg = SimpleMessage.new :a => 123
170
+ str = ""
171
+ msg.encode(str)
172
+ assert_equal "\b{", str
173
+ end
174
+
175
+ def test_delimited_end_to_end
176
+ msg = SimpleMessage.new :a => 123, :b => "hi mom!"
177
+ str = ""
178
+
179
+ 1000.times do
180
+ msg.write_delimited(str)
181
+ end
182
+
183
+ 1000.times do
184
+ dec = SimpleMessage.read_delimited(str)
185
+ assert_equal msg, dec
186
+ end
187
+ end
188
+
189
+ def test_empty_buffer_delimited_read
190
+ assert_equal SimpleMessage.read_delimited(""), nil
191
+ end
192
+
193
+ def test_encode_enum
194
+ buf = Beefcake::Buffer.new
195
+ buf.append(:int32, 2, 1)
196
+
197
+ msg = EnumsMessage.new :a => EnumsMessage::X::A
198
+ assert_equal "\b\001", msg.encode.to_s
199
+ end
200
+
201
+ def test_encode_invalid_enum_value
202
+ assert_raises Beefcake::Message::InvalidValueError do
203
+ EnumsMessage.new(:a => 99).encode
204
+ end
205
+ end
206
+
207
+ def test_encode_unset_required_field
208
+ assert_raises Beefcake::Message::RequiredFieldNotSetError do
209
+ NumericsMessage.new.encode
210
+ end
211
+ end
212
+
213
+ def test_decode_required_bool
214
+ msg = BoolMessage.new :bool => false
215
+ enc = msg.encode
216
+ dec = BoolMessage.decode(enc)
217
+ assert_equal false, dec.bool
218
+ end
219
+
220
+ def test_encode_repeated_field
221
+ buf = Beefcake::Buffer.new
222
+
223
+ buf.append(:int32, 1, 1)
224
+ buf.append(:int32, 2, 1)
225
+ buf.append(:int32, 3, 1)
226
+ buf.append(:int32, 4, 1)
227
+ buf.append(:int32, 5, 1)
228
+
229
+ msg = RepeatedMessage.new :a => [1, 2, 3, 4, 5]
230
+
231
+ assert_equal buf.to_s, msg.encode.to_s
232
+ end
233
+
234
+ def test_encode_packed_repeated_field
235
+ buf = Beefcake::Buffer.new
236
+
237
+ # Varint
238
+ buf.append_info(1, 0)
239
+
240
+ # Give size in bytes
241
+ buf.append_uint64 5
242
+
243
+ # Values
244
+ buf.append_int32 1
245
+ buf.append_int32 2
246
+ buf.append_int32 3
247
+ buf.append_int32 4
248
+ buf.append_int32 5
249
+
250
+ msg = PackedRepeatedMessage.new :a => [1, 2, 3, 4, 5]
251
+
252
+ assert_equal buf.to_s, msg.encode.to_s
253
+ end
254
+
255
+ def test_encode_large_field_number
256
+ buf = Beefcake::Buffer.new
257
+ buf.append(:string, "abc", 1)
258
+ buf.append(:string, "123", 100)
259
+
260
+ msg = LargeFieldNumberMessage.new
261
+ msg.field_1 = "abc"
262
+ msg.field_2 = "123"
263
+
264
+ assert_equal buf.to_s, msg.encode.to_s
265
+ end
266
+
267
+ def test_encode_unknown_field
268
+ msg = SimpleMessage.new :mystery_field => 'asdf'
269
+ msg.encode.to_s
270
+ end
271
+
272
+ ## Decoding
273
+ def test_decode_numerics
274
+ msg = NumericsMessage.new({
275
+ :int32 => B::MaxInt32,
276
+ :uint32 => B::MaxUint32,
277
+ :sint32 => B::MinInt32,
278
+ :fixed32 => B::MaxInt32,
279
+ :sfixed32 => B::MinInt32,
280
+
281
+ :int64 => B::MaxInt64,
282
+ :uint64 => B::MaxUint64,
283
+ :sint64 => B::MinInt64,
284
+ :fixed64 => B::MaxInt64,
285
+ :sfixed64 => B::MinInt64
286
+ })
287
+
288
+ got = NumericsMessage.decode(msg.encode)
289
+
290
+ msg.__beefcake_fields__.values.each do |fld|
291
+ assert_equal msg[fld.name], got[fld.name], fld.name
292
+ end
293
+ end
294
+
295
+ def test_wire_does_not_match_decoded_info
296
+ buf = Beefcake::Buffer.new
297
+ buf.append(:string, "testing", 1)
298
+
299
+ assert_raises Beefcake::Message::WrongTypeError do
300
+ SimpleMessage.decode(buf)
301
+ end
302
+ end
303
+
304
+ def test_decode_unknown_field_number
305
+ buf = Beefcake::Buffer.new
306
+ buf.append(:string, "testing", 2)
307
+
308
+ msg = SimpleMessage.decode(buf)
309
+
310
+ assert_equal nil, msg.a
311
+ end
312
+
313
+ def test_decode_repeated_field
314
+ msg = RepeatedMessage.new :a => [1, 2, 3, 4, 5]
315
+ got = RepeatedMessage.decode(msg.encode)
316
+
317
+ assert_equal msg.a, got.a
318
+ end
319
+
320
+ def test_decode_packed_repeated_field
321
+ msg = PackedRepeatedMessage.new :a => [1, 2, 3, 4, 5]
322
+ got = PackedRepeatedMessage.decode(msg.encode)
323
+
324
+ assert_equal msg.a, got.a
325
+ end
326
+
327
+ def test_decode_merge
328
+ a = SimpleMessage.new :a => 1
329
+ b = SimpleMessage.new :a => 2
330
+
331
+ x = SimpleMessage.decode(a.encode)
332
+ SimpleMessage.decode(b.encode, x)
333
+
334
+ assert_equal b.a, x.a
335
+ end
336
+
337
+ def test_decode_default
338
+ got = EnumsDefaultMessage.decode("")
339
+ assert_equal EnumsDefaultMessage.fields[1].opts[:default], got.a
340
+ end
341
+
342
+ def test_decode_unset_required_fields
343
+ assert_raises Beefcake::Message::RequiredFieldNotSetError do
344
+ NumericsMessage.decode("")
345
+ end
346
+ end
347
+
348
+ def test_decode_enum
349
+ msg = EnumsMessage.new(:a => 1).encode
350
+ got = EnumsMessage.decode(msg)
351
+ assert_equal 1, got.a
352
+ end
353
+
354
+ def test_decode_repeated_nested
355
+ simple = [
356
+ SimpleMessage.new(:a => 1),
357
+ SimpleMessage.new(:b => "hello")
358
+ ]
359
+ msg = RepeatedNestedMessage.new(:simple => simple).encode
360
+ got = RepeatedNestedMessage.decode(msg)
361
+ assert_equal 2, got.simple.size
362
+ assert_equal 1, got.simple[0].a
363
+ assert_equal "hello", got.simple[1].b
364
+ end
365
+
366
+ def test_equality
367
+ a = SimpleMessage.new :a => 1
368
+ b = SimpleMessage.new :a => 1
369
+ assert_equal a, b
370
+ c = SimpleMessage.new :a => 2
371
+ refute_equal b, c
372
+
373
+ d = EnumsMessage.new :a => 5
374
+ e = EnumsDefaultMessage.new :a => 5
375
+
376
+ refute_equal d, e
377
+ refute_equal d, :symbol
378
+ refute_equal :symbol, d
379
+ end
380
+
381
+ def test_inspect
382
+ msg = SimpleMessage.new :a => 1
383
+ assert_equal "<SimpleMessage a: 1>", msg.inspect
384
+ msg.b = "testing"
385
+ assert_equal "<SimpleMessage a: 1, b: \"testing\">", msg.inspect
386
+ msg.a = nil
387
+ assert_equal "<SimpleMessage b: \"testing\">", msg.inspect
388
+ end
389
+
390
+ def test_inspect_enums
391
+ msg = EnumsMessage.new :a => 1
392
+ assert_equal "<EnumsMessage a: A(1)>", msg.inspect
393
+ msg.a = 2
394
+ assert_equal "<EnumsMessage a: -NA-(2)>", msg.inspect
395
+ end
396
+
397
+ def test_inspect_nested_types
398
+ msg = CompositeMessage.new(:encodable => SimpleMessage.new(:a => 1))
399
+ assert_equal "<CompositeMessage encodable: <SimpleMessage a: 1>>", msg.inspect
400
+ end
401
+
402
+ def test_decode_nested_types
403
+ msg = CompositeMessage.new(:encodable => SimpleMessage.new(:a => 1))
404
+ enc = msg.encode
405
+ dec = CompositeMessage.decode(enc)
406
+ assert_equal "<CompositeMessage encodable: <SimpleMessage a: 1>>", dec.inspect
407
+ end
408
+
409
+ def test_to_hash
410
+ msg = SimpleMessage.new :a => 1
411
+ exp = { :a => 1 }
412
+ assert_equal(exp, msg.to_hash)
413
+ end
414
+
415
+ def test_duplicate_index
416
+ assert_raises Beefcake::Message::DuplicateFieldNumber do
417
+ Class.new do
418
+ include Beefcake::Message
419
+
420
+ required :clever_name, :int32, 1
421
+ required :naughty_field, :string, 1
422
+ end
423
+ end
424
+ end
425
+
426
+ def test_bool_to_hash
427
+ true_message = BoolMessage.new :bool => true
428
+ true_expectation = { :bool => true }
429
+ assert_equal true_expectation, true_message.to_hash
430
+
431
+ false_message = BoolMessage.new :bool => false
432
+ false_expectation = { :bool => false }
433
+ assert_equal false_expectation, false_message.to_hash
434
+ end
435
+
436
+ def test_fields_named_fields
437
+ contents = %w{fields named fields}
438
+ msg = FieldsMessage.new fields: contents
439
+ assert_equal 1, msg.__beefcake_fields__.length
440
+ assert_equal contents, msg.fields
441
+
442
+ assert_equal msg, FieldsMessage.decode(msg.encode)
443
+
444
+ assert_equal "<FieldsMessage fields: [\"fields\", \"named\", \"fields\"]>", msg.inspect
445
+ end
446
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prepor-beefcake
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Blake Mizerany
8
+ - Matt Proud
9
+ - Bryce Kerley
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2015-04-04 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rake
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - "~>"
20
+ - !ruby/object:Gem::Version
21
+ version: 10.1.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: 10.1.0
29
+ - !ruby/object:Gem::Dependency
30
+ name: minitest
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - "~>"
34
+ - !ruby/object:Gem::Version
35
+ version: '5.3'
36
+ type: :development
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: '5.3'
43
+ description: A sane protobuf library for Ruby
44
+ email:
45
+ - blake.mizerany@gmail.com
46
+ - matt.proud@gmail.com
47
+ - bkerley@brycekerley.net
48
+ executables:
49
+ - protoc-gen-beefcake
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - ".gitignore"
54
+ - ".travis.yml"
55
+ - Gemfile
56
+ - LICENSE
57
+ - README.md
58
+ - RELEASE_NOTES.md
59
+ - Rakefile
60
+ - beefcake.gemspec
61
+ - bench/simple.rb
62
+ - bin/protoc-gen-beefcake
63
+ - dat/code_generator_request.dat
64
+ - lib/beefcake.rb
65
+ - lib/beefcake/buffer.rb
66
+ - lib/beefcake/buffer/base.rb
67
+ - lib/beefcake/buffer/decode.rb
68
+ - lib/beefcake/buffer/encode.rb
69
+ - lib/beefcake/generator.rb
70
+ - lib/beefcake/version.rb
71
+ - test/benchmark_test.rb
72
+ - test/buffer_decode_test.rb
73
+ - test/buffer_encode_test.rb
74
+ - test/buffer_test.rb
75
+ - test/generator_test.rb
76
+ - test/message_test.rb
77
+ homepage: https://github.com/protobuf-ruby/beefcake
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 1.9.3
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.4.3
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: A sane protobuf library for Ruby
101
+ test_files:
102
+ - test/benchmark_test.rb
103
+ - test/buffer_decode_test.rb
104
+ - test/buffer_encode_test.rb
105
+ - test/buffer_test.rb
106
+ - test/generator_test.rb
107
+ - test/message_test.rb