2019-04-29 19:27:42 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
require "email"
|
2013-03-01 16:05:39 -06:00
|
|
|
|
2022-07-27 21:27:38 -05:00
|
|
|
RSpec.describe Enum do
|
2016-01-08 04:53:52 -06:00
|
|
|
let(:array_enum) { Enum.new(:jake, :finn, :princess_bubblegum, :peppermint_butler) }
|
|
|
|
let(:hash_enum) { Enum.new(jake: 1, finn: 2, princess_bubblegum: 3, peppermint_butler: 4) }
|
2013-03-01 16:05:39 -06:00
|
|
|
|
2013-03-02 01:40:32 -06:00
|
|
|
describe ".[]" do
|
|
|
|
it "looks up a number by symbol" do
|
2016-01-08 04:53:52 -06:00
|
|
|
expect(array_enum[:princess_bubblegum]).to eq(3)
|
|
|
|
expect(hash_enum[:princess_bubblegum]).to eq(3)
|
2013-03-01 16:05:39 -06:00
|
|
|
end
|
|
|
|
|
2013-03-02 01:40:32 -06:00
|
|
|
it "looks up a symbol by number" do
|
2016-01-08 04:53:52 -06:00
|
|
|
expect(array_enum[2]).to eq(:finn)
|
|
|
|
expect(hash_enum[2]).to eq(:finn)
|
2013-03-01 16:05:39 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-02 01:40:32 -06:00
|
|
|
describe ".valid?" do
|
|
|
|
it "returns true if a key exists" do
|
2016-01-08 04:53:52 -06:00
|
|
|
expect(array_enum.valid?(:finn)).to eq(true)
|
|
|
|
expect(hash_enum.valid?(:finn)).to eq(true)
|
2013-03-01 16:05:39 -06:00
|
|
|
end
|
|
|
|
|
2013-03-02 01:40:32 -06:00
|
|
|
it "returns false if a key does not exist" do
|
2016-01-08 04:53:52 -06:00
|
|
|
expect(array_enum.valid?(:obama)).to eq(false)
|
|
|
|
expect(hash_enum.valid?(:obama)).to eq(false)
|
2013-03-01 16:05:39 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-02 01:40:32 -06:00
|
|
|
describe ".only" do
|
2013-03-01 16:05:39 -06:00
|
|
|
it "returns only the values we ask for" do
|
2017-07-27 20:20:09 -05:00
|
|
|
expect(array_enum.only(:jake, :princess_bubblegum)).to eq(jake: 1, princess_bubblegum: 3)
|
|
|
|
expect(hash_enum.only(:jake, :princess_bubblegum)).to eq(jake: 1, princess_bubblegum: 3)
|
2013-03-01 16:05:39 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-02 01:40:32 -06:00
|
|
|
describe ".except" do
|
|
|
|
it "returns everything but the values we ask to delete" do
|
2017-07-27 20:20:09 -05:00
|
|
|
expect(array_enum.except(:jake, :princess_bubblegum)).to eq(finn: 2, peppermint_butler: 4)
|
|
|
|
expect(hash_enum.except(:jake, :princess_bubblegum)).to eq(finn: 2, peppermint_butler: 4)
|
2016-01-08 04:53:52 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
describe "allows to specify number of first enum member" do
|
2016-01-08 04:53:52 -06:00
|
|
|
it "number of first enum member should be 0 " do
|
|
|
|
start_enum = Enum.new(:jake, :finn, :princess_bubblegum, :peppermint_butler, start: 0)
|
|
|
|
expect(start_enum[:princess_bubblegum]).to eq(2)
|
2013-03-01 16:05:39 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|