From cd03fafb27f2e77d4c87bebd2b28926f20124c4b Mon Sep 17 00:00:00 2001 From: Kyle Banker Date: Tue, 12 Oct 2010 15:53:10 -0400 Subject: [PATCH] RUBY-187 ByteBuffer should take another ByteBuffer as initial data --- lib/bson/byte_buffer.rb | 18 +++++++++++------- test/bson/bson_test.rb | 5 +++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/bson/byte_buffer.rb b/lib/bson/byte_buffer.rb index f4e6016..56bd8da 100644 --- a/lib/bson/byte_buffer.rb +++ b/lib/bson/byte_buffer.rb @@ -23,15 +23,19 @@ module BSON attr_reader :order def initialize(initial_data="") - if initial_data.is_a?(String) - if initial_data.respond_to?(:force_encoding) - @str = initial_data.force_encoding('binary') + @str = case initial_data + when String then + if initial_data.respond_to?(:force_encoding) + initial_data.force_encoding('binary') + else + initial_data + end + when BSON::ByteBuffer then + initial_data.to_a.pac('C*') else - @str = initial_data - end - else - @str = initial_data.pack('C*') + initial_data.pack('C*') end + @cursor = @str.length @order = :little_endian @int_pack_order = 'V' diff --git a/test/bson/bson_test.rb b/test/bson/bson_test.rb index c91a510..f158ec4 100644 --- a/test/bson/bson_test.rb +++ b/test/bson/bson_test.rb @@ -74,6 +74,11 @@ class BSONTest < Test::Unit::TestCase end end + def test_round_trip + doc = {'doc' => 123} + @encoder.deserialize(@encoder.serialize(doc)) + end + # In 1.8 we test that other string encodings raise an exception. # In 1.9 we test that they get auto-converted. if RUBY_VERSION < '1.9'