Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid calling count on Enumerable #3

Merged
merged 3 commits into from
Jun 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions lib/first_and_only.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@

module Enumerable
def first_and_only!
fail(FirstAndOnly::CountNotOne.new count) if first(2).count != 1
first
case first(2).count
when 0
fail FirstAndOnly::CountZero, FirstAndOnly::COUNT_ZERO_ERROR_MESSAGE
when 1
first
else
fail FirstAndOnly::CountGreaterThanOne, FirstAndOnly::COUNT_GREATER_THAN_ONE_ERROR_MESSAGE
end
end

module FirstAndOnly
class CountNotOne < StandardError
def initialize(count)
super("Expected the count to be 1, but it was #{count}.")
end
end
CountNotOne = Class.new(StandardError)
CountZero = Class.new(CountNotOne)
CountGreaterThanOne = Class.new(CountNotOne)

COUNT_ZERO_ERROR_MESSAGE = 'Expected the count to be 1, was 0.'.freeze
COUNT_GREATER_THAN_ONE_ERROR_MESSAGE = 'Expected the count to be 1, was greater than 1.'.freeze
end
end
38 changes: 33 additions & 5 deletions spec/first_and_only_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,33 @@
shared_examples_for "it_is_empty" do
specify do
expect { subject.first_and_only! }.to raise_error(
Enumerable::FirstAndOnly::CountNotOne,
"Expected the count to be 1, but it was 0."
Enumerable::FirstAndOnly::CountZero,
"Expected the count to be 1, was 0."
)
end
end

shared_examples_for "it_has_more_than_one_element" do
specify do
fail "bad example" if subject.count < 2
fail "bad example" if subject.first(2).count < 2

expect { subject.first_and_only! }.to raise_error(
Enumerable::FirstAndOnly::CountNotOne,
"Expected the count to be 1, but it was #{subject.count}."
Enumerable::FirstAndOnly::CountGreaterThanOne,
"Expected the count to be 1, was greater than 1."
)
end
end

describe "backwards compatibility with CountNotOne error class" do
specify do
expect(Enumerable::FirstAndOnly::CountZero).to be < Enumerable::FirstAndOnly::CountNotOne
end

specify do
expect(Enumerable::FirstAndOnly::CountGreaterThanOne).to be < Enumerable::FirstAndOnly::CountNotOne
end
end

describe "an array" do

context "with one element" do
Expand Down Expand Up @@ -76,4 +86,22 @@ def each

end

describe "infinite enumerable" do
let(:infinite_enumerable) do
Class.new do
include Enumerable

def each
yield(rand(1_000_000)) while true
end
end
end

context "with more than on element" do
subject { infinite_enumerable.new }
it_behaves_like "it_has_more_than_one_element"
end

end

end