Skip to content

Commit b4ed308

Browse files
p-mongop
andauthored
RUBY-2435 Document & test ByteBuffer length decreasing after reads (#223)
Co-authored-by: Oleg Pudeyev <[email protected]>
1 parent 42bcd09 commit b4ed308

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

ext/bson/init.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ void Init_bson_native()
5656

5757
rb_define_alloc_func(rb_byte_buffer_class, rb_bson_byte_buffer_allocate);
5858
rb_define_method(rb_byte_buffer_class, "initialize", rb_bson_byte_buffer_initialize, -1);
59+
60+
/*
61+
* call-seq:
62+
* buffer.length -> Fixnum
63+
*
64+
* Returns the number of bytes available to be read in the buffer.
65+
*
66+
* When a buffer is being written to, each added byte increases its length.
67+
* When a buffer is being read from, each read byte decreases its length.
68+
*/
5969
rb_define_method(rb_byte_buffer_class, "length", rb_bson_byte_buffer_length, 0);
6070

6171
/*

spec/bson/byte_buffer_spec.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
end
4343
end
4444

45-
4645
context 'when the byte buffer is initialized with some bytes' do
4746

4847
let(:buffer) do
@@ -53,6 +52,19 @@
5352
expect(buffer.length).to eq(2)
5453
end
5554
end
55+
56+
context 'after the byte buffer was read from' do
57+
58+
let(:buffer) do
59+
described_class.new({}.to_bson.to_s)
60+
end
61+
62+
it 'returns the number of bytes remaining in the buffer' do
63+
expect(buffer.length).to eq(5)
64+
buffer.get_int32
65+
expect(buffer.length).to eq(1)
66+
end
67+
end
5668
end
5769

5870
describe '#rewind!' do

0 commit comments

Comments
 (0)