@@ -519,16 +519,47 @@ def flatten(struct, results = [])
519519 end
520520 end
521521
522- context 'encoding' , :encodings do
522+ context 'binary encoding' , :encodings do
523523 let :buffer do
524524 MessagePack . pack ( { 'hello' => 'world' , 'nested' => [ 'object' , { 'structure' => true } ] } )
525525 end
526526
527527 let :unpacker do
528- described_class . new ( :encoding => 'UTF-8' )
528+ described_class . new ( )
529529 end
530530
531- it 'can hardcode encoding when using #each' do
531+ it 'decodes binary as ascii-8bit when using #feed' do
532+ objs = [ ]
533+ unpacker . feed ( buffer )
534+ unpacker . each do |obj |
535+ objs << obj
536+ end
537+ strings = flatten ( objs ) . grep ( String )
538+ strings . should == %w[ hello world nested object structure ]
539+ strings . map ( &:encoding ) . uniq . should == [ Encoding ::ASCII_8BIT ]
540+ end
541+
542+ it 'decodes binary as ascii-8bit when using #feed_each' do
543+ objs = [ ]
544+ unpacker . feed_each ( buffer ) do |obj |
545+ objs << obj
546+ end
547+ strings = flatten ( objs ) . grep ( String )
548+ strings . should == %w[ hello world nested object structure ]
549+ strings . map ( &:encoding ) . uniq . should == [ Encoding ::ASCII_8BIT ]
550+ end
551+ end
552+
553+ context 'string encoding' , :encodings do
554+ let :buffer do
555+ MessagePack . pack ( { 'hello' . force_encoding ( Encoding ::UTF_8 ) => 'world' . force_encoding ( Encoding ::UTF_8 ) , 'nested' . force_encoding ( Encoding ::UTF_8 ) => [ 'object' . force_encoding ( Encoding ::UTF_8 ) , { 'structure' . force_encoding ( Encoding ::UTF_8 ) => true } ] } )
556+ end
557+
558+ let :unpacker do
559+ described_class . new ( )
560+ end
561+
562+ it 'decodes string as utf-8 when using #feed' do
532563 objs = [ ]
533564 unpacker . feed ( buffer )
534565 unpacker . each do |obj |
@@ -539,7 +570,7 @@ def flatten(struct, results = [])
539570 strings . map ( &:encoding ) . uniq . should == [ Encoding ::UTF_8 ]
540571 end
541572
542- it 'can hardcode encoding when using #feed_each' do
573+ it 'decodes binary as ascii-8bit when using #feed_each' do
543574 objs = [ ]
544575 unpacker . feed_each ( buffer ) do |obj |
545576 objs << obj
0 commit comments