bson 4.15.0 → 5.0.2

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.
Files changed (153) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +94 -10
  3. data/Rakefile +68 -39
  4. data/ext/bson/bson-native.h +12 -4
  5. data/ext/bson/extconf.rb +8 -3
  6. data/ext/bson/init.c +11 -11
  7. data/ext/bson/read.c +39 -9
  8. data/ext/bson/util.c +171 -16
  9. data/ext/bson/write.c +34 -39
  10. data/lib/bson/active_support.rb +1 -0
  11. data/lib/bson/array.rb +58 -32
  12. data/lib/bson/big_decimal.rb +16 -6
  13. data/lib/bson/binary.rb +271 -129
  14. data/lib/bson/boolean.rb +1 -0
  15. data/lib/bson/code.rb +10 -12
  16. data/lib/bson/code_with_scope.rb +9 -11
  17. data/lib/bson/config.rb +1 -27
  18. data/lib/bson/date.rb +2 -1
  19. data/lib/bson/date_time.rb +2 -1
  20. data/lib/bson/db_pointer.rb +12 -13
  21. data/lib/bson/dbref.rb +11 -9
  22. data/lib/bson/decimal128/builder.rb +10 -9
  23. data/lib/bson/decimal128.rb +25 -111
  24. data/lib/bson/document.rb +1 -0
  25. data/lib/bson/environment.rb +1 -0
  26. data/lib/bson/error/bson_decode_error.rb +11 -0
  27. data/lib/bson/error/ext_json_parse_error.rb +11 -0
  28. data/lib/bson/error/illegal_key.rb +23 -0
  29. data/lib/bson/error/invalid_binary_type.rb +37 -0
  30. data/lib/bson/error/invalid_dbref_argument.rb +12 -0
  31. data/lib/bson/error/invalid_decimal128_argument.rb +25 -0
  32. data/lib/bson/error/invalid_decimal128_range.rb +27 -0
  33. data/lib/bson/error/invalid_decimal128_string.rb +26 -0
  34. data/lib/bson/error/invalid_key.rb +24 -0
  35. data/lib/bson/error/invalid_object_id.rb +11 -0
  36. data/lib/bson/error/invalid_regexp_pattern.rb +13 -0
  37. data/lib/bson/error/unrepresentable_precision.rb +19 -0
  38. data/lib/bson/error/unserializable_class.rb +13 -0
  39. data/lib/bson/error/unsupported_binary_subtype.rb +12 -0
  40. data/lib/bson/error/unsupported_type.rb +11 -0
  41. data/lib/bson/error.rb +16 -28
  42. data/lib/bson/ext_json.rb +2 -1
  43. data/lib/bson/false_class.rb +2 -1
  44. data/lib/bson/float.rb +3 -2
  45. data/lib/bson/hash.rb +128 -73
  46. data/lib/bson/int32.rb +17 -5
  47. data/lib/bson/int64.rb +17 -5
  48. data/lib/bson/integer.rb +4 -5
  49. data/lib/bson/json.rb +1 -0
  50. data/lib/bson/max_key.rb +8 -10
  51. data/lib/bson/min_key.rb +8 -10
  52. data/lib/bson/nil_class.rb +1 -0
  53. data/lib/bson/object.rb +7 -27
  54. data/lib/bson/object_id.rb +84 -120
  55. data/lib/bson/open_struct.rb +3 -2
  56. data/lib/bson/regexp.rb +36 -65
  57. data/lib/bson/registry.rb +2 -6
  58. data/lib/bson/specialized.rb +2 -1
  59. data/lib/bson/string.rb +4 -27
  60. data/lib/bson/symbol.rb +23 -20
  61. data/lib/bson/time.rb +3 -2
  62. data/lib/bson/time_with_zone.rb +13 -1
  63. data/lib/bson/timestamp.rb +3 -2
  64. data/lib/bson/true_class.rb +2 -1
  65. data/lib/bson/undefined.rb +15 -1
  66. data/lib/bson/version.rb +3 -1
  67. data/lib/bson.rb +3 -2
  68. data/spec/bson/array_spec.rb +19 -60
  69. data/spec/bson/big_decimal_spec.rb +16 -4
  70. data/spec/bson/binary_spec.rb +129 -81
  71. data/spec/bson/binary_uuid_spec.rb +1 -0
  72. data/spec/bson/boolean_spec.rb +1 -0
  73. data/spec/bson/byte_buffer_read_spec.rb +1 -0
  74. data/spec/bson/byte_buffer_spec.rb +1 -0
  75. data/spec/bson/byte_buffer_write_spec.rb +1 -0
  76. data/spec/bson/code_spec.rb +5 -3
  77. data/spec/bson/code_with_scope_spec.rb +5 -3
  78. data/spec/bson/config_spec.rb +1 -35
  79. data/spec/bson/date_spec.rb +1 -0
  80. data/spec/bson/date_time_spec.rb +1 -0
  81. data/spec/bson/dbref_legacy_spec.rb +20 -3
  82. data/spec/bson/dbref_spec.rb +9 -9
  83. data/spec/bson/decimal128_spec.rb +40 -20
  84. data/spec/bson/document_as_spec.rb +1 -0
  85. data/spec/bson/document_spec.rb +1 -1
  86. data/spec/bson/ext_json_parse_spec.rb +1 -0
  87. data/spec/bson/false_class_spec.rb +8 -0
  88. data/spec/bson/float_spec.rb +8 -3
  89. data/spec/bson/hash_as_spec.rb +1 -0
  90. data/spec/bson/hash_spec.rb +87 -75
  91. data/spec/bson/int32_spec.rb +21 -6
  92. data/spec/bson/int64_spec.rb +21 -6
  93. data/spec/bson/integer_spec.rb +45 -13
  94. data/spec/bson/json_spec.rb +1 -0
  95. data/spec/bson/max_key_spec.rb +5 -3
  96. data/spec/bson/min_key_spec.rb +5 -3
  97. data/spec/bson/nil_class_spec.rb +1 -0
  98. data/spec/bson/object_id_spec.rb +57 -4
  99. data/spec/bson/object_spec.rb +2 -1
  100. data/spec/bson/open_struct_spec.rb +14 -71
  101. data/spec/bson/raw_spec.rb +9 -15
  102. data/spec/bson/regexp_spec.rb +4 -3
  103. data/spec/bson/registry_spec.rb +2 -1
  104. data/spec/bson/string_spec.rb +13 -38
  105. data/spec/bson/symbol_raw_spec.rb +25 -0
  106. data/spec/bson/symbol_spec.rb +15 -18
  107. data/spec/bson/time_spec.rb +1 -0
  108. data/spec/bson/time_with_zone_spec.rb +1 -0
  109. data/spec/bson/timestamp_spec.rb +1 -0
  110. data/spec/bson/true_class_spec.rb +8 -0
  111. data/spec/bson/undefined_spec.rb +27 -0
  112. data/spec/bson_spec.rb +1 -0
  113. data/spec/runners/common_driver.rb +6 -5
  114. data/spec/runners/corpus.rb +6 -0
  115. data/spec/runners/corpus_legacy.rb +1 -0
  116. data/spec/spec_helper.rb +1 -0
  117. data/spec/spec_tests/common_driver_spec.rb +9 -4
  118. data/spec/spec_tests/corpus_legacy_spec.rb +1 -0
  119. data/spec/spec_tests/corpus_spec.rb +13 -3
  120. data/spec/spec_tests/data/corpus/binary.json +5 -0
  121. data/spec/spec_tests/data/corpus/code.json +13 -13
  122. data/spec/spec_tests/data/corpus/decimal128-4.json +48 -0
  123. data/spec/spec_tests/data/corpus/decimal128-6.json +12 -0
  124. data/spec/spec_tests/data/corpus/decimal128-7.json +4 -0
  125. data/spec/spec_tests/data/corpus/document.json +20 -0
  126. data/spec/spec_tests/data/corpus/symbol.json +7 -7
  127. data/spec/spec_tests/data/corpus/top.json +18 -3
  128. data/spec/support/shared_examples.rb +28 -5
  129. data/spec/support/spec_config.rb +1 -0
  130. data/spec/support/utils.rb +49 -1
  131. metadata +114 -164
  132. checksums.yaml.gz.sig +0 -0
  133. data/spec/shared/LICENSE +0 -20
  134. data/spec/shared/bin/get-mongodb-download-url +0 -17
  135. data/spec/shared/bin/s3-copy +0 -45
  136. data/spec/shared/bin/s3-upload +0 -69
  137. data/spec/shared/lib/mrss/child_process_helper.rb +0 -80
  138. data/spec/shared/lib/mrss/cluster_config.rb +0 -231
  139. data/spec/shared/lib/mrss/constraints.rb +0 -386
  140. data/spec/shared/lib/mrss/docker_runner.rb +0 -271
  141. data/spec/shared/lib/mrss/event_subscriber.rb +0 -200
  142. data/spec/shared/lib/mrss/lite_constraints.rb +0 -191
  143. data/spec/shared/lib/mrss/server_version_registry.rb +0 -120
  144. data/spec/shared/lib/mrss/spec_organizer.rb +0 -179
  145. data/spec/shared/lib/mrss/utils.rb +0 -15
  146. data/spec/shared/share/Dockerfile.erb +0 -338
  147. data/spec/shared/share/haproxy-1.conf +0 -16
  148. data/spec/shared/share/haproxy-2.conf +0 -17
  149. data/spec/shared/shlib/distro.sh +0 -74
  150. data/spec/shared/shlib/server.sh +0 -367
  151. data/spec/shared/shlib/set_env.sh +0 -131
  152. data.tar.gz.sig +0 -0
  153. metadata.gz.sig +0 -1
@@ -1,3 +1,4 @@
1
+ # rubocop:todo all
1
2
  # Copyright (C) 2016-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -28,10 +29,10 @@ describe BSON::Decimal128 do
28
29
 
29
30
  context 'when the argument is neither a BigDecimal or String' do
30
31
 
31
- it 'raises an ArgumentError' do
32
+ it 'raises an InvalidDecimal128Argument error' do
32
33
  expect {
33
34
  described_class.new(:invalid)
34
- }.to raise_exception(ArgumentError)
35
+ }.to raise_exception(BSON::Error::InvalidDecimal128Argument, /A Decimal128 can only be created from a String or BigDecimal./ )
35
36
  end
36
37
  end
37
38
 
@@ -46,7 +47,7 @@ describe BSON::Decimal128 do
46
47
  end
47
48
 
48
49
  let(:from_bson) do
49
- described_class.from_bson(buffer)
50
+ described_class.from_bson(buffer, mode: :bson)
50
51
  end
51
52
 
52
53
  let(:expected_bson) do
@@ -314,7 +315,7 @@ describe BSON::Decimal128 do
314
315
  it 'raises InvalidRange' do
315
316
  lambda do
316
317
  described_class.new('1e10000')
317
- end.should raise_error(BSON::Decimal128::InvalidRange, /Value out of range/)
318
+ end.should raise_error(BSON::Error::InvalidDecimal128Range, /Value out of range/)
318
319
  end
319
320
  end
320
321
 
@@ -322,7 +323,7 @@ describe BSON::Decimal128 do
322
323
  it 'raises UnrepresentablePrecision' do
323
324
  lambda do
324
325
  described_class.new('1.000000000000000000000000000000000000000000000000001')
325
- end.should raise_error(BSON::Decimal128::UnrepresentablePrecision, /The value contains too much precision/)
326
+ end.should raise_error(BSON::Error::UnrepresentablePrecision, /The value contains too much precision/)
326
327
  end
327
328
  end
328
329
  end
@@ -344,7 +345,7 @@ describe BSON::Decimal128 do
344
345
  end
345
346
 
346
347
  let(:decimal128) do
347
- BSON::Document.from_bson(buffer)['d']
348
+ BSON::Document.from_bson(buffer, mode: :bson)['d']
348
349
  end
349
350
 
350
351
  let(:object_from_string) do
@@ -1270,13 +1271,39 @@ describe BSON::Decimal128 do
1270
1271
  described_class.new(BigDecimal('1.23'))
1271
1272
  end
1272
1273
 
1273
- it "returns the decimal128 with $numberDecimal key" do
1274
- expect(object.as_json).to eq({ "$numberDecimal" => object.to_s })
1274
+ it "returns the decimal128 as a string" do
1275
+ expect(object.as_json).to eq('1.23')
1276
+ end
1277
+
1278
+ %w[NaN -NaN SNaN -SNaN Infinity -Infinity].each do |value|
1279
+ context "when special value #{value}" do
1280
+
1281
+ let(:object) do
1282
+ described_class.new(value)
1283
+ end
1284
+
1285
+ it "returns nil to be consistent with ActiveSupport" do
1286
+ expect(object.as_json).to eq(nil)
1287
+ end
1288
+ end
1275
1289
  end
1276
1290
 
1277
1291
  it_behaves_like "a JSON serializable object"
1278
1292
  end
1279
1293
 
1294
+ describe "#as_extended_json" do
1295
+
1296
+ let(:object) do
1297
+ described_class.new(BigDecimal('1.23'))
1298
+ end
1299
+
1300
+ it "returns the decimal128 with $numberDecimal key" do
1301
+ expect(object.as_extended_json).to eq({ "$numberDecimal" => '1.23' })
1302
+ end
1303
+
1304
+ it_behaves_like "an Extended JSON serializable object"
1305
+ end
1306
+
1280
1307
  describe "::BSON_TYPE" do
1281
1308
 
1282
1309
  it "returns 0x13" do
@@ -1361,7 +1388,7 @@ describe BSON::Decimal128 do
1361
1388
  end
1362
1389
  end
1363
1390
 
1364
- describe "#to_big_decimal" do
1391
+ describe "#to_d" do
1365
1392
 
1366
1393
  shared_examples_for 'a decimal128 convertible to a Ruby BigDecimal' do
1367
1394
 
@@ -1370,6 +1397,10 @@ describe BSON::Decimal128 do
1370
1397
  end
1371
1398
 
1372
1399
  it 'properly converts the Decimal128 to a BigDecimal' do
1400
+ expect(decimal128.to_d).to eq(expected_big_decimal)
1401
+ end
1402
+
1403
+ it 'permits the alias #to_big_decimal' do
1373
1404
  expect(decimal128.to_big_decimal).to eq(expected_big_decimal)
1374
1405
  end
1375
1406
  end
@@ -1592,17 +1623,6 @@ describe BSON::Decimal128 do
1592
1623
  end
1593
1624
  end
1594
1625
 
1595
- context "when the class is loaded" do
1596
-
1597
- let(:registered) do
1598
- BSON::Registry.get(described_class::BSON_TYPE, 'field')
1599
- end
1600
-
1601
- it "registers the type" do
1602
- expect(registered).to eq(described_class)
1603
- end
1604
- end
1605
-
1606
1626
  %w(== ===).each do |eq_op|
1607
1627
  let(:lhs) { described_class.new('1.2e12') }
1608
1628
 
@@ -1,3 +1,4 @@
1
+ # rubocop:todo all
1
2
  # Copyright (C) 2021 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # rubocop:todo all
2
2
 
3
3
  # Copyright (C) 2009-2020 MongoDB Inc.
4
4
  #
@@ -1,3 +1,4 @@
1
+ # rubocop:todo all
1
2
  # Copyright (C) 2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -1,3 +1,4 @@
1
+ # rubocop:todo all
1
2
  # Copyright (C) 2009-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -25,4 +26,11 @@ describe FalseClass do
25
26
  it_behaves_like "a bson element"
26
27
  it_behaves_like "a serializable bson element"
27
28
  end
29
+
30
+ describe '#as_extended_json' do
31
+ let(:object) { false }
32
+
33
+ it_behaves_like '#as_extended_json returns self'
34
+ it_behaves_like 'an Extended JSON serializable object'
35
+ end
28
36
  end
@@ -1,3 +1,4 @@
1
+ # rubocop:todo all
1
2
  # Copyright (C) 2009-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -34,15 +35,17 @@ describe Float do
34
35
  end
35
36
 
36
37
  describe '#as_extended_json' do
38
+ let(:object) { 42.0 }
39
+
37
40
  context 'canonical mode' do
38
41
  it 'returns $numberDouble' do
39
- 42.0.as_extended_json.should == {'$numberDouble' => '42.0'}
42
+ object.as_extended_json.should == {'$numberDouble' => '42.0'}
40
43
  end
41
44
  end
42
45
 
43
46
  context 'relaxed mode' do
44
47
  let(:serialized) do
45
- 42.0.as_extended_json(mode: :relaxed)
48
+ object.as_extended_json(mode: :relaxed)
46
49
  end
47
50
 
48
51
  it 'returns float' do
@@ -53,7 +56,7 @@ describe Float do
53
56
 
54
57
  context 'legacy mode' do
55
58
  let(:serialized) do
56
- 42.0.as_extended_json(mode: :legacy)
59
+ object.as_extended_json(mode: :legacy)
57
60
  end
58
61
 
59
62
  it 'returns float' do
@@ -61,5 +64,7 @@ describe Float do
61
64
  serialized.should be_within(0.00001).of(42)
62
65
  end
63
66
  end
67
+
68
+ it_behaves_like "an Extended JSON serializable object"
64
69
  end
65
70
  end
@@ -1,3 +1,4 @@
1
+ # rubocop:todo all
1
2
  # Copyright (C) 2021 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -1,3 +1,4 @@
1
+ # rubocop:todo all
1
2
  # Copyright (C) 2009-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -52,93 +53,36 @@ describe Hash do
52
53
  end
53
54
  end
54
55
 
55
- context "when the hash has invalid keys" do
56
+ context "when the hash has dollar keys" do
56
57
 
57
58
  let(:obj) do
58
59
  { "$testing" => "value" }
59
60
  end
60
61
 
61
- context "when validating keys" do
62
62
 
63
- context "when validating globally" do
64
-
65
- before do
66
- BSON::Config.validating_keys = true
67
- end
68
-
69
- after do
70
- BSON::Config.validating_keys = false
71
- end
72
-
73
- it "raises an error" do
74
- expect {
75
- obj.to_bson
76
- }.to raise_error(BSON::String::IllegalKey)
77
- end
78
-
79
- context "when the hash contains an array of documents containing invalid keys" do
80
-
81
- let(:obj) do
82
- { "array" => [{ "$testing" => "value" }] }
83
- end
84
-
85
- it "raises an error" do
86
- expect {
87
- obj.to_bson
88
- }.to raise_error(BSON::String::IllegalKey)
89
- end
90
- end
91
- end
92
-
93
- context "when validating locally" do
94
-
95
- it "raises an error" do
96
- expect {
97
- obj.to_bson(BSON::ByteBuffer.new, true)
98
- }.to raise_error(BSON::String::IllegalKey)
99
- end
63
+ let(:bson) do
64
+ "#{25.to_bson.to_s}#{String::BSON_TYPE}$testing#{BSON::NULL_BYTE}" +
65
+ "#{6.to_bson.to_s}value#{BSON::NULL_BYTE}#{BSON::NULL_BYTE}"
66
+ end
100
67
 
101
- context "when the hash contains an array of documents containing invalid keys" do
68
+ it "serializes the hash" do
69
+ expect(obj.to_bson.to_s).to eq(bson)
70
+ end
102
71
 
103
- let(:obj) do
104
- { "array" => [{ "$testing" => "value" }] }
105
- end
72
+ context "when the hash contains an array of documents containing invalid keys" do
106
73
 
107
- it "raises an error" do
108
- expect {
109
- obj.to_bson(BSON::ByteBuffer.new, true)
110
- }.to raise_error(BSON::String::IllegalKey)
111
- end
112
- end
74
+ let(:obj) do
75
+ { "array" => [{ "$testing" => "value" }] }
113
76
  end
114
- end
115
-
116
- context "when not validating keys" do
117
77
 
118
78
  let(:bson) do
119
- "#{25.to_bson.to_s}#{String::BSON_TYPE}$testing#{BSON::NULL_BYTE}" +
120
- "#{6.to_bson.to_s}value#{BSON::NULL_BYTE}#{BSON::NULL_BYTE}"
79
+ "#{45.to_bson.to_s}#{Array::BSON_TYPE}array#{BSON::NULL_BYTE}" +
80
+ "#{[{ "$testing" => "value" }].to_bson.to_s}#{BSON::NULL_BYTE}"
121
81
  end
122
82
 
123
83
  it "serializes the hash" do
124
84
  expect(obj.to_bson.to_s).to eq(bson)
125
85
  end
126
-
127
- context "when the hash contains an array of documents containing invalid keys" do
128
-
129
- let(:obj) do
130
- { "array" => [{ "$testing" => "value" }] }
131
- end
132
-
133
- let(:bson) do
134
- "#{45.to_bson.to_s}#{Array::BSON_TYPE}array#{BSON::NULL_BYTE}" +
135
- "#{[{ "$testing" => "value" }].to_bson.to_s}#{BSON::NULL_BYTE}"
136
- end
137
-
138
- it "serializes the hash" do
139
- expect(obj.to_bson.to_s).to eq(bson)
140
- end
141
- end
142
86
  end
143
87
  end
144
88
 
@@ -270,8 +214,28 @@ describe Hash do
270
214
  end.to_not raise_error
271
215
  end
272
216
 
273
- it 'deserializes as a BSON::Decimal128' do
274
- expect(from_bson).to eq({"x" => BSON::Decimal128.new('1')})
217
+ it 'deserializes as a BigDecimal' do
218
+ expect(from_bson).to eq({"x" => BigDecimal(1)})
219
+ end
220
+ end
221
+
222
+ context 'when deserializing round-tripping a Decimal128' do
223
+ let(:to_bson) do
224
+ {x:BSON::Decimal128.new('1')}.to_bson
225
+ end
226
+
227
+ let(:from_bson) do
228
+ Hash.from_bson(to_bson)
229
+ end
230
+
231
+ it 'doesn\'t raise on serialization' do
232
+ expect do
233
+ to_bson
234
+ end.to_not raise_error
235
+ end
236
+
237
+ it 'deserializes as a BigDecimal' do
238
+ expect(from_bson).to eq({"x" => BigDecimal(1)})
275
239
  end
276
240
  end
277
241
  end
@@ -348,7 +312,7 @@ describe Hash do
348
312
  it 'works' do
349
313
  expect do
350
314
  hash.to_bson
351
- end.not_to raise_error
315
+ end.to_not raise_error
352
316
  end
353
317
  end
354
318
  end
@@ -372,7 +336,7 @@ describe Hash do
372
336
  let(:doc) { Hash.from_bson(buf) }
373
337
 
374
338
  it 'overwrites first value with second value' do
375
- doc.should == {'foo' => 'overwrite'}
339
+ expect(doc).to eq({ 'foo' => 'overwrite' })
376
340
  end
377
341
  end
378
342
 
@@ -394,8 +358,56 @@ describe Hash do
394
358
  let(:doc) { Hash.from_bson(buf) }
395
359
 
396
360
  it 'overwrites first value with second value' do
397
- doc.should == {'foo' => :bar}
361
+ expect(doc).to eq({ 'foo' => :bar })
398
362
  end
399
363
  end
364
+
365
+ context 'when deserializing a hash with a BigDecimal' do
366
+ let(:to_bson) do
367
+ {x: BigDecimal('1')}.to_bson
368
+ end
369
+
370
+ context 'when it has mode: :bson' do
371
+
372
+ let(:from_bson) do
373
+ Hash.from_bson(to_bson, mode: :bson)
374
+ end
375
+
376
+ it 'deserializes as a BigDecimal' do
377
+ expect(from_bson).to eq({"x" => BSON::Decimal128.new('1')})
378
+ end
379
+ end
380
+
381
+ context 'when it doesn\'t have mode: :bson' do
382
+
383
+ let(:from_bson) do
384
+ Hash.from_bson(to_bson)
385
+ end
386
+
387
+ it 'deserializes as a BigDecimal' do
388
+ expect(from_bson).to eq({"x" => BigDecimal(1)})
389
+ end
390
+ end
391
+ end
392
+ end
393
+
394
+ describe '#as_extended_json' do
395
+ let(:object) do
396
+ { 'foo' => :bar, 'baz' => ['qux', 1, 2.0, { 'lorem' => 1 }] }
397
+ end
398
+
399
+ let(:expected) do
400
+ { "foo" => { "$symbol" => "bar" },
401
+ "baz" => [ "qux",
402
+ { "$numberInt" => "1" },
403
+ { "$numberDouble" => "2.0" },
404
+ { "lorem" => { "$numberInt" => "1" } } ] }
405
+ end
406
+
407
+ it "returns the binary data plus type" do
408
+ expect(object.as_extended_json).to eq(expected)
409
+ end
410
+
411
+ it_behaves_like 'an Extended JSON serializable object'
400
412
  end
401
413
  end
@@ -1,3 +1,4 @@
1
+ # rubocop:todo all
1
2
  # Copyright (C) 2009-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -254,23 +255,37 @@ describe BSON::Int32 do
254
255
  end
255
256
  end
256
257
 
258
+ describe '#as_json' do
259
+ let(:object) { described_class.new(42) }
260
+
261
+ it 'returns an Integer' do
262
+ expect(object.as_json).to eq 42
263
+ end
264
+
265
+ it_behaves_like "a JSON serializable object"
266
+ end
267
+
257
268
  describe '#as_extended_json' do
269
+ let(:object) { described_class.new(42) }
270
+
258
271
  context 'canonical mode' do
259
- it 'returns $numberInt' do
260
- described_class.new(42).as_extended_json.should == {'$numberInt' => '42'}
272
+ it 'returns a Hash with key $numberInt' do
273
+ expect(object.as_extended_json).to eq({ '$numberInt' => '42' })
261
274
  end
262
275
  end
263
276
 
264
277
  context 'relaxed mode' do
265
- it 'returns integer' do
266
- described_class.new(42).as_extended_json(mode: :relaxed).should == 42
278
+ it 'returns an Integer' do
279
+ expect(object.as_extended_json(mode: :relaxed)).to eq 42
267
280
  end
268
281
  end
269
282
 
270
283
  context 'legacy mode' do
271
- it 'returns integer' do
272
- described_class.new(42).as_extended_json(mode: :legacy).should be 42
284
+ it 'returns an Integer' do
285
+ expect(object.as_extended_json(mode: :legacy)).to eq 42
273
286
  end
274
287
  end
288
+
289
+ it_behaves_like "an Extended JSON serializable object"
275
290
  end
276
291
  end
@@ -1,3 +1,4 @@
1
+ # rubocop:todo all
1
2
  # Copyright (C) 2009-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -298,23 +299,37 @@ describe BSON::Int64 do
298
299
  end
299
300
  end
300
301
 
302
+ describe '#as_json' do
303
+ let(:object) { described_class.new(42) }
304
+
305
+ it 'returns an Integer' do
306
+ expect(object.as_json).to eq 42
307
+ end
308
+
309
+ it_behaves_like "a JSON serializable object"
310
+ end
311
+
301
312
  describe '#as_extended_json' do
313
+ let(:object) { described_class.new(42) }
314
+
302
315
  context 'canonical mode' do
303
- it 'returns $numberLong' do
304
- described_class.new(42).as_extended_json.should == {'$numberLong' => '42'}
316
+ it 'returns a Hash with key $numberLong' do
317
+ expect(object.as_extended_json).to eq({ '$numberLong' => '42' })
305
318
  end
306
319
  end
307
320
 
308
321
  context 'relaxed mode' do
309
- it 'returns integer' do
310
- described_class.new(42).as_extended_json(mode: :relaxed).should == 42
322
+ it 'returns an Integer' do
323
+ expect(object.as_extended_json(mode: :relaxed)).to eq 42
311
324
  end
312
325
  end
313
326
 
314
327
  context 'legacy mode' do
315
- it 'returns integer' do
316
- described_class.new(42).as_extended_json(mode: :legacy).should be 42
328
+ it 'returns an Integer' do
329
+ expect(object.as_extended_json(mode: :legacy)).to eq 42
317
330
  end
318
331
  end
332
+
333
+ it_behaves_like "an Extended JSON serializable object"
319
334
  end
320
335
  end
@@ -1,3 +1,4 @@
1
+ # rubocop:todo all
1
2
  # Copyright (C) 2009-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -69,29 +70,60 @@ describe Integer do
69
70
  end
70
71
  end
71
72
 
72
- describe '#to_json' do
73
- it 'returns integer' do
74
- 42.to_json.should == '42'
73
+ describe '#as_json' do
74
+ it 'returns an integer string' do
75
+ expect(42.to_json).to eq '42'
75
76
  end
76
77
  end
77
78
 
78
79
  describe '#as_extended_json' do
79
- context 'canonical mode' do
80
- it 'returns $numberInt' do
81
- 42.as_extended_json.should == {'$numberInt' => '42'}
80
+
81
+ context 'when 32-bit representable' do
82
+ let(:object) { 42 }
83
+
84
+ context 'canonical mode' do
85
+ it 'returns $numberInt when small' do
86
+ expect(object.as_extended_json).to eq({ '$numberInt' => '42' })
87
+ end
82
88
  end
83
- end
84
89
 
85
- context 'relaxed mode' do
86
- it 'returns integer' do
87
- 42.as_extended_json(mode: :relaxed).should be 42
90
+ context 'relaxed mode' do
91
+ it 'returns integer' do
92
+ expect(object.as_extended_json(mode: :relaxed)).to eq 42
93
+ end
88
94
  end
95
+
96
+ context 'legacy mode' do
97
+ it 'returns integer' do
98
+ expect(object.as_extended_json(mode: :legacy)).to eq 42
99
+ end
100
+ end
101
+
102
+ it_behaves_like "an Extended JSON serializable object"
89
103
  end
90
104
 
91
- context 'legacy mode' do
92
- it 'returns integer' do
93
- 42.as_extended_json(mode: :legacy).should be 42
105
+ context 'when not 32-bit representable' do
106
+ let(:object) { 18014398241046527 }
107
+
108
+ context 'canonical mode' do
109
+ it 'returns $numberInt when small' do
110
+ expect(object.as_extended_json).to eq({ '$numberLong' => '18014398241046527' })
111
+ end
112
+ end
113
+
114
+ context 'relaxed mode' do
115
+ it 'returns integer' do
116
+ expect(object.as_extended_json(mode: :relaxed)).to eq 18014398241046527
117
+ end
94
118
  end
119
+
120
+ context 'legacy mode' do
121
+ it 'returns integer' do
122
+ expect(object.as_extended_json(mode: :legacy)).to eq 18014398241046527
123
+ end
124
+ end
125
+
126
+ it_behaves_like "an Extended JSON serializable object"
95
127
  end
96
128
  end
97
129
  end
@@ -1,3 +1,4 @@
1
+ # rubocop:todo all
1
2
  # Copyright (C) 2009-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -1,3 +1,4 @@
1
+ # rubocop:todo all
1
2
  # Copyright (C) 2009-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -49,17 +50,18 @@ describe BSON::MaxKey do
49
50
  end
50
51
  end
51
52
 
52
- describe "#as_json" do
53
+ describe "#as_extended_json" do
53
54
 
54
55
  let(:object) do
55
56
  described_class.new
56
57
  end
57
58
 
58
59
  it "returns the binary data plus type" do
59
- expect(object.as_json).to eq({ "$maxKey" => 1 })
60
+ expect(object.as_extended_json).to eq({ "$maxKey" => 1 })
60
61
  end
61
62
 
62
- it_behaves_like "a JSON serializable object"
63
+ it_behaves_like 'an Extended JSON serializable object'
64
+ it_behaves_like '#as_json calls #as_extended_json'
63
65
  end
64
66
 
65
67
  describe "#to_bson/#from_bson" do
@@ -1,3 +1,4 @@
1
+ # rubocop:todo all
1
2
  # Copyright (C) 2009-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,17 +17,18 @@ require "spec_helper"
16
17
 
17
18
  describe BSON::MinKey do
18
19
 
19
- describe "#as_json" do
20
+ describe "#as_extended_json" do
20
21
 
21
22
  let(:object) do
22
23
  described_class.new
23
24
  end
24
25
 
25
26
  it "returns the binary data plus type" do
26
- expect(object.as_json).to eq({ "$minKey" => 1 })
27
+ expect(object.as_extended_json).to eq({ "$minKey" => 1 })
27
28
  end
28
29
 
29
- it_behaves_like "a JSON serializable object"
30
+ it_behaves_like 'an Extended JSON serializable object'
31
+ it_behaves_like '#as_json calls #as_extended_json'
30
32
  end
31
33
 
32
34
  describe "#==" do
@@ -1,3 +1,4 @@
1
+ # rubocop:todo all
1
2
  # Copyright (C) 2009-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");