1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
|
# rubocop:todo all
require 'spec_helper'
describe BSON::ByteBuffer do
describe '#allocate' do
let(:buffer) do
described_class.allocate
end
it 'allocates a buffer' do
expect(buffer).to be_a(BSON::ByteBuffer)
end
end
describe '#length' do
context 'empty buffer' do
let(:buffer) do
described_class.new
end
it 'is zero' do
expect(buffer.length).to eq(0)
end
end
context 'when the byte buffer is initialized with no bytes' do
let(:buffer) do
described_class.new
end
context '#put_int32' do
before do
buffer.put_int32(5)
end
it 'returns the length of the buffer' do
expect(buffer.length).to eq(4)
end
end
end
context 'when the byte buffer is initialized with some bytes' do
let(:buffer) do
described_class.new("#{BSON::Int32::BSON_TYPE}#{BSON::Int32::BSON_TYPE}")
end
it 'returns the length' do
expect(buffer.length).to eq(2)
end
end
context 'after the byte buffer was read from' do
let(:buffer) do
described_class.new({}.to_bson.to_s)
end
it 'returns the number of bytes remaining in the buffer' do
expect(buffer.length).to eq(5)
buffer.get_int32
expect(buffer.length).to eq(1)
end
end
context 'after the byte buffer was converted to string' do
shared_examples 'returns the total buffer length' do
it 'returns the total buffer length' do
expect(buffer.length).to eq(5)
buffer.to_s.length.should == 5
expect(buffer.length).to eq(5)
end
end
context 'read buffer' do
let(:buffer) do
described_class.new({}.to_bson.to_s)
end
include_examples 'returns the total buffer length'
end
context 'write buffer' do
let(:buffer) do
described_class.new.tap do |buffer|
buffer.put_bytes('hello')
end
end
include_examples 'returns the total buffer length'
end
end
end
describe '#rewind!' do
shared_examples_for 'a rewindable buffer' do
let(:string) do
"#{BSON::Int32::BSON_TYPE}#{BSON::Int32::BSON_TYPE}"
end
before do
buffer.get_bytes(1)
buffer.rewind!
end
it 'resets the read position to 0' do
expect(buffer.read_position).to eq(0)
end
it 'starts subsequent reads at position 0' do
expect(buffer.get_bytes(2)).to eq(string)
end
end
context 'when the buffer is instantiated with a string' do
let(:buffer) do
described_class.new(string)
end
it_behaves_like 'a rewindable buffer'
end
context 'when the buffer is instantiated with nothing' do
let(:buffer) do
described_class.new
end
before do
buffer.put_byte(BSON::Int32::BSON_TYPE).put_byte(BSON::Int32::BSON_TYPE)
end
it_behaves_like 'a rewindable buffer'
end
it 'does not change write position' do
buffer = described_class.new
buffer.put_byte(BSON::Int32::BSON_TYPE)
expect(buffer.write_position).to eq(1)
buffer.rewind!
expect(buffer.write_position).to eq(1)
end
end
describe 'write followed by read' do
let(:buffer) do
described_class.new
end
context 'one cycle' do
it 'returns the written data' do
buffer.put_cstring('hello')
buffer.get_cstring.should == 'hello'
end
end
context 'two cycles' do
it 'returns the written data' do
buffer.put_cstring('hello')
buffer.get_cstring.should == 'hello'
buffer.put_cstring('world')
buffer.get_cstring.should == 'world'
end
end
context 'mixed cycles' do
it 'returns the written data' do
if BSON::Environment.jruby?
pending 'RUBY-2334'
end
buffer.put_int32(1)
buffer.put_int32(2)
buffer.get_int32.should == 1
buffer.put_int32(3)
buffer.get_int32.should == 2
buffer.get_int32.should == 3
end
end
end
describe '#to_s' do
context 'read buffer' do
let(:buffer) do
described_class.new("\x18\x00\x00\x00*\x00\x00\x00")
end
it 'returns the data' do
buffer.to_s.should == "\x18\x00\x00\x00*\x00\x00\x00"
end
it 'returns the remaining buffer contents after a read' do
buffer.to_s.should == "\x18\x00\x00\x00*\x00\x00\x00"
buffer.get_int32.should == 24
buffer.to_s.should == "*\x00\x00\x00"
end
end
context 'write buffer' do
let(:buffer) do
described_class.new.tap do |buffer|
buffer.put_int32(24)
end
end
it 'returns the data' do
buffer.to_s.should == "\x18\x00\x00\x00".force_encoding('binary')
end
it 'returns the complete buffer contents after a write' do
buffer.to_s.should == "\x18\x00\x00\x00".force_encoding('binary')
buffer.put_int32(42)
buffer.to_s.should == "\x18\x00\x00\x00*\x00\x00\x00".force_encoding('binary')
end
end
end
end
|