bson 4.7.1 → 4.9.1

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. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +2 -1
  4. data/README.md +6 -2
  5. data/ext/bson/bson-endian.h +1 -1
  6. data/ext/bson/bson-native.h +15 -5
  7. data/ext/bson/bytebuf.c +1 -1
  8. data/ext/bson/endian.c +1 -1
  9. data/ext/bson/init.c +26 -3
  10. data/ext/bson/read.c +127 -33
  11. data/ext/bson/util.c +41 -1
  12. data/ext/bson/write.c +18 -14
  13. data/lib/bson.rb +4 -1
  14. data/lib/bson/active_support.rb +1 -1
  15. data/lib/bson/array.rb +34 -5
  16. data/lib/bson/binary.rb +42 -4
  17. data/lib/bson/boolean.rb +12 -3
  18. data/lib/bson/code.rb +16 -2
  19. data/lib/bson/code_with_scope.rb +32 -5
  20. data/lib/bson/config.rb +1 -1
  21. data/lib/bson/date.rb +1 -1
  22. data/lib/bson/date_time.rb +1 -1
  23. data/lib/bson/db_pointer.rb +110 -0
  24. data/lib/bson/decimal128.rb +16 -2
  25. data/lib/bson/decimal128/builder.rb +1 -1
  26. data/lib/bson/document.rb +1 -1
  27. data/lib/bson/environment.rb +2 -1
  28. data/lib/bson/error.rb +21 -0
  29. data/lib/bson/ext_json.rb +375 -0
  30. data/lib/bson/false_class.rb +1 -1
  31. data/lib/bson/float.rb +48 -2
  32. data/lib/bson/hash.rb +36 -5
  33. data/lib/bson/int32.rb +22 -2
  34. data/lib/bson/int64.rb +29 -4
  35. data/lib/bson/integer.rb +35 -1
  36. data/lib/bson/json.rb +1 -1
  37. data/lib/bson/max_key.rb +13 -1
  38. data/lib/bson/min_key.rb +13 -1
  39. data/lib/bson/nil_class.rb +4 -2
  40. data/lib/bson/object.rb +28 -1
  41. data/lib/bson/object_id.rb +16 -2
  42. data/lib/bson/open_struct.rb +1 -1
  43. data/lib/bson/regexp.rb +20 -3
  44. data/lib/bson/registry.rb +1 -1
  45. data/lib/bson/specialized.rb +4 -2
  46. data/lib/bson/string.rb +4 -2
  47. data/lib/bson/symbol.rb +93 -4
  48. data/lib/bson/time.rb +63 -4
  49. data/lib/bson/time_with_zone.rb +1 -1
  50. data/lib/bson/timestamp.rb +16 -2
  51. data/lib/bson/true_class.rb +1 -1
  52. data/lib/bson/undefined.rb +12 -1
  53. data/lib/bson/version.rb +2 -2
  54. data/spec/bson/array_spec.rb +1 -1
  55. data/spec/bson/binary_spec.rb +34 -4
  56. data/spec/bson/binary_uuid_spec.rb +1 -1
  57. data/spec/bson/boolean_spec.rb +1 -1
  58. data/spec/bson/code_spec.rb +1 -1
  59. data/spec/bson/code_with_scope_spec.rb +1 -1
  60. data/spec/bson/date_spec.rb +1 -1
  61. data/spec/bson/date_time_spec.rb +1 -1
  62. data/spec/bson/decimal128_spec.rb +1 -1
  63. data/spec/bson/document_spec.rb +1 -1
  64. data/spec/bson/ext_json_parse_spec.rb +308 -0
  65. data/spec/bson/false_class_spec.rb +1 -1
  66. data/spec/bson/float_spec.rb +37 -1
  67. data/spec/bson/hash_spec.rb +71 -1
  68. data/spec/bson/int32_spec.rb +21 -1
  69. data/spec/bson/int64_spec.rb +39 -1
  70. data/spec/bson/integer_spec.rb +27 -1
  71. data/spec/bson/json_spec.rb +1 -1
  72. data/spec/bson/max_key_spec.rb +1 -1
  73. data/spec/bson/min_key_spec.rb +1 -1
  74. data/spec/bson/nil_class_spec.rb +1 -1
  75. data/spec/bson/object_id_spec.rb +1 -1
  76. data/spec/bson/object_spec.rb +1 -1
  77. data/spec/bson/open_struct_spec.rb +1 -1
  78. data/spec/bson/raw_spec.rb +22 -1
  79. data/spec/bson/regexp_spec.rb +1 -1
  80. data/spec/bson/registry_spec.rb +1 -1
  81. data/spec/bson/string_spec.rb +1 -1
  82. data/spec/bson/symbol_raw_spec.rb +45 -0
  83. data/spec/bson/symbol_spec.rb +61 -1
  84. data/spec/bson/time_spec.rb +205 -2
  85. data/spec/bson/time_with_zone_spec.rb +1 -1
  86. data/spec/bson/timestamp_spec.rb +1 -1
  87. data/spec/bson/true_class_spec.rb +1 -1
  88. data/spec/bson/undefined_spec.rb +1 -1
  89. data/spec/bson_spec.rb +1 -1
  90. data/spec/{support → runners}/common_driver.rb +1 -1
  91. data/spec/runners/corpus.rb +185 -0
  92. data/spec/{support/corpus.rb → runners/corpus_legacy.rb} +41 -59
  93. data/spec/spec_helper.rb +10 -3
  94. data/spec/{bson/driver_bson_spec.rb → spec_tests/common_driver_spec.rb} +1 -0
  95. data/spec/{bson/corpus_spec.rb → spec_tests/corpus_legacy_spec.rb} +4 -4
  96. data/spec/spec_tests/corpus_spec.rb +124 -0
  97. data/spec/spec_tests/data/corpus/README.md +15 -0
  98. data/spec/spec_tests/data/corpus/array.json +49 -0
  99. data/spec/spec_tests/data/corpus/binary.json +85 -0
  100. data/spec/spec_tests/data/corpus/boolean.json +27 -0
  101. data/spec/spec_tests/data/corpus/code.json +67 -0
  102. data/spec/spec_tests/data/corpus/code_w_scope.json +78 -0
  103. data/spec/spec_tests/data/corpus/datetime.json +42 -0
  104. data/spec/spec_tests/data/corpus/dbpointer.json +56 -0
  105. data/spec/spec_tests/data/corpus/dbref.json +31 -0
  106. data/spec/spec_tests/data/corpus/decimal128-1.json +317 -0
  107. data/spec/spec_tests/data/corpus/decimal128-2.json +793 -0
  108. data/spec/spec_tests/data/corpus/decimal128-3.json +1771 -0
  109. data/spec/spec_tests/data/corpus/decimal128-4.json +117 -0
  110. data/spec/spec_tests/data/corpus/decimal128-5.json +402 -0
  111. data/spec/spec_tests/data/corpus/decimal128-6.json +119 -0
  112. data/spec/spec_tests/data/corpus/decimal128-7.json +323 -0
  113. data/spec/spec_tests/data/corpus/document.json +36 -0
  114. data/spec/spec_tests/data/corpus/double.json +87 -0
  115. data/spec/spec_tests/data/corpus/int32.json +43 -0
  116. data/spec/spec_tests/data/corpus/int64.json +43 -0
  117. data/spec/spec_tests/data/corpus/maxkey.json +12 -0
  118. data/spec/spec_tests/data/corpus/minkey.json +12 -0
  119. data/spec/spec_tests/data/corpus/multi-type-deprecated.json +15 -0
  120. data/spec/spec_tests/data/corpus/multi-type.json +11 -0
  121. data/spec/spec_tests/data/corpus/null.json +12 -0
  122. data/spec/spec_tests/data/corpus/oid.json +28 -0
  123. data/spec/spec_tests/data/corpus/regex.json +65 -0
  124. data/spec/spec_tests/data/corpus/string.json +72 -0
  125. data/spec/spec_tests/data/corpus/symbol.json +80 -0
  126. data/spec/spec_tests/data/corpus/timestamp.json +24 -0
  127. data/spec/spec_tests/data/corpus/top.json +236 -0
  128. data/spec/spec_tests/data/corpus/undefined.json +15 -0
  129. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/array.json +0 -0
  130. data/spec/{support/corpus-tests/failures → spec_tests/data/corpus_legacy}/binary.json +0 -0
  131. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/boolean.json +0 -0
  132. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/code.json +1 -1
  133. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/code_w_scope.json +1 -1
  134. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/document.json +1 -1
  135. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/double.json +1 -1
  136. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/datetime.json +0 -0
  137. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/dbpointer.json +0 -0
  138. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/int64.json +0 -0
  139. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/symbol.json +0 -0
  140. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/int32.json +1 -1
  141. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/maxkey.json +1 -1
  142. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/minkey.json +1 -1
  143. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/null.json +1 -1
  144. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/oid.json +0 -0
  145. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/regex.json +1 -1
  146. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/string.json +0 -0
  147. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/timestamp.json +1 -1
  148. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/top.json +0 -0
  149. data/spec/{support/corpus-tests/failures → spec_tests/data/corpus_legacy}/undefined.json +0 -0
  150. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-1.json +0 -0
  151. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-2.json +0 -0
  152. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-3.json +0 -0
  153. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-4.json +0 -0
  154. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-5.json +0 -0
  155. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-6.json +0 -0
  156. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-7.json +0 -0
  157. data/spec/support/shared_examples.rb +1 -1
  158. metadata +176 -102
  159. metadata.gz.sig +0 -0
@@ -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.
@@ -253,4 +253,24 @@ describe BSON::Int32 do
253
253
  expect(obj.value).to eq(12345)
254
254
  end
255
255
  end
256
+
257
+ describe '#as_extended_json' do
258
+ context 'canonical mode' do
259
+ it 'returns $numberInt' do
260
+ described_class.new(42).as_extended_json.should == {'$numberInt' => '42'}
261
+ end
262
+ end
263
+
264
+ context 'relaxed mode' do
265
+ it 'returns integer' do
266
+ described_class.new(42).as_extended_json(mode: :relaxed).should == 42
267
+ end
268
+ end
269
+
270
+ context 'legacy mode' do
271
+ it 'returns integer' do
272
+ described_class.new(42).as_extended_json(mode: :legacy).should be 42
273
+ end
274
+ end
275
+ end
256
276
  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.
@@ -71,6 +71,25 @@ describe BSON::Int64 do
71
71
  it_behaves_like "a bson element"
72
72
  it_behaves_like "a deserializable bson element"
73
73
 
74
+ context 'canonical deserialization' do
75
+ let(:integer) { 42 }
76
+
77
+ let(:bson) do
78
+ BSON::ByteBuffer.new(BSON::Int64.new(integer).to_bson.to_s)
79
+ end
80
+
81
+ let(:deserialized) do
82
+ described_class.from_bson(bson, mode: :bson)
83
+ end
84
+
85
+ it 'deserializes to BSON::Int64' do
86
+ deserialized.class.should be BSON::Int64
87
+ end
88
+
89
+ it 'has the correct value' do
90
+ deserialized.value.should == 42
91
+ end
92
+ end
74
93
 
75
94
  context "when the integer is within the MRI Fixnum range" do
76
95
 
@@ -299,4 +318,23 @@ describe BSON::Int64 do
299
318
  end
300
319
  end
301
320
 
321
+ describe '#as_extended_json' do
322
+ context 'canonical mode' do
323
+ it 'returns $numberLong' do
324
+ described_class.new(42).as_extended_json.should == {'$numberLong' => '42'}
325
+ end
326
+ end
327
+
328
+ context 'relaxed mode' do
329
+ it 'returns integer' do
330
+ described_class.new(42).as_extended_json(mode: :relaxed).should == 42
331
+ end
332
+ end
333
+
334
+ context 'legacy mode' do
335
+ it 'returns integer' do
336
+ described_class.new(42).as_extended_json(mode: :legacy).should be 42
337
+ end
338
+ end
339
+ end
302
340
  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.
@@ -68,4 +68,30 @@ describe Integer do
68
68
  expect(obj.to_bson_key).to eq(encoded)
69
69
  end
70
70
  end
71
+
72
+ describe '#to_json' do
73
+ it 'returns integer' do
74
+ 42.to_json.should == '42'
75
+ end
76
+ end
77
+
78
+ describe '#as_extended_json' do
79
+ context 'canonical mode' do
80
+ it 'returns $numberInt' do
81
+ 42.as_extended_json.should == {'$numberInt' => '42'}
82
+ end
83
+ end
84
+
85
+ context 'relaxed mode' do
86
+ it 'returns integer' do
87
+ 42.as_extended_json(mode: :relaxed).should be 42
88
+ end
89
+ end
90
+
91
+ context 'legacy mode' do
92
+ it 'returns integer' do
93
+ 42.as_extended_json(mode: :legacy).should be 42
94
+ end
95
+ end
96
+ end
71
97
  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.
@@ -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.
@@ -12,13 +12,34 @@ describe Regexp::Raw do
12
12
  described_class.new(pattern, 'im')
13
13
  end
14
14
 
15
- it "returns the regex pattern and options" do
15
+ it "returns the legacy serialization including regex pattern and options" do
16
16
  expect(object.as_json).to eq({ "$regex" => "\\W+", "$options" => "im" })
17
17
  end
18
18
 
19
19
  it_behaves_like "a JSON serializable object"
20
20
  end
21
21
 
22
+ describe '#as_extended_json' do
23
+
24
+ let(:object) do
25
+ described_class.new(pattern, 'im')
26
+ end
27
+
28
+ context 'legacy mode' do
29
+ it "returns the legacy serialization including regex pattern and options" do
30
+ expect(object.as_extended_json(mode: :legacy)).to eq({ "$regex" => "\\W+", "$options" => "im" })
31
+ end
32
+ end
33
+
34
+ context 'canonical/relaxed mode' do
35
+ it "returns the extended json 2.0 serialization" do
36
+ expect(object.as_extended_json).to eq(
37
+ "$regularExpression" => {'pattern' => "\\W+", "options" => "im"}
38
+ )
39
+ end
40
+ end
41
+ end
42
+
22
43
  describe "#to_bson/#from_bson" do
23
44
 
24
45
  let(:options) { 'ilmsux' }
@@ -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,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,45 @@
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::Symbol::Raw do
18
+ describe '#==' do
19
+ let(:one) { described_class.new('foo') }
20
+ let(:two) { described_class.new('foo') }
21
+ let(:three) { described_class.new('bar') }
22
+
23
+ it 'compares equal' do
24
+ one.should == two
25
+ end
26
+
27
+ it 'compares not equal' do
28
+ one.should_not == three
29
+ end
30
+ end
31
+
32
+ describe '#eql?' do
33
+ let(:one) { described_class.new('foo') }
34
+ let(:two) { described_class.new('foo') }
35
+ let(:three) { described_class.new('bar') }
36
+
37
+ it 'compares equal' do
38
+ one.should be_eql(two)
39
+ end
40
+
41
+ it 'compares not equal' do
42
+ one.should_not be_eql(three)
43
+ end
44
+ end
45
+ 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.
@@ -32,6 +32,66 @@ describe Symbol do
32
32
  it_behaves_like "a bson element"
33
33
  it_behaves_like "a serializable bson element"
34
34
  it_behaves_like "a deserializable bson element"
35
+
36
+ context 'canonical deserialization' do
37
+ let(:bson) do
38
+ BSON::ByteBuffer.new(BSON::Symbol::Raw.new(obj).to_bson.to_s)
39
+ end
40
+
41
+ let(:deserialized) do
42
+ described_class.from_bson(bson, mode: :bson)
43
+ end
44
+
45
+ it 'deserializes to BSON::Symbol::Raw' do
46
+ deserialized.class.should be BSON::Symbol::Raw
47
+ end
48
+
49
+ it 'has the correct value' do
50
+ deserialized.to_sym.should be obj
51
+ end
52
+ end
53
+
54
+ context 'when changing bson_type' do
55
+ def perform_test(bson_type_to_use)
56
+ Symbol.class_eval do
57
+ alias_method :bson_type_orig, :bson_type
58
+ define_method(:bson_type) do
59
+ bson_type_to_use
60
+ end
61
+ end
62
+
63
+ begin
64
+ yield
65
+ ensure
66
+ Symbol.class_eval do
67
+ alias_method :bson_type, :bson_type_orig
68
+ remove_method :bson_type_orig
69
+ end
70
+ end
71
+ end
72
+
73
+ let(:value) { :foo }
74
+
75
+ let(:serialized) do
76
+ value.to_bson.to_s
77
+ end
78
+
79
+ context 'when bson_type is set to symbol' do
80
+ it 'serializes to BSON string' do
81
+ perform_test(BSON::Symbol::BSON_TYPE) do
82
+ serialized
83
+ end.should == "\x04\x00\x00\x00foo\x00".force_encoding('binary')
84
+ end
85
+ end
86
+
87
+ context 'when bson_type is set to string' do
88
+ it 'serializes to BSON string' do
89
+ perform_test(BSON::String::BSON_TYPE) do
90
+ serialized
91
+ end.should == "\x04\x00\x00\x00foo\x00".force_encoding('binary')
92
+ end
93
+ end
94
+ end
35
95
  end
36
96
 
37
97
  describe "#to_bson_key" do
@@ -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.
@@ -43,7 +43,7 @@ describe Time do
43
43
  end
44
44
  end
45
45
 
46
- context "when the time is pre epoch" do
46
+ context "when the time precedes epoch" do
47
47
 
48
48
  let(:obj) { Time.utc(1969, 1, 1, 0, 0, 0) }
49
49
  let(:bson) { [ (obj.to_f * 1000).to_i ].pack(BSON::Int64::PACK) }
@@ -51,5 +51,208 @@ describe Time do
51
51
  it_behaves_like "a serializable bson element"
52
52
  it_behaves_like "a deserializable bson element"
53
53
  end
54
+
55
+ context 'when value has sub-millisecond precision' do
56
+ let(:obj) { Time.utc(2012, 1, 1, 0, 0, 0, 999_999) }
57
+
58
+ let(:expected_round_tripped_obj) do
59
+ Time.utc(2012, 1, 1, 0, 0, 0, 999_000)
60
+ end
61
+
62
+ let(:round_tripped_obj) do
63
+ Time.from_bson(obj.to_bson)
64
+ end
65
+
66
+ it 'truncates to milliseconds when round-tripping' do
67
+ round_tripped_obj.should == expected_round_tripped_obj
68
+ end
69
+ end
70
+ end
71
+
72
+ describe '#as_extended_json' do
73
+
74
+ context 'canonical mode' do
75
+ context 'when value has sub-millisecond precision' do
76
+ let(:obj) { Time.utc(2012, 1, 1, 0, 0, 0, 999_999) }
77
+
78
+ let(:expected_serialization) do
79
+ {'$date' => {'$numberLong' => '1325376000999'}}
80
+ end
81
+
82
+ let(:serialization) do
83
+ obj.as_extended_json
84
+ end
85
+
86
+ shared_examples_for 'truncates to milliseconds when serializing' do
87
+ it 'truncates to milliseconds when serializing' do
88
+ serialization.should == expected_serialization
89
+ end
90
+ end
91
+
92
+ it_behaves_like 'truncates to milliseconds when serializing'
93
+
94
+ context 'when value has sub-microsecond precision' do
95
+ let(:obj) { Time.utc(2012, 1, 1, 0, 0, 0, 999_999_999/1000r) }
96
+
97
+ it_behaves_like 'truncates to milliseconds when serializing'
98
+ end
99
+
100
+ context "when the time precedes epoch" do
101
+ let(:obj) { Time.utc(1960, 1, 1, 0, 0, 0, 999_999) }
102
+
103
+ let(:expected_serialization) do
104
+ {'$date' => {'$numberLong' => '-315619199001'}}
105
+ end
106
+
107
+ it_behaves_like 'truncates to milliseconds when serializing'
108
+ end
109
+ end
110
+ end
111
+
112
+ context 'relaxed mode' do
113
+ context 'when value has sub-millisecond precision' do
114
+ let(:obj) { Time.utc(2012, 1, 1, 0, 0, 0, 999_999) }
115
+
116
+ let(:expected_serialization) do
117
+ {'$date' => '2012-01-01T00:00:00.999Z'}
118
+ end
119
+
120
+ let(:serialization) do
121
+ obj.as_extended_json(mode: :relaxed)
122
+ end
123
+
124
+ shared_examples_for 'truncates to milliseconds when serializing' do
125
+ it 'truncates to milliseconds when serializing' do
126
+ serialization.should == expected_serialization
127
+ end
128
+ end
129
+
130
+ it_behaves_like 'truncates to milliseconds when serializing'
131
+
132
+ context 'when value has sub-microsecond precision' do
133
+ let(:obj) { Time.utc(2012, 1, 1, 0, 0, 0, 999_999_999/1000r) }
134
+
135
+ it_behaves_like 'truncates to milliseconds when serializing'
136
+ end
137
+
138
+ context "when the time precedes epoch" do
139
+ let(:obj) { Time.utc(1960, 1, 1, 0, 0, 0, 999_999) }
140
+
141
+ let(:expected_serialization) do
142
+ {'$date' => {'$numberLong' => '-315619199001'}}
143
+ end
144
+
145
+ it_behaves_like 'truncates to milliseconds when serializing'
146
+ end
147
+ end
148
+ end
149
+ end
150
+
151
+ describe '#to_extended_json' do
152
+
153
+ context 'canonical mode' do
154
+ context 'when value has sub-millisecond precision' do
155
+ let(:obj) { Time.utc(2012, 1, 1, 0, 0, 0, 999_999) }
156
+
157
+ let(:expected_serialization) do
158
+ %q`{"$date":{"$numberLong":"1325376000999"}}`
159
+ end
160
+
161
+ let(:serialization) do
162
+ obj.to_extended_json
163
+ end
164
+
165
+ shared_examples_for 'truncates to milliseconds when serializing' do
166
+ it 'truncates to milliseconds when serializing' do
167
+ serialization.should == expected_serialization
168
+ end
169
+ end
170
+
171
+ it_behaves_like 'truncates to milliseconds when serializing'
172
+
173
+ context 'when value has sub-microsecond precision' do
174
+ let(:obj) { Time.utc(2012, 1, 1, 0, 0, 0, 999_999_999/1000r) }
175
+
176
+ it_behaves_like 'truncates to milliseconds when serializing'
177
+ end
178
+
179
+ context "when the time precedes epoch" do
180
+ let(:obj) { Time.utc(1960, 1, 1, 0, 0, 0, 999_999) }
181
+
182
+ let(:expected_serialization) do
183
+ %q`{"$date":{"$numberLong":"-315619199001"}}`
184
+ end
185
+
186
+ it_behaves_like 'truncates to milliseconds when serializing'
187
+ end
188
+ end
189
+ end
190
+
191
+ context 'relaxed mode' do
192
+ context 'when value has sub-millisecond precision' do
193
+ let(:obj) { Time.utc(2012, 1, 1, 0, 0, 0, 999_999) }
194
+
195
+ let(:expected_serialization) do
196
+ %q`{"$date":"2012-01-01T00:00:00.999Z"}`
197
+ end
198
+
199
+ let(:serialization) do
200
+ obj.to_extended_json(mode: :relaxed)
201
+ end
202
+
203
+ shared_examples_for 'truncates to milliseconds when serializing' do
204
+ it 'truncates to milliseconds when serializing' do
205
+ serialization.should == expected_serialization
206
+ end
207
+ end
208
+
209
+ it_behaves_like 'truncates to milliseconds when serializing'
210
+
211
+ context 'when value has sub-microsecond precision' do
212
+ let(:obj) { Time.utc(2012, 1, 1, 0, 0, 0, 999_999_999/1000r) }
213
+
214
+ it_behaves_like 'truncates to milliseconds when serializing'
215
+ end
216
+ end
217
+ end
218
+ end
219
+
220
+ describe '#to_json' do
221
+
222
+ context 'when value has sub-millisecond precision' do
223
+ let(:obj) { Time.utc(2012, 1, 1, 0, 0, 0, 999_999) }
224
+
225
+ let(:expected_serialization) do
226
+ %q`"2012-01-01 00:00:00 UTC"`
227
+ end
228
+
229
+ let(:serialization) do
230
+ obj.to_json
231
+ end
232
+
233
+ shared_examples_for 'truncates to milliseconds when serializing' do
234
+ it 'truncates to milliseconds when serializing' do
235
+ serialization.should == expected_serialization
236
+ end
237
+ end
238
+
239
+ it_behaves_like 'truncates to milliseconds when serializing'
240
+
241
+ context 'when value has sub-microsecond precision' do
242
+ let(:obj) { Time.utc(2012, 1, 1, 0, 0, 0, 999_999_999/1000r) }
243
+
244
+ it_behaves_like 'truncates to milliseconds when serializing'
245
+ end
246
+
247
+ context "when the time precedes epoch" do
248
+ let(:obj) { Time.utc(1960, 1, 1, 0, 0, 0, 999_999) }
249
+
250
+ let(:expected_serialization) do
251
+ %q`"1960-01-01 00:00:00 UTC"`
252
+ end
253
+
254
+ it_behaves_like 'truncates to milliseconds when serializing'
255
+ end
256
+ end
54
257
  end
55
258
  end