bson 2.0.0.beta → 2.0.0.rc

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of bson might be problematic. Click here for more details.

Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/CHANGELOG.md +80 -0
  5. data/CONTRIBUTING.md +42 -0
  6. data/LICENSE +190 -0
  7. data/NOTICE +2 -0
  8. data/README.md +6 -1
  9. data/Rakefile +15 -1
  10. data/ext/bson/native.c +16 -0
  11. data/lib/bson.rb +12 -50
  12. data/lib/bson/array.rb +14 -1
  13. data/lib/bson/binary.rb +14 -1
  14. data/lib/bson/boolean.rb +14 -1
  15. data/lib/bson/code.rb +14 -1
  16. data/lib/bson/code_with_scope.rb +14 -1
  17. data/lib/bson/document.rb +15 -2
  18. data/lib/bson/encodable.rb +14 -1
  19. data/lib/bson/environment.rb +98 -0
  20. data/lib/bson/false_class.rb +14 -1
  21. data/lib/bson/float.rb +14 -1
  22. data/lib/bson/hash.rb +14 -1
  23. data/lib/bson/int32.rb +15 -2
  24. data/lib/bson/int64.rb +15 -2
  25. data/lib/bson/integer.rb +14 -1
  26. data/lib/bson/json.rb +14 -1
  27. data/lib/bson/max_key.rb +14 -1
  28. data/lib/bson/min_key.rb +14 -1
  29. data/lib/bson/nil_class.rb +14 -1
  30. data/lib/bson/object_id.rb +15 -2
  31. data/lib/bson/regexp.rb +14 -1
  32. data/lib/bson/registry.rb +14 -1
  33. data/lib/bson/specialized.rb +14 -1
  34. data/lib/bson/string.rb +14 -1
  35. data/lib/bson/symbol.rb +14 -1
  36. data/lib/bson/time.rb +14 -1
  37. data/lib/bson/timestamp.rb +14 -1
  38. data/lib/bson/true_class.rb +14 -1
  39. data/lib/bson/undefined.rb +15 -2
  40. data/lib/bson/version.rb +15 -2
  41. data/lib/native.bundle +0 -0
  42. data/spec/bson/array_spec.rb +31 -0
  43. data/spec/bson/binary_spec.rb +103 -0
  44. data/spec/bson/boolean_spec.rb +48 -0
  45. data/spec/bson/code_spec.rb +42 -0
  46. data/spec/bson/code_with_scope_spec.rb +70 -0
  47. data/spec/bson/document_spec.rb +778 -0
  48. data/spec/bson/false_class_spec.rb +28 -0
  49. data/spec/bson/float_spec.rb +29 -0
  50. data/spec/bson/hash_spec.rb +56 -0
  51. data/spec/bson/int32_spec.rb +28 -0
  52. data/spec/bson/int64_spec.rb +28 -0
  53. data/spec/bson/integer_spec.rb +76 -0
  54. data/spec/bson/json_spec.rb +53 -0
  55. data/spec/bson/max_key_spec.rb +75 -0
  56. data/spec/bson/min_key_spec.rb +75 -0
  57. data/spec/bson/nil_class_spec.rb +29 -0
  58. data/spec/bson/object_id_spec.rb +418 -0
  59. data/spec/bson/regexp_spec.rb +89 -0
  60. data/spec/bson/registry_spec.rb +55 -0
  61. data/spec/bson/string_spec.rb +271 -0
  62. data/spec/bson/symbol_spec.rb +45 -0
  63. data/spec/bson/time_spec.rb +43 -0
  64. data/spec/bson/timestamp_spec.rb +74 -0
  65. data/spec/bson/true_class_spec.rb +28 -0
  66. data/spec/bson/undefined_spec.rb +29 -0
  67. data/spec/bson_spec.rb +46 -0
  68. data/spec/spec_helper.rb +31 -0
  69. data/spec/support/shared_examples.rb +95 -0
  70. metadata +85 -5
  71. metadata.gz.sig +1 -0
  72. data/LICENSE.md +0 -13
@@ -0,0 +1,55 @@
1
+ # Copyright (C) 2013 10gen 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::Registry do
18
+
19
+ describe ".get" do
20
+
21
+ context "when the type has a correspoding class" do
22
+
23
+ before do
24
+ described_class.register(BSON::MinKey::BSON_TYPE, BSON::MinKey)
25
+ end
26
+
27
+ let(:klass) do
28
+ described_class.get(BSON::MinKey::BSON_TYPE)
29
+ end
30
+
31
+ it "returns the class" do
32
+ expect(klass).to eq(BSON::MinKey)
33
+ end
34
+ end
35
+
36
+ context "when the type has no corresponding class" do
37
+
38
+ unless BSON::Environment.ruby_18?
39
+
40
+ it "raises an error" do
41
+ expect {
42
+ described_class.get("test")
43
+ }.to raise_error(KeyError)
44
+ end
45
+ else
46
+
47
+ it "raises an error" do
48
+ expect {
49
+ described_class.get("test")
50
+ }.to raise_error(IndexError)
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,271 @@
1
+ # encoding: utf-8
2
+
3
+ # Copyright (C) 2013 10gen Inc.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require "spec_helper"
18
+
19
+ describe String do
20
+
21
+ describe "#to_bson/#from_bson" do
22
+
23
+ let(:type) { 2.chr }
24
+ let(:obj) { "test" }
25
+ let(:bson) { "#{5.to_bson}test#{BSON::NULL_BYTE}" }
26
+
27
+ it_behaves_like "a bson element"
28
+ it_behaves_like "a serializable bson element"
29
+ it_behaves_like "a deserializable bson element"
30
+ end
31
+
32
+ describe "#to_bson_cstring" do
33
+
34
+ context "when the string is valid" do
35
+
36
+ let(:string) do
37
+ "test"
38
+ end
39
+
40
+ let(:encoded) do
41
+ string.to_bson_cstring
42
+ end
43
+
44
+ let(:previous_content) do
45
+ 'previous_content'.force_encoding(BSON::BINARY)
46
+ end
47
+
48
+ it "returns the encoded string" do
49
+ expect(encoded).to eq("test#{BSON::NULL_BYTE}")
50
+ end
51
+
52
+ it_behaves_like "a binary encoded string"
53
+
54
+ it "appends to optional previous content" do
55
+ expect(string.to_bson_cstring(previous_content)).to eq(previous_content << encoded)
56
+ end
57
+ end
58
+
59
+ context "when the string contains a null byte" do
60
+
61
+ let(:string) do
62
+ "test#{BSON::NULL_BYTE}ing"
63
+ end
64
+
65
+ it "raises an error" do
66
+ expect {
67
+ string.to_bson_cstring
68
+ }.to raise_error(RuntimeError)
69
+ end
70
+ end
71
+
72
+ context "when the string contains utf-8 characters" do
73
+
74
+ let(:string) do
75
+ "Straße"
76
+ end
77
+
78
+ let(:encoded) do
79
+ string.to_bson_cstring
80
+ end
81
+
82
+ let(:char) do
83
+ "ß".chr.force_encoding(BSON::BINARY)
84
+ end
85
+
86
+ it "returns the encoded string" do
87
+ expect(encoded).to eq("Stra#{char}e#{BSON::NULL_BYTE}")
88
+ end
89
+
90
+ it_behaves_like "a binary encoded string"
91
+ end
92
+
93
+ context "when the string is encoded in non utf-8" do
94
+
95
+ let(:string) do
96
+ "Straße".encode("iso-8859-1")
97
+ end
98
+
99
+ let(:encoded) do
100
+ string.to_bson_cstring
101
+ end
102
+
103
+ let(:char) do
104
+ "ß".chr.force_encoding(BSON::BINARY)
105
+ end
106
+
107
+ it "returns the encoded string" do
108
+ expect(encoded).to eq("Stra#{char}e#{BSON::NULL_BYTE}")
109
+ end
110
+
111
+ it_behaves_like "a binary encoded string"
112
+ end
113
+
114
+ unless BSON::Environment.ruby_18?
115
+
116
+ context "when the string contains non utf-8 characters" do
117
+
118
+ let(:string) do
119
+ 255.chr
120
+ end
121
+
122
+ it "raises an error" do
123
+ expect {
124
+ string.to_bson_cstring
125
+ }.to raise_error(EncodingError)
126
+ end
127
+ end
128
+ end
129
+ end
130
+
131
+ describe "#to_bson_string" do
132
+
133
+ context "when the string is valid" do
134
+
135
+ let(:string) do
136
+ "test"
137
+ end
138
+
139
+ let(:encoded) do
140
+ string.to_bson_string
141
+ end
142
+
143
+ let(:previous_content) do
144
+ 'previous_content'.force_encoding(BSON::BINARY)
145
+ end
146
+
147
+ it "returns the string" do
148
+ expect(encoded).to eq(string)
149
+ end
150
+
151
+ it_behaves_like "a binary encoded string"
152
+
153
+ it "appends to optional previous content" do
154
+ expect(string.to_bson_string(previous_content)).to eq(previous_content << encoded)
155
+ end
156
+
157
+ end
158
+
159
+ context "when the string contains a null byte" do
160
+
161
+ let(:string) do
162
+ "test#{BSON::NULL_BYTE}ing"
163
+ end
164
+
165
+ let(:encoded) do
166
+ string.to_bson_string
167
+ end
168
+
169
+ it "retains the null byte" do
170
+ expect(encoded).to eq(string)
171
+ end
172
+
173
+ it_behaves_like "a binary encoded string"
174
+ end
175
+
176
+ context "when the string contains utf-8 characters" do
177
+
178
+ let(:string) do
179
+ "Straße"
180
+ end
181
+
182
+ let(:encoded) do
183
+ string.to_bson_string
184
+ end
185
+
186
+ let(:char) do
187
+ "ß".chr.force_encoding(BSON::BINARY)
188
+ end
189
+
190
+ it "returns the encoded string" do
191
+ expect(encoded).to eq("Stra#{char}e")
192
+ end
193
+
194
+ it_behaves_like "a binary encoded string"
195
+ end
196
+
197
+ context "when the string is encoded in non utf-8" do
198
+
199
+ let(:string) do
200
+ "Straße".encode("iso-8859-1")
201
+ end
202
+
203
+ let(:encoded) do
204
+ string.to_bson_string
205
+ end
206
+
207
+ let(:char) do
208
+ "ß".chr.force_encoding(BSON::BINARY)
209
+ end
210
+
211
+ it "returns the encoded string" do
212
+ expect(encoded).to eq("Stra#{char}e")
213
+ end
214
+
215
+ it_behaves_like "a binary encoded string"
216
+ end
217
+
218
+ unless BSON::Environment.ruby_18?
219
+
220
+ context "when the string contains non utf-8 characters" do
221
+
222
+ let(:string) do
223
+ 255.chr
224
+ end
225
+
226
+ it "raises an error" do
227
+ expect {
228
+ string.to_bson_string
229
+ }.to raise_error(EncodingError)
230
+ end
231
+ end
232
+ end
233
+ end
234
+
235
+ context "when the class is loaded" do
236
+
237
+ let(:registered) do
238
+ BSON::Registry.get(String::BSON_TYPE)
239
+ end
240
+
241
+ it "registers the type" do
242
+ expect(registered).to eq(String)
243
+ end
244
+ end
245
+
246
+ describe "#to_bson_key" do
247
+
248
+ let(:string) { "test" }
249
+ let(:encoded) { string.to_s + BSON::NULL_BYTE }
250
+ let(:previous_content) { 'previous_content'.force_encoding(BSON::BINARY) }
251
+
252
+ it "returns the encoded string" do
253
+ expect(string.to_bson_key).to eq(encoded)
254
+ end
255
+
256
+ it "appends to optional previous content" do
257
+ expect(string.to_bson_key(previous_content)).to eq(previous_content << encoded)
258
+ end
259
+ end
260
+
261
+ describe "#to_hex_string" do
262
+
263
+ let(:string) do
264
+ "testing123"
265
+ end
266
+
267
+ it "converts the string to hex" do
268
+ expect(string.to_hex_string).to eq("74657374696e67313233")
269
+ end
270
+ end
271
+ end
@@ -0,0 +1,45 @@
1
+ # Copyright (C) 2013 10gen 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 Symbol do
18
+
19
+ describe "#to_bson/#from_bson" do
20
+
21
+ let(:type) { 14.chr }
22
+ let(:obj) { :test }
23
+ let(:bson) { "#{5.to_bson}test#{BSON::NULL_BYTE}" }
24
+
25
+ it_behaves_like "a bson element"
26
+ it_behaves_like "a serializable bson element"
27
+ it_behaves_like "a deserializable bson element"
28
+
29
+ end
30
+
31
+ describe "#to_bson_key" do
32
+
33
+ let(:symbol) { :test }
34
+ let(:encoded) { symbol.to_s + BSON::NULL_BYTE }
35
+ let(:previous_content) { 'previous_content'.force_encoding(BSON::BINARY) }
36
+
37
+ it "returns the encoded string" do
38
+ expect(symbol.to_bson_key).to eq(encoded)
39
+ end
40
+
41
+ it "appends to optional previous content" do
42
+ expect(symbol.to_bson_key(previous_content)).to eq(previous_content << encoded)
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,43 @@
1
+ # Copyright (C) 2013 10gen 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 Time do
18
+
19
+ describe "#to_bson/#from_bson" do
20
+
21
+ let(:type) { 9.chr }
22
+
23
+ it_behaves_like "a bson element"
24
+
25
+ context "when the time is post epoch" do
26
+
27
+ let(:obj) { Time.utc(2012, 1, 1, 0, 0, 0) }
28
+ let(:bson) { [ (obj.to_f * 1000).to_i ].pack(BSON::Int64::PACK) }
29
+
30
+ it_behaves_like "a serializable bson element"
31
+ it_behaves_like "a deserializable bson element"
32
+ end
33
+
34
+ context "when the time is pre epoch" do
35
+
36
+ let(:obj) { Time.utc(1969, 1, 1, 0, 0, 0) }
37
+ let(:bson) { [ (obj.to_f * 1000).to_i ].pack(BSON::Int64::PACK) }
38
+
39
+ it_behaves_like "a serializable bson element"
40
+ it_behaves_like "a deserializable bson element"
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,74 @@
1
+ # Copyright (C) 2013 10gen 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::Timestamp do
18
+
19
+ describe "#==" do
20
+
21
+ let(:timestamp) do
22
+ described_class.new(1, 10)
23
+ end
24
+
25
+ context "when the objects are equal" do
26
+
27
+ let(:other) { described_class.new(1, 10) }
28
+
29
+ it "returns true" do
30
+ expect(timestamp).to eq(other)
31
+ end
32
+ end
33
+
34
+ context "when the objects are not equal" do
35
+
36
+ let(:other) { described_class.new(1, 15) }
37
+
38
+ it "returns false" do
39
+ expect(timestamp).to_not eq(other)
40
+ end
41
+ end
42
+
43
+ context "when the other object is not a timestamp" do
44
+
45
+ it "returns false" do
46
+ expect(timestamp).to_not eq("test")
47
+ end
48
+ end
49
+ end
50
+
51
+ describe "#as_json" do
52
+
53
+ let(:object) do
54
+ described_class.new(10, 50)
55
+ end
56
+
57
+ it "returns the binary data plus type" do
58
+ expect(object.as_json).to eq({ "t" => 10, "i" => 50 })
59
+ end
60
+
61
+ it_behaves_like "a JSON serializable object"
62
+ end
63
+
64
+ describe "#to_bson/#from_bson" do
65
+
66
+ let(:type) { 17.chr }
67
+ let(:obj) { described_class.new(1, 10) }
68
+ let(:bson) { [ 10, 1 ].pack("l2") }
69
+
70
+ it_behaves_like "a bson element"
71
+ it_behaves_like "a serializable bson element"
72
+ it_behaves_like "a deserializable bson element"
73
+ end
74
+ end