bson 3.2.7 → 4.0.0.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/Rakefile +2 -10
  5. data/ext/bson/native-endian.h +120 -0
  6. data/ext/bson/native.c +547 -581
  7. data/lib/bson.rb +0 -1
  8. data/lib/bson/array.rb +15 -14
  9. data/lib/bson/binary.rb +13 -13
  10. data/lib/bson/boolean.rb +3 -3
  11. data/lib/bson/code.rb +5 -8
  12. data/lib/bson/code_with_scope.rb +10 -13
  13. data/lib/bson/date.rb +2 -2
  14. data/lib/bson/date_time.rb +2 -2
  15. data/lib/bson/document.rb +33 -0
  16. data/lib/bson/false_class.rb +2 -2
  17. data/lib/bson/float.rb +5 -11
  18. data/lib/bson/hash.rb +15 -14
  19. data/lib/bson/int32.rb +8 -9
  20. data/lib/bson/int64.rb +3 -9
  21. data/lib/bson/integer.rb +6 -20
  22. data/lib/bson/nil_class.rb +4 -16
  23. data/lib/bson/object.rb +1 -1
  24. data/lib/bson/object_id.rb +14 -16
  25. data/lib/bson/regexp.rb +7 -7
  26. data/lib/bson/specialized.rb +6 -6
  27. data/lib/bson/string.rb +7 -91
  28. data/lib/bson/symbol.rb +8 -7
  29. data/lib/bson/time.rb +5 -5
  30. data/lib/bson/timestamp.rb +8 -6
  31. data/lib/bson/true_class.rb +2 -2
  32. data/lib/bson/undefined.rb +1 -26
  33. data/lib/bson/version.rb +1 -1
  34. data/spec/bson/array_spec.rb +1 -1
  35. data/spec/bson/byte_buffer_spec.rb +445 -0
  36. data/spec/bson/code_with_scope_spec.rb +3 -7
  37. data/spec/bson/document_spec.rb +66 -10
  38. data/spec/bson/hash_spec.rb +5 -5
  39. data/spec/bson/int32_spec.rb +7 -5
  40. data/spec/bson/integer_spec.rb +1 -6
  41. data/spec/bson/object_id_spec.rb +2 -39
  42. data/spec/bson/regexp_spec.rb +1 -1
  43. data/spec/bson/string_spec.rb +2 -204
  44. data/spec/bson/symbol_spec.rb +2 -17
  45. data/spec/support/shared_examples.rb +3 -26
  46. metadata +13 -11
  47. metadata.gz.sig +0 -0
  48. data/lib/bson/encodable.rb +0 -86
@@ -63,8 +63,8 @@ describe BSON::CodeWithScope do
63
63
  end
64
64
  let(:obj) { described_class.new(code, scope) }
65
65
  let(:bson) do
66
- "#{47.to_bson}#{(code.length + 1).to_bson}#{code}#{BSON::NULL_BYTE}" +
67
- "#{scope.to_bson}"
66
+ "#{47.to_bson.to_s}#{(code.length + 1).to_bson.to_s}#{code}#{BSON::NULL_BYTE}" +
67
+ "#{scope.to_bson.to_s}"
68
68
  end
69
69
 
70
70
  it_behaves_like "a bson element"
@@ -79,7 +79,7 @@ describe BSON::CodeWithScope do
79
79
  { "name" => "test" }
80
80
  end
81
81
  let(:obj) { described_class.new(code, scope) }
82
- let(:bson) { StringIO.new(obj.to_bson) }
82
+ let(:bson) { BSON::ByteBuffer.new(obj.to_bson.to_s) }
83
83
  let!(:deserialized) { described_class.from_bson(bson) }
84
84
 
85
85
  it "deserializes the javascript" do
@@ -89,9 +89,5 @@ describe BSON::CodeWithScope do
89
89
  it "deserializes the scope" do
90
90
  expect(deserialized.scope).to eq(scope)
91
91
  end
92
-
93
- it "does not leave any extra bytes" do
94
- expect(bson.read(1)).to be_nil
95
- end
96
92
  end
97
93
  end
@@ -159,6 +159,20 @@ describe BSON::Document do
159
159
  expect(doc.send(method, "indigo")).to be false
160
160
  end
161
161
  end
162
+
163
+ context "when the key exists and is requested with a symbol" do
164
+
165
+ it "returns true" do
166
+ expect(doc.send(method, :blue)).to be true
167
+ end
168
+ end
169
+
170
+ context "when the key does not exist and is requested with a symbol" do
171
+
172
+ it "returns false" do
173
+ expect(doc.send(method, :indigo)).to be false
174
+ end
175
+ end
162
176
  end
163
177
  end
164
178
 
@@ -166,6 +180,13 @@ describe BSON::Document do
166
180
 
167
181
  describe "##{method}" do
168
182
 
183
+ let(:key) { :purple }
184
+ let(:val) { :'5422a8' }
185
+
186
+ before do
187
+ doc[key] = val
188
+ end
189
+
169
190
  context "when the value exists" do
170
191
 
171
192
  it "returns true" do
@@ -179,6 +200,21 @@ describe BSON::Document do
179
200
  expect(doc.send(method, "ABCABC")).to be false
180
201
  end
181
202
  end
203
+
204
+ context "when the value exists and is requested with a symbol" do
205
+
206
+ it "returns true" do
207
+
208
+ expect(doc.send(method, :'5422a8')).to be true
209
+ end
210
+ end
211
+
212
+ context "when the value does not exist and is requested with a symbol" do
213
+
214
+ it "returns false" do
215
+ expect(doc.send(method, :ABCABC)).to be false
216
+ end
217
+ end
182
218
  end
183
219
  end
184
220
 
@@ -653,11 +689,11 @@ describe BSON::Document do
653
689
  end
654
690
 
655
691
  let(:serialized) do
656
- document.to_bson
692
+ document.to_bson.to_s
657
693
  end
658
694
 
659
695
  let(:deserialized) do
660
- described_class.from_bson(StringIO.new(serialized))
696
+ described_class.from_bson(BSON::ByteBuffer.new(serialized))
661
697
  end
662
698
 
663
699
  it 'deserializes the documents' do
@@ -688,7 +724,7 @@ describe BSON::Document do
688
724
  end
689
725
 
690
726
  it "properly serializes the symbol" do
691
- expect(obj.to_bson).to eq(bson)
727
+ expect(obj.to_bson.to_s).to eq(bson)
692
728
  end
693
729
  end
694
730
 
@@ -723,7 +759,7 @@ describe BSON::Document do
723
759
  it_behaves_like "a deserializable bson element"
724
760
 
725
761
  let(:raw) do
726
- StringIO.new(bson)
762
+ BSON::ByteBuffer.new(bson)
727
763
  end
728
764
 
729
765
  it "returns an instance of a BSON::Document" do
@@ -768,7 +804,7 @@ describe BSON::Document do
768
804
  end
769
805
 
770
806
  let(:deserialized) do
771
- described_class.from_bson(StringIO.new(document.to_bson))
807
+ described_class.from_bson(BSON::ByteBuffer.new(document.to_bson.to_s))
772
808
  end
773
809
 
774
810
  it "serializes and deserializes properly" do
@@ -812,30 +848,50 @@ describe BSON::Document do
812
848
  it_behaves_like "a document able to handle utf-8"
813
849
  end
814
850
 
815
- context "when non utf-8 values exist" do
851
+ context "when utf-8 values exist in wrong encoding" do
816
852
 
817
853
  let(:string) { "gültig" }
818
854
  let(:document) do
819
855
  described_class["type", string.encode("iso-8859-1")]
820
856
  end
821
857
 
858
+ it "raises an exception", unless: BSON::Environment.jruby? do
859
+ expect {
860
+ document.to_bson
861
+ }.to raise_error(ArgumentError)
862
+ end
863
+
864
+ it 'converts the values', if: BSON::Environment.jruby? do
865
+ expect(
866
+ BSON::Document.from_bson(BSON::ByteBuffer.new(document.to_bson.to_s))
867
+ ).to eq({ "type" => string })
868
+ end
869
+ end
870
+
871
+ context "when binary strings with utf-8 values exist", if: BSON::Environment.jruby? && !(ENV['RUBY_VERSION'] =~ /jruby-9/) do
872
+
873
+ let(:string) { "europäisch" }
874
+ let(:document) do
875
+ described_class["type", string.encode("binary")]
876
+ end
877
+
822
878
  it "encodes and decodes the document properly" do
823
879
  expect(
824
- BSON::Document.from_bson(StringIO.new(document.to_bson))
880
+ BSON::Document.from_bson(BSON::ByteBuffer.new(document.to_bson.to_s))
825
881
  ).to eq({ "type" => string })
826
882
  end
827
883
  end
828
884
 
829
- context "when binary strings with utf-8 values exist" do
885
+ context "when binary strings with utf-8 values exist", unless: BSON::Environment.jruby? do
830
886
 
831
- let(:string) { "europäischen" }
887
+ let(:string) { "europäisch" }
832
888
  let(:document) do
833
889
  described_class["type", string.encode("binary", "binary")]
834
890
  end
835
891
 
836
892
  it "encodes and decodes the document properly" do
837
893
  expect(
838
- BSON::Document.from_bson(StringIO.new(document.to_bson))
894
+ BSON::Document.from_bson(BSON::ByteBuffer.new(document.to_bson.to_s))
839
895
  ).to eq({ "type" => string })
840
896
  end
841
897
  end
@@ -29,8 +29,8 @@ describe Hash do
29
29
  end
30
30
 
31
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}"
32
+ "#{20.to_bson.to_s}#{String::BSON_TYPE}key#{BSON::NULL_BYTE}" +
33
+ "#{6.to_bson.to_s}value#{BSON::NULL_BYTE}#{BSON::NULL_BYTE}"
34
34
  end
35
35
 
36
36
  it_behaves_like "a serializable bson element"
@@ -44,9 +44,9 @@ describe Hash do
44
44
  end
45
45
 
46
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}"
47
+ "#{32.to_bson.to_s}#{Hash::BSON_TYPE}field#{BSON::NULL_BYTE}" +
48
+ "#{20.to_bson.to_s}#{String::BSON_TYPE}key#{BSON::NULL_BYTE}" +
49
+ "#{6.to_bson.to_s}value#{BSON::NULL_BYTE}#{BSON::NULL_BYTE}#{BSON::NULL_BYTE}"
50
50
  end
51
51
 
52
52
  it_behaves_like "a serializable bson element"
@@ -27,16 +27,18 @@ describe BSON::Int32 do
27
27
  end
28
28
 
29
29
  describe "when the integer is negative" do
30
+
30
31
  let(:decoded) { -1 }
31
- let(:encoded) {StringIO.new([ -1 ].pack(BSON::Int32::PACK))}
32
+ let(:encoded) { BSON::ByteBuffer.new([ -1 ].pack(BSON::Int32::PACK)) }
32
33
  let(:decoded_2) { -50 }
33
- let(:encoded_2) {StringIO.new([ -50 ].pack(BSON::Int32::PACK))}
34
+ let(:encoded_2) { BSON::ByteBuffer.new([ -50 ].pack(BSON::Int32::PACK)) }
35
+
34
36
  it "decodes a -1 correctly" do
35
37
  expect(BSON::Int32.from_bson(encoded)).to eq(decoded)
36
- end
38
+ end
39
+
37
40
  it "decodes a -50 correctly" do
38
41
  expect(BSON::Int32.from_bson(encoded_2)).to eq(decoded_2)
39
- end
42
+ end
40
43
  end
41
-
42
44
  end
@@ -63,14 +63,9 @@ describe Integer do
63
63
 
64
64
  let(:obj) { Integer::MAX_32BIT - 1 }
65
65
  let(:encoded) { obj.to_s + BSON::NULL_BYTE }
66
- let(:previous_content) { 'previous_content'.force_encoding(BSON::BINARY) }
67
66
 
68
67
  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)
68
+ expect(obj.to_bson_key.to_s).to eq(encoded)
74
69
  end
75
70
  end
76
71
  end
@@ -379,44 +379,7 @@ describe BSON::ObjectId do
379
379
  end
380
380
 
381
381
  it "returns a hash of the raw bytes" do
382
- expect(object_id.hash).to eq(object_id.to_bson.hash)
383
- end
384
- end
385
-
386
- describe "#clone" do
387
-
388
- context "when the data has not been generated yet" do
389
-
390
- let!(:object_id) do
391
- described_class.new
392
- end
393
-
394
- let!(:clone) do
395
- object_id.clone
396
- end
397
-
398
- it "generates and copies the data" do
399
- expect(clone).to eq(object_id)
400
- end
401
- end
402
-
403
- context "when the data has been generated" do
404
-
405
- let!(:object_id) do
406
- described_class.new
407
- end
408
-
409
- let(:clone) do
410
- object_id.clone
411
- end
412
-
413
- before do
414
- object_id.to_s
415
- end
416
-
417
- it "copies the data" do
418
- expect(clone).to eq(object_id)
419
- end
382
+ expect(object_id.hash).to eq(object_id.to_bson.to_s.hash)
420
383
  end
421
384
  end
422
385
 
@@ -525,7 +488,7 @@ describe BSON::ObjectId do
525
488
  let(:time) { Time.utc(2013, 1, 1) }
526
489
  let(:type) { 7.chr }
527
490
  let(:obj) { described_class.from_time(time) }
528
- let(:bson) { obj.to_bson }
491
+ let(:bson) { obj.to_bson.to_s }
529
492
 
530
493
  it_behaves_like "a bson element"
531
494
  it_behaves_like "a serializable bson element"
@@ -37,7 +37,7 @@ describe Regexp do
37
37
  let(:obj) { /test/ }
38
38
 
39
39
  let(:io) do
40
- StringIO.new(bson)
40
+ BSON::ByteBuffer.new(bson)
41
41
  end
42
42
 
43
43
  let(:regex) do
@@ -22,109 +22,13 @@ describe String do
22
22
 
23
23
  let(:type) { 2.chr }
24
24
  let(:obj) { "test" }
25
- let(:bson) { "#{5.to_bson}test#{BSON::NULL_BYTE}" }
25
+ let(:bson) { "#{5.to_bson.to_s}test#{BSON::NULL_BYTE}" }
26
26
 
27
27
  it_behaves_like "a bson element"
28
28
  it_behaves_like "a serializable bson element"
29
29
  it_behaves_like "a deserializable bson element"
30
30
  end
31
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(ArgumentError)
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
- context "when the string contains non utf-8 characters" do
115
-
116
- let(:string) do
117
- 255.chr
118
- end
119
-
120
- it "raises an error" do
121
- expect {
122
- string.to_bson_cstring
123
- }.to raise_error(EncodingError)
124
- end
125
- end
126
- end
127
-
128
32
  describe "#to_bson_object_id" do
129
33
 
130
34
  context "when the string has 12 characters" do
@@ -152,107 +56,6 @@ describe String do
152
56
  end
153
57
  end
154
58
 
155
- describe "#to_bson_string" do
156
-
157
- context "when the string is valid" do
158
-
159
- let(:string) do
160
- "test"
161
- end
162
-
163
- let(:encoded) do
164
- string.to_bson_string
165
- end
166
-
167
- let(:previous_content) do
168
- 'previous_content'.force_encoding(BSON::BINARY)
169
- end
170
-
171
- it "returns the string" do
172
- expect(encoded).to eq(string)
173
- end
174
-
175
- it_behaves_like "a binary encoded string"
176
-
177
- it "appends to optional previous content" do
178
- expect(string.to_bson_string(previous_content)).to eq(previous_content << encoded)
179
- end
180
-
181
- end
182
-
183
- context "when the string contains a null byte" do
184
-
185
- let(:string) do
186
- "test#{BSON::NULL_BYTE}ing"
187
- end
188
-
189
- let(:encoded) do
190
- string.to_bson_string
191
- end
192
-
193
- it "retains the null byte" do
194
- expect(encoded).to eq(string)
195
- end
196
-
197
- it_behaves_like "a binary encoded string"
198
- end
199
-
200
- context "when the string contains utf-8 characters" do
201
-
202
- let(:string) do
203
- "Straße"
204
- end
205
-
206
- let(:encoded) do
207
- string.to_bson_string
208
- end
209
-
210
- let(:char) do
211
- "ß".chr.force_encoding(BSON::BINARY)
212
- end
213
-
214
- it "returns the encoded string" do
215
- expect(encoded).to eq("Stra#{char}e")
216
- end
217
-
218
- it_behaves_like "a binary encoded string"
219
- end
220
-
221
- context "when the string is encoded in non utf-8" do
222
-
223
- let(:string) do
224
- "Straße".encode("iso-8859-1")
225
- end
226
-
227
- let(:encoded) do
228
- string.to_bson_string
229
- end
230
-
231
- let(:char) do
232
- "ß".chr.force_encoding(BSON::BINARY)
233
- end
234
-
235
- it "returns the encoded string" do
236
- expect(encoded).to eq("Stra#{char}e")
237
- end
238
-
239
- it_behaves_like "a binary encoded string"
240
- end
241
-
242
- context "when the string contains non utf-8 characters" do
243
-
244
- let(:string) do
245
- 255.chr
246
- end
247
-
248
- it "raises an error" do
249
- expect {
250
- string.to_bson_string
251
- }.to raise_error(EncodingError)
252
- end
253
- end
254
- end
255
-
256
59
  context "when the class is loaded" do
257
60
 
258
61
  let(:registered) do
@@ -267,16 +70,11 @@ describe String do
267
70
  describe "#to_bson_key" do
268
71
 
269
72
  let(:string) { "test" }
270
- let(:encoded) { string.to_s + BSON::NULL_BYTE }
271
- let(:previous_content) { 'previous_content'.force_encoding(BSON::BINARY) }
73
+ let(:encoded) { string.to_s }
272
74
 
273
75
  it "returns the encoded string" do
274
76
  expect(string.to_bson_key).to eq(encoded)
275
77
  end
276
-
277
- it "appends to optional previous content" do
278
- expect(string.to_bson_key(previous_content)).to eq(previous_content << encoded)
279
- end
280
78
  end
281
79
 
282
80
  describe "#to_hex_string" do