@@ -437,6 +437,69 @@ def baz(foo, bar) foo + bar; end
437437 end
438438 end
439439 end
440+
441+ context 'copy' do
442+ let ( :this ) do
443+ described_class . new ( :foo , :bar , :baz ) . new ( 'foo' . freeze , [ 'bar' ] , 42 )
444+ end
445+
446+ context '#dup' do
447+ it 'shallowly duplicates all members along with the struct' do
448+ copy = this . dup
449+ expect ( copy . foo ) . not_to be this . foo
450+ expect ( copy . bar ) . not_to be this . bar
451+ expect ( copy . bar . first ) . to be this . bar . first
452+ expect ( copy . baz ) . to be this . baz
453+ end
454+
455+ it 'discards frozen state of the struct' do
456+ expect ( this . freeze . dup ) . not_to be_frozen
457+ end
458+
459+ it 'retains frozen state of members' do
460+ expect ( this . dup . foo ) . to be_frozen
461+ end
462+
463+ it 'discards singleton class' do
464+ this . define_singleton_method ( :qux ) { 'qux' }
465+ expect ( this . qux ) . to eq ( 'qux' )
466+ expect { this . dup . qux } . to raise_error ( NoMethodError )
467+ end
468+
469+ it 'copies the singleton class of members' do
470+ this . bar . define_singleton_method ( :qux ) { 'qux' }
471+ expect ( this . bar . qux ) . to eq ( 'qux' )
472+ expect ( this . dup . bar . qux ) . to eq ( 'qux' )
473+ end
474+ end
475+
476+ context '#clone' do
477+ it 'shallowly clones all members along with the struct' do
478+ copy = this . clone
479+ expect ( copy . foo ) . not_to be this . foo
480+ expect ( copy . bar ) . not_to be this . bar
481+ expect ( copy . bar . first ) . to be this . bar . first
482+ expect ( copy . baz ) . to be this . baz
483+ end
484+
485+ it 'retains frozen state' do
486+ expect ( this . freeze . clone ) . to be_frozen
487+ expect ( this . clone . foo ) . to be_frozen
488+ end
489+
490+ it 'copies the singleton class' do
491+ this . define_singleton_method ( :qux ) { 'qux' }
492+ expect ( this . qux ) . to eq ( 'qux' )
493+ expect ( this . clone . qux ) . to eq ( 'qux' )
494+ end
495+
496+ it 'copies the singleton class of members' do
497+ this . bar . define_singleton_method ( :qux ) { 'qux' }
498+ expect ( this . bar . qux ) . to eq ( 'qux' )
499+ expect ( this . clone . bar . qux ) . to eq ( 'qux' )
500+ end
501+ end
502+ end
440503end
441504
442505RSpec . shared_examples :mergeable_struct do
0 commit comments