Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add be_positive_zero and be_negative_zero matchers
- Loading branch information
1 parent
3dacc10
commit 0fd3200
Showing
3 changed files
with
61 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class SignedZeroMatcher | ||
def initialize(expected_sign) | ||
@expected_sign = expected_sign | ||
end | ||
|
||
def matches?(actual) | ||
@actual = actual | ||
(1.0/actual).infinite? == @expected_sign | ||
end | ||
|
||
def failure_message | ||
["Expected #{@actual}", "to be #{"-" if @expected_sign == -1}0.0"] | ||
end | ||
|
||
def negative_failure_message | ||
["Expected #{@actual}", "not to be #{"-" if @expected_sign == -1}0.0"] | ||
end | ||
end | ||
|
||
class Object | ||
def be_positive_zero | ||
SignedZeroMatcher.new(1) | ||
end | ||
|
||
def be_negative_zero | ||
SignedZeroMatcher.new(-1) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require File.dirname(__FILE__) + '/../spec_helper' | ||
require 'mspec/expectations/expectations' | ||
require 'mspec/matchers/signed_zero' | ||
|
||
describe SignedZeroMatcher do | ||
it "matches when actual is zero and has the correct sign" do | ||
SignedZeroMatcher.new(1).matches?(0.0).should == true | ||
SignedZeroMatcher.new(-1).matches?(-0.0).should == true | ||
end | ||
|
||
it "does not match when actual is non-zero" do | ||
SignedZeroMatcher.new(1).matches?(1.0).should == false | ||
SignedZeroMatcher.new(-1).matches?(-1.0).should == false | ||
end | ||
|
||
it "does not match when actual is zero but has the incorrect sign" do | ||
SignedZeroMatcher.new(1).matches?(-0.0).should == false | ||
SignedZeroMatcher.new(-1).matches?(0.0).should == false | ||
end | ||
|
||
it "provides a useful failure message" do | ||
matcher = SignedZeroMatcher.new(-1) | ||
matcher.matches?(0.0) | ||
matcher.failure_message.should == ["Expected 0.0", "to be -0.0"] | ||
end | ||
|
||
it "provides a useful negative failure message" do | ||
matcher = SignedZeroMatcher.new(-1) | ||
matcher.matches?(-0.0) | ||
matcher.negative_failure_message.should == ["Expected -0.0", "not to be -0.0"] | ||
end | ||
end |