spec/ruby-enum/enum_spec.rb in ruby-enum-0.9.0 vs spec/ruby-enum/enum_spec.rb in ruby-enum-1.0.0
- old
+ new
@@ -20,14 +20,32 @@
describe Ruby::Enum do
it 'returns an enum value' do
expect(Colors::RED).to eq 'red'
expect(Colors::GREEN).to eq 'green'
end
- it 'raises UninitializedConstantError on an invalid constant' do
- expect { Colors::ANYTHING }.to raise_error Ruby::Enum::Errors::UninitializedConstantError, /The constant Colors::ANYTHING has not been defined./
+
+ context 'when the i18n gem is loaded' do
+ it 'raises UninitializedConstantError on an invalid constant' do
+ expect do
+ Colors::ANYTHING
+ end.to raise_error Ruby::Enum::Errors::UninitializedConstantError, /The constant Colors::ANYTHING has not been defined./
+ end
end
- context '#each' do
+
+ context 'when the i18n gem is not loaded' do
+ before do
+ allow(described_class).to receive(:i18n).and_return(Ruby::Enum::I18nMock)
+ end
+
+ it 'raises UninitializedConstantError on an invalid constant' do
+ expect do
+ Colors::ANYTHING
+ end.to raise_error Ruby::Enum::Errors::UninitializedConstantError, /ruby.enum.errors.messages.uninitialized_constant.summary/
+ end
+ end
+
+ describe '#each' do
it 'iterates over constants' do
keys = []
enum_keys = []
enum_values = []
Colors.each do |key, enum|
@@ -38,118 +56,168 @@
expect(keys).to eq %i[RED GREEN]
expect(enum_keys).to eq %i[RED GREEN]
expect(enum_values).to eq %w[red green]
end
end
- context '#map' do
+
+ describe '#map' do
it 'maps constants' do
key_key_values = Colors.map do |key, enum|
[key, enum.key, enum.value]
end
expect(key_key_values.count).to eq 2
expect(key_key_values[0]).to eq [:RED, :RED, 'red']
expect(key_key_values[1]).to eq [:GREEN, :GREEN, 'green']
end
end
- context '#parse' do
+
+ describe '#parse' do
it 'parses exact value' do
expect(Colors.parse('red')).to eq(Colors::RED)
end
+
it 'is case-insensitive' do
expect(Colors.parse('ReD')).to eq(Colors::RED)
end
+
it 'returns nil for a null value' do
expect(Colors.parse(nil)).to be_nil
end
+
it 'returns nil for an invalid value' do
expect(Colors.parse('invalid')).to be_nil
end
end
- context '#key?' do
+
+ describe '#key?' do
it 'returns true for valid keys accessed directly' do
Colors.keys.each do |key| # rubocop:disable Style/HashEachMethods
- expect(Colors.key?(key)).to eq(true)
+ expect(Colors.key?(key)).to be(true)
end
end
+
it 'returns true for valid keys accessed via each_keys' do
Colors.each_key do |key|
- expect(Colors.key?(key)).to eq(true)
+ expect(Colors.key?(key)).to be(true)
end
end
+
it 'returns false for invalid keys' do
- expect(Colors.key?(:NOT_A_KEY)).to eq(false)
+ expect(Colors.key?(:NOT_A_KEY)).to be(false)
end
end
- context '#value' do
+
+ describe '#value' do
it 'returns string values for keys' do
Colors.each do |key, enum|
expect(Colors.value(key)).to eq(enum.value)
end
end
+
it 'returns nil for an invalid key' do
expect(Colors.value(:NOT_A_KEY)).to be_nil
end
end
- context '#value?' do
+
+ describe '#value?' do
it 'returns true for valid values accessed directly' do
Colors.values.each do |value| # rubocop:disable Style/HashEachMethods
- expect(Colors.value?(value)).to eq(true)
+ expect(Colors.value?(value)).to be(true)
end
end
+
it 'returns true for valid values accessed via each_value' do
Colors.each_value do |value|
- expect(Colors.value?(value)).to eq(true)
+ expect(Colors.value?(value)).to be(true)
end
end
+
it 'returns false for invalid values' do
- expect(Colors.value?('I am not a value')).to eq(false)
+ expect(Colors.value?('I am not a value')).to be(false)
end
end
- context '#key' do
+
+ describe '#key' do
it 'returns enum instances for values' do
- Colors.each do |_, enum|
+ Colors.each do |_, enum| # rubocop:disable Style/HashEachMethods
expect(Colors.key(enum.value)).to eq(enum.key)
end
end
+
it 'returns nil for an invalid value' do
expect(Colors.key('invalid')).to be_nil
end
end
- context '#keys' do
+
+ describe '#keys' do
it 'returns keys' do
expect(Colors.keys).to eq(%i[RED GREEN])
end
end
- context '#values' do
+
+ describe '#values' do
it 'returns values' do
expect(Colors.values).to eq(%w[red green])
end
end
- context '#to_h' do
+
+ describe '#to_h' do
it 'returns a hash of key:values' do
expect(Colors.to_h).to eq(RED: 'red', GREEN: 'green')
end
end
- context 'on duplicate keys' do
- it 'raises DuplicateKeyError' do
- expect do
- Colors.class_eval do
- define :RED, 'some'
- end
- end.to raise_error Ruby::Enum::Errors::DuplicateKeyError, /The constant Colors::RED has already been defined./
+ context 'when a duplicate key is used' do
+ context 'when the i18n gem is loaded' do
+ it 'raises DuplicateKeyError' do
+ expect do
+ Colors.class_eval do
+ define :RED, 'some'
+ end
+ end.to raise_error Ruby::Enum::Errors::DuplicateKeyError, /The constant Colors::RED has already been defined./
+ end
end
+
+ context 'when the i18n gem is not loaded' do
+ before do
+ allow(described_class).to receive(:i18n).and_return(Ruby::Enum::I18nMock)
+ end
+
+ it 'raises DuplicateKeyError' do
+ expect do
+ Colors.class_eval do
+ define :RED, 'some'
+ end
+ end.to raise_error Ruby::Enum::Errors::DuplicateKeyError, /ruby.enum.errors.messages.duplicate_key.message/
+ end
+ end
end
- context 'on duplicate values' do
- it 'raises a DuplicateValueError' do
- expect do
- Colors.class_eval do
- define :Other, 'red'
- end
- end.to raise_error Ruby::Enum::Errors::DuplicateValueError, /The value red has already been defined./
+ context 'when a duplicate value is used' do
+ context 'when the i18n gem is loaded' do
+ it 'raises a DuplicateValueError' do
+ expect do
+ Colors.class_eval do
+ define :Other, 'red'
+ end
+ end.to raise_error Ruby::Enum::Errors::DuplicateValueError, /The value red has already been defined./
+ end
end
+
+ context 'when the i18n gem is not loaded' do
+ before do
+ allow(described_class).to receive(:i18n).and_return(Ruby::Enum::I18nMock)
+ end
+
+ it 'raises a DuplicateValueError' do
+ expect do
+ Colors.class_eval do
+ define :Other, 'red'
+ end
+ end.to raise_error Ruby::Enum::Errors::DuplicateValueError, /ruby.enum.errors.messages.duplicate_value.summary/
+ end
+ end
end
describe 'Given a class that has not defined any enums' do
class EmptyEnums
include Ruby::Enum
@@ -174,15 +242,18 @@
end
it 'contains its own enums' do
expect(FirstSubclass::ORANGE).to eq 'orange'
end
+
it 'parent class should not have enums defined in child classes' do
expect { Colors::ORANGE }.to raise_error Ruby::Enum::Errors::UninitializedConstantError
end
- context 'Given a 2 level depth subclass' do
+
+ context 'when defining a 2 level depth subclass' do
subject { SecondSubclass }
+
it 'contains its own enums and all the enums defined in the parent classes' do
expect(subject::RED).to eq 'red'
expect(subject::GREEN).to eq 'green'
expect(subject::ORANGE).to eq 'orange'
expect(subject::PINK).to eq 'pink'
@@ -225,9 +296,10 @@
define :created, 'Created'
define :published, 'Published'
define :undefined
end
subject { States }
+
it 'behaves like an enum' do
expect(subject.created).to eq 'Created'
expect(subject.published).to eq 'Published'
expect(subject.undefined).to eq :undefined