tests/test_round_trip.rb
# Round-trip comparisons of Ruby-to-BSON and back.
# * Take the objects that were read from XML
# * Turn them into BSON bytes
# * Compare that with the BSON files we have
# * Turn those BSON bytes back in to Ruby objects
# * Turn them back into BSON bytes
# * Compare that with the BSON files we have (or the bytes that were already
#   generated)
Round-trip comparisons of Ruby-to-BSON and back.
Take the objects that were read from XML
Turn them into BSON bytes
Compare that with the BSON files we have
Turn those BSON bytes back in to Ruby objects
Turn them back into BSON bytes
Compare that with the BSON files we have (or the bytes that were already
generated)
Round - trip comparisons of Ruby - to - BSON and back . Take the objects that were read from XML Turn them into BSON bytes Compare that with the BSON files we have Turn those BSON bytes back in to Ruby objects Turn them back into BSON bytes Compare that with the BSON files we have ( or the bytes that were already generated )
def one_round_trip(dir, name)
    obj = File.open(File.join(dir, "{name}.xson")) { |f|
      XMLToRuby.new.xml_to_ruby(f)
    }
    File.open(File.join(dir, "{name}.bson"), 'r') { |f|
      bson = f.read
      bson = if RUBY_VERSION >= '1.9'
               bson.bytes.to_a
             else
               bson.split(//).collect { |c| c[0] }
             end
      bson_from_ruby = BSON.new.serialize(obj).to_a
      begin
        assert_equal bson.length, bson_from_ruby.length
        assert_equal bson, bson_from_ruby
      rescue => ex
        raise ex
      end
       We're passing a nil db to the contructor here, but that's OK because
       the BSON DBRef bytes don't contain the db object in any case, and we
       don't care what the database is.
      obj_from_bson = BSON.new.deserialize(ByteBuffer.new(bson_from_ruby))
      assert_kind_of OrderedHash, obj_from_bson
       Turn that Ruby object into BSON and compare it to the original BSON
       bytes.
      bson_from_ruby = BSON.new.serialize(obj_from_bson).to_a
      begin
        assert_equal bson.length, bson_from_ruby.length
        assert_equal bson, bson_from_ruby
      rescue => ex
         File.open(File.join(dir, "{name}_out_b.bson"), 'wb') { |f|  DEBUG
        raise ex
      end
    }
  end
def one_round_trip ( dir , name ) obj = File . open ( File . join ( dir , "#{name}.xson" ) ) { | f | XMLToRuby . new . xml_to_ruby ( f ) } File . open ( File . join ( dir , "#{name}.bson" ) , 'r' ) { | f | bson = f . read bson = if RUBY_VERSION >= '1.9' bson . bytes . to_a else bson . split ( / / ) . collect { | c | c [ 0 ] } end bson_from_ruby = BSON . new . serialize ( obj ) . to_a begin assert_equal bson . length , bson_from_ruby . length assert_equal bson , bson_from_ruby rescue => ex raise ex end obj_from_bson = BSON . new . deserialize ( ByteBuffer . new ( bson_from_ruby ) ) assert_kind_of OrderedHash , obj_from_bson bson_from_ruby = BSON . new . serialize ( obj_from_bson ) . to_a begin assert_equal bson . length , bson_from_ruby . length assert_equal bson , bson_from_ruby rescue => ex raise ex end } end
Round-trip comparisons of Ruby-to-BSON and back.
Round - trip comparisons of Ruby - to - BSON and back .
# Read the BSON from the file # Turn the Ruby object into BSON bytes and compare with the BSON bytes # from the file. #         File.open(File.join(dir, "#{name}_out_a.bson"), 'wb') { |f| # DEBUG #           bson_from_ruby.each { |b| f.putc(b) } #         } # Turn those BSON bytes back into a Ruby object. # # We're passing a nil db to the contructor here, but that's OK because # the BSON DBRef bytes don't contain the db object in any case, and we # don't care what the database is. # Turn that Ruby object into BSON and compare it to the original BSON # bytes. #         File.open(File.join(dir, "#{name}_out_b.bson"), 'wb') { |f| # DEBUG #           bson_from_ruby.each { |b| f.putc(b) } #         }
dir
<not_specific>
name
<not_specific>