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,28 @@
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 FalseClass do
18
+
19
+ describe "#to_bson" do
20
+
21
+ let(:obj) { false }
22
+ let(:bson) { 0.chr }
23
+ let(:type) { 8.chr }
24
+
25
+ it_behaves_like "a bson element"
26
+ it_behaves_like "a serializable bson element"
27
+ end
28
+ end
@@ -0,0 +1,29 @@
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 Float do
18
+
19
+ describe "#to_bson/#from_bson" do
20
+
21
+ let(:type) { 1.chr }
22
+ let(:obj) { 1.2332 }
23
+ let(:bson) { [ obj ].pack(Float::PACK) }
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
+ end
29
+ end
@@ -0,0 +1,56 @@
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 Hash do
18
+
19
+ describe "#to_bson/#from_bson" do
20
+
21
+ let(:type) { 3.chr }
22
+
23
+ it_behaves_like "a bson element"
24
+
25
+ context "when the hash is a single level" do
26
+
27
+ let(:obj) do
28
+ { "key" => "value" }
29
+ end
30
+
31
+ let(:bson) do
32
+ "#{20.to_bson}#{String::BSON_TYPE}key#{BSON::NULL_BYTE}" +
33
+ "#{6.to_bson}value#{BSON::NULL_BYTE}#{BSON::NULL_BYTE}"
34
+ end
35
+
36
+ it_behaves_like "a serializable bson element"
37
+ it_behaves_like "a deserializable bson element"
38
+ end
39
+
40
+ context "when the hash is embedded" do
41
+
42
+ let(:obj) do
43
+ { "field" => { "key" => "value" }}
44
+ end
45
+
46
+ let(:bson) do
47
+ "#{32.to_bson}#{Hash::BSON_TYPE}field#{BSON::NULL_BYTE}" +
48
+ "#{20.to_bson}#{String::BSON_TYPE}key#{BSON::NULL_BYTE}" +
49
+ "#{6.to_bson}value#{BSON::NULL_BYTE}#{BSON::NULL_BYTE}#{BSON::NULL_BYTE}"
50
+ end
51
+
52
+ it_behaves_like "a serializable bson element"
53
+ it_behaves_like "a deserializable bson element"
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,28 @@
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::Int32 do
18
+
19
+ describe "#from_bson" do
20
+
21
+ let(:type) { 16.chr }
22
+ let(:obj) { 123 }
23
+ let(:bson) { [ obj ].pack(BSON::Int32::PACK) }
24
+
25
+ it_behaves_like "a bson element"
26
+ it_behaves_like "a deserializable bson element"
27
+ end
28
+ end
@@ -0,0 +1,28 @@
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::Int64 do
18
+
19
+ describe "#from_bson" do
20
+
21
+ let(:type) { 18.chr }
22
+ let(:obj) { 1325376000000 }
23
+ let(:bson) { [ obj ].pack(BSON::Int64::PACK) }
24
+
25
+ it_behaves_like "a bson element"
26
+ it_behaves_like "a deserializable bson element"
27
+ end
28
+ end
@@ -0,0 +1,76 @@
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 Integer do
18
+
19
+ describe "#to_bson" do
20
+
21
+ context "when the integer is 32 bit" do
22
+
23
+ let(:type) { 16.chr }
24
+ let(:obj) { Integer::MAX_32BIT - 1 }
25
+ let(:bson) { [ obj ].pack(BSON::Int32::PACK) }
26
+
27
+ it_behaves_like "a serializable bson element"
28
+ end
29
+
30
+ context "when the integer is 64 bit" do
31
+
32
+ let(:type) { 18.chr }
33
+ let(:obj) { Integer::MAX_64BIT - 1 }
34
+ let(:bson) { [ obj ].pack(BSON::Int64::PACK) }
35
+
36
+ it_behaves_like "a serializable bson element"
37
+ end
38
+
39
+ context "when the integer is too large" do
40
+
41
+ let(:integer) { Integer::MAX_64BIT + 1 }
42
+
43
+ it "raises an out of range error" do
44
+ expect {
45
+ integer.to_bson
46
+ }.to raise_error(RangeError)
47
+ end
48
+ end
49
+
50
+ context "when the intger is too small" do
51
+
52
+ let(:integer) { Integer::MIN_64BIT - 1 }
53
+
54
+ it "raises an out of range error" do
55
+ expect {
56
+ integer.to_bson
57
+ }.to raise_error(RangeError)
58
+ end
59
+ end
60
+ end
61
+
62
+ describe "#to_bson_key" do
63
+
64
+ let(:obj) { Integer::MAX_32BIT - 1 }
65
+ let(:encoded) { obj.to_s + BSON::NULL_BYTE }
66
+ let(:previous_content) { 'previous_content'.force_encoding(BSON::BINARY) }
67
+
68
+ it "returns the encoded string" do
69
+ expect(obj.to_bson_key).to eq(encoded)
70
+ end
71
+
72
+ it "appends to optional previous content" do
73
+ expect(obj.to_bson_key(previous_content)).to eq(previous_content << encoded)
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,53 @@
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::JSON do
18
+
19
+ describe "#to_json" do
20
+
21
+ let(:klass) do
22
+ Class.new do
23
+ include BSON::JSON
24
+
25
+ def as_json(*args)
26
+ { :test => "value" }
27
+ end
28
+ end
29
+ end
30
+
31
+ context "when provided no arguments" do
32
+
33
+ let(:json) do
34
+ klass.new.to_json
35
+ end
36
+
37
+ it "returns the object as json" do
38
+ expect(json).to eq("{\"test\":\"value\"}")
39
+ end
40
+ end
41
+
42
+ context "when provided arguments" do
43
+
44
+ let(:json) do
45
+ klass.new.to_json(:test)
46
+ end
47
+
48
+ it "returns the object as json" do
49
+ expect(json).to eq("{\"test\":\"value\"}")
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,75 @@
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::MaxKey do
18
+
19
+ describe "#==" do
20
+
21
+ context "when the objects are equal" do
22
+
23
+ let(:other) { described_class.new }
24
+
25
+ it "returns true" do
26
+ expect(subject).to eq(other)
27
+ end
28
+ end
29
+
30
+ context "when the other object is not a max_key" do
31
+
32
+ it "returns false" do
33
+ expect(subject).to_not eq("test")
34
+ end
35
+ end
36
+ end
37
+
38
+ describe "#>" do
39
+
40
+ it "always returns true" do
41
+ expect(subject > Integer::MAX_64BIT).to be_true
42
+ end
43
+ end
44
+
45
+ describe "#<" do
46
+
47
+ it "always returns false" do
48
+ expect(subject < Integer::MAX_64BIT).to be_false
49
+ end
50
+ end
51
+
52
+ describe "#as_json" do
53
+
54
+ let(:object) do
55
+ described_class.new
56
+ end
57
+
58
+ it "returns the binary data plus type" do
59
+ expect(object.as_json).to eq({ "$maxKey" => 1 })
60
+ end
61
+
62
+ it_behaves_like "a JSON serializable object"
63
+ end
64
+
65
+ describe "#to_bson/#from_bson" do
66
+
67
+ let(:type) { 127.chr }
68
+ let(:obj) { described_class.new }
69
+ let(:bson) { BSON::NO_VALUE }
70
+
71
+ it_behaves_like "a bson element"
72
+ it_behaves_like "a serializable bson element"
73
+ it_behaves_like "a deserializable bson element"
74
+ end
75
+ end
@@ -0,0 +1,75 @@
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::MinKey do
18
+
19
+ describe "#as_json" do
20
+
21
+ let(:object) do
22
+ described_class.new
23
+ end
24
+
25
+ it "returns the binary data plus type" do
26
+ expect(object.as_json).to eq({ "$minKey" => 1 })
27
+ end
28
+
29
+ it_behaves_like "a JSON serializable object"
30
+ end
31
+
32
+ describe "#==" do
33
+
34
+ context "when the objects are equal" do
35
+
36
+ let(:other) { described_class.new }
37
+
38
+ it "returns true" do
39
+ expect(subject).to eq(other)
40
+ end
41
+ end
42
+
43
+ context "when the other object is not a max_key" do
44
+
45
+ it "returns false" do
46
+ expect(subject).to_not eq("test")
47
+ end
48
+ end
49
+ end
50
+
51
+ describe "#>" do
52
+
53
+ it "always returns false" do
54
+ expect(subject > Integer::MAX_64BIT).to be_false
55
+ end
56
+ end
57
+
58
+ describe "#<" do
59
+
60
+ it "always returns true" do
61
+ expect(subject < Integer::MAX_64BIT).to be_true
62
+ end
63
+ end
64
+
65
+ describe "#to_bson/#from_bson" do
66
+
67
+ let(:type) { 255.chr }
68
+ let(:obj) { described_class.new }
69
+ let(:bson) { BSON::NO_VALUE }
70
+
71
+ it_behaves_like "a bson element"
72
+ it_behaves_like "a serializable bson element"
73
+ it_behaves_like "a deserializable bson element"
74
+ end
75
+ end