bson 4.2.0.rc1 → 4.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +1 -4
  3. data.tar.gz.sig +0 -0
  4. data/lib/bson/array.rb +1 -1
  5. data/lib/bson/binary.rb +4 -2
  6. data/lib/bson/code.rb +1 -1
  7. data/lib/bson/code_with_scope.rb +1 -1
  8. data/lib/bson/date.rb +1 -1
  9. data/lib/bson/date_time.rb +1 -1
  10. data/lib/bson/decimal128.rb +1 -1
  11. data/lib/bson/false_class.rb +1 -1
  12. data/lib/bson/float.rb +1 -1
  13. data/lib/bson/hash.rb +1 -1
  14. data/lib/bson/integer.rb +1 -1
  15. data/lib/bson/object_id.rb +1 -1
  16. data/lib/bson/open_struct.rb +1 -1
  17. data/lib/bson/regexp.rb +86 -18
  18. data/lib/bson/specialized.rb +1 -1
  19. data/lib/bson/string.rb +1 -1
  20. data/lib/bson/symbol.rb +1 -1
  21. data/lib/bson/time.rb +1 -1
  22. data/lib/bson/timestamp.rb +1 -1
  23. data/lib/bson/true_class.rb +1 -1
  24. data/lib/bson/version.rb +1 -1
  25. data/spec/bson/array_spec.rb +1 -1
  26. data/spec/bson/binary_spec.rb +2 -1
  27. data/spec/bson/corpus_spec.rb +68 -0
  28. data/spec/bson/raw_spec.rb +540 -0
  29. data/spec/bson/regexp_spec.rb +7 -7
  30. data/spec/spec_helper.rb +1 -0
  31. data/spec/support/corpus-tests/array.json +43 -0
  32. data/spec/support/corpus-tests/boolean.json +27 -0
  33. data/spec/support/corpus-tests/code.json +67 -0
  34. data/spec/support/corpus-tests/code_w_scope.json +78 -0
  35. data/spec/support/corpus-tests/document.json +36 -0
  36. data/spec/support/corpus-tests/double.json +69 -0
  37. data/spec/support/corpus-tests/failures/binary.json +69 -0
  38. data/spec/support/corpus-tests/failures/datetime.json +31 -0
  39. data/spec/support/corpus-tests/failures/dbpointer.json +42 -0
  40. data/spec/support/corpus-tests/failures/int64.json +38 -0
  41. data/spec/support/corpus-tests/failures/symbol.json +62 -0
  42. data/spec/support/corpus-tests/failures/undefined.json +13 -0
  43. data/spec/support/corpus-tests/int32.json +38 -0
  44. data/spec/support/corpus-tests/maxkey.json +12 -0
  45. data/spec/support/corpus-tests/minkey.json +12 -0
  46. data/spec/support/corpus-tests/null.json +12 -0
  47. data/spec/support/corpus-tests/oid.json +28 -0
  48. data/spec/support/corpus-tests/regex.json +37 -0
  49. data/spec/support/corpus-tests/string.json +67 -0
  50. data/spec/support/corpus-tests/timestamp.json +18 -0
  51. data/spec/support/corpus-tests/top.json +62 -0
  52. data/spec/support/corpus.rb +265 -0
  53. data/spec/support/shared_examples.rb +1 -1
  54. metadata +51 -3
  55. metadata.gz.sig +0 -0
@@ -0,0 +1,540 @@
1
+ require 'spec_helper'
2
+
3
+ describe Regexp::Raw do
4
+
5
+ let(:pattern) { '\W+' }
6
+ let(:options) { '' }
7
+ let(:bson) { "#{pattern}#{BSON::NULL_BYTE}#{options}#{BSON::NULL_BYTE}" }
8
+
9
+ describe "#as_json" do
10
+
11
+ let(:object) do
12
+ described_class.new(pattern, 'im')
13
+ end
14
+
15
+ it "returns the regex pattern and options" do
16
+ expect(object.as_json).to eq({ "$regex" => "\\W+", "$options" => "im" })
17
+ end
18
+
19
+ it_behaves_like "a JSON serializable object"
20
+ end
21
+
22
+ describe "#to_bson/#from_bson" do
23
+
24
+ let(:options) { 'ilmsux' }
25
+ let(:obj) { described_class.new(pattern, options) }
26
+ let(:type) { 11.chr }
27
+ let(:bson) { "#{pattern}#{BSON::NULL_BYTE}#{options}#{BSON::NULL_BYTE}" }
28
+
29
+ let(:klass) { ::Regexp }
30
+
31
+ it_behaves_like "a bson element"
32
+ it_behaves_like "a serializable bson element"
33
+ it_behaves_like "a deserializable bson element"
34
+ end
35
+
36
+ describe "#initialize" do
37
+
38
+ let(:object) do
39
+ described_class.new(pattern, options)
40
+ end
41
+
42
+ context "when options are not passed" do
43
+
44
+ it "sets the options on the raw regex" do
45
+ expect(object.options). to eq(options)
46
+ end
47
+
48
+ context "When the raw regexp is compiled" do
49
+
50
+ let(:regexp) do
51
+ object.compile
52
+ end
53
+
54
+ it "sets the options on the compiled regexp object" do
55
+ expect(regexp.options).to eq(0)
56
+ end
57
+ end
58
+ end
59
+
60
+ context "when options are passed" do
61
+
62
+ context "when options are an Integer" do
63
+
64
+ let(:options) { ::Regexp::EXTENDED }
65
+
66
+ it "sets the options on the raw regex" do
67
+ expect(object.options). to eq(options)
68
+ end
69
+
70
+ context "When the raw regexp is compiled" do
71
+
72
+ let(:regexp) do
73
+ object.compile
74
+ end
75
+
76
+ it "sets the options on the compiled regexp object" do
77
+ expect(regexp.options).to eq(options)
78
+ end
79
+ end
80
+ end
81
+
82
+ context "when options are a String" do
83
+
84
+ let(:options) { 'x' }
85
+
86
+ it "sets the options on the raw regex" do
87
+ expect(object.options). to eq(options)
88
+ end
89
+
90
+ context "When the raw regexp is compiled" do
91
+
92
+ let(:regexp) do
93
+ object.compile
94
+ end
95
+
96
+ it "sets the options on the compiled regexp object" do
97
+ expect(regexp.options).to eq(::Regexp::EXTENDED)
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
103
+
104
+ describe "#from_bson" do
105
+
106
+ let(:obj) { ::Regexp.from_bson(io) }
107
+ let(:io) { BSON::ByteBuffer.new(bson) }
108
+
109
+ it "deserializes to a Regexp::Raw object" do
110
+ expect(obj).to be_a(Regexp::Raw)
111
+ end
112
+
113
+ it "deserializes the pattern" do
114
+ expect(obj.pattern).to eq(pattern)
115
+ end
116
+
117
+ context "when there are no options" do
118
+
119
+ it "does not set any options on the raw regexp object" do
120
+ expect(obj.options).to eq(options)
121
+ end
122
+ end
123
+
124
+ context "when there are options" do
125
+
126
+ context "when there is the i ignorecase option" do
127
+
128
+ let(:options) { 'i' }
129
+
130
+ it "deserializes the pattern" do
131
+ expect(obj.pattern).to eq(pattern)
132
+ end
133
+
134
+ it "sets the i option on the raw regexp object" do
135
+ expect(obj.options).to eq(options)
136
+ end
137
+ end
138
+
139
+ context "when there is the l locale dependent option" do
140
+
141
+ let(:options) { 'l' }
142
+
143
+ it "deserializes the pattern" do
144
+ expect(obj.pattern).to eq(pattern)
145
+ end
146
+
147
+ it "sets the l option on the raw regexp object" do
148
+ expect(obj.options).to eq(options)
149
+ end
150
+ end
151
+
152
+ context "when there is the m multiline option" do
153
+
154
+ let(:options) { 'm' }
155
+
156
+ it "deserializes the pattern" do
157
+ expect(obj.pattern).to eq(pattern)
158
+ end
159
+
160
+ it "sets the m option on the raw regexp object" do
161
+ expect(obj.options).to eq(options)
162
+ end
163
+ end
164
+
165
+ context "when there is the s dotall option" do
166
+
167
+ let(:options) { 's' }
168
+
169
+ it "deserializes the pattern" do
170
+ expect(obj.pattern).to eq(pattern)
171
+ end
172
+
173
+ it "sets the s option on the raw regexp object" do
174
+ expect(obj.options).to eq(options)
175
+ end
176
+ end
177
+
178
+ context "when there is the u match unicode option" do
179
+
180
+ let(:options) { 'u' }
181
+
182
+ it "deserializes the pattern" do
183
+ expect(obj.pattern).to eq(pattern)
184
+ end
185
+
186
+ it "sets the u option on the raw regexp object" do
187
+ expect(obj.options).to eq(options)
188
+ end
189
+ end
190
+
191
+ context "when there is the x verbose option" do
192
+
193
+ let(:options) { 'x' }
194
+
195
+ it "deserializes the pattern" do
196
+ expect(obj.pattern).to eq(pattern)
197
+ end
198
+
199
+ it "sets the x option on the raw regexp object" do
200
+ expect(obj.options).to eq(options)
201
+ end
202
+ end
203
+
204
+ context "when all options are set" do
205
+
206
+ let(:options) { 'ilmsux' }
207
+
208
+ it "deserializes the pattern" do
209
+ expect(obj.pattern).to eq(pattern)
210
+ end
211
+
212
+ it "sets all options on the raw regexp object" do
213
+ expect(obj.options).to eq(options)
214
+ end
215
+ end
216
+ end
217
+ end
218
+
219
+ context "when a method is called on a Raw regexp object" do
220
+
221
+ let(:obj) { ::Regexp.from_bson(io) }
222
+ let(:io) { BSON::ByteBuffer.new(bson) }
223
+
224
+ it "forwards the method call on to the compiled Ruby Regexp object" do
225
+ expect(obj.source).to eq(pattern)
226
+ end
227
+ end
228
+
229
+ context "when respond_to? is called on the Raw Regexp object" do
230
+
231
+ let(:obj) { Regexp::Raw.new(pattern, options) }
232
+
233
+ context "when include_private is false" do
234
+
235
+ it "does not consider private methods" do
236
+ expect(obj.respond_to?(:initialize_copy)).to eq(false)
237
+ end
238
+ end
239
+
240
+ context "when include private is true" do
241
+
242
+ it "considers private methods" do
243
+ expect(obj.respond_to?(:initialize_copy, true)).to eq(true)
244
+ end
245
+ end
246
+
247
+ context "when include_private is not specified" do
248
+
249
+ it "does not consider private methods" do
250
+ expect(obj.respond_to?(:initialize_copy)).to eq(false)
251
+ end
252
+ end
253
+ end
254
+
255
+ context "#to_bson" do
256
+
257
+ let(:obj) { Regexp::Raw.new(pattern, options) }
258
+ let(:options) { '' }
259
+ let(:bson) { "#{pattern}#{BSON::NULL_BYTE}#{options}#{BSON::NULL_BYTE}" }
260
+ let(:serialized) { obj.to_bson.to_s }
261
+
262
+ it "serializes the pattern" do
263
+ expect(serialized).to eq(bson)
264
+ end
265
+
266
+ context "where there are no options" do
267
+
268
+ it "does not set any options on the bson regex object" do
269
+ expect(serialized).to eq(bson)
270
+ end
271
+ end
272
+
273
+ context "when there are options" do
274
+
275
+ context "when options are specified as an Integer" do
276
+
277
+ let(:options) { ::Regexp::EXTENDED }
278
+ let(:bson) { "#{pattern}#{BSON::NULL_BYTE}mx#{BSON::NULL_BYTE}" }
279
+
280
+ it "sets the option on the serialized bson object" do
281
+ expect(serialized).to eq(bson)
282
+ end
283
+ end
284
+
285
+ context "when there is the i ignorecase option" do
286
+
287
+ let(:options) { 'i' }
288
+
289
+ it "sets the option on the serialized bson object" do
290
+ expect(serialized).to eq(bson)
291
+ end
292
+ end
293
+
294
+ context "when there is the l locale dependent option" do
295
+
296
+ let(:options) { 'l' }
297
+
298
+ it "sets the option on the serialized bson object" do
299
+ expect(serialized).to eq(bson)
300
+ end
301
+ end
302
+
303
+ context "when there is the m multiline option" do
304
+
305
+ let(:options) { 'm' }
306
+
307
+ it "sets the option on the serialized bson object" do
308
+ expect(serialized).to eq(bson)
309
+ end
310
+ end
311
+
312
+ context "when there is the s dotall option" do
313
+
314
+ let(:options) { 's' }
315
+
316
+ it "sets the option on the serialized bson object" do
317
+ expect(serialized).to eq(bson)
318
+ end
319
+ end
320
+
321
+ context "when there is the u match unicode option" do
322
+
323
+ let(:options) { 'u' }
324
+
325
+ it "sets the option on the serialized bson object" do
326
+ expect(serialized).to eq(bson)
327
+ end
328
+ end
329
+
330
+ context "when there is the x verbose option" do
331
+
332
+ let(:options) { 'x' }
333
+
334
+ it "sets the option on the serialized bson object" do
335
+ expect(serialized).to eq(bson)
336
+ end
337
+ end
338
+
339
+ context "when all options are set" do
340
+
341
+ let(:options) { 'ilmsux' }
342
+
343
+ it "sets all options on the serialized bson object" do
344
+ expect(serialized).to eq(bson)
345
+ end
346
+
347
+ context "when the options are not provided in alphabetical order" do
348
+
349
+ let(:options) { 'mislxu' }
350
+ let(:bson) { "#{pattern}#{BSON::NULL_BYTE}ilmsux#{BSON::NULL_BYTE}" }
351
+
352
+ it "serializes the options in alphabetical order" do
353
+ expect(serialized).to eq(bson)
354
+ end
355
+ end
356
+ end
357
+ end
358
+ end
359
+
360
+ describe "#compile" do
361
+
362
+ let(:obj) { Regexp.from_bson(io) }
363
+ let(:io) { BSON::ByteBuffer.new(bson) }
364
+ let(:ruby_regexp) { obj.compile }
365
+
366
+ it "sets the pattern on the Ruby Regexp object" do
367
+ expect(obj.pattern).to eq(ruby_regexp.source)
368
+ end
369
+
370
+ context "when there are no options set" do
371
+
372
+ it "does not set any options on the Ruby Regexp object" do
373
+ expect(ruby_regexp.options).to eq(0)
374
+ end
375
+ end
376
+
377
+ context "when there are options set" do
378
+
379
+ context "when there is the i ignorecase option" do
380
+
381
+ let(:options) { 'i' }
382
+
383
+ it "sets the i option on the Ruby Regexp object" do
384
+ expect(ruby_regexp.options).to eq(::Regexp::IGNORECASE)
385
+ end
386
+ end
387
+
388
+ context "when there is the l locale dependent option" do
389
+
390
+ let(:options) { 'l' }
391
+
392
+ it "does not set an option on the Ruby Regexp object" do
393
+ expect(ruby_regexp.options).to eq(0)
394
+ end
395
+ end
396
+
397
+ context "when there is the m multiline option" do
398
+
399
+ let(:options) { 'm' }
400
+
401
+ it "does not set an option on the Ruby Regexp object" do
402
+ expect(ruby_regexp.options).to eq(0)
403
+ end
404
+ end
405
+
406
+ context "when there is the s dotall option" do
407
+
408
+ let(:options) { 's' }
409
+
410
+ # s in a bson regex maps to a Ruby Multiline Regexp option
411
+ it "sets the m option on the Ruby Regexp object" do
412
+ expect(ruby_regexp.options).to eq(::Regexp::MULTILINE)
413
+ end
414
+ end
415
+
416
+ context "when there is the u match unicode option" do
417
+
418
+ let(:options) { 'u' }
419
+
420
+ it "does not set an option on the Ruby Regexp object" do
421
+ expect(ruby_regexp.options).to eq(0)
422
+ end
423
+ end
424
+
425
+ context "when there is the x verbose option" do
426
+
427
+ let(:options) { 'x' }
428
+
429
+ it "sets the x option on the Ruby Regexp object" do
430
+ expect(ruby_regexp.options).to eq(::Regexp::EXTENDED)
431
+ end
432
+ end
433
+
434
+ context "when all options are set" do
435
+
436
+ let(:options) { 'ilmsux' }
437
+
438
+ # s in a bson regex maps to a Ruby Multiline Regexp option
439
+ it "sets the i, m, and x options on the Ruby Regexp object" do
440
+ expect(ruby_regexp.options).to eq(::Regexp::IGNORECASE | ::Regexp::MULTILINE | ::Regexp::EXTENDED)
441
+ end
442
+ end
443
+ end
444
+ end
445
+
446
+ context "when a Regexp::Raw object is roundtripped" do
447
+
448
+ let(:obj) { Regexp::Raw.new(pattern, options) }
449
+ let(:serialized) { obj.to_bson.to_s }
450
+ let(:roundtripped) { Regexp.from_bson(BSON::ByteBuffer.new(serialized)) }
451
+
452
+ it "roundtrips the pattern" do
453
+ expect(roundtripped.pattern).to eq(pattern)
454
+ end
455
+
456
+ context "when there are no options" do
457
+
458
+ let(:options) { '' }
459
+
460
+ it "does not set any options on the roundtripped Regexp::Raw object" do
461
+ expect(roundtripped.options).to eq(options)
462
+ end
463
+ end
464
+
465
+ context "when there are options set" do
466
+
467
+ context "when there is the i ignorecase option" do
468
+
469
+ let(:options) { 'i' }
470
+
471
+ it "sets the i option on the roundtripped Regexp::Raw object" do
472
+ expect(roundtripped.options).to eq(options)
473
+ end
474
+ end
475
+
476
+ context "when there is the l locale dependent option" do
477
+
478
+ let(:options) { 'l' }
479
+
480
+ it "sets the l option on the roundtripped Regexp::Raw object" do
481
+ expect(roundtripped.options).to eq(options)
482
+ end
483
+ end
484
+
485
+ context "when there is the m multiline option" do
486
+
487
+ let(:options) { 'm' }
488
+
489
+ it "sets the m option on the roundtripped Regexp::Raw object" do
490
+ expect(roundtripped.options).to eq(options)
491
+ end
492
+ end
493
+
494
+ context "when there is the s dotall option" do
495
+
496
+ let(:options) { 's' }
497
+
498
+ it "sets the s option on the roundtripped Regexp::Raw object" do
499
+ expect(roundtripped.options).to eq(options)
500
+ end
501
+ end
502
+
503
+ context "when there is the u match unicode option" do
504
+
505
+ let(:options) { 'u' }
506
+
507
+ it "sets the u option on the roundtripped Regexp::Raw object" do
508
+ expect(roundtripped.options).to eq(options)
509
+ end
510
+ end
511
+
512
+ context "when there is the x verbose option" do
513
+
514
+ let(:options) { 'x' }
515
+
516
+ it "sets the x option on the roundtripped Regexp::Raw object" do
517
+ expect(roundtripped.options).to eq(options)
518
+ end
519
+ end
520
+
521
+ context "when all options are set" do
522
+
523
+ let(:options) { 'ilmsux' }
524
+
525
+ it "sets all the options on the roundtripped Regexp::Raw object" do
526
+ expect(roundtripped.options).to eq(options)
527
+ end
528
+
529
+ context "when the options are passed in not in alphabetical order" do
530
+
531
+ let(:options) { 'sumlxi' }
532
+
533
+ it "sets all the options on the roundtripped Regexp::Raw object in order" do
534
+ expect(roundtripped.options).to eq(options.chars.sort.join)
535
+ end
536
+ end
537
+ end
538
+ end
539
+ end
540
+ end