-
# frozen_string_literal: true
-
-
1
require 'spec_helper'
-
-
1
class Colors
-
1
include Ruby::Enum
-
-
1
define :RED, 'red'
-
1
define :GREEN, 'green'
-
end
-
-
1
class FirstSubclass < Colors
-
1
define :ORANGE, 'orange'
-
end
-
-
1
class SecondSubclass < FirstSubclass
-
1
define :PINK, 'pink'
-
end
-
-
1
describe Ruby::Enum do
-
1
it 'returns an enum value' do
-
1
expect(Colors::RED).to eq 'red'
-
1
expect(Colors::GREEN).to eq 'green'
-
end
-
-
1
context 'when the i18n gem is loaded' do
-
1
it 'raises UninitializedConstantError on an invalid constant' do
-
1
expect do
-
1
Colors::ANYTHING
-
end.to raise_error Ruby::Enum::Errors::UninitializedConstantError, /The constant Colors::ANYTHING has not been defined./
-
end
-
end
-
-
1
context 'when the i18n gem is not loaded' do
-
1
before do
-
1
allow(described_class).to receive(:i18n).and_return(Ruby::Enum::I18nMock)
-
end
-
-
1
it 'raises UninitializedConstantError on an invalid constant' do
-
1
expect do
-
1
Colors::ANYTHING
-
end.to raise_error Ruby::Enum::Errors::UninitializedConstantError, /ruby.enum.errors.messages.uninitialized_constant.summary/
-
end
-
end
-
-
1
describe '#each' do
-
1
it 'iterates over constants' do
-
1
keys = []
-
1
enum_keys = []
-
1
enum_values = []
-
1
Colors.each do |key, enum|
-
2
keys << key
-
2
enum_keys << enum.key
-
2
enum_values << enum.value
-
end
-
1
expect(keys).to eq %i[RED GREEN]
-
1
expect(enum_keys).to eq %i[RED GREEN]
-
1
expect(enum_values).to eq %w[red green]
-
end
-
end
-
-
1
describe '#map' do
-
1
it 'maps constants' do
-
1
key_key_values = Colors.map do |key, enum|
-
2
[key, enum.key, enum.value]
-
end
-
1
expect(key_key_values.count).to eq 2
-
1
expect(key_key_values[0]).to eq [:RED, :RED, 'red']
-
1
expect(key_key_values[1]).to eq [:GREEN, :GREEN, 'green']
-
end
-
end
-
-
1
describe '#parse' do
-
1
it 'parses exact value' do
-
1
expect(Colors.parse('red')).to eq(Colors::RED)
-
end
-
-
1
it 'is case-insensitive' do
-
1
expect(Colors.parse('ReD')).to eq(Colors::RED)
-
end
-
-
1
it 'returns nil for a null value' do
-
1
expect(Colors.parse(nil)).to be_nil
-
end
-
-
1
it 'returns nil for an invalid value' do
-
1
expect(Colors.parse('invalid')).to be_nil
-
end
-
end
-
-
1
describe '#key?' do
-
1
it 'returns true for valid keys accessed directly' do
-
1
Colors.keys.each do |key| # rubocop:disable Style/HashEachMethods
-
2
expect(Colors.key?(key)).to be(true)
-
end
-
end
-
-
1
it 'returns true for valid keys accessed via each_keys' do
-
1
Colors.each_key do |key|
-
2
expect(Colors.key?(key)).to be(true)
-
end
-
end
-
-
1
it 'returns false for invalid keys' do
-
1
expect(Colors.key?(:NOT_A_KEY)).to be(false)
-
end
-
end
-
-
1
describe '#value' do
-
1
it 'returns string values for keys' do
-
1
Colors.each do |key, enum|
-
2
expect(Colors.value(key)).to eq(enum.value)
-
end
-
end
-
-
1
it 'returns nil for an invalid key' do
-
1
expect(Colors.value(:NOT_A_KEY)).to be_nil
-
end
-
end
-
-
1
describe '#value?' do
-
1
it 'returns true for valid values accessed directly' do
-
1
Colors.values.each do |value| # rubocop:disable Style/HashEachMethods
-
2
expect(Colors.value?(value)).to be(true)
-
end
-
end
-
-
1
it 'returns true for valid values accessed via each_value' do
-
1
Colors.each_value do |value|
-
2
expect(Colors.value?(value)).to be(true)
-
end
-
end
-
-
1
it 'returns false for invalid values' do
-
1
expect(Colors.value?('I am not a value')).to be(false)
-
end
-
end
-
-
1
describe '#key' do
-
1
it 'returns enum instances for values' do
-
1
Colors.each do |_, enum| # rubocop:disable Style/HashEachMethods
-
2
expect(Colors.key(enum.value)).to eq(enum.key)
-
end
-
end
-
-
1
it 'returns nil for an invalid value' do
-
1
expect(Colors.key('invalid')).to be_nil
-
end
-
end
-
-
1
describe '#keys' do
-
1
it 'returns keys' do
-
1
expect(Colors.keys).to eq(%i[RED GREEN])
-
end
-
end
-
-
1
describe '#values' do
-
1
it 'returns values' do
-
1
expect(Colors.values).to eq(%w[red green])
-
end
-
end
-
-
1
describe '#to_h' do
-
1
it 'returns a hash of key:values' do
-
1
expect(Colors.to_h).to eq(RED: 'red', GREEN: 'green')
-
end
-
end
-
-
1
context 'when a duplicate key is used' do
-
1
context 'when the i18n gem is loaded' do
-
1
it 'raises DuplicateKeyError' do
-
1
expect do
-
1
Colors.class_eval do
-
1
define :RED, 'some'
-
end
-
end.to raise_error Ruby::Enum::Errors::DuplicateKeyError, /The constant Colors::RED has already been defined./
-
end
-
end
-
-
1
context 'when the i18n gem is not loaded' do
-
1
before do
-
1
allow(described_class).to receive(:i18n).and_return(Ruby::Enum::I18nMock)
-
end
-
-
1
it 'raises DuplicateKeyError' do
-
1
expect do
-
1
Colors.class_eval do
-
1
define :RED, 'some'
-
end
-
end.to raise_error Ruby::Enum::Errors::DuplicateKeyError, /ruby.enum.errors.messages.duplicate_key.message/
-
end
-
end
-
end
-
-
1
context 'when a duplicate value is used' do
-
1
context 'when the i18n gem is loaded' do
-
1
it 'raises a DuplicateValueError' do
-
1
expect do
-
1
Colors.class_eval do
-
1
define :Other, 'red'
-
end
-
end.to raise_error Ruby::Enum::Errors::DuplicateValueError, /The value red has already been defined./
-
end
-
end
-
-
1
context 'when the i18n gem is not loaded' do
-
1
before do
-
1
allow(described_class).to receive(:i18n).and_return(Ruby::Enum::I18nMock)
-
end
-
-
1
it 'raises a DuplicateValueError' do
-
1
expect do
-
1
Colors.class_eval do
-
1
define :Other, 'red'
-
end
-
end.to raise_error Ruby::Enum::Errors::DuplicateValueError, /ruby.enum.errors.messages.duplicate_value.summary/
-
end
-
end
-
end
-
-
1
describe 'Given a class that has not defined any enums' do
-
1
class EmptyEnums
-
1
include Ruby::Enum
-
end
-
1
it do
-
2
expect { EmptyEnums::ORANGE }.to raise_error Ruby::Enum::Errors::UninitializedConstantError
-
end
-
end
-
-
1
context 'when a constant is redefined in a global namespace' do
-
1
before do
-
1
RED = 'black'
-
end
-
-
2
it { expect(Colors::RED).to eq 'red' }
-
end
-
-
1
describe 'Subclass behavior' do
-
1
it 'contains the enums defined in the parent class' do
-
1
expect(FirstSubclass::GREEN).to eq 'green'
-
1
expect(FirstSubclass::RED).to eq 'red'
-
end
-
-
1
it 'contains its own enums' do
-
1
expect(FirstSubclass::ORANGE).to eq 'orange'
-
end
-
-
1
it 'parent class should not have enums defined in child classes' do
-
2
expect { Colors::ORANGE }.to raise_error Ruby::Enum::Errors::UninitializedConstantError
-
end
-
-
1
context 'when defining a 2 level depth subclass' do
-
2
subject { SecondSubclass }
-
-
1
it 'contains its own enums and all the enums defined in the parent classes' do
-
1
expect(subject::RED).to eq 'red'
-
1
expect(subject::GREEN).to eq 'green'
-
1
expect(subject::ORANGE).to eq 'orange'
-
1
expect(subject::PINK).to eq 'pink'
-
end
-
-
1
describe '#values' do
-
2
subject { SecondSubclass.values }
-
-
1
it 'contains the values from all of the parent classes' do
-
1
expect(subject).to eq(%w[red green orange pink])
-
end
-
end
-
end
-
-
1
describe '#values' do
-
2
subject { FirstSubclass.values }
-
-
1
it 'contains the values from the parent class' do
-
1
expect(subject).to eq(%w[red green orange])
-
end
-
end
-
end
-
-
1
describe 'default value' do
-
1
class Default
-
1
include Ruby::Enum
-
1
define :KEY
-
end
-
-
2
subject { Default::KEY }
-
-
1
it 'equals the key' do
-
1
expect(subject).to eq(:KEY)
-
end
-
end
-
-
1
describe 'non constant definitions' do
-
1
class States
-
1
include Ruby::Enum
-
1
define :created, 'Created'
-
1
define :published, 'Published'
-
1
define :undefined
-
end
-
2
subject { States }
-
-
1
it 'behaves like an enum' do
-
1
expect(subject.created).to eq 'Created'
-
1
expect(subject.published).to eq 'Published'
-
1
expect(subject.undefined).to eq :undefined
-
-
1
expect(subject.key?(:created)).to be true
-
1
expect(subject.key('Created')).to eq :created
-
-
1
expect(subject.value?('Created')).to be true
-
1
expect(subject.value(:created)).to eq 'Created'
-
-
1
expect(subject.key?(:undefined)).to be true
-
1
expect(subject.key(:undefined)).to eq :undefined
-
-
1
expect(subject.value?(:undefined)).to be true
-
1
expect(subject.value(:undefined)).to eq :undefined
-
end
-
end
-
end