File: data_spec.rb

package info (click to toggle)
qpid-proton 0.37.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,384 kB
  • sloc: ansic: 37,828; cpp: 37,140; python: 15,302; ruby: 6,018; xml: 477; sh: 320; pascal: 52; makefile: 18
file content (497 lines) | stat: -rw-r--r-- 12,419 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.



require "spec_helper"

module Qpid

  module Proton

    describe "A data object" do

      before :each do
        @data = Qpid::Proton::Codec::Data.new
      end

      it "can be initialized" do
        @data.wont_be_nil
      end

      it "can hold a null" do
        @data.null = nil
        @data.null?.must_equal(true)
      end

      it "can hold a true boolean" do
        @data.bool = true
        @data.bool.must_equal(true)
      end

      it "can hold a false boolean" do
        @data.bool = false
        @data.bool.must_equal(false)
      end

      it "raises an error on a negative ubyte" do
        proc {
          @data.ubyte = (0 - (rand(127) + 1))
        }.must_raise(RangeError)
      end

      it "raises an error on a null ubyte" do
        proc {
          @data.ubyte = nil
        }.must_raise(TypeError)
      end

      it "can hold an unsigned byte" do
        value = rand(255)
        @data.ubyte = value
        @data.ubyte.must_equal(value)
      end

      it "can hold a byte" do
        value = rand(128)
        @data.byte = value
        @data.byte.must_equal(value)
      end

      it "can hold a negative byte" do
        value = 0 - (rand(126) + 1)
        @data.byte = value
        @data.byte.must_equal(value)
      end

      it "raises an error on a negative ushort" do
        proc {
          @data.ushort = (0 - (rand(65535) + 1))
        }.must_raise(RangeError)
      end

      it "raises an error on a nil ushort" do
        proc {
          @data.ushort = nil
        }.must_raise(TypeError)
      end

      it "can hold a zero unsigned short" do
        @data.ushort = 0
        @data.ushort.must_equal(0)
      end

      it "can hold an unsigned short" do
        value = rand(2**15) + 1
        @data.ushort = value
        @data.ushort.must_equal(value)
      end

      it "raises an error on a nil short" do
        proc {
          @data.short = nil
        }.must_raise(TypeError)
      end

      it "can hold a short" do
        value = rand(2**15) + 1
        @data.short = value
        @data.short.must_equal(value)
      end

      it "can hold a zero short" do
        @data.short = 0
        @data.short.must_equal(0)
      end

      it "can hold a negative short" do
        value = (0 - (rand(2**15) + 1))
        @data.short = value
        @data.short.must_equal(value)
      end

      it "raises an error on a nil uint" do
        proc {
          @data.uint = nil
        }.must_raise(TypeError)
      end

      it "raises an error on a negative uint" do
        proc {
          @data.uint = (0 - (rand(2**32) + 1))
        }.must_raise(RangeError)
      end

      it "can hold an unsigned integer" do
        value = rand(2**32) + 1
        @data.uint = value
        @data.uint.must_equal(value)
      end

      it "can hold a zero unsigned integer" do
        @data.uint = 0
        @data.uint.must_equal(0)
      end

      it "raise an error on a null integer" do
        proc {
          @data.int = nil
        }.must_raise(TypeError)
      end

      it "can hold an integer" do
        value = rand(2**31) + 1
        @data.int = value
        @data.int.must_equal(value)
      end

      it "can hold zero as an integer" do
        @data.int = 0
        @data.int.must_equal(0)
      end

      it "raises an error on a null character" do
        proc {
          @data.char = nil
        }.must_raise(TypeError)
      end

      it "can hold a character" do
        source = random_string(256)
        index = rand(source.length)
        value = source[index,1].bytes.to_a[0]
        @data.char = value
        @data.char.must_equal(value)
      end

      it "raises an error on a null ulong" do
        proc {
          @data.ulong = nil
        }.must_raise(TypeError)
      end

      it "raises an error on a negative ulong" do
        proc {
          @data.ulong = (0 - (rand(2**63) + 1))
        }.must_raise(RangeError)
      end

      it "can have a zero unsigned long" do
        @data.ulong = 0
        @data.ulong.must_equal(0)
      end

      it "can hold an unsigned long" do
        value = rand(2**63) + 1
        @data.ulong = value
        @data.ulong.must_equal(value)
      end

      it "raises an error on a null long" do
        proc {
          @data.long = nil
        }.must_raise(TypeError)
      end

      it "can have a zero long" do
        @data.long = 0
        @data.long.must_equal(0)
      end

      it "can hold a long" do
        value = rand(2**63) + 1
        @data.long = value
        @data.long.must_equal(value)
      end

      it "raise an error on a null timestamp" do
        proc {
          @data.timestamp = nil
        }.must_raise(TypeError)
      end

      it "can handle a negative timestamp" do
        last_year = Time.now - (60*60*24*365)
        @data.timestamp = last_year
        @data.timestamp.must_equal(last_year.to_i)
      end

      it "can handle a zero timestamp" do
        @data.timestamp = 0
        @data.timestamp.must_equal(0)
      end

      it "can hold a timestamp" do
        next_year = Time.now + (60*60*24*365)
        @data.timestamp = next_year
        @data.timestamp.must_equal(next_year.to_i)
      end

      it "raises an error on a null float" do
        proc {
          @data.float = nil
        }.must_raise(TypeError)
      end

      it "can hold a negative float" do
        value = 0.0 - (1.0 + rand(2.0**15)).to_f
        @data.float = value
        @data.float.must_equal(value)
      end

      it "can hold a zero float" do
        @data.float = 0.0
        @data.float.must_equal(0.0)
      end

      it "can hold a float" do
        value = (1.0 + rand(2.0**15)).to_f
        @data.float = value
        @data.float.must_equal(value)
      end

      it "raise an error on a null double" do
        proc {
          @data.double = nil
        }.must_raise(TypeError)
      end

      it "can hold a negative double" do
        value = 0.0 - (1.0 + rand(2.0**31)).to_f
        @data.double = value
        @data.double.must_equal(value)
      end

      it "can hold a zero double" do
        @data.double = 0.0
        @data.double.must_equal(0.0)
      end

      it "can hold a double" do
        value = (1.0 + rand(2.0**31)).to_f
        @data.double = value
        @data.double.must_equal(value)
      end

      it "raises an error on a null decimal32" do
        proc {
          @data.decimal32 = nil
        }.must_raise(TypeError)
      end

      it "can hold a zero decimal32" do
        @data.decimal32 = 0
        @data.decimal32.must_equal(0)
      end

      it "can hold a decimal32" do
        value = 1 + rand(2**31)
        @data.decimal32 = value
        @data.decimal32.must_equal(value)
      end

      it "raises an error on a null decimal64" do
        proc {
          @data.decimal64 = nil
        }.must_raise(TypeError)
      end

      it "can hold a zero decimal64" do
        @data.decimal64 = 0
        @data.decimal64.must_equal(0)
      end

      it "can hold a decimal64" do
        value = 1 + rand(2**63)
        @data.decimal64 = value
        @data.decimal64.must_equal(value)
      end

      it "raises an error on a null decimal128" do
        proc {
          @data.decimal128 = nil
        }.must_raise(TypeError)
      end

      it "can hold a zero decimal128" do
        @data.decimal128 = 0
        @data.decimal128.must_equal(0)
      end

      it "can hold a decimal128" do
        value = rand(2**127)
        @data.decimal128 = value
        @data.decimal128.must_equal(value)
      end

      it "raises an error on a null UUID" do
        proc {
          @data.uuid = nil
        }.must_raise(::ArgumentError)
      end

      it "raises an error on a malformed UUID" do
        proc {
          @data.uuid = random_string(36)
        }.must_raise(::ArgumentError)
      end

      it "can set a UUID from an integer value" do
        @data.uuid = 336307859334295828133695192821923655679
        @data.uuid.must_equal("fd0289a5-8eec-4a08-9283-81d02c9d2fff")
      end

      it "can hold a UUID" do
        value = "fd0289a5-8eec-4a08-9283-81d02c9d2fff"
        @data.uuid = value
        @data.uuid.must_equal(value)
      end

      it "can hold a null binary" do
        @data.binary = nil
        @data.binary.must_equal("")
      end

      it "can hold a binary" do
        value = random_string(128)
        @data.binary = value
        @data.binary.must_equal(value)
      end

      it "can hold a null string" do
        @data.string = nil
        @data.string.must_equal("")
      end

      it "can hold a string" do
        value = random_string(128)
        @data.string = value
        @data.string.must_equal(value)
      end

      it "can hold a null symbol" do
        @data.symbol = nil
        @data.symbol.must_equal(:"")
      end

      it "can hold a symbol" do
        value = random_string(128).to_sym
        @data.symbol = value
        @data.symbol.must_equal(value)
      end

      it "can hold a described value" do
        name = random_string(16).to_sym
        value = random_string(16)
        @data.put_described
        @data.enter
        @data.symbol = name
        @data.string = value
        @data.exit

        @data.described?.must_equal(true)
        @data.enter
        @data.next
        @data.symbol.must_equal(name)
        @data.next
        @data.string.must_equal(value)
      end

      it "raises an error when setting the wrong type in an array" do
        proc {
          @data << Qpid::Proton::Types::UniformArray.new(Qpid::Proton::Types::INT, [1, 2.0, :a, "b"])
        }.must_raise(TypeError)
      end

      it "can hold an array" do
        values = []
        (1..(rand(100) + 5)).each { values << rand(2**16) }
        @data.put_array false, Qpid::Proton::Codec::INT.code
        @data.enter
        values.each { |value| @data.int = value }
        @data.exit

        @data.enter
        values.each do |value|
          @data.next
          @data.int.must_equal(value)
        end
      end

      it "can hold a described array" do
        values = []
        (1..(rand(100) + 5)).each { values << random_string(64) }
        descriptor = random_string(32).to_sym
        @data.put_array true, Qpid::Proton::Codec::STRING.code
        @data.enter
        @data.symbol = descriptor
        values.each { |value| @data.string = value }
        @data.exit

        @data.get_array.must_equal([values.size, true, Qpid::Proton::Codec::STRING.code])
        @data.enter
        @data.next
        @data.symbol.must_equal(descriptor)
        values.each do |value|
          @data.next
          @data.string.must_equal(value)
        end
      end

      it "can hold a list" do
        values = []
        (1..(rand(100) + 5)).each { values << random_string(128) }
        @data.put_list
        @data.enter
        values.each {|value| @data.string = value}
        @data.exit

        @data.enter
        values.each do |value|
          @data.next
          @data.string.must_equal(value)
        end
      end

      it "can hold a map" do
        keys = []
        (1..(rand(100) + 5)).each {keys << random_string(128)}
        values = {}
        keys.each {|key| values[key] = random_string(128)}

        @data.put_map
        @data.enter
        keys.each do |key|
          @data.string = key
          @data.string = values[key]
        end
        @data.exit

        @data.enter
        keys.each do |key|
          @data.next
          @data.string.must_equal(key)
          @data.next
          @data.string.must_equal(values[key])
        end
      end

    end

  end

end