bson 4.8.0-java → 4.9.2-java

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 (89) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/README.md +18 -6
  5. data/lib/bson-ruby.jar +0 -0
  6. data/lib/bson.rb +1 -1
  7. data/lib/bson/active_support.rb +1 -1
  8. data/lib/bson/array.rb +14 -3
  9. data/lib/bson/binary.rb +1 -1
  10. data/lib/bson/boolean.rb +9 -2
  11. data/lib/bson/code.rb +1 -1
  12. data/lib/bson/code_with_scope.rb +16 -3
  13. data/lib/bson/config.rb +1 -1
  14. data/lib/bson/date.rb +1 -1
  15. data/lib/bson/date_time.rb +1 -1
  16. data/lib/bson/db_pointer.rb +7 -1
  17. data/lib/bson/decimal128.rb +1 -1
  18. data/lib/bson/decimal128/builder.rb +1 -1
  19. data/lib/bson/document.rb +1 -1
  20. data/lib/bson/environment.rb +2 -1
  21. data/lib/bson/error.rb +4 -0
  22. data/lib/bson/ext_json.rb +4 -3
  23. data/lib/bson/false_class.rb +1 -1
  24. data/lib/bson/float.rb +1 -1
  25. data/lib/bson/hash.rb +14 -3
  26. data/lib/bson/int32.rb +1 -1
  27. data/lib/bson/int64.rb +1 -1
  28. data/lib/bson/integer.rb +1 -1
  29. data/lib/bson/json.rb +1 -1
  30. data/lib/bson/max_key.rb +1 -1
  31. data/lib/bson/min_key.rb +1 -1
  32. data/lib/bson/nil_class.rb +1 -1
  33. data/lib/bson/object.rb +1 -1
  34. data/lib/bson/object_id.rb +1 -1
  35. data/lib/bson/open_struct.rb +1 -1
  36. data/lib/bson/regexp.rb +8 -2
  37. data/lib/bson/registry.rb +1 -1
  38. data/lib/bson/specialized.rb +1 -1
  39. data/lib/bson/string.rb +1 -1
  40. data/lib/bson/symbol.rb +1 -1
  41. data/lib/bson/time.rb +40 -6
  42. data/lib/bson/time_with_zone.rb +1 -1
  43. data/lib/bson/timestamp.rb +1 -1
  44. data/lib/bson/true_class.rb +1 -1
  45. data/lib/bson/undefined.rb +1 -1
  46. data/lib/bson/version.rb +2 -2
  47. data/spec/bson/array_spec.rb +1 -1
  48. data/spec/bson/binary_spec.rb +1 -1
  49. data/spec/bson/binary_uuid_spec.rb +1 -1
  50. data/spec/bson/boolean_spec.rb +1 -1
  51. data/spec/bson/code_spec.rb +1 -1
  52. data/spec/bson/code_with_scope_spec.rb +1 -1
  53. data/spec/bson/date_spec.rb +1 -1
  54. data/spec/bson/date_time_spec.rb +1 -1
  55. data/spec/bson/decimal128_spec.rb +1 -1
  56. data/spec/bson/document_spec.rb +1 -1
  57. data/spec/bson/ext_json_parse_spec.rb +34 -2
  58. data/spec/bson/false_class_spec.rb +1 -1
  59. data/spec/bson/float_spec.rb +1 -1
  60. data/spec/bson/hash_spec.rb +1 -1
  61. data/spec/bson/int32_spec.rb +1 -1
  62. data/spec/bson/int64_spec.rb +1 -1
  63. data/spec/bson/integer_spec.rb +1 -1
  64. data/spec/bson/json_spec.rb +1 -1
  65. data/spec/bson/max_key_spec.rb +1 -1
  66. data/spec/bson/min_key_spec.rb +1 -1
  67. data/spec/bson/nil_class_spec.rb +1 -1
  68. data/spec/bson/object_id_spec.rb +1 -1
  69. data/spec/bson/object_spec.rb +1 -1
  70. data/spec/bson/open_struct_spec.rb +1 -1
  71. data/spec/bson/raw_spec.rb +12 -1
  72. data/spec/bson/regexp_spec.rb +1 -1
  73. data/spec/bson/registry_spec.rb +1 -1
  74. data/spec/bson/string_spec.rb +1 -1
  75. data/spec/bson/symbol_spec.rb +1 -1
  76. data/spec/bson/time_spec.rb +205 -2
  77. data/spec/bson/time_with_zone_spec.rb +1 -1
  78. data/spec/bson/timestamp_spec.rb +1 -1
  79. data/spec/bson/true_class_spec.rb +1 -1
  80. data/spec/bson/undefined_spec.rb +1 -1
  81. data/spec/bson_spec.rb +1 -1
  82. data/spec/runners/common_driver.rb +1 -1
  83. data/spec/runners/corpus.rb +13 -10
  84. data/spec/runners/corpus_legacy.rb +1 -1
  85. data/spec/spec_helper.rb +1 -1
  86. data/spec/spec_tests/data/corpus/top.json +0 -4
  87. data/spec/support/shared_examples.rb +1 -1
  88. metadata +79 -82
  89. metadata.gz.sig +0 -0
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2018-2019 MongoDB Inc.
1
+ # Copyright (C) 2018-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.
@@ -13,5 +13,5 @@
13
13
  # limitations under the License.
14
14
 
15
15
  module BSON
16
- VERSION = "4.8.0".freeze
16
+ VERSION = "4.9.2".freeze
17
17
  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) 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.
@@ -50,14 +50,46 @@ describe "BSON::ExtJSON.parse" do
50
50
  end
51
51
  end
52
52
 
53
- context 'when input is a timestamp' do
53
+ context 'when input is a BSON timestamp' do
54
54
  let(:input) { {'$timestamp' => {'t' => 12345, 'i' => 42}} }
55
55
 
56
- it 'returns a timestamp object' do
56
+ it 'returns a BSON::Timestamp instance' do
57
57
  parsed.should == BSON::Timestamp.new(12345, 42)
58
58
  end
59
59
  end
60
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
+
61
93
  context 'when input is an int32' do
62
94
  let(:input) do
63
95
  {'$numberInt' => '42'}
@@ -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) 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.
@@ -580,4 +580,15 @@ describe Regexp::Raw do
580
580
  end
581
581
  end
582
582
  end
583
- end
583
+
584
+ describe 'yaml loading' do
585
+ let(:regexp) { described_class.new('hello.world', 's') }
586
+
587
+ it 'round-trips' do
588
+ actual = YAML.load(regexp.to_yaml)
589
+ actual.pattern.should == 'hello.world'
590
+ actual.options.should == 's'
591
+ actual.compile.should =~ "hello\nworld"
592
+ end
593
+ end
594
+ 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,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.
@@ -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.
@@ -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