prepor-beefcake 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ module Beefcake
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,42 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/benchmark'
3
+ require 'beefcake'
4
+
5
+ class Inner
6
+ include Beefcake::Message
7
+
8
+ required :content, :string, 1
9
+ end
10
+
11
+ class Outer
12
+ include Beefcake::Message
13
+
14
+ repeated :inner, Inner, 1
15
+ end
16
+
17
+ class BenchmarkTest < Minitest::Benchmark
18
+ def setup
19
+ @buf = Beefcake::Buffer.new
20
+ end
21
+
22
+ def bench_read_string
23
+ assert_performance_linear 0.75 do |n|
24
+ candidate = 'x' * (n * 1_000)
25
+ @buf.append_string candidate
26
+ assert_equal candidate, @buf.read_string
27
+ end
28
+ end
29
+
30
+ def bench_decode
31
+ assert_performance_linear 0.75 do |n|
32
+ inners = n.times.map{ Inner.new :content => 'x' * 100 }
33
+ outer = Outer.new :inner => inners
34
+
35
+ encoded = outer.encode.to_s
36
+
37
+ decoded = Outer.decode encoded
38
+
39
+ assert_equal n, decoded.inner.length
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,117 @@
1
+ require 'minitest/autorun'
2
+ require 'beefcake/buffer'
3
+
4
+ class BufferDecodeTest < Minitest::Test
5
+
6
+ B = Beefcake::Buffer
7
+
8
+ def setup
9
+ @buf = B.new
10
+ end
11
+
12
+ def test_read_info
13
+ @buf.append_info(1, 2)
14
+ assert_equal [1, 2], @buf.read_info
15
+
16
+ @buf.append_info(2, 5)
17
+ assert_equal [2, 5], @buf.read_info
18
+ end
19
+
20
+ def test_read_string
21
+ @buf.append_string("testing")
22
+ decoded = @buf.read_string
23
+ assert_equal "testing", decoded
24
+ assert_equal Encoding.find('utf-8'), decoded.encoding
25
+ end
26
+
27
+ def test_read_fixed32
28
+ @buf.append_fixed32(123)
29
+ assert_equal 123, @buf.read_fixed32
30
+ end
31
+
32
+ def test_read_fixed64
33
+ @buf.append_fixed64(456)
34
+ assert_equal 456, @buf.read_fixed64
35
+ end
36
+
37
+ def test_read_uint32
38
+ @buf.append_uint32(1)
39
+ assert_equal 1, @buf.read_uint32
40
+ end
41
+
42
+ def test_read_int32
43
+ @buf.append_int32(999)
44
+ assert_equal 999, @buf.read_int32
45
+
46
+ @buf.append_int32(-999)
47
+ assert_equal -999, @buf.read_int32
48
+ end
49
+
50
+ def test_read_int64
51
+ @buf.append_int64(999)
52
+ assert_equal 999, @buf.read_int64
53
+
54
+ @buf.append_int64(-999)
55
+ assert_equal -999, @buf.read_int64
56
+ end
57
+
58
+ def test_read_uint64
59
+ @buf.append_uint64(1)
60
+ assert_equal 1, @buf.read_uint64
61
+ end
62
+
63
+ def test_read_float
64
+ @buf.append_float(0.5)
65
+ assert_equal 0.5, @buf.read_float
66
+ end
67
+
68
+ def test_read_double
69
+ @buf.append_double(Math::PI)
70
+ assert_equal Math::PI, @buf.read_double
71
+ end
72
+
73
+ def test_read_bool
74
+ @buf.append_bool(true)
75
+ assert_equal true, @buf.read_bool
76
+
77
+ @buf.append_bool(false)
78
+ assert_equal false, @buf.read_bool
79
+ end
80
+
81
+ def test_read_sint32
82
+ @buf.append_sint32(B::MinInt32)
83
+ assert_equal B::MinInt32, @buf.read_sint32
84
+
85
+ @buf.buf = ""
86
+ @buf.append_sint32(B::MaxInt32)
87
+ assert_equal B::MaxInt32, @buf.read_sint32
88
+ end
89
+
90
+ def test_read_sfixed32
91
+ @buf.append_sfixed32(B::MinInt32)
92
+ assert_equal B::MinInt32, @buf.read_sfixed32
93
+
94
+ @buf.buf = ""
95
+ @buf.append_sfixed32(B::MaxInt32)
96
+ assert_equal B::MaxInt32, @buf.read_sfixed32
97
+ end
98
+
99
+ def test_read_sint64
100
+ @buf.append_sint64(B::MinInt64)
101
+ assert_equal B::MinInt64, @buf.read_sint64
102
+
103
+ @buf.buf = ""
104
+ @buf.append_sint64(B::MaxInt64)
105
+ assert_equal B::MaxInt64, @buf.read_sint64
106
+ end
107
+
108
+ def test_read_sfixed64
109
+ @buf.append_sfixed64(B::MinInt64)
110
+ assert_equal B::MinInt64, @buf.read_sfixed64
111
+
112
+ @buf.buf = ""
113
+ @buf.append_sfixed64(B::MaxInt64)
114
+ assert_equal B::MaxInt64, @buf.read_sfixed64
115
+ end
116
+
117
+ end
@@ -0,0 +1,234 @@
1
+ # encoding: ASCII-8BIT
2
+ require 'minitest/autorun'
3
+ require 'beefcake/buffer/encode'
4
+
5
+ class BufferEncodeTest < Minitest::Test
6
+
7
+ B = Beefcake::Buffer
8
+
9
+ def setup
10
+ @buf = B.new
11
+ end
12
+
13
+ def test_append_info
14
+ @buf.append_info(1, 0)
15
+ assert_equal "\010", @buf.to_s
16
+
17
+ @buf.buf = ""
18
+ @buf.append_info(2, 1)
19
+ assert_equal "\021", @buf.to_s
20
+ end
21
+
22
+ def test_append_string
23
+ @buf.append_string("testing")
24
+ assert_equal "\007testing", @buf.to_s
25
+ end
26
+
27
+ def test_append_fixed32
28
+ @buf.append_fixed32(1)
29
+ assert_equal "\001\0\0\0", @buf.to_s
30
+
31
+ @buf.buf = ""
32
+ @buf.append_fixed32(B::MinUint32)
33
+ assert_equal "\0\0\0\0", @buf.to_s
34
+
35
+ @buf.buf = ""
36
+ @buf.append_fixed32(B::MaxUint32)
37
+ assert_equal "\377\377\377\377", @buf.to_s
38
+
39
+ assert_raises B::OutOfRangeError do
40
+ @buf.append_fixed32(B::MinUint32 - 1)
41
+ end
42
+
43
+ assert_raises B::OutOfRangeError do
44
+ @buf.append_fixed32(B::MaxUint32 + 1)
45
+ end
46
+ end
47
+
48
+ def test_append_fixed64
49
+ @buf.append_fixed64(1)
50
+ assert_equal "\001\0\0\0\0\0\0\0", @buf.to_s
51
+
52
+ @buf.buf = ""
53
+ @buf.append_fixed64(B::MinUint64)
54
+ assert_equal "\000\0\0\0\0\0\0\0", @buf.to_s
55
+
56
+ @buf.buf = ""
57
+ @buf.append_fixed64(B::MaxUint64)
58
+ assert_equal "\377\377\377\377\377\377\377\377", @buf.to_s
59
+
60
+ assert_raises B::OutOfRangeError do
61
+ @buf.append_fixed64(B::MinUint64 - 1)
62
+ end
63
+
64
+ assert_raises B::OutOfRangeError do
65
+ @buf.append_fixed64(B::MaxUint64 + 1)
66
+ end
67
+ end
68
+
69
+ def test_append_uint32
70
+ @buf.append_uint32(1)
71
+ assert_equal "\001", @buf.to_s
72
+
73
+ @buf.buf = ""
74
+ @buf.append_uint32(B::MinUint32)
75
+ assert_equal "\000", @buf.to_s
76
+
77
+ @buf.buf = ""
78
+ @buf.append_uint32(B::MaxUint32)
79
+ assert_equal "\377\377\377\377\017", @buf.to_s
80
+
81
+ assert_raises B::OutOfRangeError do
82
+ @buf.append_uint32(B::MinUint32 - 1)
83
+ end
84
+
85
+ assert_raises B::OutOfRangeError do
86
+ @buf.append_uint32(B::MaxUint32 + 1)
87
+ end
88
+ end
89
+
90
+ def test_append_int32
91
+ @buf.append_int32(1)
92
+ assert_equal "\001", @buf.to_s
93
+
94
+ @buf.buf = ""
95
+ @buf.append_int32(-1)
96
+ assert_equal "\377\377\377\377\377\377\377\377\377\001", @buf.to_s
97
+
98
+ @buf.buf = ""
99
+ @buf.append_int32(B::MinInt32)
100
+ assert_equal "\200\200\200\200\370\377\377\377\377\001", @buf.to_s
101
+
102
+ @buf.buf = ""
103
+ @buf.append_int32(B::MaxInt32)
104
+ assert_equal "\377\377\377\377\007", @buf.to_s
105
+
106
+ assert_raises B::OutOfRangeError do
107
+ @buf.append_int32(B::MinInt32 - 1)
108
+ end
109
+
110
+ assert_raises B::OutOfRangeError do
111
+ @buf.append_int32(B::MaxInt32 + 1)
112
+ end
113
+ end
114
+
115
+ def test_append_int64
116
+ @buf.append_int64(1)
117
+ assert_equal "\001", @buf.to_s
118
+
119
+ @buf.buf = ""
120
+ @buf.append_int64(-1)
121
+ assert_equal "\377\377\377\377\377\377\377\377\377\001", @buf.to_s
122
+
123
+ @buf.buf = ""
124
+ @buf.append_int64(B::MinInt64)
125
+ assert_equal "\200\200\200\200\200\200\200\200\200\001", @buf.to_s
126
+
127
+ @buf.buf = ""
128
+ @buf.append_int64(B::MaxInt64)
129
+ assert_equal "\377\377\377\377\377\377\377\377\177", @buf.to_s
130
+
131
+ assert_raises B::OutOfRangeError do
132
+ @buf.append_int64(B::MinInt64 - 1)
133
+ end
134
+
135
+ assert_raises B::OutOfRangeError do
136
+ @buf.append_int64(B::MaxInt64 + 1)
137
+ end
138
+ end
139
+
140
+ def test_append_uint64
141
+ @buf.append_uint64(1)
142
+ assert_equal "\001", @buf.to_s
143
+
144
+ @buf.buf = ""
145
+ @buf.append_uint64(B::MinUint64)
146
+ assert_equal "\000", @buf.to_s
147
+
148
+ @buf.buf = ""
149
+ @buf.append_uint64(B::MaxUint64)
150
+ assert_equal "\377\377\377\377\377\377\377\377\377\001", @buf.to_s
151
+
152
+ assert_raises B::OutOfRangeError do
153
+ @buf.append_uint64(B::MinUint64 - 1)
154
+ end
155
+
156
+ assert_raises B::OutOfRangeError do
157
+ @buf.append_uint64(B::MaxUint64 + 1)
158
+ end
159
+ end
160
+
161
+ def test_append_float
162
+ @buf.append_float(3.14)
163
+ assert_equal "\303\365H@", @buf.to_s
164
+ end
165
+
166
+ def test_append_double
167
+ @buf.buf = ""
168
+ @buf.append_float(Math::PI)
169
+ assert_equal "\333\017I@", @buf.to_s
170
+ end
171
+
172
+ def test_append_bool
173
+ @buf.append_bool(true)
174
+ @buf.append_bool(false)
175
+ assert_equal "\001\000", @buf.to_s
176
+ end
177
+
178
+ def test_append_sint32
179
+ @buf.append_sint32(-2)
180
+ assert_equal "\003", @buf.to_s
181
+
182
+ @buf.buf = ""
183
+ @buf.append_sint32(B::MaxInt32)
184
+ assert_equal "\376\377\377\377\017", @buf.to_s
185
+
186
+ @buf.buf = ""
187
+ @buf.append_sint32(B::MinInt32)
188
+ assert_equal "\377\377\377\377\017", @buf.to_s
189
+ end
190
+
191
+ def test_append_sfixed32
192
+ @buf.append_sfixed32(-2)
193
+ assert_equal "\003\000\000\000", @buf.to_s
194
+ end
195
+
196
+ def test_append_sint64
197
+ @buf.append_sint64(-2)
198
+ assert_equal "\003", @buf.to_s
199
+
200
+ @buf.buf = ""
201
+ @buf.append_sint64(B::MaxInt64)
202
+ assert_equal "\376\377\377\377\377\377\377\377\377\001", @buf.to_s
203
+
204
+ @buf.buf = ""
205
+ @buf.append_sint64(B::MinInt64)
206
+ assert_equal "\377\377\377\377\377\377\377\377\377\001", @buf.to_s
207
+ end
208
+
209
+ def test_append_sfixed64
210
+ @buf.append_sfixed64(B::MaxInt64)
211
+ assert_equal "\376\377\377\377\377\377\377\377", @buf.to_s
212
+ end
213
+
214
+ def test_append_unicode_string
215
+ ingest = "\u{1f63a}" * 5
216
+ assert_equal 5, ingest.chars.to_a.length
217
+ expected = ingest.bytes.to_a.length.chr + ingest
218
+ @buf.append_string(ingest)
219
+ actual = @buf.to_s
220
+ assert_equal expected.bytes.to_a.length, actual.bytes.to_a.length
221
+ assert_equal expected.bytes.to_a, actual.bytes.to_a
222
+ end
223
+
224
+ def test_append_bytes
225
+ @buf.append_bytes("testing")
226
+ assert_equal "\007testing", @buf.to_s
227
+ end
228
+
229
+ def test_append_frozen_string
230
+ @buf.append_string "testing".freeze
231
+ assert_equal "\007testing", @buf.to_s
232
+ end
233
+
234
+ end
@@ -0,0 +1,45 @@
1
+ require 'minitest/autorun'
2
+ require 'beefcake/buffer'
3
+
4
+ class BufferTest < Minitest::Test
5
+
6
+ def setup
7
+ @buf = Beefcake::Buffer.new
8
+ end
9
+
10
+ def test_simple
11
+ assert_equal "", @buf.to_s
12
+
13
+ @buf << "asdf"
14
+ assert_equal "asdf", @buf.to_s
15
+ assert_equal "asdf", @buf.to_str
16
+
17
+ @buf.buf = ""
18
+ assert_equal "", @buf.to_s
19
+ end
20
+
21
+ def test_wire_for
22
+ assert_equal 0, Beefcake::Buffer.wire_for(:int32)
23
+ assert_equal 0, Beefcake::Buffer.wire_for(:uint32)
24
+ assert_equal 0, Beefcake::Buffer.wire_for(:sint32)
25
+ assert_equal 0, Beefcake::Buffer.wire_for(:int64)
26
+ assert_equal 0, Beefcake::Buffer.wire_for(:uint64)
27
+ assert_equal 0, Beefcake::Buffer.wire_for(:sint64)
28
+
29
+ assert_equal 1, Beefcake::Buffer.wire_for(:fixed64)
30
+ assert_equal 1, Beefcake::Buffer.wire_for(:sfixed64)
31
+ assert_equal 1, Beefcake::Buffer.wire_for(:double)
32
+
33
+ assert_equal 2, Beefcake::Buffer.wire_for(:string)
34
+ assert_equal 2, Beefcake::Buffer.wire_for(:bytes)
35
+
36
+ assert_equal 5, Beefcake::Buffer.wire_for(:fixed32)
37
+ assert_equal 5, Beefcake::Buffer.wire_for(:sfixed32)
38
+ assert_equal 5, Beefcake::Buffer.wire_for(:float)
39
+
40
+ assert_raises Beefcake::Buffer::UnknownType do
41
+ Beefcake::Buffer.wire_for(:asdf)
42
+ end
43
+ end
44
+
45
+ end
@@ -0,0 +1,46 @@
1
+ require 'minitest/autorun'
2
+ require 'beefcake/generator'
3
+
4
+ class GeneratorTest < Minitest::Test
5
+
6
+ def setup
7
+ # Load up the generator request for the addressbook.proto example
8
+ dat = File.dirname(__FILE__) + "/../dat/code_generator_request.dat"
9
+ mock_request = File.read(dat)
10
+ @req = CodeGeneratorRequest.decode(mock_request)
11
+ end
12
+
13
+ def test_request_has_filenames_as_string
14
+ @req.proto_file.each do |file|
15
+ assert_equal Encoding.find("UTF-8"), file.name.encoding
16
+ end
17
+ end
18
+
19
+ def test_generate_empty_namespace
20
+ @res = Beefcake::Generator.compile([], @req)
21
+ assert_equal(CodeGeneratorResponse, @res.class)
22
+ end
23
+
24
+ def test_generate_top_namespace
25
+ @res = Beefcake::Generator.compile(["Top"], @req)
26
+ assert_equal(CodeGeneratorResponse, @res.class)
27
+ assert_match(/module Top/, @res.file.first.content)
28
+ end
29
+
30
+ def test_generate_two_level_namespace
31
+ @res = Beefcake::Generator.compile(["Top", "Bottom"], @req)
32
+ assert_equal(CodeGeneratorResponse, @res.class)
33
+ assert_match(/module Top\s*\n\s*module Bottom/m, @res.file.first.content)
34
+ end
35
+
36
+ # Covers the regression of encoding a CodeGeneratorResponse under 1.9.2-p136 raising
37
+ # Encoding::CompatibilityError: incompatible character encodings: ASCII-8BIT and US-ASCII
38
+ def test_encode_decode_generated_response
39
+ @res = Beefcake::Generator.compile([], @req)
40
+
41
+ assert_equal(CodeGeneratorResponse, @res.class)
42
+
43
+ round_trip = CodeGeneratorResponse.decode(@res.encode)
44
+ assert_equal round_trip, @res
45
+ end
46
+ end