Skip to content

Commit

Permalink
RUBY-2435 Document & test ByteBuffer length decreasing after reads (#223
Browse files Browse the repository at this point in the history
)

Co-authored-by: Oleg Pudeyev <[email protected]>
  • Loading branch information
p-mongo and p authored Nov 3, 2020
1 parent 42bcd09 commit b4ed308
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
10 changes: 10 additions & 0 deletions ext/bson/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ void Init_bson_native()

rb_define_alloc_func(rb_byte_buffer_class, rb_bson_byte_buffer_allocate);
rb_define_method(rb_byte_buffer_class, "initialize", rb_bson_byte_buffer_initialize, -1);

/*
* call-seq:
* buffer.length -> Fixnum
*
* Returns the number of bytes available to be read in the buffer.
*
* When a buffer is being written to, each added byte increases its length.
* When a buffer is being read from, each read byte decreases its length.
*/
rb_define_method(rb_byte_buffer_class, "length", rb_bson_byte_buffer_length, 0);

/*
Expand Down
14 changes: 13 additions & 1 deletion spec/bson/byte_buffer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
end
end


context 'when the byte buffer is initialized with some bytes' do

let(:buffer) do
Expand All @@ -53,6 +52,19 @@
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
end

describe '#rewind!' do
Expand Down

0 comments on commit b4ed308

Please sign in to comment.