Skip to content

Commit

Permalink
Use #tap to distinguish Duration from UnknownDuration
Browse files Browse the repository at this point in the history
  • Loading branch information
brasmusson committed Oct 29, 2014
1 parent ce386a5 commit 47ec1e1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
14 changes: 7 additions & 7 deletions lib/cucumber/core/test/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,20 +195,20 @@ def increment_total(status)
end

class Duration
attr_reader :duration
attr_reader :nanoseconds

def initialize(duration)
@duration = duration
def initialize(nanoseconds)
@nanoseconds = nanoseconds
end
end

class UnknownDuration
def nil?
true
def tap(&block)
self
end

def duration
0
def nanoseconds
raise "#nanoseconds only allowed to be used in #tap block"
end
end
end
Expand Down
6 changes: 4 additions & 2 deletions spec/cucumber/core/test/duration_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
module Cucumber::Core::Test
RSpec::Matchers.define :be_duration do |expected|
match do |actual|
not actual.nil? and actual.duration == expected
actual.tap { |duration| @nanoseconds = duration.nanoseconds }
@nanoseconds == expected
end
end

RSpec::Matchers.define :an_unknown_duration do
match do |actual|
actual.nil? and expect(actual).to respond_to(:duration)
actual.tap { raise "#tap block was executed, not an UnknownDuration" }
expect(actual).to respond_to(:nanoseconds)
end
end
end
17 changes: 7 additions & 10 deletions spec/cucumber/core/test/result_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,24 +191,21 @@ def describe_to(visitor, *args)
describe Result::Duration do
subject(:duration) { Result::Duration.new(10) }

it "nil? returns false" do
expect( duration.nil? ).to be_falsy
end

it "has a duration" do
expect( duration.duration ).to eq 10
it "#nanoseconds can be accessed in #tap" do
expect( duration.tap { |duration| @duration = duration.nanoseconds } ).to eq duration
expect( @duration ).to eq 10
end
end

describe Result::UnknownDuration do
subject(:duration) { Result::UnknownDuration.new }

it "nil? returns true" do
expect( duration.nil? ).to be_truthy
it "#tap does not execute the passed block" do
expect( duration.tap { raise "tap executed block" } ).to eq duration
end

it "return duration 0" do
expect( duration.duration ).to eq 0
it "accessing #nanoseconds outside #tap block raises exception" do
expect { duration.nanoseconds }.to raise_error(RuntimeError)
end
end
end
Expand Down

0 comments on commit 47ec1e1

Please sign in to comment.