RUBY-187 ByteBuffer should take another ByteBuffer as initial data

This commit is contained in:
Kyle Banker 2010-10-12 15:53:10 -04:00
parent 0c8d27c0b2
commit cd03fafb27
2 changed files with 16 additions and 7 deletions

View File

@ -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'

View File

@ -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'