bson 4.11.1 → 4.14.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 (95) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/README.md +4 -7
  4. data/ext/bson/bson-native.h +2 -0
  5. data/ext/bson/init.c +9 -0
  6. data/ext/bson/read.c +29 -0
  7. data/lib/bson/active_support.rb +1 -0
  8. data/lib/bson/array.rb +2 -1
  9. data/lib/bson/big_decimal.rb +67 -0
  10. data/lib/bson/binary.rb +8 -5
  11. data/lib/bson/boolean.rb +2 -1
  12. data/lib/bson/code.rb +2 -1
  13. data/lib/bson/code_with_scope.rb +2 -1
  14. data/lib/bson/config.rb +1 -0
  15. data/lib/bson/date.rb +1 -0
  16. data/lib/bson/date_time.rb +1 -0
  17. data/lib/bson/db_pointer.rb +2 -1
  18. data/lib/bson/dbref.rb +125 -0
  19. data/lib/bson/decimal128/builder.rb +27 -20
  20. data/lib/bson/decimal128.rb +27 -12
  21. data/lib/bson/document.rb +61 -18
  22. data/lib/bson/environment.rb +1 -0
  23. data/lib/bson/error.rb +7 -0
  24. data/lib/bson/ext_json.rb +24 -11
  25. data/lib/bson/false_class.rb +2 -1
  26. data/lib/bson/float.rb +21 -32
  27. data/lib/bson/hash.rb +15 -6
  28. data/lib/bson/int32.rb +3 -2
  29. data/lib/bson/int64.rb +3 -2
  30. data/lib/bson/integer.rb +3 -2
  31. data/lib/bson/json.rb +1 -0
  32. data/lib/bson/max_key.rb +3 -2
  33. data/lib/bson/min_key.rb +3 -2
  34. data/lib/bson/nil_class.rb +2 -1
  35. data/lib/bson/object.rb +1 -0
  36. data/lib/bson/object_id.rb +4 -3
  37. data/lib/bson/open_struct.rb +1 -0
  38. data/lib/bson/regexp.rb +17 -6
  39. data/lib/bson/registry.rb +1 -0
  40. data/lib/bson/specialized.rb +1 -0
  41. data/lib/bson/string.rb +3 -2
  42. data/lib/bson/symbol.rb +2 -1
  43. data/lib/bson/time.rb +4 -3
  44. data/lib/bson/time_with_zone.rb +1 -0
  45. data/lib/bson/timestamp.rb +3 -2
  46. data/lib/bson/true_class.rb +2 -1
  47. data/lib/bson/undefined.rb +2 -1
  48. data/lib/bson/version.rb +2 -1
  49. data/lib/bson.rb +8 -5
  50. data/lib/bson_native.bundle +0 -0
  51. data/spec/README.md +14 -0
  52. data/spec/bson/big_decimal_spec.rb +316 -0
  53. data/spec/bson/binary_spec.rb +1 -1
  54. data/spec/bson/binary_uuid_spec.rb +12 -0
  55. data/spec/bson/dbref_spec.rb +461 -0
  56. data/spec/bson/decimal128_spec.rb +16 -0
  57. data/spec/bson/document_as_spec.rb +46 -0
  58. data/spec/bson/document_spec.rb +43 -1
  59. data/spec/bson/ext_json_parse_spec.rb +37 -0
  60. data/spec/bson/hash_as_spec.rb +57 -0
  61. data/spec/bson/hash_spec.rb +32 -0
  62. data/spec/bson/int64_spec.rb +4 -24
  63. data/spec/bson/raw_spec.rb +7 -1
  64. data/spec/bson/regexp_spec.rb +52 -0
  65. data/spec/runners/common_driver.rb +1 -1
  66. data/spec/shared/LICENSE +20 -0
  67. data/spec/shared/bin/get-mongodb-download-url +17 -0
  68. data/spec/shared/bin/s3-copy +45 -0
  69. data/spec/shared/bin/s3-upload +69 -0
  70. data/spec/shared/lib/mrss/child_process_helper.rb +80 -0
  71. data/spec/shared/lib/mrss/cluster_config.rb +231 -0
  72. data/spec/shared/lib/mrss/constraints.rb +386 -0
  73. data/spec/shared/lib/mrss/docker_runner.rb +271 -0
  74. data/spec/shared/lib/mrss/event_subscriber.rb +200 -0
  75. data/spec/shared/lib/mrss/lite_constraints.rb +191 -0
  76. data/spec/shared/lib/mrss/server_version_registry.rb +120 -0
  77. data/spec/shared/lib/mrss/spec_organizer.rb +179 -0
  78. data/spec/shared/lib/mrss/utils.rb +15 -0
  79. data/spec/shared/share/Dockerfile.erb +338 -0
  80. data/spec/shared/share/haproxy-1.conf +16 -0
  81. data/spec/shared/share/haproxy-2.conf +17 -0
  82. data/spec/shared/shlib/distro.sh +74 -0
  83. data/spec/shared/shlib/server.sh +367 -0
  84. data/spec/shared/shlib/set_env.sh +131 -0
  85. data/spec/spec_helper.rb +20 -0
  86. data/spec/spec_tests/common_driver_spec.rb +2 -1
  87. data/spec/spec_tests/data/corpus/binary.json +33 -0
  88. data/spec/spec_tests/data/corpus/dbref.json +21 -1
  89. data/spec/spec_tests/data/corpus/document.json +4 -0
  90. data/spec/spec_tests/data/corpus/regex.json +2 -2
  91. data/spec/spec_tests/data/corpus/top.json +20 -9
  92. data/spec/support/spec_config.rb +2 -1
  93. data.tar.gz.sig +0 -0
  94. metadata +157 -106
  95. metadata.gz.sig +0 -0
data/lib/bson/regexp.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # Copyright (C) 2009-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -26,34 +27,34 @@ module BSON
26
27
  # A regular expression is type 0x0B in the BSON spec.
27
28
  #
28
29
  # @since 2.0.0
29
- BSON_TYPE = 11.chr.force_encoding(BINARY).freeze
30
+ BSON_TYPE = ::String.new(11.chr, encoding: BINARY).freeze
30
31
 
31
32
  # Extended value constant.
32
33
  #
33
34
  # @since 3.2.6
34
- EXTENDED_VALUE = 'x'.freeze
35
+ EXTENDED_VALUE = 'x'
35
36
 
36
37
  # Ignore case constant.
37
38
  #
38
39
  # @since 3.2.6
39
- IGNORECASE_VALUE = 'i'.freeze
40
+ IGNORECASE_VALUE = 'i'
40
41
 
41
42
  # Multiline constant.
42
43
  #
43
44
  # @since 3.2.6
44
- MULTILINE_VALUE = 'm'.freeze
45
+ MULTILINE_VALUE = 'm'
45
46
 
46
47
  # Newline constant.
47
48
  #
48
49
  # @since 3.2.6
49
- NEWLINE_VALUE = 's'.freeze
50
+ NEWLINE_VALUE = 's'
50
51
 
51
52
  # Ruby multiline constant.
52
53
  #
53
54
  # @since 3.2.6
54
55
  #
55
56
  # @deprecated Will be removed in 5.0
56
- RUBY_MULTILINE_VALUE = 'ms'.freeze
57
+ RUBY_MULTILINE_VALUE = 'ms'
57
58
 
58
59
  # Get the regexp as JSON hash data.
59
60
  #
@@ -154,6 +155,16 @@ module BSON
154
155
  #
155
156
  # @since 3.0.0
156
157
  def initialize(pattern, options = '')
158
+ if pattern.include?(NULL_BYTE)
159
+ raise Error::InvalidRegexpPattern, "Regexp pattern cannot contain a null byte: #{pattern}"
160
+ elsif options.is_a?(String) || options.is_a?(Symbol)
161
+ if options.to_s.include?(NULL_BYTE)
162
+ raise Error::InvalidRegexpPattern, "Regexp options cannot contain a null byte: #{options}"
163
+ end
164
+ elsif !options.is_a?(Integer)
165
+ raise ArgumentError, "Regexp options must be a String, Symbol, or Integer"
166
+ end
167
+
157
168
  @pattern = pattern
158
169
  @options = options
159
170
  end
data/lib/bson/registry.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # Copyright (C) 2009-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # Copyright (C) 2009-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
data/lib/bson/string.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
+ # frozen_string_literal: true
2
3
  # Copyright (C) 2009-2020 MongoDB Inc.
3
4
  #
4
5
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -26,12 +27,12 @@ module BSON
26
27
  # A string is type 0x02 in the BSON spec.
27
28
  #
28
29
  # @since 2.0.0
29
- BSON_TYPE = 2.chr.force_encoding(BINARY).freeze
30
+ BSON_TYPE = ::String.new(2.chr, encoding: BINARY).freeze
30
31
 
31
32
  # Regex for matching illegal BSON keys.
32
33
  #
33
34
  # @since 4.1.0
34
- ILLEGAL_KEY = /(\A[$])|(\.)/.freeze
35
+ ILLEGAL_KEY = /(\A[$])|(\.)/
35
36
 
36
37
  # Get the string as encoded BSON.
37
38
  #
data/lib/bson/symbol.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # Copyright (C) 2009-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -28,7 +29,7 @@ module BSON
28
29
  # A symbol is type 0x0E in the BSON spec.
29
30
  #
30
31
  # @since 2.0.0
31
- BSON_TYPE = 14.chr.force_encoding(BINARY).freeze
32
+ BSON_TYPE = ::String.new(14.chr, encoding: BINARY).freeze
32
33
 
33
34
  # Symbols are serialized as strings as symbols are now removed from the
34
35
  # BSON specification. Therefore the bson_type when serializing must be a
data/lib/bson/time.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # Copyright (C) 2009-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -39,7 +40,7 @@ module BSON
39
40
  # A time is type 0x09 in the BSON spec.
40
41
  #
41
42
  # @since 2.0.0
42
- BSON_TYPE = 9.chr.force_encoding(BINARY).freeze
43
+ BSON_TYPE = ::String.new(9.chr, encoding: BINARY).freeze
43
44
 
44
45
  # Get the time as encoded BSON.
45
46
  #
@@ -63,8 +64,8 @@ module BSON
63
64
  #
64
65
  # @note The time is floored to the nearest millisecond.
65
66
  #
66
- # @option options [ true | false ] :relaxed Whether to produce relaxed
67
- # extended JSON representation.
67
+ # @option opts [ nil | :relaxed | :legacy ] :mode Serialization mode
68
+ # (default is canonical extended JSON)
68
69
  #
69
70
  # @return [ Hash ] The extended json representation.
70
71
  def as_extended_json(**options)
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # Copyright (C) 2018-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # Copyright (C) 2009-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -26,12 +27,12 @@ module BSON
26
27
  # A timestamp is type 0x11 in the BSON spec.
27
28
  #
28
29
  # @since 2.0.0
29
- BSON_TYPE = 17.chr.force_encoding(BINARY).freeze
30
+ BSON_TYPE = ::String.new(17.chr, encoding: BINARY).freeze
30
31
 
31
32
  # Error message if an object other than a Timestamp is compared with this object.
32
33
  #
33
34
  # @since 4.3.0
34
- COMPARISON_ERROR_MESSAGE = 'comparison of %s with Timestamp failed'.freeze
35
+ COMPARISON_ERROR_MESSAGE = 'comparison of %s with Timestamp failed'
35
36
 
36
37
  # @!attribute seconds
37
38
  # @return [ Integer ] The number of seconds.
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # Copyright (C) 2009-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -25,7 +26,7 @@ module BSON
25
26
  # A true value in the BSON spec is 0x01.
26
27
  #
27
28
  # @since 2.0.0
28
- TRUE_BYTE = 1.chr.force_encoding(BINARY).freeze
29
+ TRUE_BYTE = ::String.new(1.chr, encoding: BINARY).freeze
29
30
 
30
31
  # The BSON type for true values is the general boolean type of 0x08.
31
32
  #
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # Copyright (C) 2009-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -25,7 +26,7 @@ module BSON
25
26
  # Undefined is type 0x06 in the BSON spec.
26
27
  #
27
28
  # @since 2.0.0
28
- BSON_TYPE = 6.chr.force_encoding(BINARY).freeze
29
+ BSON_TYPE = ::String.new(6.chr, encoding: BINARY).freeze
29
30
 
30
31
  # Determine if undefined is equal to another object.
31
32
  #
data/lib/bson/version.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # Copyright (C) 2009-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,5 +14,5 @@
13
14
  # limitations under the License.
14
15
 
15
16
  module BSON
16
- VERSION = "4.11.1".freeze
17
+ VERSION = "4.14.0"
17
18
  end
data/lib/bson.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # Copyright (C) 2009-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -38,22 +39,22 @@ module BSON
38
39
  # Constant for binary string encoding.
39
40
  #
40
41
  # @since 2.0.0
41
- BINARY = "BINARY".freeze
42
+ BINARY = "BINARY"
42
43
 
43
44
  # Constant for bson types that don't actually serialize a value.
44
45
  #
45
46
  # @since 2.0.0
46
- NO_VALUE = "".force_encoding(BINARY).freeze
47
+ NO_VALUE = ::String.new(encoding: BINARY).freeze
47
48
 
48
49
  # Constant for a null byte (0x00).
49
50
  #
50
51
  # @since 2.0.0
51
- NULL_BYTE = 0.chr.force_encoding(BINARY).freeze
52
+ NULL_BYTE = ::String.new(0.chr, encoding: BINARY).freeze
52
53
 
53
54
  # Constant for UTF-8 string encoding.
54
55
  #
55
56
  # @since 2.0.0
56
- UTF8 = "UTF-8".freeze
57
+ UTF8 = "UTF-8"
57
58
  end
58
59
 
59
60
  require "bson/config"
@@ -73,11 +74,13 @@ require "bson/date"
73
74
  require "bson/date_time"
74
75
  require "bson/db_pointer"
75
76
  require "bson/decimal128"
77
+ require "bson/big_decimal"
76
78
  require "bson/document"
77
79
  require "bson/ext_json"
78
80
  require "bson/false_class"
79
81
  require "bson/float"
80
82
  require "bson/hash"
83
+ require "bson/dbref"
81
84
  require "bson/open_struct"
82
85
  require "bson/max_key"
83
86
  require "bson/min_key"
@@ -100,7 +103,7 @@ require "bson/version"
100
103
  begin
101
104
  if BSON::Environment.jruby?
102
105
  require "bson-ruby.jar"
103
- org.bson.NativeService.new.basicLoad(JRuby.runtime)
106
+ JRuby::Util.load_ext("org.bson.NativeService")
104
107
  else
105
108
  require "bson_native"
106
109
  end
Binary file
data/spec/README.md ADDED
@@ -0,0 +1,14 @@
1
+ # Running BSON Ruby Tests
2
+
3
+ ## Quick Start
4
+
5
+ The test suite requires shared tooling that is stored in a separate repository
6
+ and is referenced as a submodule. After checking out the desired bson-ruby
7
+ branch, check out the matching submodules:
8
+
9
+ git submodule init
10
+ git submodule update
11
+
12
+ Then, to run the test suite:
13
+
14
+ rake
@@ -0,0 +1,316 @@
1
+ # Copyright (C) 2016-2021 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::BigDecimal do
18
+
19
+ describe '#from_bson' do
20
+ shared_examples_for 'a BSON::BigDecimal deserializer' do
21
+
22
+ let(:decimal128) do
23
+ BSON::Decimal128.new(argument)
24
+ end
25
+
26
+ let(:deserialized_big_decimal) do
27
+ BigDecimal.from_bson(decimal128.to_bson)
28
+ end
29
+
30
+ let(:deserialized_decimal128) do
31
+ BSON::Decimal128.from_bson(decimal128.to_bson)
32
+ end
33
+
34
+ it 'deserializes Decimal128 encoded bson correctly' do
35
+ if deserialized_decimal128.to_s == "NaN"
36
+ expect(deserialized_big_decimal.nan?).to be true
37
+ else
38
+ expect(deserialized_big_decimal).to eq(deserialized_decimal128.to_big_decimal)
39
+ end
40
+ end
41
+ end
42
+
43
+ context 'when Infinity is passed' do
44
+
45
+ let(:argument) { "Infinity" }
46
+
47
+ it_behaves_like 'a BSON::BigDecimal deserializer'
48
+ end
49
+
50
+ context 'when -Infinity is passed' do
51
+
52
+ let(:argument) { "-Infinity" }
53
+
54
+ it_behaves_like 'a BSON::BigDecimal deserializer'
55
+ end
56
+
57
+ context 'when NaN is passed' do
58
+
59
+ let(:argument) { "NaN" }
60
+
61
+ it_behaves_like 'a BSON::BigDecimal deserializer'
62
+ end
63
+
64
+ context 'when -NaN is passed' do
65
+ let(:argument) { "-NaN" }
66
+
67
+ it_behaves_like 'a BSON::BigDecimal deserializer'
68
+ end
69
+
70
+ context 'when SNaN is passed' do
71
+ let(:argument) { "SNaN" }
72
+
73
+ it_behaves_like 'a BSON::BigDecimal deserializer'
74
+ end
75
+
76
+ context 'when -SNaN is passed' do
77
+ let(:argument) { "SNaN" }
78
+
79
+ it_behaves_like 'a BSON::BigDecimal deserializer'
80
+ end
81
+
82
+ context 'when -0 is passed' do
83
+ let(:argument) { "-0" }
84
+
85
+ it_behaves_like 'a BSON::BigDecimal deserializer'
86
+ end
87
+
88
+ context 'when a positive integer is passed' do
89
+ let(:argument) { "12" }
90
+
91
+ it_behaves_like 'a BSON::BigDecimal deserializer'
92
+ end
93
+
94
+ context 'when a negative integer is passed' do
95
+ let(:argument) { "-12" }
96
+
97
+ it_behaves_like 'a BSON::BigDecimal deserializer'
98
+ end
99
+
100
+ context 'when a positive float is passed' do
101
+ let(:argument) { "0.12345" }
102
+
103
+ it_behaves_like 'a BSON::BigDecimal deserializer'
104
+ end
105
+
106
+ context 'when a negative float is passed' do
107
+ let(:argument) { "-0.12345" }
108
+
109
+ it_behaves_like 'a BSON::BigDecimal deserializer'
110
+ end
111
+
112
+ context 'when a large positive integer is passed' do
113
+ let(:argument) { "1234567890123456789012345678901234" }
114
+
115
+ it_behaves_like 'a BSON::BigDecimal deserializer'
116
+ end
117
+
118
+ context 'when a large negative integer is passed' do
119
+ let(:argument) { "-1234567890123456789012345678901234" }
120
+
121
+ it_behaves_like 'a BSON::BigDecimal deserializer'
122
+ end
123
+ end
124
+
125
+ describe "#to_bson" do
126
+ shared_examples_for 'a BSON::BigDecimal serializer' do
127
+
128
+ let(:decimal128) do
129
+ BSON::Decimal128.new(BigDecimal(argument).to_s)
130
+ end
131
+
132
+ let(:decimal_128_bson) do
133
+ decimal128.to_bson
134
+ end
135
+
136
+ let(:big_decimal_bson) do
137
+ BigDecimal(argument).to_bson
138
+ end
139
+
140
+ it 'serializes BigDecimals correctly' do
141
+ expect(decimal_128_bson.to_s).to eq(big_decimal_bson.to_s)
142
+ end
143
+ end
144
+
145
+ context 'when Infinity is passed' do
146
+
147
+ let(:argument) { "Infinity" }
148
+
149
+ it_behaves_like 'a BSON::BigDecimal serializer'
150
+ end
151
+
152
+ context 'when -Infinity is passed' do
153
+
154
+ let(:argument) { "-Infinity" }
155
+
156
+ it_behaves_like 'a BSON::BigDecimal serializer'
157
+ end
158
+
159
+ context 'when NaN is passed' do
160
+
161
+ let(:argument) { "NaN" }
162
+
163
+ it_behaves_like 'a BSON::BigDecimal serializer'
164
+ end
165
+
166
+ context 'when -0 is passed' do
167
+ let(:argument) { "-0" }
168
+
169
+ it_behaves_like 'a BSON::BigDecimal serializer'
170
+ end
171
+
172
+ context 'when a positive integer is passed' do
173
+ let(:argument) { "12" }
174
+
175
+ it_behaves_like 'a BSON::BigDecimal serializer'
176
+ end
177
+
178
+ context 'when a negative integer is passed' do
179
+ let(:argument) { "-12" }
180
+
181
+ it_behaves_like 'a BSON::BigDecimal serializer'
182
+ end
183
+
184
+ context 'when a positive float is passed' do
185
+ let(:argument) { "0.12345" }
186
+
187
+ it_behaves_like 'a BSON::BigDecimal serializer'
188
+ end
189
+
190
+ context 'when a negative float is passed' do
191
+ let(:argument) { "-0.12345" }
192
+
193
+ it_behaves_like 'a BSON::BigDecimal serializer'
194
+ end
195
+
196
+ context 'when a large positive integer is passed' do
197
+ let(:argument) { "1234567890123456789012345678901234" }
198
+
199
+ it_behaves_like 'a BSON::BigDecimal serializer'
200
+ end
201
+
202
+ context 'when a large negative integer is passed' do
203
+ let(:argument) { "-1234567890123456789012345678901234" }
204
+
205
+ it_behaves_like 'a BSON::BigDecimal serializer'
206
+ end
207
+
208
+ context "when passing an out of range Decimal128" do
209
+ let(:argument) { "1E1000000" }
210
+
211
+ it "raises an error" do
212
+ expect do
213
+ BigDecimal(argument).to_bson
214
+ end.to raise_error(BSON::Decimal128::InvalidRange)
215
+ end
216
+ end
217
+
218
+ context "when passing a number with too much precision for Decimal128" do
219
+ let(:argument) { "1.000000000000000000000000000000000000000000000000001" }
220
+
221
+ it "raises an error" do
222
+ expect do
223
+ BigDecimal(argument).to_bson
224
+ end.to raise_error(BSON::Decimal128::UnrepresentablePrecision)
225
+ end
226
+ end
227
+ end
228
+
229
+ describe "#from_bson/#to_bson" do
230
+ shared_examples_for 'a BSON::BigDecimal round trip' do
231
+
232
+ let(:big_decimal) do
233
+ BigDecimal(argument)
234
+ end
235
+
236
+ let(:big_decimal_bson) do
237
+ big_decimal.to_bson
238
+ end
239
+
240
+ let(:deserialized_big_decimal) do
241
+ BigDecimal.from_bson(big_decimal_bson)
242
+ end
243
+
244
+ it 'serializes BigDecimals correctly' do
245
+ if big_decimal.nan?
246
+ expect(deserialized_big_decimal.nan?).to be true
247
+ else
248
+ expect(deserialized_big_decimal).to eq(big_decimal)
249
+ end
250
+ end
251
+ end
252
+
253
+ context 'when Infinity is passed' do
254
+
255
+ let(:argument) { "Infinity" }
256
+
257
+ it_behaves_like 'a BSON::BigDecimal round trip'
258
+ end
259
+
260
+ context 'when -Infinity is passed' do
261
+
262
+ let(:argument) { "-Infinity" }
263
+
264
+ it_behaves_like 'a BSON::BigDecimal round trip'
265
+ end
266
+
267
+ context 'when NaN is passed' do
268
+
269
+ let(:argument) { "NaN" }
270
+
271
+ it_behaves_like 'a BSON::BigDecimal round trip'
272
+ end
273
+
274
+ context 'when -0 is passed' do
275
+ let(:argument) { "-0" }
276
+
277
+ it_behaves_like 'a BSON::BigDecimal round trip'
278
+ end
279
+
280
+ context 'when a positive integer is passed' do
281
+ let(:argument) { "12" }
282
+
283
+ it_behaves_like 'a BSON::BigDecimal round trip'
284
+ end
285
+
286
+ context 'when a negative integer is passed' do
287
+ let(:argument) { "-12" }
288
+
289
+ it_behaves_like 'a BSON::BigDecimal round trip'
290
+ end
291
+
292
+ context 'when a positive float is passed' do
293
+ let(:argument) { "0.12345" }
294
+
295
+ it_behaves_like 'a BSON::BigDecimal round trip'
296
+ end
297
+
298
+ context 'when a negative float is passed' do
299
+ let(:argument) { "-0.12345" }
300
+
301
+ it_behaves_like 'a BSON::BigDecimal round trip'
302
+ end
303
+
304
+ context 'when a large positive integer is passed' do
305
+ let(:argument) { "1234567890123456789012345678901234" }
306
+
307
+ it_behaves_like 'a BSON::BigDecimal round trip'
308
+ end
309
+
310
+ context 'when a large negative integer is passed' do
311
+ let(:argument) { "-1234567890123456789012345678901234" }
312
+
313
+ it_behaves_like 'a BSON::BigDecimal round trip'
314
+ end
315
+ end
316
+ end
@@ -255,7 +255,7 @@ describe BSON::Binary do
255
255
  let(:obj) { described_class.new("\x00" * 16, :uuid) }
256
256
 
257
257
  it 'accepts symbol representation' do
258
- expect(obj.to_uuid(:standard)).to eq('00000000-0000-0000-0000000000000000')
258
+ expect(obj.to_uuid(:standard)).to eq('00000000-0000-0000-0000-000000000000')
259
259
  end
260
260
 
261
261
  it 'rejects string representation' do
@@ -136,6 +136,10 @@ describe "BSON::Binary - UUID spec tests" do
136
136
  it 'decodes as python legacy' do
137
137
  expect(binary.to_uuid(:python_legacy).gsub('-', '').upcase).not_to eq("00112233445566778899AABBCCDDEEFF")
138
138
  end
139
+
140
+ it 'expects four dashes when output as String' do
141
+ expect(binary.to_uuid(:csharp_legacy)).to eq("00112233-4455-6677-8899-aabbccddeeff")
142
+ end
139
143
  end
140
144
 
141
145
  context ':uuid_old, java legacy encoded' do
@@ -154,6 +158,10 @@ describe "BSON::Binary - UUID spec tests" do
154
158
  it 'decodes as python legacy' do
155
159
  expect(binary.to_uuid(:python_legacy).gsub('-', '').upcase).not_to eq("00112233445566778899AABBCCDDEEFF")
156
160
  end
161
+
162
+ it 'expects four dashes when output as String' do
163
+ expect(binary.to_uuid(:java_legacy)).to eq("00112233-4455-6677-8899-aabbccddeeff")
164
+ end
157
165
  end
158
166
 
159
167
  context ':uuid_old, python legacy encoded' do
@@ -172,6 +180,10 @@ describe "BSON::Binary - UUID spec tests" do
172
180
  it 'decodes as python legacy' do
173
181
  expect(binary.to_uuid(:python_legacy).gsub('-', '').upcase).to eq("00112233445566778899AABBCCDDEEFF")
174
182
  end
183
+
184
+ it 'expects four dashes when output as String' do
185
+ expect(binary.to_uuid(:python_legacy)).to eq("00112233-4455-6677-8899-aabbccddeeff")
186
+ end
175
187
  end
176
188
  end
177
189
  end