-
-
Notifications
You must be signed in to change notification settings - Fork 50
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
Introduce a Duration object #71
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# -*- encoding: utf-8 -*- | ||
require 'cucumber/core/test/result' | ||
require 'rspec/expectations' | ||
|
||
module Cucumber::Core::Test | ||
RSpec::Matchers.define :be_duration do |expected| | ||
match do |actual| | ||
actual.exist? and actual.duration == expected | ||
end | ||
end | ||
|
||
RSpec::Matchers.define :an_unknown_duration do | ||
match do |actual| | ||
not actual.exist? and expect(actual).to respond_to(:duration) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be better to use
nil?
here? (obviously the opposite way around to thisexist?
method). That way we're not inventing a new protocol for null objects.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is best to adhere to the standard protocol. In the back of my head I remember seeing the construct
isNull()
in relation to null objects, but I can tell you that there a lot more search result for having the null object quacking like a real object, than about naming the query to distinguish the null object from a real object.Fixed in cc5d1cb and cucumber/common@7b1d1d9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Relevant http://devblog.avdi.org/2011/05/30/null-objects-and-falsiness/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's your take-away from that @tooky
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mattwynne sending
.nil?
or.exist?
is really a type check disguised as a message send.The example where I've seen it used, something like:
I think perhaps we could do something like:
The standard duration could then yield the value to the block, and the unknown duration could be a no-op.
WDYT? /cc @brasmusson
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's fine if
#tap
yields the integer (ms) value, or doesn't yield for the null object. Don't you agree @tooky?If we wanted to make it crystal-clear, we could call it
#tap_value
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mattwynne @brasmusson I think it would be a bit weird to yield the value, would it violate the POLS not to get the actual object?
#tap_value
is ok... but how about we give the value a good name? Is it recorded in milli seconds?Then you could do:
(Incidentally, should the duration deal with time conversions?)
@brasmusson I think that UnkownDuration should implement the same interface as a duration, but perhaps rather than return 0, it could return itself?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed @tooky - #tap should yield the object itself. A good name for the method is either
#ms
or#milliseconds
IMO - no need for underscores.It seems to me that with this API, bug-free code would never call that method on the null object, so it doesn't really need to implement it. Or it could throw an error telling the client to use the
#tap
trick. I think returning self might be a bit surprising.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The duration is actually recorded in nano seconds so
#nanoseconds
could be the method name. As a side note, the gherkin_formatter_adapter converts the duration to seconds, and then the gherkin::json_formatter converts it back to nanoseconds.If we look at the duration object API as
#nanoseconds
is only allowed to be called inside a#tap
block, than throwing an error is reasonable.If we look at
#nanoseconds
as a regular method of the API, then also UnkownDuration should return something reasonable for it. 0 is kind of the most neutral integer value (would it have been a float, thenFloat::NAN
could be used). Returning self is possible, if UnkownDuration also use some other tricks from http://devblog.avdi.org/2011/05/30/null-objects-and-falsiness/, so it responds reasonable "nullish" to the methods that could be sent to an integer:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With 47ec1e1 and cucumber/common@00e9701
#tap
and#nanoseconds
are used to pass the duration value to the JSON formatter, unless its anUnkownDuration
.