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) 2009-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -220,13 +221,24 @@ describe BSON::ObjectId do
220
221
  described_class.new
221
222
  end
222
223
 
223
- it "returns the object id with $oid key" do
224
- expect(object.as_json).to eq({ "$oid" => object.to_s })
224
+ it "returns the object id as a string" do
225
+ expect(object.as_json).to eq(object.to_s)
225
226
  end
226
227
 
227
228
  it_behaves_like "a JSON serializable object"
228
229
  end
229
230
 
231
+ describe "#as_extended_json" do
232
+
233
+ let(:object) do
234
+ described_class.new
235
+ end
236
+
237
+ it "returns the object id with $oid key" do
238
+ expect(object.as_extended_json).to eq({ "$oid" => object.to_s })
239
+ end
240
+ end
241
+
230
242
  describe "::BSON_TYPE" do
231
243
 
232
244
  it "returns 0x07" do
@@ -311,7 +323,7 @@ describe BSON::ObjectId do
311
323
  it "raises an error" do
312
324
  expect {
313
325
  described_class.from_string("asadsf")
314
- }.to raise_error(BSON::ObjectId::Invalid)
326
+ }.to raise_error(BSON::Error::InvalidObjectId)
315
327
  end
316
328
  end
317
329
  end
@@ -592,7 +604,48 @@ describe BSON::ObjectId do
592
604
  it "raises an exception on serialization" do
593
605
  expect {
594
606
  hash.to_bson
595
- }.to raise_error(BSON::InvalidKey)
607
+ }.to raise_error(BSON::Error::InvalidKey)
608
+ end
609
+ end
610
+
611
+ context 'when the counter wraps' do
612
+ before do
613
+ BSON::ObjectId._generator.reset_counter(0xFFFFFF)
614
+ end
615
+
616
+ let(:before) { BSON::ObjectId.new }
617
+ let(:after) { BSON::ObjectId.new }
618
+
619
+ it 'resets the counter portion to 0' do
620
+ expect(before._counter_part).to be == "ffffff"
621
+ expect(after._counter_part).to be == "000000"
622
+ end
623
+ end
624
+
625
+ context 'when the timestamp is larger than a 32-bit integer' do
626
+ let(:distant_future) { Time.at(2 ** 32) }
627
+
628
+ before do
629
+ allow(Time).to receive(:now).and_return(distant_future)
630
+ end
631
+
632
+ let(:object_id) { BSON::ObjectId.new }
633
+
634
+ it 'wraps the timestamp to 0' do
635
+ expect(object_id.to_time).to be == Time.at(0)
636
+ end
637
+ end
638
+
639
+ context 'when fork changes the pid' do
640
+ before do
641
+ skip 'requires Process.fork' unless Process.respond_to?(:fork)
642
+ end
643
+
644
+ let(:parent_id) { BSON::ObjectId.new }
645
+ let(:child_id) { Utils.perform_in_child { BSON::ObjectId.new } }
646
+
647
+ it 'changes the process portion of the object-id' do
648
+ expect(child_id._process_part).not_to be == parent_id._process_part
596
649
  end
597
650
  end
598
651
  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");
@@ -25,7 +26,7 @@ describe BSON::Object do
25
26
  it "raises an exception" do
26
27
  expect {
27
28
  object.to_bson_key
28
- }.to raise_error(BSON::InvalidKey)
29
+ }.to raise_error(BSON::Error::InvalidKey)
29
30
  end
30
31
  end
31
32
  end
@@ -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");
@@ -36,93 +37,35 @@ describe OpenStruct do
36
37
  it_behaves_like "a serializable bson element"
37
38
  end
38
39
 
39
- context "when the struct has invalid keys" do
40
+ context "when the struct has dollar keys" do
40
41
 
41
42
  let(:obj) do
42
43
  described_class.new({ "$testing" => "value" })
43
44
  end
44
45
 
45
- context "when validating keys" do
46
-
47
- context "when validating globally" do
48
-
49
- before do
50
- BSON::Config.validating_keys = true
51
- end
52
-
53
- after do
54
- BSON::Config.validating_keys = false
55
- end
56
-
57
- it "raises an error" do
58
- expect {
59
- obj.to_bson
60
- }.to raise_error(BSON::String::IllegalKey)
61
- end
62
-
63
- context "when the struct contains an array of documents containing invalid keys" do
64
-
65
- let(:obj) do
66
- described_class.new({ "array" => [{ "$testing" => "value" }] })
67
- end
68
-
69
- it "raises an error" do
70
- expect {
71
- obj.to_bson
72
- }.to raise_error(BSON::String::IllegalKey)
73
- end
74
- end
75
- end
76
-
77
- context "when validating locally" do
78
-
79
- it "raises an error" do
80
- expect {
81
- obj.to_bson(BSON::ByteBuffer.new, true)
82
- }.to raise_error(BSON::String::IllegalKey)
83
- end
46
+ let(:bson) do
47
+ "#{25.to_bson.to_s}#{String::BSON_TYPE}$testing#{BSON::NULL_BYTE}" +
48
+ "#{6.to_bson.to_s}value#{BSON::NULL_BYTE}#{BSON::NULL_BYTE}"
49
+ end
84
50
 
85
- context "when the struct contains an array of documents containing invalid keys" do
51
+ it "serializes the struct" do
52
+ expect(obj.to_bson.to_s).to eq(bson)
53
+ end
86
54
 
87
- let(:obj) do
88
- described_class.new({ "array" => [{ "$testing" => "value" }] })
89
- end
55
+ context "when the struct contains an array of documents containing invalid keys" do
90
56
 
91
- it "raises an error" do
92
- expect {
93
- obj.to_bson(BSON::ByteBuffer.new, true)
94
- }.to raise_error(BSON::String::IllegalKey)
95
- end
96
- end
57
+ let(:obj) do
58
+ described_class.new({ "array" => [{ "$testing" => "value" }] })
97
59
  end
98
- end
99
-
100
- context "when not validating keys" do
101
60
 
102
61
  let(:bson) do
103
- "#{25.to_bson.to_s}#{String::BSON_TYPE}$testing#{BSON::NULL_BYTE}" +
104
- "#{6.to_bson.to_s}value#{BSON::NULL_BYTE}#{BSON::NULL_BYTE}"
62
+ "#{45.to_bson.to_s}#{Array::BSON_TYPE}array#{BSON::NULL_BYTE}" +
63
+ "#{[{ "$testing" => "value" }].to_bson.to_s}#{BSON::NULL_BYTE}"
105
64
  end
106
65
 
107
66
  it "serializes the struct" do
108
67
  expect(obj.to_bson.to_s).to eq(bson)
109
68
  end
110
-
111
- context "when the struct contains an array of documents containing invalid keys" do
112
-
113
- let(:obj) do
114
- described_class.new({ "array" => [{ "$testing" => "value" }] })
115
- end
116
-
117
- let(:bson) do
118
- "#{45.to_bson.to_s}#{Array::BSON_TYPE}array#{BSON::NULL_BYTE}" +
119
- "#{[{ "$testing" => "value" }].to_bson.to_s}#{BSON::NULL_BYTE}"
120
- end
121
-
122
- it "serializes the struct" do
123
- expect(obj.to_bson.to_s).to eq(bson)
124
- end
125
- end
126
69
  end
127
70
  end
128
71
 
@@ -1,3 +1,4 @@
1
+ # rubocop:todo all
1
2
  require 'spec_helper'
2
3
 
3
4
  describe Regexp::Raw do
@@ -84,19 +85,10 @@ describe Regexp::Raw do
84
85
 
85
86
  let(:options) { ::Regexp::EXTENDED }
86
87
 
87
- it "sets the options on the raw regex" do
88
- expect(object.options). to eq(options)
89
- end
90
-
91
- context "When the raw regexp is compiled" do
92
-
93
- let(:regexp) do
94
- object.compile
95
- end
96
-
97
- it "sets the options on the compiled regexp object" do
98
- expect(regexp.options).to eq(options)
99
- end
88
+ it "raises an error" do
89
+ expect do
90
+ object
91
+ end.to raise_error(ArgumentError, /Regexp options must be a String or Symbol/)
100
92
  end
101
93
  end
102
94
 
@@ -320,8 +312,10 @@ describe Regexp::Raw do
320
312
  let(:options) { ::Regexp::EXTENDED }
321
313
  let(:bson) { "#{pattern}#{BSON::NULL_BYTE}mx#{BSON::NULL_BYTE}" }
322
314
 
323
- it "sets the option on the serialized bson object" do
324
- expect(serialized).to eq(bson)
315
+ it "raises an error" do
316
+ expect do
317
+ serialized
318
+ end.to raise_error(ArgumentError, /Regexp options must be a String or Symbol/)
325
319
  end
326
320
  end
327
321
 
@@ -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");
@@ -147,10 +148,10 @@ describe Regexp do
147
148
  Regexp::Raw.new("pattern", 1)
148
149
  end
149
150
 
150
- it "doesn't raise an error" do
151
+ it "raises an error" do
151
152
  expect do
152
153
  regexp
153
- end.to_not raise_error
154
+ end.to raise_error(ArgumentError, /Regexp options must be a String or Symbol/)
154
155
  end
155
156
  end
156
157
 
@@ -163,7 +164,7 @@ describe Regexp do
163
164
  it "raises an error" do
164
165
  expect do
165
166
  regexp
166
- end.to raise_error(ArgumentError, /Regexp options must be a String, Symbol, or Integer/)
167
+ end.to raise_error(ArgumentError, /Regexp options must be a String or Symbol/)
167
168
  end
168
169
  end
169
170
  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");
@@ -38,7 +39,7 @@ describe BSON::Registry do
38
39
  it "raises an error" do
39
40
  expect {
40
41
  described_class.get(25.chr, "field")
41
- }.to raise_error(BSON::Registry::UnsupportedType)
42
+ }.to raise_error(BSON::Error::UnsupportedType)
42
43
  end
43
44
  end
44
45
  end
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # rubocop:todo all
2
2
 
3
3
  # Copyright (C) 2009-2020 MongoDB Inc.
4
4
  #
@@ -51,7 +51,7 @@ describe String do
51
51
  it "raises an exception" do
52
52
  expect {
53
53
  "test".to_bson_object_id
54
- }.to raise_error(BSON::ObjectId::Invalid)
54
+ }.to raise_error(BSON::Error::InvalidObjectId)
55
55
  end
56
56
  end
57
57
  end
@@ -90,44 +90,12 @@ describe String do
90
90
 
91
91
  describe "#to_bson_key" do
92
92
 
93
- context "when validating keys" do
94
-
95
- context "when validating globally" do
96
-
97
- before do
98
- BSON::Config.validating_keys = true
99
- end
100
-
101
- after do
102
- BSON::Config.validating_keys = false
103
- end
104
-
105
- let(:validated) do
106
- string.to_bson_key
107
- end
108
-
109
- it_behaves_like "a validated BSON key"
110
- end
111
-
112
- context "when validating locally" do
113
-
114
- let(:validated) do
115
- string.to_bson_key(true)
116
- end
117
-
118
- it_behaves_like "a validated BSON key"
119
- end
93
+ let(:string) do
94
+ "$testing.testing"
120
95
  end
121
96
 
122
- context "when allowing invalid keys" do
123
-
124
- let(:string) do
125
- "$testing.testing"
126
- end
127
-
128
- it "allows invalid keys" do
129
- expect(string.to_bson_key).to eq(string)
130
- end
97
+ it "allows dots/dollars keys" do
98
+ expect(string.to_bson_key).to eq(string)
131
99
  end
132
100
  end
133
101
 
@@ -148,4 +116,11 @@ describe String do
148
116
  end
149
117
  end
150
118
  end
119
+
120
+ describe '#as_extended_json' do
121
+ let(:object) { 'Hello world!' }
122
+
123
+ it_behaves_like '#as_extended_json returns self'
124
+ it_behaves_like 'an Extended JSON serializable object'
125
+ end
151
126
  end
@@ -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");
@@ -42,4 +43,28 @@ describe BSON::Symbol::Raw do
42
43
  one.should_not be_eql(three)
43
44
  end
44
45
  end
46
+
47
+ describe '#as_json' do
48
+ let(:object) do
49
+ described_class.new(:foobar)
50
+ end
51
+
52
+ it 'returns a string' do
53
+ expect(object.as_json).to eq('foobar')
54
+ end
55
+
56
+ it_behaves_like 'a JSON serializable object'
57
+ end
58
+
59
+ describe '#as_extended_json' do
60
+ let(:object) do
61
+ described_class.new(:foobar)
62
+ end
63
+
64
+ it 'returns the binary data plus type' do
65
+ expect(object.as_extended_json).to eq({ '$symbol' => 'foobar' })
66
+ end
67
+
68
+ it_behaves_like 'an Extended JSON serializable object'
69
+ end
45
70
  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");
@@ -106,28 +107,24 @@ describe Symbol do
106
107
 
107
108
  describe "#to_bson_key" do
108
109
 
109
- context "when validating keys" do
110
-
111
- let(:symbol) do
112
- :'$testing.testing'
113
- end
114
-
115
- it "raises an exception" do
116
- expect {
117
- symbol.to_bson_key(true)
118
- }.to raise_error(BSON::String::IllegalKey)
119
- end
110
+ let(:symbol) do
111
+ :'$testing.testing'
120
112
  end
121
113
 
122
- context "when not validating keys" do
114
+ it "returns the key" do
115
+ expect(symbol.to_bson_key).to eq(symbol)
116
+ end
117
+ end
123
118
 
124
- let(:symbol) do
125
- :'$testing.testing'
126
- end
119
+ describe '#as_extended_json' do
120
+ let(:object) do
121
+ :foobar
122
+ end
127
123
 
128
- it "allows invalid keys" do
129
- expect(symbol.to_bson_key).to eq(symbol)
130
- end
124
+ it 'returns the binary data plus type' do
125
+ expect(object.as_extended_json).to eq({ '$symbol' => 'foobar' })
131
126
  end
127
+
128
+ it_behaves_like 'an Extended JSON serializable object'
132
129
  end
133
130
  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) 2018-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");
@@ -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 TrueClass 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) { true }
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");
@@ -26,4 +27,30 @@ describe BSON::Undefined do
26
27
  it_behaves_like "a serializable bson element"
27
28
  it_behaves_like "a deserializable bson element"
28
29
  end
30
+
31
+ describe "#as_json" do
32
+
33
+ let(:object) do
34
+ described_class.new
35
+ end
36
+
37
+ it "returns nil" do
38
+ expect(object.as_json).to eq(nil)
39
+ end
40
+
41
+ it_behaves_like 'a JSON serializable object'
42
+ end
43
+
44
+ describe "#as_extended_json" do
45
+
46
+ let(:object) do
47
+ described_class.new
48
+ end
49
+
50
+ it "returns the binary data plus type" do
51
+ expect(object.as_extended_json).to eq({ "$undefined" => true })
52
+ end
53
+
54
+ it_behaves_like 'an Extended JSON serializable object'
55
+ end
29
56
  end
data/spec/bson_spec.rb CHANGED
@@ -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) 2016-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -184,13 +185,13 @@ module BSON
184
185
  # The object as json, in a document with the test key.
185
186
  #
186
187
  # @example Get a document with the object at the test key.
187
- # test.document_as_json
188
+ # test.document_as_extended_json
188
189
  #
189
- # @return [ BSON::Document ] The json document.
190
+ # @return [ Hash ] The extended_json representation of document.
190
191
  #
191
192
  # @since 4.2.0
192
- def document_as_json
193
- { @test_key => object.as_json }
193
+ def document_as_extended_json
194
+ { @test_key => object.as_extended_json }
194
195
  end
195
196
 
196
197
  # Use the string in the extended json to instantiate the bson object.
@@ -340,7 +341,7 @@ module BSON
340
341
  def decoded_document
341
342
  @document ||= (data = [ @subject ].pack('H*')
342
343
  buffer = BSON::ByteBuffer.new(data)
343
- BSON::Document.from_bson(buffer))
344
+ BSON::Document.from_bson(buffer, mode: :bson))
344
345
  end
345
346
  end
346
347
  end
@@ -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");
@@ -57,6 +58,10 @@ module BSON
57
58
  @spec['test_key']
58
59
  end
59
60
 
61
+ def bson_type
62
+ @bson_type ||= @spec['bson_type'].to_i(16).chr
63
+ end
64
+
60
65
  def valid_tests
61
66
  @valid_tests ||=
62
67
  @spec['valid']&.map do |test_spec|
@@ -179,6 +184,7 @@ module BSON
179
184
  @string = test_params['string']
180
185
  end
181
186
 
187
+ attr_reader :spec
182
188
  attr_reader :description, :string
183
189
  end
184
190
  end
@@ -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");
data/spec/spec_helper.rb CHANGED
@@ -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
  require 'spec_helper'
2
3
  require 'runners/common_driver'
3
4
 
@@ -25,8 +26,12 @@ describe 'Driver common bson tests' do
25
26
  expect(test.from_json_string).to eq(test.object)
26
27
  end
27
28
 
29
+ it 'serializes to a string', if: test.to_ext_json? do
30
+ expect(test.document_as_extended_json).to eq(test.ext_json)
31
+ end
32
+
28
33
  it 'creates the correct extended json document from the decoded object', if: test.to_ext_json? do
29
- expect(test.document_as_json).to eq(test.ext_json)
34
+ expect(test.document_as_extended_json).to eq(test.ext_json)
30
35
  end
31
36
 
32
37
  it 'parses the string value to the same value as the decoded document', if: test.from_string? do
@@ -63,9 +68,9 @@ describe 'Driver common bson tests' do
63
68
 
64
69
  let(:valid_errors) do
65
70
  [
66
- BSON::Decimal128::InvalidString,
67
- BSON::Decimal128::InvalidRange,
68
- BSON::Decimal128::UnrepresentablePrecision,
71
+ BSON::Error::InvalidDecimal128String,
72
+ BSON::Error::InvalidDecimal128Range,
73
+ BSON::Error::UnrepresentablePrecision,
69
74
  ]
70
75
  end
71
76
 
@@ -1,3 +1,4 @@
1
+ # rubocop:todo all
1
2
  require 'spec_helper'
2
3
  require 'runners/corpus_legacy'
3
4
 
@@ -1,3 +1,4 @@
1
+ # rubocop:todo all
1
2
  require 'spec_helper'
2
3
  require 'runners/corpus'
3
4
 
@@ -105,8 +106,17 @@ describe 'BSON Corpus spec tests' do
105
106
 
106
107
  context("parse error: #{test.description}") do
107
108
 
108
- let(:parsed_extjson) do
109
- BSON::ExtJSON.parse(test.string, mode: :bson)
109
+ # As per:
110
+ # https://github.com/mongodb/specifications/blob/e7ee829329400786e01279b4f37d4e440d1e9cfa/source/bson-corpus/bson-corpus.rst#decimal128-type-0x13
111
+ #
112
+ # Values under test must be parsed in a type-specific manner.
113
+ let(:parsed_value) do
114
+ case spec.bson_type
115
+ when BSON::Decimal128::BSON_TYPE
116
+ BSON::Decimal128.new(test.string)
117
+ else
118
+ BSON::ExtJSON.parse(test.string, mode: :bson)
119
+ end
110
120
  end
111
121
 
112
122
  # Until bson-ruby gets an exception hierarchy, we can only rescue
@@ -114,7 +124,7 @@ describe 'BSON Corpus spec tests' do
114
124
  # https://jira.mongodb.org/browse/RUBY-1806
115
125
  it 'raises an exception' do
116
126
  expect do
117
- parsed_extjson
127
+ parsed_value
118
128
  end.to raise_error(Exception)
119
129
  end
120
130
  end