Debian Codegraph @

ruby/spec/data_spec.rb

raw
1 # Licensed to the Apache Software Foundation (ASF) under one
2 # or more contributor license agreements. See the NOTICE file
3 # distributed with this work for additional information
4 # regarding copyright ownership. The ASF licenses this file
5 # to you under the Apache License, Version 2.0 (the
6 # "License"); you may not use this file except in compliance
7 # with the License. You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing,
12 # software distributed under the License is distributed on an
13 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 # KIND, either express or implied. See the License for the
15 # specific language governing permissions and limitations
16 # under the License.
17
18
19
20 require "spec_helper"
21
22 module Qpid
23
24 module Proton
25
26 describe "A data object" do
27
28 before :each do
29 @data = Qpid::Proton::Codec::Data.new
30 end
31
32 it "can be initialized" do
33 @data.wont_be_nil
34 end
35
36 it "can hold a null" do
37 @data.null = nil
38 @data.null?.must_equal(true)
39 end
40
41 it "can hold a true boolean" do
42 @data.bool = true
43 @data.bool.must_equal(true)
44 end
45
46 it "can hold a false boolean" do
47 @data.bool = false
48 @data.bool.must_equal(false)
49 end
50
51 it "raises an error on a negative ubyte" do
52 proc {
53 @data.ubyte = (0 - (rand(127) + 1))
54 }.must_raise(RangeError)
55 end
56
57 it "raises an error on a null ubyte" do
58 proc {
59 @data.ubyte = nil
60 }.must_raise(TypeError)
61 end
62
63 it "can hold an unsigned byte" do
64 value = rand(255)
65 @data.ubyte = value
66 @data.ubyte.must_equal(value)
67 end
68
69 it "can hold a byte" do
70 value = rand(128)
71 @data.byte = value
72 @data.byte.must_equal(value)
73 end
74
75 it "can hold a negative byte" do
76 value = 0 - (rand(126) + 1)
77 @data.byte = value
78 @data.byte.must_equal(value)
79 end
80
81 it "raises an error on a negative ushort" do
82 proc {
83 @data.ushort = (0 - (rand(65535) + 1))
84 }.must_raise(RangeError)
85 end
86
87 it "raises an error on a nil ushort" do
88 proc {
89 @data.ushort = nil
90 }.must_raise(TypeError)
91 end
92
93 it "can hold a zero unsigned short" do
94 @data.ushort = 0
95 @data.ushort.must_equal(0)
96 end
97
98 it "can hold an unsigned short" do
99 value = rand(2**15) + 1
100 @data.ushort = value
101 @data.ushort.must_equal(value)
102 end
103
104 it "raises an error on a nil short" do
105 proc {
106 @data.short = nil
107 }.must_raise(TypeError)
108 end
109
110 it "can hold a short" do
111 value = rand(2**15) + 1
112 @data.short = value
113 @data.short.must_equal(value)
114 end
115
116 it "can hold a zero short" do
117 @data.short = 0
118 @data.short.must_equal(0)
119 end
120
121 it "can hold a negative short" do
122 value = (0 - (rand(2**15) + 1))
123 @data.short = value
124 @data.short.must_equal(value)
125 end
126
127 it "raises an error on a nil uint" do
128 proc {
129 @data.uint = nil
130 }.must_raise(TypeError)
131 end
132
133 it "raises an error on a negative uint" do
134 proc {
135 @data.uint = (0 - (rand(2**32) + 1))
136 }.must_raise(RangeError)
137 end
138
139 it "can hold an unsigned integer" do
140 value = rand(2**32) + 1
141 @data.uint = value
142 @data.uint.must_equal(value)
143 end
144
145 it "can hold a zero unsigned integer" do
146 @data.uint = 0
147 @data.uint.must_equal(0)
148 end
149
150 it "raise an error on a null integer" do
151 proc {
152 @data.int = nil
153 }.must_raise(TypeError)
154 end
155
156 it "can hold an integer" do
157 value = rand(2**31) + 1
158 @data.int = value
159 @data.int.must_equal(value)
160 end
161
162 it "can hold zero as an integer" do
163 @data.int = 0
164 @data.int.must_equal(0)
165 end
166
167 it "raises an error on a null character" do
168 proc {
169 @data.char = nil
170 }.must_raise(TypeError)
171 end
172
173 it "can hold a character" do
174 source = random_string(256)
175 index = rand(source.length)
176 value = source[index,1].bytes.to_a[0]
177 @data.char = value
178 @data.char.must_equal(value)
179 end
180
181 it "raises an error on a null ulong" do
182 proc {
183 @data.ulong = nil
184 }.must_raise(TypeError)
185 end
186
187 it "raises an error on a negative ulong" do
188 proc {
189 @data.ulong = (0 - (rand(2**63) + 1))
190 }.must_raise(RangeError)
191 end
192
193 it "can have a zero unsigned long" do
194 @data.ulong = 0
195 @data.ulong.must_equal(0)
196 end
197
198 it "can hold an unsigned long" do
199 value = rand(2**63) + 1
200 @data.ulong = value
201 @data.ulong.must_equal(value)
202 end
203
204 it "raises an error on a null long" do
205 proc {
206 @data.long = nil
207 }.must_raise(TypeError)
208 end
209
210 it "can have a zero long" do
211 @data.long = 0
212 @data.long.must_equal(0)
213 end
214
215 it "can hold a long" do
216 value = rand(2**63) + 1
217 @data.long = value
218 @data.long.must_equal(value)
219 end
220
221 it "raise an error on a null timestamp" do
222 proc {
223 @data.timestamp = nil
224 }.must_raise(TypeError)
225 end
226
227 it "can handle a negative timestamp" do
228 last_year = Time.now - (60*60*24*365)
229 @data.timestamp = last_year
230 @data.timestamp.must_equal(last_year.to_i)
231 end
232
233 it "can handle a zero timestamp" do
234 @data.timestamp = 0
235 @data.timestamp.must_equal(0)
236 end
237
238 it "can hold a timestamp" do
239 next_year = Time.now + (60*60*24*365)
240 @data.timestamp = next_year
241 @data.timestamp.must_equal(next_year.to_i)
242 end
243
244 it "raises an error on a null float" do
245 proc {
246 @data.float = nil
247 }.must_raise(TypeError)
248 end
249
250 it "can hold a negative float" do
251 value = 0.0 - (1.0 + rand(2.0**15)).to_f
252 @data.float = value
253 @data.float.must_equal(value)
254 end
255
256 it "can hold a zero float" do
257 @data.float = 0.0
258 @data.float.must_equal(0.0)
259 end
260
261 it "can hold a float" do
262 value = (1.0 + rand(2.0**15)).to_f
263 @data.float = value
264 @data.float.must_equal(value)
265 end
266
267 it "raise an error on a null double" do
268 proc {
269 @data.double = nil
270 }.must_raise(TypeError)
271 end
272
273 it "can hold a negative double" do
274 value = 0.0 - (1.0 + rand(2.0**31)).to_f
275 @data.double = value
276 @data.double.must_equal(value)
277 end
278
279 it "can hold a zero double" do
280 @data.double = 0.0
281 @data.double.must_equal(0.0)
282 end
283
284 it "can hold a double" do
285 value = (1.0 + rand(2.0**31)).to_f
286 @data.double = value
287 @data.double.must_equal(value)
288 end
289
290 it "raises an error on a null decimal32" do
291 proc {
292 @data.decimal32 = nil
293 }.must_raise(TypeError)
294 end
295
296 it "can hold a zero decimal32" do
297 @data.decimal32 = 0
298 @data.decimal32.must_equal(0)
299 end
300
301 it "can hold a decimal32" do
302 value = 1 + rand(2**31)
303 @data.decimal32 = value
304 @data.decimal32.must_equal(value)
305 end
306
307 it "raises an error on a null decimal64" do
308 proc {
309 @data.decimal64 = nil
310 }.must_raise(TypeError)
311 end
312
313 it "can hold a zero decimal64" do
314 @data.decimal64 = 0
315 @data.decimal64.must_equal(0)
316 end
317
318 it "can hold a decimal64" do
319 value = 1 + rand(2**63)
320 @data.decimal64 = value
321 @data.decimal64.must_equal(value)
322 end
323
324 it "raises an error on a null decimal128" do
325 proc {
326 @data.decimal128 = nil
327 }.must_raise(TypeError)
328 end
329
330 it "can hold a zero decimal128" do
331 @data.decimal128 = 0
332 @data.decimal128.must_equal(0)
333 end
334
335 it "can hold a decimal128" do
336 value = rand(2**127)
337 @data.decimal128 = value
338 @data.decimal128.must_equal(value)
339 end
340
341 it "raises an error on a null UUID" do
342 proc {
343 @data.uuid = nil
344 }.must_raise(::ArgumentError)
345 end
346
347 it "raises an error on a malformed UUID" do
348 proc {
349 @data.uuid = random_string(36)
350 }.must_raise(::ArgumentError)
351 end
352
353 it "can set a UUID from an integer value" do
354 @data.uuid = 336307859334295828133695192821923655679
355 @data.uuid.must_equal("fd0289a5-8eec-4a08-9283-81d02c9d2fff")
356 end
357
358 it "can hold a UUID" do
359 value = "fd0289a5-8eec-4a08-9283-81d02c9d2fff"
360 @data.uuid = value
361 @data.uuid.must_equal(value)
362 end
363
364 it "can hold a null binary" do
365 @data.binary = nil
366 @data.binary.must_equal("")
367 end
368
369 it "can hold a binary" do
370 value = random_string(128)
371 @data.binary = value
372 @data.binary.must_equal(value)
373 end
374
375 it "can hold a null string" do
376 @data.string = nil
377 @data.string.must_equal("")
378 end
379
380 it "can hold a string" do
381 value = random_string(128)
382 @data.string = value
383 @data.string.must_equal(value)
384 end
385
386 it "can hold a null symbol" do
387 @data.symbol = nil
388 @data.symbol.must_equal(:"")
389 end
390
391 it "can hold a symbol" do
392 value = random_string(128).to_sym
393 @data.symbol = value
394 @data.symbol.must_equal(value)
395 end
396
397 it "can hold a described value" do
398 name = random_string(16).to_sym
399 value = random_string(16)
400 @data.put_described
401 @data.enter
402 @data.symbol = name
403 @data.string = value
404 @data.exit
405
406 @data.described?.must_equal(true)
407 @data.enter
408 @data.next
409 @data.symbol.must_equal(name)
410 @data.next
411 @data.string.must_equal(value)
412 end
413
414 it "raises an error when setting the wrong type in an array" do
415 proc {
416 @data << Qpid::Proton::Types::UniformArray.new(Qpid::Proton::Types::INT, [1, 2.0, :a, "b"])
417 }.must_raise(TypeError)
418 end
419
420 it "can hold an array" do
421 values = []
422 (1..(rand(100) + 5)).each { values << rand(2**16) }
423 @data.put_array false, Qpid::Proton::Codec::INT.code
424 @data.enter
425 values.each { |value| @data.int = value }
426 @data.exit
427
428 @data.enter
429 values.each do |value|
430 @data.next
431 @data.int.must_equal(value)
432 end
433 end
434
435 it "can hold a described array" do
436 values = []
437 (1..(rand(100) + 5)).each { values << random_string(64) }
438 descriptor = random_string(32).to_sym
439 @data.put_array true, Qpid::Proton::Codec::STRING.code
440 @data.enter
441 @data.symbol = descriptor
442 values.each { |value| @data.string = value }
443 @data.exit
444
445 @data.get_array.must_equal([values.size, true, Qpid::Proton::Codec::STRING.code])
446 @data.enter
447 @data.next
448 @data.symbol.must_equal(descriptor)
449 values.each do |value|
450 @data.next
451 @data.string.must_equal(value)
452 end
453 end
454
455 it "can hold a list" do
456 values = []
457 (1..(rand(100) + 5)).each { values << random_string(128) }
458 @data.put_list
459 @data.enter
460 values.each {|value| @data.string = value}
461 @data.exit
462
463 @data.enter
464 values.each do |value|
465 @data.next
466 @data.string.must_equal(value)
467 end
468 end
469
470 it "can hold a map" do
471 keys = []
472 (1..(rand(100) + 5)).each {keys << random_string(128)}
473 values = {}
474 keys.each {|key| values[key] = random_string(128)}
475
476 @data.put_map
477 @data.enter
478 keys.each do |key|
479 @data.string = key
480 @data.string = values[key]
481 end
482 @data.exit
483
484 @data.enter
485 keys.each do |key|
486 @data.next
487 @data.string.must_equal(key)
488 @data.next
489 @data.string.must_equal(values[key])
490 end
491 end
492
493 end
494
495 end
496
497 end