bson 4.1.1 → 5.2.0

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 (226) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +106 -7
  3. data/Rakefile +86 -43
  4. data/ext/bson/{native-endian.h → bson-endian.h} +14 -107
  5. data/ext/bson/bson-native.h +135 -0
  6. data/ext/bson/bytebuf.c +133 -0
  7. data/ext/bson/endian.c +117 -0
  8. data/ext/bson/extconf.rb +8 -3
  9. data/ext/bson/init.c +364 -0
  10. data/ext/bson/libbson-utf8.c +230 -0
  11. data/ext/bson/read.c +470 -0
  12. data/ext/bson/util.c +250 -0
  13. data/ext/bson/write.c +675 -0
  14. data/lib/bson/active_support.rb +19 -0
  15. data/lib/bson/array.rb +97 -30
  16. data/lib/bson/big_decimal.rb +77 -0
  17. data/lib/bson/binary.rb +510 -70
  18. data/lib/bson/boolean.rb +15 -4
  19. data/lib/bson/code.rb +25 -12
  20. data/lib/bson/code_with_scope.rb +41 -15
  21. data/lib/bson/config.rb +3 -28
  22. data/lib/bson/date.rb +16 -4
  23. data/lib/bson/date_time.rb +6 -4
  24. data/lib/bson/db_pointer.rb +110 -0
  25. data/lib/bson/dbref.rb +154 -0
  26. data/lib/bson/decimal128/builder.rb +456 -0
  27. data/lib/bson/decimal128.rb +272 -0
  28. data/lib/bson/document.rb +177 -7
  29. data/lib/bson/environment.rb +17 -2
  30. data/lib/bson/error/bson_decode_error.rb +11 -0
  31. data/lib/bson/error/ext_json_parse_error.rb +11 -0
  32. data/lib/bson/error/illegal_key.rb +23 -0
  33. data/lib/bson/error/invalid_binary_type.rb +37 -0
  34. data/lib/bson/error/invalid_dbref_argument.rb +12 -0
  35. data/lib/bson/error/invalid_decimal128_argument.rb +25 -0
  36. data/lib/bson/error/invalid_decimal128_range.rb +27 -0
  37. data/lib/bson/error/invalid_decimal128_string.rb +26 -0
  38. data/lib/bson/error/invalid_key.rb +24 -0
  39. data/lib/bson/error/invalid_object_id.rb +11 -0
  40. data/lib/bson/error/invalid_regexp_pattern.rb +13 -0
  41. data/lib/bson/error/unrepresentable_precision.rb +19 -0
  42. data/lib/bson/error/unserializable_class.rb +13 -0
  43. data/lib/bson/error/unsupported_binary_subtype.rb +12 -0
  44. data/lib/bson/error/unsupported_type.rb +11 -0
  45. data/lib/bson/error.rb +22 -0
  46. data/lib/bson/ext_json.rb +389 -0
  47. data/lib/bson/false_class.rb +6 -4
  48. data/lib/bson/float.rb +43 -7
  49. data/lib/bson/hash.rb +152 -37
  50. data/lib/bson/int32.rb +104 -6
  51. data/lib/bson/int64.rb +111 -8
  52. data/lib/bson/integer.rb +43 -9
  53. data/lib/bson/json.rb +3 -1
  54. data/lib/bson/max_key.rb +21 -10
  55. data/lib/bson/min_key.rb +21 -10
  56. data/lib/bson/nil_class.rb +7 -3
  57. data/lib/bson/object.rb +25 -17
  58. data/lib/bson/object_id.rb +98 -113
  59. data/lib/bson/open_struct.rb +59 -0
  60. data/lib/bson/regexp.rb +129 -56
  61. data/lib/bson/registry.rb +7 -10
  62. data/lib/bson/specialized.rb +8 -4
  63. data/lib/bson/string.rb +12 -32
  64. data/lib/bson/symbol.rb +107 -11
  65. data/lib/bson/time.rb +68 -7
  66. data/lib/bson/time_with_zone.rb +67 -0
  67. data/lib/bson/timestamp.rb +50 -10
  68. data/lib/bson/true_class.rb +6 -4
  69. data/lib/bson/undefined.rb +28 -2
  70. data/lib/bson/vector.rb +44 -0
  71. data/lib/bson/version.rb +6 -14
  72. data/lib/bson.rb +22 -12
  73. data/spec/README.md +14 -0
  74. data/spec/bson/array_spec.rb +38 -62
  75. data/spec/bson/big_decimal_spec.rb +328 -0
  76. data/spec/bson/binary_spec.rb +199 -53
  77. data/spec/bson/binary_uuid_spec.rb +190 -0
  78. data/spec/bson/boolean_spec.rb +2 -1
  79. data/spec/bson/byte_buffer_read_spec.rb +198 -0
  80. data/spec/bson/byte_buffer_spec.rb +122 -381
  81. data/spec/bson/byte_buffer_write_spec.rb +855 -0
  82. data/spec/bson/code_spec.rb +6 -4
  83. data/spec/bson/code_with_scope_spec.rb +6 -4
  84. data/spec/bson/config_spec.rb +1 -35
  85. data/spec/bson/date_spec.rb +2 -1
  86. data/spec/bson/date_time_spec.rb +55 -1
  87. data/spec/bson/dbref_legacy_spec.rb +186 -0
  88. data/spec/bson/dbref_spec.rb +487 -0
  89. data/spec/bson/decimal128_spec.rb +1840 -0
  90. data/spec/bson/document_as_spec.rb +61 -0
  91. data/spec/bson/document_spec.rb +205 -32
  92. data/spec/bson/ext_json_parse_spec.rb +346 -0
  93. data/spec/bson/false_class_spec.rb +9 -1
  94. data/spec/bson/float_spec.rb +42 -1
  95. data/spec/bson/hash_as_spec.rb +58 -0
  96. data/spec/bson/hash_spec.rb +318 -66
  97. data/spec/bson/int32_spec.rb +248 -1
  98. data/spec/bson/int64_spec.rb +308 -1
  99. data/spec/bson/integer_spec.rb +61 -3
  100. data/spec/bson/json_spec.rb +2 -1
  101. data/spec/bson/max_key_spec.rb +6 -4
  102. data/spec/bson/min_key_spec.rb +6 -4
  103. data/spec/bson/nil_class_spec.rb +2 -1
  104. data/spec/bson/object_id_spec.rb +95 -5
  105. data/spec/bson/object_spec.rb +3 -2
  106. data/spec/bson/open_struct_spec.rb +87 -0
  107. data/spec/bson/raw_spec.rb +594 -0
  108. data/spec/bson/regexp_spec.rb +61 -8
  109. data/spec/bson/registry_spec.rb +3 -2
  110. data/spec/bson/string_spec.rb +26 -33
  111. data/spec/bson/symbol_raw_spec.rb +70 -0
  112. data/spec/bson/symbol_spec.rb +77 -20
  113. data/spec/bson/time_spec.rb +206 -2
  114. data/spec/bson/time_with_zone_spec.rb +69 -0
  115. data/spec/bson/timestamp_spec.rb +58 -2
  116. data/spec/bson/true_class_spec.rb +9 -1
  117. data/spec/bson/undefined_spec.rb +28 -1
  118. data/spec/bson/vector_spec.rb +33 -0
  119. data/spec/bson_spec.rb +2 -1
  120. data/spec/runners/binary_vector.rb +78 -0
  121. data/spec/runners/common_driver.rb +348 -0
  122. data/spec/runners/corpus.rb +191 -0
  123. data/spec/runners/corpus_legacy.rb +248 -0
  124. data/spec/shared/LICENSE +20 -0
  125. data/spec/shared/bin/get-mongodb-download-url +17 -0
  126. data/spec/shared/bin/s3-copy +45 -0
  127. data/spec/shared/bin/s3-upload +69 -0
  128. data/spec/shared/lib/mrss/child_process_helper.rb +80 -0
  129. data/spec/shared/lib/mrss/cluster_config.rb +231 -0
  130. data/spec/shared/lib/mrss/constraints.rb +378 -0
  131. data/spec/shared/lib/mrss/docker_runner.rb +298 -0
  132. data/spec/shared/lib/mrss/eg_config_utils.rb +51 -0
  133. data/spec/shared/lib/mrss/event_subscriber.rb +210 -0
  134. data/spec/shared/lib/mrss/lite_constraints.rb +238 -0
  135. data/spec/shared/lib/mrss/release/candidate.rb +281 -0
  136. data/spec/shared/lib/mrss/release/product_data.rb +144 -0
  137. data/spec/shared/lib/mrss/server_version_registry.rb +113 -0
  138. data/spec/shared/lib/mrss/session_registry.rb +69 -0
  139. data/spec/shared/lib/mrss/session_registry_legacy.rb +60 -0
  140. data/spec/shared/lib/mrss/spec_organizer.rb +179 -0
  141. data/spec/shared/lib/mrss/utils.rb +37 -0
  142. data/spec/shared/lib/tasks/candidate.rake +64 -0
  143. data/spec/shared/share/Dockerfile.erb +251 -0
  144. data/spec/shared/share/haproxy-1.conf +16 -0
  145. data/spec/shared/share/haproxy-2.conf +17 -0
  146. data/spec/shared/shlib/config.sh +27 -0
  147. data/spec/shared/shlib/distro.sh +84 -0
  148. data/spec/shared/shlib/server.sh +423 -0
  149. data/spec/shared/shlib/set_env.sh +110 -0
  150. data/spec/spec_helper.rb +61 -1
  151. data/spec/spec_tests/binary_vector_spec.rb +82 -0
  152. data/spec/spec_tests/common_driver_spec.rb +84 -0
  153. data/spec/spec_tests/corpus_legacy_spec.rb +72 -0
  154. data/spec/spec_tests/corpus_spec.rb +134 -0
  155. data/spec/spec_tests/data/binary_vector/README.md +61 -0
  156. data/spec/spec_tests/data/binary_vector/float32.json +65 -0
  157. data/spec/spec_tests/data/binary_vector/int8.json +57 -0
  158. data/spec/spec_tests/data/binary_vector/packed_bit.json +83 -0
  159. data/spec/spec_tests/data/corpus/README.md +15 -0
  160. data/spec/spec_tests/data/corpus/array.json +49 -0
  161. data/spec/spec_tests/data/corpus/binary.json +153 -0
  162. data/spec/spec_tests/data/corpus/boolean.json +27 -0
  163. data/spec/spec_tests/data/corpus/code.json +67 -0
  164. data/spec/spec_tests/data/corpus/code_w_scope.json +78 -0
  165. data/spec/spec_tests/data/corpus/datetime.json +42 -0
  166. data/spec/spec_tests/data/corpus/dbpointer.json +56 -0
  167. data/spec/spec_tests/data/corpus/dbref.json +51 -0
  168. data/spec/spec_tests/data/corpus/decimal128-1.json +317 -0
  169. data/spec/spec_tests/data/corpus/decimal128-2.json +793 -0
  170. data/spec/spec_tests/data/corpus/decimal128-3.json +1771 -0
  171. data/spec/spec_tests/data/corpus/decimal128-4.json +165 -0
  172. data/spec/spec_tests/data/corpus/decimal128-5.json +402 -0
  173. data/spec/spec_tests/data/corpus/decimal128-6.json +131 -0
  174. data/spec/spec_tests/data/corpus/decimal128-7.json +327 -0
  175. data/spec/spec_tests/data/corpus/document.json +60 -0
  176. data/spec/spec_tests/data/corpus/double.json +87 -0
  177. data/spec/spec_tests/data/corpus/int32.json +43 -0
  178. data/spec/spec_tests/data/corpus/int64.json +43 -0
  179. data/spec/spec_tests/data/corpus/maxkey.json +12 -0
  180. data/spec/spec_tests/data/corpus/minkey.json +12 -0
  181. data/spec/spec_tests/data/corpus/multi-type-deprecated.json +15 -0
  182. data/spec/spec_tests/data/corpus/multi-type.json +11 -0
  183. data/spec/spec_tests/data/corpus/null.json +12 -0
  184. data/spec/spec_tests/data/corpus/oid.json +28 -0
  185. data/spec/spec_tests/data/corpus/regex.json +65 -0
  186. data/spec/spec_tests/data/corpus/string.json +72 -0
  187. data/spec/spec_tests/data/corpus/symbol.json +80 -0
  188. data/spec/spec_tests/data/corpus/timestamp.json +34 -0
  189. data/spec/spec_tests/data/corpus/top.json +262 -0
  190. data/spec/spec_tests/data/corpus/undefined.json +15 -0
  191. data/spec/spec_tests/data/corpus_legacy/array.json +49 -0
  192. data/spec/spec_tests/data/corpus_legacy/binary.json +69 -0
  193. data/spec/spec_tests/data/corpus_legacy/boolean.json +27 -0
  194. data/spec/spec_tests/data/corpus_legacy/code.json +67 -0
  195. data/spec/spec_tests/data/corpus_legacy/code_w_scope.json +78 -0
  196. data/spec/spec_tests/data/corpus_legacy/document.json +36 -0
  197. data/spec/spec_tests/data/corpus_legacy/double.json +69 -0
  198. data/spec/spec_tests/data/corpus_legacy/failures/datetime.json +31 -0
  199. data/spec/spec_tests/data/corpus_legacy/failures/dbpointer.json +42 -0
  200. data/spec/spec_tests/data/corpus_legacy/failures/int64.json +38 -0
  201. data/spec/spec_tests/data/corpus_legacy/failures/symbol.json +62 -0
  202. data/spec/spec_tests/data/corpus_legacy/int32.json +38 -0
  203. data/spec/spec_tests/data/corpus_legacy/maxkey.json +12 -0
  204. data/spec/spec_tests/data/corpus_legacy/minkey.json +12 -0
  205. data/spec/spec_tests/data/corpus_legacy/null.json +12 -0
  206. data/spec/spec_tests/data/corpus_legacy/oid.json +28 -0
  207. data/spec/spec_tests/data/corpus_legacy/regex.json +37 -0
  208. data/spec/spec_tests/data/corpus_legacy/string.json +67 -0
  209. data/spec/spec_tests/data/corpus_legacy/timestamp.json +18 -0
  210. data/spec/spec_tests/data/corpus_legacy/top.json +62 -0
  211. data/spec/spec_tests/data/corpus_legacy/undefined.json +13 -0
  212. data/spec/spec_tests/data/decimal128/decimal128-1.json +363 -0
  213. data/spec/spec_tests/data/decimal128/decimal128-2.json +793 -0
  214. data/spec/spec_tests/data/decimal128/decimal128-3.json +1771 -0
  215. data/spec/spec_tests/data/decimal128/decimal128-4.json +165 -0
  216. data/spec/spec_tests/data/decimal128/decimal128-5.json +402 -0
  217. data/spec/spec_tests/data/decimal128/decimal128-6.json +131 -0
  218. data/spec/spec_tests/data/decimal128/decimal128-7.json +327 -0
  219. data/spec/support/shared_examples.rb +32 -11
  220. data/spec/support/spec_config.rb +17 -0
  221. data/spec/support/utils.rb +58 -0
  222. metadata +284 -45
  223. checksums.yaml.gz.sig +0 -3
  224. data/ext/bson/native.c +0 -722
  225. data.tar.gz.sig +0 -0
  226. metadata.gz.sig +0 -0
@@ -1,4 +1,5 @@
1
- # Copyright (C) 2009-2014 MongoDB Inc.
1
+ # rubocop:todo all
2
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
4
5
  # you may not use this file except in compliance with the License.
@@ -16,6 +17,52 @@ require "spec_helper"
16
17
 
17
18
  describe BSON::Int32 do
18
19
 
20
+ describe "#intiialize" do
21
+
22
+ let(:obj) { described_class.new(integer) }
23
+
24
+ context "when the integer is 32-bit" do
25
+
26
+ let(:integer) { Integer::MAX_32BIT }
27
+
28
+ it "wraps the integer" do
29
+ expect(obj.value).to be(integer)
30
+ end
31
+ end
32
+
33
+ context "when the integer is too large" do
34
+
35
+ let(:integer) { Integer::MAX_32BIT + 1 }
36
+
37
+ it "raises an out of range error" do
38
+ expect do
39
+ obj
40
+ end.to raise_error(RangeError, /#{integer} cannot be stored in 32 bits/)
41
+ end
42
+ end
43
+
44
+ context "when the integer is too small" do
45
+
46
+ let(:integer) { Integer::MIN_32BIT - 1 }
47
+
48
+ it "raises an out of range error" do
49
+ expect {
50
+ obj
51
+ }.to raise_error(RangeError)
52
+ end
53
+ end
54
+
55
+ context 'when argument is an Int32' do
56
+ let(:integer) do
57
+ described_class.new(described_class.new(50))
58
+ end
59
+
60
+ it 'works' do
61
+ expect(integer.value).to be 50
62
+ end
63
+ end
64
+ end
65
+
19
66
  describe "#from_bson" do
20
67
 
21
68
  let(:type) { 16.chr }
@@ -41,4 +88,204 @@ describe BSON::Int32 do
41
88
  expect(BSON::Int32.from_bson(encoded_2)).to eq(decoded_2)
42
89
  end
43
90
  end
91
+
92
+ describe "#to_bson" do
93
+
94
+ context "when the integer is 32 bit" do
95
+
96
+ let(:type) { 16.chr }
97
+ let(:obj) { BSON::Int32.new(Integer::MAX_32BIT - 1) }
98
+ let(:bson) { [ Integer::MAX_32BIT - 1 ].pack(BSON::Int32::PACK) }
99
+
100
+ it_behaves_like "a serializable bson element"
101
+ end
102
+ end
103
+
104
+ describe "#to_bson_key" do
105
+
106
+ let(:obj) { BSON::Int32.new(Integer::MAX_32BIT - 1) }
107
+ let(:encoded) { (Integer::MAX_32BIT - 1) }
108
+
109
+ it "returns the key as an integer" do
110
+ expect(obj.to_bson_key).to eq(encoded)
111
+ end
112
+ end
113
+
114
+ describe "#==" do
115
+
116
+ let(:object) do
117
+ described_class.new(1)
118
+ end
119
+
120
+ context "when data is identical" do
121
+
122
+ let(:other_object) do
123
+ described_class.new(1)
124
+ end
125
+
126
+ it "returns true" do
127
+ expect(object).to eq(other_object)
128
+ end
129
+
130
+ context "other object is of another integer type" do
131
+
132
+ let(:other_object) do
133
+ BSON::Int64.new(1)
134
+ end
135
+
136
+ it "returns false" do
137
+ expect(object).not_to eq(other_object)
138
+ end
139
+ end
140
+ end
141
+
142
+ context "when the data is different" do
143
+
144
+ let(:other_object) do
145
+ described_class.new(2)
146
+ end
147
+
148
+ it "returns false" do
149
+ expect(object).not_to eq(other_object)
150
+ end
151
+ end
152
+
153
+ context "when other is not a BSON integer" do
154
+
155
+ it "returns false" do
156
+ expect(described_class.new(1)).to_not eq('1')
157
+ end
158
+ end
159
+ end
160
+
161
+ describe "#===" do
162
+
163
+ let(:object) do
164
+ described_class.new(1)
165
+ end
166
+
167
+ context "when comparing with another BSON int32" do
168
+
169
+ context "when the data is equal" do
170
+
171
+ let(:other_object) do
172
+ described_class.new(1)
173
+ end
174
+
175
+ it "returns true" do
176
+ expect(object === other_object).to be true
177
+ end
178
+
179
+ context "other object is of another integer type" do
180
+
181
+ let(:other_object) do
182
+ BSON::Int64.new(1)
183
+ end
184
+
185
+ it "returns false" do
186
+ expect(object === other_object).to be false
187
+ end
188
+ end
189
+ end
190
+
191
+ context "when the data is not equal" do
192
+
193
+ let(:other_object) do
194
+ described_class.new(2)
195
+ end
196
+
197
+ it "returns false" do
198
+ expect(object === other_object).to be false
199
+ end
200
+ end
201
+ end
202
+
203
+ context "when comparing to an object id class" do
204
+
205
+ it "returns false" do
206
+ expect(described_class.new(1) === described_class).to be false
207
+ end
208
+ end
209
+
210
+ context "when comparing with a string" do
211
+
212
+ context "when the data is equal" do
213
+
214
+ let(:other) do
215
+ '1'
216
+ end
217
+
218
+ it "returns false" do
219
+ expect(object === other).to be false
220
+ end
221
+ end
222
+
223
+ context "when the data is not equal" do
224
+
225
+ let(:other) do
226
+ '2'
227
+ end
228
+
229
+ it "returns false" do
230
+ expect(object === other).to be false
231
+ end
232
+ end
233
+ end
234
+
235
+ context "when comparing with a non-bson integer object" do
236
+
237
+ it "returns false" do
238
+ expect(object === []).to be false
239
+ end
240
+ end
241
+
242
+ context "when comparing with a non int64 class" do
243
+
244
+ it "returns false" do
245
+ expect(object === String).to be false
246
+ end
247
+ end
248
+ end
249
+
250
+ describe '#value' do
251
+ let(:obj) { described_class.new(12345) }
252
+
253
+ it 'returns value passed to initializer' do
254
+ expect(obj.value).to eq(12345)
255
+ end
256
+ end
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
+
268
+ describe '#as_extended_json' do
269
+ let(:object) { described_class.new(42) }
270
+
271
+ context 'canonical mode' do
272
+ it 'returns a Hash with key $numberInt' do
273
+ expect(object.as_extended_json).to eq({ '$numberInt' => '42' })
274
+ end
275
+ end
276
+
277
+ context 'relaxed mode' do
278
+ it 'returns an Integer' do
279
+ expect(object.as_extended_json(mode: :relaxed)).to eq 42
280
+ end
281
+ end
282
+
283
+ context 'legacy mode' do
284
+ it 'returns an Integer' do
285
+ expect(object.as_extended_json(mode: :legacy)).to eq 42
286
+ end
287
+ end
288
+
289
+ it_behaves_like "an Extended JSON serializable object"
290
+ end
44
291
  end
@@ -1,4 +1,5 @@
1
- # Copyright (C) 2009-2014 MongoDB Inc.
1
+ # rubocop:todo all
2
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
4
5
  # you may not use this file except in compliance with the License.
@@ -16,6 +17,52 @@ require "spec_helper"
16
17
 
17
18
  describe BSON::Int64 do
18
19
 
20
+ describe "#intiialize" do
21
+
22
+ let(:obj) { described_class.new(integer) }
23
+
24
+ context "when the integer is 64-bit" do
25
+
26
+ let(:integer) { Integer::MAX_64BIT - 1 }
27
+
28
+ it "wraps the integer" do
29
+ expect(obj.value).to be(integer)
30
+ end
31
+ end
32
+
33
+ context "when the integer is too large" do
34
+
35
+ let(:integer) { Integer::MAX_64BIT + 1 }
36
+
37
+ it "raises an out of range error" do
38
+ expect do
39
+ obj
40
+ end.to raise_error(RangeError, /#{integer} cannot be stored in 64 bits/)
41
+ end
42
+ end
43
+
44
+ context "when the integer is too small" do
45
+
46
+ let(:integer) { Integer::MIN_64BIT - 1 }
47
+
48
+ it "raises an out of range error" do
49
+ expect {
50
+ obj
51
+ }.to raise_error(RangeError)
52
+ end
53
+ end
54
+
55
+ context 'when argument is an Int64' do
56
+ let(:integer) do
57
+ described_class.new(described_class.new(50))
58
+ end
59
+
60
+ it 'works' do
61
+ expect(integer.value).to be 50
62
+ end
63
+ end
64
+ end
65
+
19
66
  describe "#from_bson" do
20
67
 
21
68
  let(:type) { 18.chr }
@@ -24,5 +71,265 @@ describe BSON::Int64 do
24
71
 
25
72
  it_behaves_like "a bson element"
26
73
  it_behaves_like "a deserializable bson element"
74
+
75
+ context 'canonical deserialization' do
76
+ let(:integer) { 42 }
77
+
78
+ let(:bson) do
79
+ BSON::ByteBuffer.new(BSON::Int64.new(integer).to_bson.to_s)
80
+ end
81
+
82
+ let(:deserialized) do
83
+ described_class.from_bson(bson, mode: :bson)
84
+ end
85
+
86
+ it 'deserializes to BSON::Int64' do
87
+ deserialized.class.should be BSON::Int64
88
+ end
89
+
90
+ it 'has the correct value' do
91
+ deserialized.value.should == 42
92
+ end
93
+ end
94
+
95
+ context "when the integer is within the MRI Fixnum range" do
96
+
97
+ let(:integer) { 2**30 - 1 }
98
+
99
+ let(:bson) do
100
+ BSON::ByteBuffer.new(BSON::Int64.new(integer).to_bson.to_s)
101
+ end
102
+
103
+ context "when on JRuby", if: BSON::Environment.jruby? && RUBY_VERSION < '3.0.0' do
104
+
105
+ it "deserializes to a Fixnum object" do
106
+ expect(described_class.from_bson(bson).class).to be(Fixnum)
107
+ end
108
+ end
109
+
110
+ it "deserializes to an Integer object" do
111
+ expect(described_class.from_bson(bson).class).to be(Integer)
112
+ end
113
+ end
114
+
115
+ context "when the 64-bit integer is the BSON max and thus larger than the MRI Fixnum range on all architectures" do
116
+
117
+ let(:integer) { Integer::MAX_64BIT }
118
+
119
+ let(:bson) do
120
+ BSON::ByteBuffer.new(integer.to_bson.to_s)
121
+ end
122
+
123
+ context "when on JRuby", if: BSON::Environment.jruby? && RUBY_VERSION < '3.0.0' do
124
+
125
+ it "deserializes to a Fixnum object" do
126
+ expect(described_class.from_bson(bson).class).to be(Fixnum)
127
+ end
128
+ end
129
+
130
+ it "deserializes to an Integer object" do
131
+ expect(described_class.from_bson(bson).class).to be(Integer)
132
+ end
133
+ end
134
+ end
135
+
136
+ describe "#to_bson" do
137
+
138
+ context "when the integer is 64 bit" do
139
+
140
+ let(:type) { 18.chr }
141
+ let(:obj) { BSON::Int64.new(Integer::MAX_64BIT - 1) }
142
+ let(:bson) { [ Integer::MAX_64BIT - 1 ].pack(BSON::Int64::PACK) }
143
+
144
+ it_behaves_like "a serializable bson element"
145
+ end
146
+ end
147
+
148
+ describe "#to_bson_key" do
149
+
150
+ let(:obj) { BSON::Int64.new(Integer::MAX_64BIT - 1) }
151
+ let(:encoded) { (Integer::MAX_64BIT - 1) }
152
+
153
+ it "returns the key as an integer" do
154
+ expect(obj.to_bson_key).to eq(encoded)
155
+ end
156
+ end
157
+
158
+ describe "#==" do
159
+
160
+ let(:object) do
161
+ described_class.new(1)
162
+ end
163
+
164
+ context "when data is identical" do
165
+
166
+ let(:other_object) do
167
+ described_class.new(1)
168
+ end
169
+
170
+ it "returns true" do
171
+ expect(object).to eq(other_object)
172
+ end
173
+
174
+ context "other object is of another integer type" do
175
+
176
+ let(:other_object) do
177
+ BSON::Int32.new(1)
178
+ end
179
+
180
+ it "returns false" do
181
+ expect(object).not_to eq(other_object)
182
+ end
183
+ end
184
+ end
185
+
186
+ context "when the data is different" do
187
+
188
+ let(:other_object) do
189
+ described_class.new(2)
190
+ end
191
+
192
+ it "returns false" do
193
+ expect(object).not_to eq(other_object)
194
+ end
195
+ end
196
+
197
+ context "when other is not a BSON integer" do
198
+
199
+ it "returns false" do
200
+ expect(described_class.new(1)).to_not eq('1')
201
+ end
202
+ end
203
+ end
204
+
205
+ describe "#===" do
206
+
207
+ let(:object) do
208
+ described_class.new(1)
209
+ end
210
+
211
+ context "when comparing with another BSON int64" do
212
+
213
+ context "when the data is equal" do
214
+
215
+ let(:other_object) do
216
+ described_class.new(1)
217
+ end
218
+
219
+ it "returns true" do
220
+ expect(object === other_object).to be true
221
+ end
222
+
223
+ context "other object is of another integer type" do
224
+
225
+ let(:other_object) do
226
+ BSON::Int32.new(1)
227
+ end
228
+
229
+ it "returns false" do
230
+ expect(object === other_object).to be false
231
+ end
232
+ end
233
+ end
234
+
235
+ context "when the data is not equal" do
236
+
237
+ let(:other_object) do
238
+ described_class.new(2)
239
+ end
240
+
241
+ it "returns false" do
242
+ expect(object === other_object).to be false
243
+ end
244
+ end
245
+ end
246
+
247
+ context "when comparing to an object id class" do
248
+
249
+ it "returns false" do
250
+ expect(described_class.new(1) === described_class).to be false
251
+ end
252
+ end
253
+
254
+ context "when comparing with a string" do
255
+
256
+ context "when the data is equal" do
257
+
258
+ let(:other) do
259
+ '1'
260
+ end
261
+
262
+ it "returns false" do
263
+ expect(object === other).to be false
264
+ end
265
+ end
266
+
267
+ context "when the data is not equal" do
268
+
269
+ let(:other) do
270
+ '2'
271
+ end
272
+
273
+ it "returns false" do
274
+ expect(object === other).to be false
275
+ end
276
+ end
277
+ end
278
+
279
+ context "when comparing with a non-bson integer object" do
280
+
281
+ it "returns false" do
282
+ expect(object === []).to be false
283
+ end
284
+ end
285
+
286
+ context "when comparing with a non int64 class" do
287
+
288
+ it "returns false" do
289
+ expect(object === String).to be false
290
+ end
291
+ end
292
+ end
293
+
294
+ describe '#value' do
295
+ let(:obj) { described_class.new(12345) }
296
+
297
+ it 'returns value passed to initializer' do
298
+ expect(obj.value).to eq(12345)
299
+ end
300
+ end
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
+
312
+ describe '#as_extended_json' do
313
+ let(:object) { described_class.new(42) }
314
+
315
+ context 'canonical mode' do
316
+ it 'returns a Hash with key $numberLong' do
317
+ expect(object.as_extended_json).to eq({ '$numberLong' => '42' })
318
+ end
319
+ end
320
+
321
+ context 'relaxed mode' do
322
+ it 'returns an Integer' do
323
+ expect(object.as_extended_json(mode: :relaxed)).to eq 42
324
+ end
325
+ end
326
+
327
+ context 'legacy mode' do
328
+ it 'returns an Integer' do
329
+ expect(object.as_extended_json(mode: :legacy)).to eq 42
330
+ end
331
+ end
332
+
333
+ it_behaves_like "an Extended JSON serializable object"
27
334
  end
28
335
  end
@@ -1,4 +1,5 @@
1
- # Copyright (C) 2009-2014 MongoDB Inc.
1
+ # rubocop:todo all
2
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
4
5
  # you may not use this file except in compliance with the License.
@@ -62,10 +63,67 @@ describe Integer do
62
63
  describe "#to_bson_key" do
63
64
 
64
65
  let(:obj) { Integer::MAX_32BIT - 1 }
65
- let(:encoded) { obj.to_s }
66
+ let(:encoded) { obj }
66
67
 
67
- it "returns the key as a string" do
68
+ it "returns the key as an integer" do
68
69
  expect(obj.to_bson_key).to eq(encoded)
69
70
  end
70
71
  end
72
+
73
+ describe '#as_json' do
74
+ it 'returns an integer string' do
75
+ expect(42.to_json).to eq '42'
76
+ end
77
+ end
78
+
79
+ describe '#as_extended_json' do
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
88
+ end
89
+
90
+ context 'relaxed mode' do
91
+ it 'returns integer' do
92
+ expect(object.as_extended_json(mode: :relaxed)).to eq 42
93
+ end
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"
103
+ end
104
+
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
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"
127
+ end
128
+ end
71
129
  end
@@ -1,4 +1,5 @@
1
- # Copyright (C) 2009-2014 MongoDB Inc.
1
+ # rubocop:todo all
2
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
4
5
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,5 @@
1
- # Copyright (C) 2009-2014 MongoDB Inc.
1
+ # rubocop:todo all
2
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
4
5
  # you may not use this file except in compliance with 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