From 80475ff19885336281281c406e2d272becc29d3b Mon Sep 17 00:00:00 2001 From: Jim Menard Date: Thu, 18 Dec 2008 16:14:56 -0500 Subject: [PATCH] Clarified ByteBuffer.get behavior: get() returns one byte, get(1) returns array of length one. --- lib/mongo/util/byte_buffer.rb | 10 ++++++---- tests/test_byte_buffer.rb | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/mongo/util/byte_buffer.rb b/lib/mongo/util/byte_buffer.rb index 67d698d..c67b93f 100644 --- a/lib/mongo/util/byte_buffer.rb +++ b/lib/mongo/util/byte_buffer.rb @@ -97,13 +97,15 @@ class ByteBuffer end end - # If +size+ == 1, returns one byte. Else returns array of bytes of length - # +size+. - def get(len=1) + # If +size+ == nil, returns one byte. Else returns array of bytes of length + # # +size+. + def get(len=nil) + one_byte = len.nil? + len ||= 1 check_read_length(len) start = @cursor @cursor += len - if len == 1 + if one_byte @buf[start] else @buf[start, len] diff --git a/tests/test_byte_buffer.rb b/tests/test_byte_buffer.rb index 5025fab..0590778 100644 --- a/tests/test_byte_buffer.rb +++ b/tests/test_byte_buffer.rb @@ -8,6 +8,24 @@ class ByteBufferTest < Test::Unit::TestCase @buf = ByteBuffer.new end + def test_nil_get_returns_one_byte + @buf.put_array([1, 2, 3, 4]) + @buf.rewind + assert_equal 1, @buf.get + end + + def test_one_get_returns_array_length_one + @buf.put_array([1, 2, 3, 4]) + @buf.rewind + assert_equal [1], @buf.get(1) + end + + def test_zero_get_returns_empty_array + @buf.put_array([1, 2, 3, 4]) + @buf.rewind + assert_equal [], @buf.get(0) + end + def test_empty assert_equal 0, @buf.length end