bson 4.7.0 → 4.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (159) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +6 -2
  3. data/ext/bson/bson-endian.h +1 -1
  4. data/ext/bson/bson-native.h +16 -5
  5. data/ext/bson/bytebuf.c +1 -1
  6. data/ext/bson/endian.c +2 -1
  7. data/ext/bson/init.c +26 -3
  8. data/ext/bson/read.c +127 -33
  9. data/ext/bson/util.c +41 -1
  10. data/ext/bson/write.c +18 -14
  11. data/lib/bson.rb +4 -1
  12. data/lib/bson/active_support.rb +1 -1
  13. data/lib/bson/array.rb +34 -5
  14. data/lib/bson/binary.rb +42 -4
  15. data/lib/bson/boolean.rb +12 -3
  16. data/lib/bson/code.rb +16 -2
  17. data/lib/bson/code_with_scope.rb +32 -5
  18. data/lib/bson/config.rb +1 -1
  19. data/lib/bson/date.rb +1 -1
  20. data/lib/bson/date_time.rb +1 -1
  21. data/lib/bson/db_pointer.rb +110 -0
  22. data/lib/bson/decimal128.rb +16 -2
  23. data/lib/bson/decimal128/builder.rb +1 -1
  24. data/lib/bson/document.rb +1 -1
  25. data/lib/bson/environment.rb +2 -1
  26. data/lib/bson/error.rb +21 -0
  27. data/lib/bson/ext_json.rb +375 -0
  28. data/lib/bson/false_class.rb +1 -1
  29. data/lib/bson/float.rb +48 -2
  30. data/lib/bson/hash.rb +36 -5
  31. data/lib/bson/int32.rb +22 -2
  32. data/lib/bson/int64.rb +29 -4
  33. data/lib/bson/integer.rb +35 -1
  34. data/lib/bson/json.rb +1 -1
  35. data/lib/bson/max_key.rb +13 -1
  36. data/lib/bson/min_key.rb +13 -1
  37. data/lib/bson/nil_class.rb +4 -2
  38. data/lib/bson/object.rb +28 -1
  39. data/lib/bson/object_id.rb +16 -2
  40. data/lib/bson/open_struct.rb +1 -1
  41. data/lib/bson/regexp.rb +20 -3
  42. data/lib/bson/registry.rb +1 -1
  43. data/lib/bson/specialized.rb +4 -2
  44. data/lib/bson/string.rb +4 -2
  45. data/lib/bson/symbol.rb +93 -4
  46. data/lib/bson/time.rb +63 -4
  47. data/lib/bson/time_with_zone.rb +1 -1
  48. data/lib/bson/timestamp.rb +16 -2
  49. data/lib/bson/true_class.rb +1 -1
  50. data/lib/bson/undefined.rb +12 -1
  51. data/lib/bson/version.rb +2 -2
  52. data/spec/bson/array_spec.rb +1 -1
  53. data/spec/bson/binary_spec.rb +34 -4
  54. data/spec/bson/binary_uuid_spec.rb +1 -1
  55. data/spec/bson/boolean_spec.rb +1 -1
  56. data/spec/bson/code_spec.rb +1 -1
  57. data/spec/bson/code_with_scope_spec.rb +1 -1
  58. data/spec/bson/date_spec.rb +1 -1
  59. data/spec/bson/date_time_spec.rb +1 -1
  60. data/spec/bson/decimal128_spec.rb +1 -1
  61. data/spec/bson/document_spec.rb +1 -1
  62. data/spec/bson/ext_json_parse_spec.rb +308 -0
  63. data/spec/bson/false_class_spec.rb +1 -1
  64. data/spec/bson/float_spec.rb +37 -1
  65. data/spec/bson/hash_spec.rb +71 -1
  66. data/spec/bson/int32_spec.rb +21 -1
  67. data/spec/bson/int64_spec.rb +39 -1
  68. data/spec/bson/integer_spec.rb +27 -1
  69. data/spec/bson/json_spec.rb +1 -1
  70. data/spec/bson/max_key_spec.rb +1 -1
  71. data/spec/bson/min_key_spec.rb +1 -1
  72. data/spec/bson/nil_class_spec.rb +1 -1
  73. data/spec/bson/object_id_spec.rb +1 -1
  74. data/spec/bson/object_spec.rb +1 -1
  75. data/spec/bson/open_struct_spec.rb +1 -1
  76. data/spec/bson/raw_spec.rb +22 -1
  77. data/spec/bson/regexp_spec.rb +1 -1
  78. data/spec/bson/registry_spec.rb +1 -1
  79. data/spec/bson/string_spec.rb +1 -1
  80. data/spec/bson/symbol_raw_spec.rb +45 -0
  81. data/spec/bson/symbol_spec.rb +61 -1
  82. data/spec/bson/time_spec.rb +205 -2
  83. data/spec/bson/time_with_zone_spec.rb +1 -1
  84. data/spec/bson/timestamp_spec.rb +1 -1
  85. data/spec/bson/true_class_spec.rb +1 -1
  86. data/spec/bson/undefined_spec.rb +1 -1
  87. data/spec/bson_spec.rb +1 -1
  88. data/spec/{support → runners}/common_driver.rb +1 -1
  89. data/spec/runners/corpus.rb +185 -0
  90. data/spec/{support/corpus.rb → runners/corpus_legacy.rb} +41 -59
  91. data/spec/spec_helper.rb +10 -3
  92. data/spec/{bson/driver_bson_spec.rb → spec_tests/common_driver_spec.rb} +1 -0
  93. data/spec/{bson/corpus_spec.rb → spec_tests/corpus_legacy_spec.rb} +4 -4
  94. data/spec/spec_tests/corpus_spec.rb +124 -0
  95. data/spec/spec_tests/data/corpus/README.md +15 -0
  96. data/spec/spec_tests/data/corpus/array.json +49 -0
  97. data/spec/spec_tests/data/corpus/binary.json +85 -0
  98. data/spec/spec_tests/data/corpus/boolean.json +27 -0
  99. data/spec/spec_tests/data/corpus/code.json +67 -0
  100. data/spec/spec_tests/data/corpus/code_w_scope.json +78 -0
  101. data/spec/spec_tests/data/corpus/datetime.json +42 -0
  102. data/spec/spec_tests/data/corpus/dbpointer.json +56 -0
  103. data/spec/spec_tests/data/corpus/dbref.json +31 -0
  104. data/spec/spec_tests/data/corpus/decimal128-1.json +317 -0
  105. data/spec/spec_tests/data/corpus/decimal128-2.json +793 -0
  106. data/spec/spec_tests/data/corpus/decimal128-3.json +1771 -0
  107. data/spec/spec_tests/data/corpus/decimal128-4.json +117 -0
  108. data/spec/spec_tests/data/corpus/decimal128-5.json +402 -0
  109. data/spec/spec_tests/data/corpus/decimal128-6.json +119 -0
  110. data/spec/spec_tests/data/corpus/decimal128-7.json +323 -0
  111. data/spec/spec_tests/data/corpus/document.json +36 -0
  112. data/spec/spec_tests/data/corpus/double.json +87 -0
  113. data/spec/spec_tests/data/corpus/int32.json +43 -0
  114. data/spec/spec_tests/data/corpus/int64.json +43 -0
  115. data/spec/spec_tests/data/corpus/maxkey.json +12 -0
  116. data/spec/spec_tests/data/corpus/minkey.json +12 -0
  117. data/spec/spec_tests/data/corpus/multi-type-deprecated.json +15 -0
  118. data/spec/spec_tests/data/corpus/multi-type.json +11 -0
  119. data/spec/spec_tests/data/corpus/null.json +12 -0
  120. data/spec/spec_tests/data/corpus/oid.json +28 -0
  121. data/spec/spec_tests/data/corpus/regex.json +65 -0
  122. data/spec/spec_tests/data/corpus/string.json +72 -0
  123. data/spec/spec_tests/data/corpus/symbol.json +80 -0
  124. data/spec/spec_tests/data/corpus/timestamp.json +24 -0
  125. data/spec/spec_tests/data/corpus/top.json +236 -0
  126. data/spec/spec_tests/data/corpus/undefined.json +15 -0
  127. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/array.json +0 -0
  128. data/spec/{support/corpus-tests/failures → spec_tests/data/corpus_legacy}/binary.json +0 -0
  129. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/boolean.json +0 -0
  130. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/code.json +1 -1
  131. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/code_w_scope.json +1 -1
  132. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/document.json +1 -1
  133. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/double.json +1 -1
  134. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/datetime.json +0 -0
  135. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/dbpointer.json +0 -0
  136. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/int64.json +0 -0
  137. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/symbol.json +0 -0
  138. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/int32.json +1 -1
  139. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/maxkey.json +1 -1
  140. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/minkey.json +1 -1
  141. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/null.json +1 -1
  142. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/oid.json +0 -0
  143. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/regex.json +1 -1
  144. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/string.json +0 -0
  145. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/timestamp.json +1 -1
  146. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/top.json +0 -0
  147. data/spec/{support/corpus-tests/failures → spec_tests/data/corpus_legacy}/undefined.json +0 -0
  148. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-1.json +0 -0
  149. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-2.json +0 -0
  150. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-3.json +0 -0
  151. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-4.json +0 -0
  152. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-5.json +0 -0
  153. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-6.json +0 -0
  154. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-7.json +0 -0
  155. data/spec/support/shared_examples.rb +1 -1
  156. metadata +172 -120
  157. checksums.yaml.gz.sig +0 -0
  158. data.tar.gz.sig +0 -1
  159. metadata.gz.sig +0 -1
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2019 MongoDB Inc.
1
+ # Copyright (C) 2019-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2016-2019 MongoDB Inc.
1
+ # Copyright (C) 2016-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
2
 
3
- # Copyright (C) 2009-2019 MongoDB Inc.
3
+ # Copyright (C) 2009-2020 MongoDB Inc.
4
4
  #
5
5
  # Licensed under the Apache License, Version 2.0 (the "License");
6
6
  # you may not use this file except in compliance with the License.
@@ -0,0 +1,308 @@
1
+ # Copyright (C) 2020 MongoDB Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require "spec_helper"
16
+
17
+ describe "BSON::ExtJSON.parse" do
18
+
19
+ let(:parsed) { BSON::ExtJSON.parse_obj(input) }
20
+
21
+ context 'when input is true' do
22
+ let(:input) { true }
23
+
24
+ it 'returns true' do
25
+ parsed.should == true
26
+ end
27
+ end
28
+
29
+ context 'when input is false' do
30
+ let(:input) { false }
31
+
32
+ it 'returns false' do
33
+ parsed.should == false
34
+ end
35
+ end
36
+
37
+ context 'when input is nil' do
38
+ let(:input) { nil }
39
+
40
+ it 'returns nil' do
41
+ parsed.should be nil
42
+ end
43
+ end
44
+
45
+ context 'when input is a string' do
46
+ let(:input) { 'hello' }
47
+
48
+ it 'returns the string' do
49
+ parsed.should == 'hello'
50
+ end
51
+ end
52
+
53
+ context 'when input is a BSON timestamp' do
54
+ let(:input) { {'$timestamp' => {'t' => 12345, 'i' => 42}} }
55
+
56
+ it 'returns a BSON::Timestamp instance' do
57
+ parsed.should == BSON::Timestamp.new(12345, 42)
58
+ end
59
+ end
60
+
61
+ context 'when input is an ISO time' do
62
+ let(:input) { {'$date' => '1970-01-01T00:00:04Z'} }
63
+
64
+ it 'returns a Time instance ' do
65
+ parsed.should be_a(Time)
66
+ end
67
+
68
+ it 'returns a Time instance with correct value' do
69
+ parsed.should == Time.at(4)
70
+ end
71
+
72
+ it 'returns a Time instance in UTC' do
73
+ parsed.zone.should == 'UTC'
74
+ end
75
+ end
76
+
77
+ context 'when input is a Unix timestamp' do
78
+ let(:input) { {'$date' => {'$numberLong' => '4000'}} }
79
+
80
+ it 'returns a Time instance ' do
81
+ parsed.should be_a(Time)
82
+ end
83
+
84
+ it 'returns a Time instance with correct value' do
85
+ parsed.should == Time.at(4)
86
+ end
87
+
88
+ it 'returns a Time instance in UTC' do
89
+ parsed.zone.should == 'UTC'
90
+ end
91
+ end
92
+
93
+ context 'when input is an int32' do
94
+ let(:input) do
95
+ {'$numberInt' => '42'}
96
+ end
97
+
98
+ let(:parsed) { BSON::ExtJSON.parse_obj(input, mode: mode) }
99
+
100
+ context 'when :mode is nil' do
101
+ let(:mode) { nil }
102
+
103
+ it 'returns Integer instance' do
104
+ parsed.should be_a(Integer)
105
+ parsed.should == 42
106
+ end
107
+ end
108
+
109
+ context 'when :mode is :bson' do
110
+ let(:mode) { :bson }
111
+
112
+ it 'returns Integer instance' do
113
+ parsed.should be_a(Integer)
114
+ parsed.should == 42
115
+ end
116
+ end
117
+ end
118
+
119
+ context 'when input is an int64' do
120
+ let(:input) do
121
+ {'$numberLong' => '42'}
122
+ end
123
+
124
+ let(:parsed) { BSON::ExtJSON.parse_obj(input, mode: mode) }
125
+
126
+ context 'when :mode is nil' do
127
+ let(:mode) { nil }
128
+
129
+ it 'returns Integer instance' do
130
+ parsed.should be_a(Integer)
131
+ parsed.should == 42
132
+ end
133
+ end
134
+
135
+ context 'when :mode is :bson' do
136
+ let(:mode) { :bson }
137
+
138
+ it 'returns Int64 instance' do
139
+ parsed.should be_a(BSON::Int64)
140
+ parsed.value.should == 42
141
+ end
142
+ end
143
+ end
144
+
145
+ context 'when input is a hash' do
146
+ let(:input) do
147
+ {}
148
+ end
149
+
150
+ let(:parsed) { BSON::ExtJSON.parse_obj(input, mode: mode) }
151
+
152
+ context 'when mode is invalid' do
153
+ let(:mode) { :foo }
154
+
155
+ it 'raises an exception' do
156
+ lambda do
157
+ parsed
158
+ end.should raise_error(ArgumentError, /Invalid value for :mode option/)
159
+ end
160
+ end
161
+ end
162
+
163
+ context 'when input is a binary' do
164
+ let(:data) do
165
+ Base64.decode64("//8=")
166
+ end
167
+
168
+ context 'in current format' do
169
+ let(:input) do
170
+ { "$binary" => { "base64"=>"//8=", "subType"=>"00" } }
171
+ end
172
+
173
+ context 'when :mode is nil' do
174
+ let(:mode) { nil }
175
+
176
+ it 'returns BSON::Binary instance' do
177
+ parsed.should be_a(BSON::Binary)
178
+ parsed.data.should == data
179
+ end
180
+ end
181
+
182
+ context 'when mode is :bson' do
183
+ let(:mode) { :bson }
184
+
185
+ it 'returns BSON::Binary instance' do
186
+ parsed.should be_a(BSON::Binary)
187
+ parsed.data.should == data
188
+ end
189
+ end
190
+ end
191
+
192
+ context 'in legacy format' do
193
+ let(:input) do
194
+ { "$binary"=>"//8=", "$type"=>"00" }
195
+ end
196
+
197
+ context 'when :mode is nil' do
198
+ let(:mode) { nil }
199
+
200
+ it 'returns BSON::Binary instance' do
201
+ parsed.should be_a(BSON::Binary)
202
+ parsed.data.should == data
203
+ end
204
+ end
205
+
206
+ context 'when mode is :bson' do
207
+ let(:mode) { :bson }
208
+
209
+ it 'returns BSON::Binary instance' do
210
+ parsed.should be_a(BSON::Binary)
211
+ parsed.data.should == data
212
+ end
213
+ end
214
+ end
215
+ end
216
+
217
+ context 'when input is a regex' do
218
+ let(:pattern) { 'abc' }
219
+ let(:options) { 'im' }
220
+
221
+ context 'in current format' do
222
+ let(:input) do
223
+ { "$regularExpression" => { "pattern" => pattern, "options" => options } }
224
+ end
225
+
226
+ context 'when :mode is nil' do
227
+ let(:mode) { nil }
228
+
229
+ it 'returns a BSON::Regexp::Raw instance' do
230
+ parsed.should be_a(BSON::Regexp::Raw)
231
+ parsed.pattern.should == pattern
232
+ parsed.options.should == options
233
+ end
234
+ end
235
+
236
+ context 'when :mode is :bson' do
237
+ let(:mode) { :bson }
238
+
239
+ it 'returns a BSON::Regexp::Raw instance' do
240
+ parsed.should be_a(BSON::Regexp::Raw)
241
+ parsed.pattern.should == pattern
242
+ parsed.options.should == options
243
+ end
244
+ end
245
+ end
246
+
247
+ context 'in legacy format' do
248
+ let(:input) do
249
+ { "$regex" => pattern, "$options" => options }
250
+ end
251
+
252
+ context 'when :mode is nil' do
253
+ let(:mode) { nil }
254
+
255
+ it 'returns a BSON::Regexp::Raw instance' do
256
+ parsed.should be_a(BSON::Regexp::Raw)
257
+ parsed.pattern.should == pattern
258
+ parsed.options.should == options
259
+ end
260
+ end
261
+
262
+ context 'when :mode is :bson' do
263
+ let(:mode) { :bson }
264
+
265
+ it 'returns a BSON::Regexp::Raw instance' do
266
+ parsed.should be_a(BSON::Regexp::Raw)
267
+ parsed.pattern.should == pattern
268
+ parsed.options.should == options
269
+ end
270
+ end
271
+ end
272
+
273
+ context 'when $regularExpression is nested in $regex' do
274
+ context 'with options' do
275
+ let(:input) do
276
+ {
277
+ "$regex" => {
278
+ "$regularExpression" => { "pattern" => "foo*", "options" => "" },
279
+ },
280
+ "$options" => "ix",
281
+ }
282
+ end
283
+
284
+ it 'parses' do
285
+ parsed.should == {
286
+ '$regex' => BSON::Regexp::Raw.new('foo*'), '$options' => 'ix'
287
+ }
288
+ end
289
+ end
290
+
291
+ context 'without options' do
292
+ let(:input) do
293
+ {
294
+ "$regex" => {
295
+ "$regularExpression" => { "pattern" => "foo*", "options" => "" },
296
+ },
297
+ }
298
+ end
299
+
300
+ it 'parses' do
301
+ parsed.should == {
302
+ '$regex' => BSON::Regexp::Raw.new('foo*'),
303
+ }
304
+ end
305
+ end
306
+ end
307
+ end
308
+ end
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -26,4 +26,40 @@ describe Float do
26
26
  it_behaves_like "a serializable bson element"
27
27
  it_behaves_like "a deserializable bson element"
28
28
  end
29
+
30
+ describe '#to_json' do
31
+ it 'returns float' do
32
+ 42.0.to_json.should == '42.0'
33
+ end
34
+ end
35
+
36
+ describe '#as_extended_json' do
37
+ context 'canonical mode' do
38
+ it 'returns $numberDouble' do
39
+ 42.0.as_extended_json.should == {'$numberDouble' => '42.0'}
40
+ end
41
+ end
42
+
43
+ context 'relaxed mode' do
44
+ let(:serialized) do
45
+ 42.0.as_extended_json(mode: :relaxed)
46
+ end
47
+
48
+ it 'returns float' do
49
+ serialized.should be_a(Float)
50
+ serialized.should be_within(0.00001).of(42)
51
+ end
52
+ end
53
+
54
+ context 'legacy mode' do
55
+ let(:serialized) do
56
+ 42.0.as_extended_json(mode: :legacy)
57
+ end
58
+
59
+ it 'returns float' do
60
+ serialized.should be_a(Float)
61
+ serialized.should be_within(0.00001).of(42)
62
+ end
63
+ end
64
+ end
29
65
  end
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -157,6 +157,76 @@ describe Hash do
157
157
  it_behaves_like "a serializable bson element"
158
158
  it_behaves_like "a deserializable bson element"
159
159
  end
160
+
161
+ context 'with symbol values' do
162
+ let(:value) { :foo }
163
+
164
+ let(:serialized) do
165
+ {foo: value}.to_bson.to_s
166
+ end
167
+
168
+ def perform_test(bson_type_to_use)
169
+ Symbol.class_eval do
170
+ alias_method :bson_type_orig, :bson_type
171
+ define_method(:bson_type) do
172
+ bson_type_to_use
173
+ end
174
+ end
175
+
176
+ begin
177
+ yield
178
+ ensure
179
+ Symbol.class_eval do
180
+ alias_method :bson_type, :bson_type_orig
181
+ remove_method :bson_type_orig
182
+ end
183
+ end
184
+ end
185
+
186
+ let(:bson_with_symbol) do
187
+ "\x12\x00\x00\x00\x0Efoo\x00\x04\x00\x00\x00bar\x00\x00".force_encoding('binary')
188
+ end
189
+
190
+ let(:deserialized) do
191
+ Hash.from_bson(BSON::ByteBuffer.new(bson_with_symbol))
192
+ end
193
+
194
+ context 'when Symbol#bson_type is set to symbol' do
195
+ let(:bson_type_to_use) { BSON::Symbol::BSON_TYPE }
196
+
197
+ let(:expected) do
198
+ "\x12\x00\x00\x00\x0Efoo\x00\x04\x00\x00\x00foo\x00\x00".force_encoding('binary')
199
+ end
200
+
201
+ it 'serializes to BSON symbol' do
202
+ perform_test(bson_type_to_use) do
203
+ serialized
204
+ end.should == expected
205
+ end
206
+
207
+ it 'deserializes to Symbol' do
208
+ deserialized.should == {'foo' => :bar}
209
+ end
210
+ end
211
+
212
+ context 'when Symbol#bson_type is set to string' do
213
+ let(:bson_type_to_use) { BSON::String::BSON_TYPE }
214
+
215
+ let(:expected) do
216
+ "\x12\x00\x00\x00\x02foo\x00\x04\x00\x00\x00foo\x00\x00".force_encoding('binary')
217
+ end
218
+
219
+ it 'serializes to BSON string' do
220
+ perform_test(bson_type_to_use) do
221
+ serialized
222
+ end.should == expected
223
+ end
224
+
225
+ it 'deserializes to Symbol' do
226
+ deserialized.should == {'foo' => :bar}
227
+ end
228
+ end
229
+ end
160
230
  end
161
231
 
162
232
  describe '#to_bson' do