bson 4.5.0-java → 4.6.0-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.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/README.md +7 -5
- data/lib/bson-ruby.jar +0 -0
- data/lib/bson/document.rb +43 -1
- data/lib/bson/hash.rb +11 -2
- data/lib/bson/int32.rb +19 -13
- data/lib/bson/int64.rb +19 -13
- data/lib/bson/version.rb +1 -1
- data/spec/bson/byte_buffer_read_spec.rb +141 -0
- data/spec/bson/byte_buffer_spec.rb +14 -451
- data/spec/bson/byte_buffer_write_spec.rb +758 -0
- data/spec/bson/corpus_spec.rb +8 -5
- data/spec/bson/document_spec.rb +29 -29
- data/spec/bson/hash_spec.rb +65 -0
- data/spec/bson/int32_spec.rb +21 -3
- data/spec/bson/int64_spec.rb +22 -3
- data/spec/bson/string_spec.rb +18 -0
- data/spec/support/corpus-tests/array.json +8 -2
- data/spec/support/shared_examples.rb +2 -4
- data/spec/support/utils.rb +10 -0
- metadata +63 -51
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 11ea9786e26cfa4b976e85a690bbb99c54bc516eb0cff72fb3e696f71ac59a71
|
4
|
+
data.tar.gz: 97871ed99465524f12d2de82d5f86f7f27b148db545a7b46626d36d68931071d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ef47b419cd7b32c16542ff041aaffebd4f137b949a1e6324b3ebc7b6bdf69367cf54c13e0b696378af4793c1d615a35e3760da2eb85e0764f7e559d02fea25b4
|
7
|
+
data.tar.gz: 53722b2aa6b4a98015b41a23e4f2abca58204f48eb83a5353df4a1640e80648b03808e1aa2760e0ee1e7ef58ccf9b4da7b80887ff4a7664d3ab404ccfe60e71e
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data.tar.gz.sig
CHANGED
Binary file
|
data/README.md
CHANGED
@@ -6,18 +6,19 @@ An implementation of the BSON specification in Ruby.
|
|
6
6
|
Compatibility
|
7
7
|
-------------
|
8
8
|
|
9
|
-
BSON is tested against MRI (
|
9
|
+
BSON is tested against MRI (2.3+) and JRuby (9.2+).
|
10
10
|
|
11
11
|
Documentation
|
12
12
|
-------------
|
13
13
|
|
14
|
-
Current documentation can be found
|
14
|
+
Current documentation can be found
|
15
|
+
[here](http://docs.mongodb.org/ecosystem/tutorial/ruby-bson-tutorial/#ruby-bson-tutorial).
|
15
16
|
|
16
17
|
API Documentation
|
17
18
|
-----------------
|
18
19
|
|
19
|
-
The [API Documentation](
|
20
|
-
located at
|
20
|
+
The [API Documentation](https://api.mongodb.com/bson-ruby/current/) is
|
21
|
+
located at api.mongodb.com.
|
21
22
|
|
22
23
|
BSON Specification
|
23
24
|
------------------
|
@@ -27,7 +28,8 @@ The [BSON specification](http://bsonspec.org) is at bsonspec.org.
|
|
27
28
|
Versioning
|
28
29
|
----------
|
29
30
|
|
30
|
-
As of 2.0.0, this project adheres to the
|
31
|
+
As of 2.0.0, this project adheres to the
|
32
|
+
[Semantic Versioning Specification](http://semver.org/).
|
31
33
|
|
32
34
|
License
|
33
35
|
-------
|
data/lib/bson-ruby.jar
CHANGED
Binary file
|
data/lib/bson/document.rb
CHANGED
@@ -88,7 +88,49 @@ module BSON
|
|
88
88
|
super(convert_key(key))
|
89
89
|
end
|
90
90
|
|
91
|
-
#
|
91
|
+
# Stores a key-value pair in the current document.
|
92
|
+
#
|
93
|
+
# Since BSON documents provide deep indifferent access (both strings and
|
94
|
+
# symbols are accepted as keys, recursively), the value may be converted
|
95
|
+
# to facilitate indifferent access. This conversion is performed for
|
96
|
+
# built-in Array and Hash classes, and other classes can override
|
97
|
+
# +to_bson_normalized_value+ method to provide custom conversion logic.
|
98
|
+
# For example:
|
99
|
+
#
|
100
|
+
# doc = BSON::Document.new
|
101
|
+
# doc[:a] = {b: {c: 'd'}}
|
102
|
+
# doc['a']['b']['c']
|
103
|
+
# # => "d"
|
104
|
+
#
|
105
|
+
# Note that due to this conversion, the object that is stored in the
|
106
|
+
# receiver Document may be different from the object supplied as the
|
107
|
+
# right hand side of the assignment. In Ruby, the result of assignment
|
108
|
+
# is the right hand side, not the return value of []= method.
|
109
|
+
# Because of this, modifying the result of assignment generally does not
|
110
|
+
# work as intended:
|
111
|
+
#
|
112
|
+
# doc = BSON::Document.new
|
113
|
+
# foo = (doc[:a] = {b: {c: 'd'}})
|
114
|
+
# # foo is original Hash with symbol keys
|
115
|
+
# foo['test'] = 'test'
|
116
|
+
# # doc is not modified
|
117
|
+
# doc
|
118
|
+
# # => {"a"=>{"b"=>{"c"=>"d"}}}
|
119
|
+
#
|
120
|
+
# This behavior can be encountered when defaulting document contents with
|
121
|
+
# []= in a method, such as:
|
122
|
+
#
|
123
|
+
# def foo
|
124
|
+
# # @doc is a BSON::Document
|
125
|
+
# @doc[:foo] ||= calculation
|
126
|
+
# end
|
127
|
+
#
|
128
|
+
# The above method should be written as follows to allow chaining:
|
129
|
+
#
|
130
|
+
# def foo
|
131
|
+
# # @doc is a BSON::Document
|
132
|
+
# @doc[:foo] ||= calculation and @doc[:foo]
|
133
|
+
# end
|
92
134
|
#
|
93
135
|
# @example Set a value on the document.
|
94
136
|
# document[:test] = "value"
|
data/lib/bson/hash.rb
CHANGED
@@ -22,7 +22,7 @@ module BSON
|
|
22
22
|
# @since 2.0.0
|
23
23
|
module Hash
|
24
24
|
|
25
|
-
#
|
25
|
+
# A hash, also called an embedded document, is type 0x03 in the BSON spec.
|
26
26
|
#
|
27
27
|
# @since 2.0.0
|
28
28
|
BSON_TYPE = 3.chr.force_encoding(BINARY).freeze
|
@@ -45,7 +45,16 @@ module BSON
|
|
45
45
|
buffer.put_int32(0)
|
46
46
|
each do |field, value|
|
47
47
|
buffer.put_byte(value.bson_type)
|
48
|
-
|
48
|
+
key = field.to_bson_key(validating_keys)
|
49
|
+
begin
|
50
|
+
buffer.put_cstring(key)
|
51
|
+
rescue ArgumentError => e
|
52
|
+
raise ArgumentError, "Error serializing key #{key}: #{e.class}: #{e}"
|
53
|
+
rescue EncodingError => e
|
54
|
+
# Note this may convert exception class from a subclass of
|
55
|
+
# EncodingError to EncodingError itself
|
56
|
+
raise EncodingError, "Error serializing key #{key}: #{e.class}: #{e}"
|
57
|
+
end
|
49
58
|
value.to_bson(buffer, validating_keys)
|
50
59
|
end
|
51
60
|
buffer.put_byte(NULL_BYTE)
|
data/lib/bson/int32.rb
CHANGED
@@ -51,16 +51,28 @@ module BSON
|
|
51
51
|
|
52
52
|
# Instantiate a BSON Int32.
|
53
53
|
#
|
54
|
-
# @param [ Integer ]
|
54
|
+
# @param [ Integer ] value The 32-bit integer.
|
55
55
|
#
|
56
56
|
# @see http://bsonspec.org/#/specification
|
57
57
|
#
|
58
58
|
# @since 4.2.0
|
59
|
-
def initialize(
|
60
|
-
|
61
|
-
|
59
|
+
def initialize(value)
|
60
|
+
if value.is_a?(self.class)
|
61
|
+
@value = value.value
|
62
|
+
return
|
63
|
+
end
|
64
|
+
|
65
|
+
unless value.bson_int32?
|
66
|
+
raise RangeError.new("#{value} cannot be stored in 32 bits")
|
67
|
+
end
|
68
|
+
@value = value.freeze
|
62
69
|
end
|
63
70
|
|
71
|
+
# Returns the value of this Int32.
|
72
|
+
#
|
73
|
+
# @return [ Integer ] The integer value.
|
74
|
+
attr_reader :value
|
75
|
+
|
64
76
|
# Append the integer as encoded BSON to a ByteBuffer.
|
65
77
|
#
|
66
78
|
# @example Encoded the integer and append to a ByteBuffer.
|
@@ -72,7 +84,7 @@ module BSON
|
|
72
84
|
#
|
73
85
|
# @since 4.2.0
|
74
86
|
def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
|
75
|
-
buffer.put_int32(
|
87
|
+
buffer.put_int32(value)
|
76
88
|
end
|
77
89
|
|
78
90
|
# Convert the integer to a BSON string key.
|
@@ -86,7 +98,7 @@ module BSON
|
|
86
98
|
#
|
87
99
|
# @since 4.2.0
|
88
100
|
def to_bson_key(validating_keys = Config.validating_keys?)
|
89
|
-
|
101
|
+
value
|
90
102
|
end
|
91
103
|
|
92
104
|
# Check equality of the int32 with another object.
|
@@ -98,17 +110,11 @@ module BSON
|
|
98
110
|
# @since 4.4.0
|
99
111
|
def ==(other)
|
100
112
|
return false unless other.is_a?(Int32)
|
101
|
-
|
113
|
+
value == other.value
|
102
114
|
end
|
103
115
|
alias :eql? :==
|
104
116
|
alias :=== :==
|
105
117
|
|
106
|
-
private
|
107
|
-
|
108
|
-
def out_of_range!
|
109
|
-
raise RangeError.new("#{self} is not a valid 4 byte integer value.")
|
110
|
-
end
|
111
|
-
|
112
118
|
# Register this type when the module is loaded.
|
113
119
|
#
|
114
120
|
# @since 2.0.0
|
data/lib/bson/int64.rb
CHANGED
@@ -46,16 +46,28 @@ module BSON
|
|
46
46
|
|
47
47
|
# Instantiate a BSON Int64.
|
48
48
|
#
|
49
|
-
# @param [ Integer ]
|
49
|
+
# @param [ Integer ] value The 64-bit integer.
|
50
50
|
#
|
51
51
|
# @see http://bsonspec.org/#/specification
|
52
52
|
#
|
53
53
|
# @since 4.2.0
|
54
|
-
def initialize(
|
55
|
-
|
56
|
-
|
54
|
+
def initialize(value)
|
55
|
+
if value.is_a?(self.class)
|
56
|
+
@value = value.value
|
57
|
+
return
|
58
|
+
end
|
59
|
+
|
60
|
+
unless value.bson_int64?
|
61
|
+
raise RangeError.new("#{value} cannot be stored in 64 bits")
|
62
|
+
end
|
63
|
+
@value = value.freeze
|
57
64
|
end
|
58
65
|
|
66
|
+
# Returns the value of this Int64.
|
67
|
+
#
|
68
|
+
# @return [ Integer ] The integer value.
|
69
|
+
attr_reader :value
|
70
|
+
|
59
71
|
# Append the integer as encoded BSON to a ByteBuffer.
|
60
72
|
#
|
61
73
|
# @example Encoded the integer and append to a ByteBuffer.
|
@@ -67,7 +79,7 @@ module BSON
|
|
67
79
|
#
|
68
80
|
# @since 4.2.0
|
69
81
|
def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
|
70
|
-
buffer.put_int64(
|
82
|
+
buffer.put_int64(value)
|
71
83
|
end
|
72
84
|
|
73
85
|
# Convert the integer to a BSON string key.
|
@@ -81,7 +93,7 @@ module BSON
|
|
81
93
|
#
|
82
94
|
# @since 4.2.0
|
83
95
|
def to_bson_key(validating_keys = Config.validating_keys?)
|
84
|
-
|
96
|
+
value
|
85
97
|
end
|
86
98
|
|
87
99
|
# Check equality of the int64 with another object.
|
@@ -93,17 +105,11 @@ module BSON
|
|
93
105
|
# @since 4.4.0
|
94
106
|
def ==(other)
|
95
107
|
return false unless other.is_a?(Int64)
|
96
|
-
|
108
|
+
value == other.value
|
97
109
|
end
|
98
110
|
alias :eql? :==
|
99
111
|
alias :=== :==
|
100
112
|
|
101
|
-
private
|
102
|
-
|
103
|
-
def out_of_range!
|
104
|
-
raise RangeError.new("#{self} is not a valid 8 byte integer value.")
|
105
|
-
end
|
106
|
-
|
107
113
|
# Register this type when the module is loaded.
|
108
114
|
#
|
109
115
|
# @since 2.0.0
|
data/lib/bson/version.rb
CHANGED
@@ -0,0 +1,141 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe BSON::ByteBuffer do
|
4
|
+
|
5
|
+
describe '#get_byte' do
|
6
|
+
|
7
|
+
let(:buffer) do
|
8
|
+
described_class.new(BSON::Int32::BSON_TYPE)
|
9
|
+
end
|
10
|
+
|
11
|
+
let!(:byte) do
|
12
|
+
buffer.get_byte
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'gets the byte from the buffer' do
|
16
|
+
expect(byte).to eq(BSON::Int32::BSON_TYPE)
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'increments the read position by 1' do
|
20
|
+
expect(buffer.read_position).to eq(1)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '#get_bytes' do
|
25
|
+
|
26
|
+
let(:string) do
|
27
|
+
"#{BSON::Int32::BSON_TYPE}#{BSON::Int32::BSON_TYPE}"
|
28
|
+
end
|
29
|
+
|
30
|
+
let(:buffer) do
|
31
|
+
described_class.new(string)
|
32
|
+
end
|
33
|
+
|
34
|
+
let!(:bytes) do
|
35
|
+
buffer.get_bytes(2)
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'gets the bytes from the buffer' do
|
39
|
+
expect(bytes).to eq(string)
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'increments the position by the length' do
|
43
|
+
expect(buffer.read_position).to eq(string.bytesize)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe '#get_cstring' do
|
48
|
+
|
49
|
+
let(:buffer) do
|
50
|
+
described_class.new("testing#{BSON::NULL_BYTE}")
|
51
|
+
end
|
52
|
+
|
53
|
+
let!(:string) do
|
54
|
+
buffer.get_cstring
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'gets the cstring from the buffer' do
|
58
|
+
expect(string).to eq("testing")
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'increments the position by string length + 1' do
|
62
|
+
expect(buffer.read_position).to eq(8)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe '#get_double' do
|
67
|
+
|
68
|
+
let(:buffer) do
|
69
|
+
described_class.new("#{12.5.to_bson.to_s}")
|
70
|
+
end
|
71
|
+
|
72
|
+
let!(:double) do
|
73
|
+
buffer.get_double
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'gets the double from the buffer' do
|
77
|
+
expect(double).to eq(12.5)
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'increments the read position by 8' do
|
81
|
+
expect(buffer.read_position).to eq(8)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe '#get_int32' do
|
86
|
+
|
87
|
+
let(:buffer) do
|
88
|
+
described_class.new("#{12.to_bson.to_s}")
|
89
|
+
end
|
90
|
+
|
91
|
+
let!(:int32) do
|
92
|
+
buffer.get_int32
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'gets the int32 from the buffer' do
|
96
|
+
expect(int32).to eq(12)
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'increments the position by 4' do
|
100
|
+
expect(buffer.read_position).to eq(4)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
describe '#get_int64' do
|
105
|
+
|
106
|
+
let(:buffer) do
|
107
|
+
described_class.new("#{(Integer::MAX_64BIT - 1).to_bson.to_s}")
|
108
|
+
end
|
109
|
+
|
110
|
+
let!(:int64) do
|
111
|
+
buffer.get_int64
|
112
|
+
end
|
113
|
+
|
114
|
+
it 'gets the int64 from the buffer' do
|
115
|
+
expect(int64).to eq(Integer::MAX_64BIT - 1)
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'increments the position by 8' do
|
119
|
+
expect(buffer.read_position).to eq(8)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
describe '#get_string' do
|
124
|
+
|
125
|
+
let(:buffer) do
|
126
|
+
described_class.new("#{8.to_bson.to_s}testing#{BSON::NULL_BYTE}")
|
127
|
+
end
|
128
|
+
|
129
|
+
let!(:string) do
|
130
|
+
buffer.get_string
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'gets the string from the buffer' do
|
134
|
+
expect(string).to eq("testing")
|
135
|
+
end
|
136
|
+
|
137
|
+
it 'increments the position by string length + 5' do
|
138
|
+
expect(buffer.read_position).to eq(12)
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
@@ -13,144 +13,17 @@ describe BSON::ByteBuffer do
|
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
describe '#
|
17
|
-
|
18
|
-
let(:buffer) do
|
19
|
-
described_class.new(BSON::Int32::BSON_TYPE)
|
20
|
-
end
|
21
|
-
|
22
|
-
let!(:byte) do
|
23
|
-
buffer.get_byte
|
24
|
-
end
|
25
|
-
|
26
|
-
it 'gets the byte from the buffer' do
|
27
|
-
expect(byte).to eq(BSON::Int32::BSON_TYPE)
|
28
|
-
end
|
29
|
-
|
30
|
-
it 'increments the read position by 1' do
|
31
|
-
expect(buffer.read_position).to eq(1)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
describe '#get_bytes' do
|
36
|
-
|
37
|
-
let(:string) do
|
38
|
-
"#{BSON::Int32::BSON_TYPE}#{BSON::Int32::BSON_TYPE}"
|
39
|
-
end
|
40
|
-
|
41
|
-
let(:buffer) do
|
42
|
-
described_class.new(string)
|
43
|
-
end
|
44
|
-
|
45
|
-
let!(:bytes) do
|
46
|
-
buffer.get_bytes(2)
|
47
|
-
end
|
48
|
-
|
49
|
-
it 'gets the bytes from the buffer' do
|
50
|
-
expect(bytes).to eq(string)
|
51
|
-
end
|
52
|
-
|
53
|
-
it 'increments the position by the length' do
|
54
|
-
expect(buffer.read_position).to eq(string.bytesize)
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
describe '#get_cstring' do
|
59
|
-
|
60
|
-
let(:buffer) do
|
61
|
-
described_class.new("testing#{BSON::NULL_BYTE}")
|
62
|
-
end
|
63
|
-
|
64
|
-
let!(:string) do
|
65
|
-
buffer.get_cstring
|
66
|
-
end
|
67
|
-
|
68
|
-
it 'gets the cstring from the buffer' do
|
69
|
-
expect(string).to eq("testing")
|
70
|
-
end
|
71
|
-
|
72
|
-
it 'increments the position by string length + 1' do
|
73
|
-
expect(buffer.read_position).to eq(8)
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
describe '#get_double' do
|
78
|
-
|
79
|
-
let(:buffer) do
|
80
|
-
described_class.new("#{12.5.to_bson.to_s}")
|
81
|
-
end
|
82
|
-
|
83
|
-
let!(:double) do
|
84
|
-
buffer.get_double
|
85
|
-
end
|
86
|
-
|
87
|
-
it 'gets the double from the buffer' do
|
88
|
-
expect(double).to eq(12.5)
|
89
|
-
end
|
90
|
-
|
91
|
-
it 'increments the read position by 8' do
|
92
|
-
expect(buffer.read_position).to eq(8)
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
describe '#get_int32' do
|
97
|
-
|
98
|
-
let(:buffer) do
|
99
|
-
described_class.new("#{12.to_bson.to_s}")
|
100
|
-
end
|
101
|
-
|
102
|
-
let!(:int32) do
|
103
|
-
buffer.get_int32
|
104
|
-
end
|
105
|
-
|
106
|
-
it 'gets the int32 from the buffer' do
|
107
|
-
expect(int32).to eq(12)
|
108
|
-
end
|
109
|
-
|
110
|
-
it 'increments the position by 4' do
|
111
|
-
expect(buffer.read_position).to eq(4)
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
describe '#get_int64' do
|
116
|
-
|
117
|
-
let(:buffer) do
|
118
|
-
described_class.new("#{(Integer::MAX_64BIT - 1).to_bson.to_s}")
|
119
|
-
end
|
120
|
-
|
121
|
-
let!(:int64) do
|
122
|
-
buffer.get_int64
|
123
|
-
end
|
124
|
-
|
125
|
-
it 'gets the int64 from the buffer' do
|
126
|
-
expect(int64).to eq(Integer::MAX_64BIT - 1)
|
127
|
-
end
|
128
|
-
|
129
|
-
it 'increments the position by 8' do
|
130
|
-
expect(buffer.read_position).to eq(8)
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
|
-
describe '#get_string' do
|
135
|
-
|
136
|
-
let(:buffer) do
|
137
|
-
described_class.new("#{8.to_bson.to_s}testing#{BSON::NULL_BYTE}")
|
138
|
-
end
|
139
|
-
|
140
|
-
let!(:string) do
|
141
|
-
buffer.get_string
|
142
|
-
end
|
16
|
+
describe '#length' do
|
17
|
+
context 'empty buffer' do
|
143
18
|
|
144
|
-
|
145
|
-
|
146
|
-
|
19
|
+
let(:buffer) do
|
20
|
+
described_class.new
|
21
|
+
end
|
147
22
|
|
148
|
-
|
149
|
-
|
23
|
+
it 'is zero' do
|
24
|
+
expect(buffer.length).to eq(0)
|
25
|
+
end
|
150
26
|
end
|
151
|
-
end
|
152
|
-
|
153
|
-
describe '#length' do
|
154
27
|
|
155
28
|
context 'when the byte buffer is initialized with no bytes' do
|
156
29
|
|
@@ -179,296 +52,6 @@ describe BSON::ByteBuffer do
|
|
179
52
|
end
|
180
53
|
end
|
181
54
|
|
182
|
-
describe '#put_byte' do
|
183
|
-
|
184
|
-
let(:buffer) do
|
185
|
-
described_class.new
|
186
|
-
end
|
187
|
-
|
188
|
-
let!(:modified) do
|
189
|
-
buffer.put_byte(BSON::Int32::BSON_TYPE)
|
190
|
-
end
|
191
|
-
|
192
|
-
it 'appends the byte to the byte buffer' do
|
193
|
-
expect(modified.to_s).to eq(BSON::Int32::BSON_TYPE.chr)
|
194
|
-
end
|
195
|
-
|
196
|
-
it 'increments the write position by 1' do
|
197
|
-
expect(modified.write_position).to eq(1)
|
198
|
-
end
|
199
|
-
|
200
|
-
context 'when it receives a numeric value' do
|
201
|
-
it 'raises the ArgumentError exception' do
|
202
|
-
expect{buffer.put_byte(1)}.to raise_error(ArgumentError)
|
203
|
-
end
|
204
|
-
end
|
205
|
-
|
206
|
-
context 'when it receives a nil value' do
|
207
|
-
it 'raises the ArgumentError exception' do
|
208
|
-
expect{buffer.put_byte(nil)}.to raise_error(ArgumentError)
|
209
|
-
end
|
210
|
-
end
|
211
|
-
end
|
212
|
-
|
213
|
-
describe '#put_cstring' do
|
214
|
-
|
215
|
-
let(:buffer) do
|
216
|
-
described_class.new
|
217
|
-
end
|
218
|
-
|
219
|
-
context 'when the string is valid' do
|
220
|
-
|
221
|
-
let!(:modified) do
|
222
|
-
buffer.put_cstring('testing')
|
223
|
-
end
|
224
|
-
|
225
|
-
it 'appends the string plus null byte to the byte buffer' do
|
226
|
-
expect(modified.to_s).to eq("testing#{BSON::NULL_BYTE}")
|
227
|
-
end
|
228
|
-
|
229
|
-
it 'increments the write position by the length + 1' do
|
230
|
-
expect(modified.write_position).to eq(8)
|
231
|
-
end
|
232
|
-
end
|
233
|
-
|
234
|
-
context "when the string contains a null byte" do
|
235
|
-
|
236
|
-
let(:string) do
|
237
|
-
"test#{BSON::NULL_BYTE}ing"
|
238
|
-
end
|
239
|
-
|
240
|
-
it "raises an error" do
|
241
|
-
expect {
|
242
|
-
buffer.put_cstring(string)
|
243
|
-
}.to raise_error(ArgumentError)
|
244
|
-
end
|
245
|
-
end
|
246
|
-
end
|
247
|
-
|
248
|
-
describe '#put_double' do
|
249
|
-
|
250
|
-
let(:buffer) do
|
251
|
-
described_class.new
|
252
|
-
end
|
253
|
-
|
254
|
-
let!(:modified) do
|
255
|
-
buffer.put_double(1.2332)
|
256
|
-
end
|
257
|
-
|
258
|
-
it 'appends the double to the buffer' do
|
259
|
-
expect(modified.to_s).to eq([ 1.2332 ].pack(Float::PACK))
|
260
|
-
end
|
261
|
-
|
262
|
-
it 'increments the write position by 8' do
|
263
|
-
expect(modified.write_position).to eq(8)
|
264
|
-
end
|
265
|
-
end
|
266
|
-
|
267
|
-
describe '#put_int32' do
|
268
|
-
|
269
|
-
let(:buffer) do
|
270
|
-
described_class.new
|
271
|
-
end
|
272
|
-
|
273
|
-
context 'when the integer is 32 bit' do
|
274
|
-
|
275
|
-
context 'when the integer is positive' do
|
276
|
-
|
277
|
-
let!(:modified) do
|
278
|
-
buffer.put_int32(Integer::MAX_32BIT - 1)
|
279
|
-
end
|
280
|
-
|
281
|
-
let(:expected) do
|
282
|
-
[ Integer::MAX_32BIT - 1 ].pack(BSON::Int32::PACK)
|
283
|
-
end
|
284
|
-
|
285
|
-
it 'appends the int32 to the byte buffer' do
|
286
|
-
expect(modified.to_s).to eq(expected)
|
287
|
-
end
|
288
|
-
|
289
|
-
it 'increments the write position by 4' do
|
290
|
-
expect(modified.write_position).to eq(4)
|
291
|
-
end
|
292
|
-
end
|
293
|
-
|
294
|
-
context 'when the integer is negative' do
|
295
|
-
|
296
|
-
let!(:modified) do
|
297
|
-
buffer.put_int32(Integer::MIN_32BIT + 1)
|
298
|
-
end
|
299
|
-
|
300
|
-
let(:expected) do
|
301
|
-
[ Integer::MIN_32BIT + 1 ].pack(BSON::Int32::PACK)
|
302
|
-
end
|
303
|
-
|
304
|
-
it 'appends the int32 to the byte buffer' do
|
305
|
-
expect(modified.to_s).to eq(expected)
|
306
|
-
end
|
307
|
-
|
308
|
-
it 'increments the write position by 4' do
|
309
|
-
expect(modified.write_position).to eq(4)
|
310
|
-
end
|
311
|
-
end
|
312
|
-
|
313
|
-
context 'when the integer is not 32 bit' do
|
314
|
-
|
315
|
-
it 'raises an exception' do
|
316
|
-
expect {
|
317
|
-
buffer.put_int32(Integer::MAX_64BIT - 1)
|
318
|
-
}.to raise_error(RangeError)
|
319
|
-
end
|
320
|
-
end
|
321
|
-
end
|
322
|
-
end
|
323
|
-
|
324
|
-
describe '#put_int64' do
|
325
|
-
|
326
|
-
let(:buffer) do
|
327
|
-
described_class.new
|
328
|
-
end
|
329
|
-
|
330
|
-
context 'when the integer is 64 bit' do
|
331
|
-
|
332
|
-
context 'when the integer is positive' do
|
333
|
-
|
334
|
-
let!(:modified) do
|
335
|
-
buffer.put_int64(Integer::MAX_64BIT - 1)
|
336
|
-
end
|
337
|
-
|
338
|
-
let(:expected) do
|
339
|
-
[ Integer::MAX_64BIT - 1 ].pack(BSON::Int64::PACK)
|
340
|
-
end
|
341
|
-
|
342
|
-
it 'appends the int64 to the byte buffer' do
|
343
|
-
expect(modified.to_s).to eq(expected)
|
344
|
-
end
|
345
|
-
|
346
|
-
it 'increments the write position by 8' do
|
347
|
-
expect(modified.write_position).to eq(8)
|
348
|
-
end
|
349
|
-
end
|
350
|
-
|
351
|
-
context 'when the integer is negative' do
|
352
|
-
|
353
|
-
let!(:modified) do
|
354
|
-
buffer.put_int64(Integer::MIN_64BIT + 1)
|
355
|
-
end
|
356
|
-
|
357
|
-
let(:expected) do
|
358
|
-
[ Integer::MIN_64BIT + 1 ].pack(BSON::Int64::PACK)
|
359
|
-
end
|
360
|
-
|
361
|
-
it 'appends the int64 to the byte buffer' do
|
362
|
-
expect(modified.to_s).to eq(expected)
|
363
|
-
end
|
364
|
-
|
365
|
-
it 'increments the write position by 8' do
|
366
|
-
expect(modified.write_position).to eq(8)
|
367
|
-
end
|
368
|
-
end
|
369
|
-
|
370
|
-
context 'when the integer is larger than 64 bit' do
|
371
|
-
|
372
|
-
it 'raises an exception' do
|
373
|
-
expect {
|
374
|
-
buffer.put_int64(Integer::MAX_64BIT + 1)
|
375
|
-
}.to raise_error(RangeError)
|
376
|
-
end
|
377
|
-
end
|
378
|
-
end
|
379
|
-
end
|
380
|
-
|
381
|
-
describe '#put_string' do
|
382
|
-
|
383
|
-
context 'when the buffer does not need to be expanded' do
|
384
|
-
|
385
|
-
let(:buffer) do
|
386
|
-
described_class.new
|
387
|
-
end
|
388
|
-
|
389
|
-
context 'when the string is UTF-8' do
|
390
|
-
|
391
|
-
let!(:modified) do
|
392
|
-
buffer.put_string('testing')
|
393
|
-
end
|
394
|
-
|
395
|
-
it 'appends the string to the byte buffer' do
|
396
|
-
expect(modified.to_s).to eq("#{8.to_bson.to_s}testing#{BSON::NULL_BYTE}")
|
397
|
-
end
|
398
|
-
|
399
|
-
it 'increments the write position by length + 5' do
|
400
|
-
expect(modified.write_position).to eq(12)
|
401
|
-
end
|
402
|
-
end
|
403
|
-
end
|
404
|
-
|
405
|
-
context 'when the buffer needs to be expanded' do
|
406
|
-
|
407
|
-
let(:buffer) do
|
408
|
-
described_class.new
|
409
|
-
end
|
410
|
-
|
411
|
-
let(:string) do
|
412
|
-
300.times.inject(""){ |s, i| s << "#{i}" }
|
413
|
-
end
|
414
|
-
|
415
|
-
context 'when no bytes exist in the buffer' do
|
416
|
-
|
417
|
-
let!(:modified) do
|
418
|
-
buffer.put_string(string)
|
419
|
-
end
|
420
|
-
|
421
|
-
it 'appends the string to the byte buffer' do
|
422
|
-
expect(modified.to_s).to eq("#{(string.bytesize + 1).to_bson.to_s}#{string}#{BSON::NULL_BYTE}")
|
423
|
-
end
|
424
|
-
|
425
|
-
it 'increments the write position by length + 5' do
|
426
|
-
expect(modified.write_position).to eq(string.bytesize + 5)
|
427
|
-
end
|
428
|
-
end
|
429
|
-
|
430
|
-
context 'when bytes exist in the buffer' do
|
431
|
-
|
432
|
-
let!(:modified) do
|
433
|
-
buffer.put_int32(4).put_string(string)
|
434
|
-
end
|
435
|
-
|
436
|
-
it 'appends the string to the byte buffer' do
|
437
|
-
expect(modified.to_s).to eq(
|
438
|
-
"#{[ 4 ].pack(BSON::Int32::PACK)}#{(string.bytesize + 1).to_bson.to_s}#{string}#{BSON::NULL_BYTE}"
|
439
|
-
)
|
440
|
-
end
|
441
|
-
|
442
|
-
it 'increments the write position by length + 5' do
|
443
|
-
expect(modified.write_position).to eq(string.bytesize + 9)
|
444
|
-
end
|
445
|
-
end
|
446
|
-
end
|
447
|
-
end
|
448
|
-
|
449
|
-
describe '#replace_int32' do
|
450
|
-
|
451
|
-
let(:buffer) do
|
452
|
-
described_class.new
|
453
|
-
end
|
454
|
-
|
455
|
-
let(:exp_first) do
|
456
|
-
[ 5 ].pack(BSON::Int32::PACK)
|
457
|
-
end
|
458
|
-
|
459
|
-
let(:exp_second) do
|
460
|
-
[ 4 ].pack(BSON::Int32::PACK)
|
461
|
-
end
|
462
|
-
|
463
|
-
let(:modified) do
|
464
|
-
buffer.put_int32(0).put_int32(4).replace_int32(0, 5)
|
465
|
-
end
|
466
|
-
|
467
|
-
it 'replaces the int32 at the location' do
|
468
|
-
expect(modified.to_s).to eq("#{exp_first}#{exp_second}")
|
469
|
-
end
|
470
|
-
end
|
471
|
-
|
472
55
|
describe '#rewind!' do
|
473
56
|
|
474
57
|
shared_examples_for 'a rewindable buffer' do
|
@@ -512,33 +95,13 @@ describe BSON::ByteBuffer do
|
|
512
95
|
|
513
96
|
it_behaves_like 'a rewindable buffer'
|
514
97
|
end
|
515
|
-
end
|
516
|
-
|
517
|
-
describe '#put_bytes' do
|
518
|
-
|
519
|
-
let(:buffer) do
|
520
|
-
described_class.new
|
521
|
-
end
|
522
|
-
|
523
|
-
let!(:modified) do
|
524
|
-
buffer.put_bytes(BSON::Int32::BSON_TYPE)
|
525
|
-
buffer
|
526
|
-
end
|
527
|
-
|
528
|
-
it 'increments the write position by 1' do
|
529
|
-
expect(modified.write_position).to eq(1)
|
530
|
-
end
|
531
98
|
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
context 'when it receives a nil value' do
|
539
|
-
it 'raises the ArgumentError exception' do
|
540
|
-
expect{buffer.put_bytes(nil)}.to raise_error(ArgumentError)
|
541
|
-
end
|
99
|
+
it 'does not change write position' do
|
100
|
+
buffer = described_class.new
|
101
|
+
buffer.put_byte(BSON::Int32::BSON_TYPE)
|
102
|
+
expect(buffer.write_position).to eq(1)
|
103
|
+
buffer.rewind!
|
104
|
+
expect(buffer.write_position).to eq(1)
|
542
105
|
end
|
543
106
|
end
|
544
107
|
end
|