@@ -27,6 +27,10 @@ class OtherSecondSubclass < FirstSubclass
2727 class SubclassWithNoOwnDefines < Colors
2828 end
2929
30+ class SubclassRedefiningParentKey < Colors
31+ define :RED , 'crimson'
32+ end
33+
3034 it 'returns an enum value' do
3135 expect ( Colors ::RED ) . to eq 'red'
3236 expect ( Colors ::GREEN ) . to eq 'green'
@@ -414,6 +418,37 @@ class EmptyEnums
414418 expect ( SubclassWithNoOwnDefines . each . map { |key , _enum | key } ) . to eq ( %i[ RED GREEN ] )
415419 end
416420 end
421+
422+ context 'when a subclass redefines a key already used by a parent class' do
423+ it 'does not raise a duplicate key error' do
424+ expect ( SubclassRedefiningParentKey ::RED ) . to eq 'crimson'
425+ end
426+
427+ it "the subclass' own value takes precedence" do
428+ expect ( SubclassRedefiningParentKey . value ( :RED ) ) . to eq 'crimson'
429+ end
430+
431+ it "the subclass' own value is used in to_h" do
432+ expect ( SubclassRedefiningParentKey . to_h ) . to eq ( RED : 'crimson' , GREEN : 'green' )
433+ end
434+
435+ it 'includes both the parent and its own redefined value in values' do
436+ expect ( SubclassRedefiningParentKey . values ) . to eq ( %w[ red green crimson ] )
437+ end
438+
439+ it "the parent class' own value is unaffected" do
440+ expect ( Colors ::RED ) . to eq 'red'
441+ expect ( Colors . value ( :RED ) ) . to eq 'red'
442+ end
443+
444+ it 'still allows the parent class to raise a duplicate key error for its own re-definitions' do
445+ expect do
446+ Colors . class_eval do
447+ define :RED , 'red'
448+ end
449+ end . to raise_error Ruby ::Enum ::Errors ::DuplicateKeyError , /RED/
450+ end
451+ end
417452 end
418453
419454 describe 'default value' do
0 commit comments