Skip to content

Commit

Permalink
Add be_positive_zero and be_negative_zero matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Nov 18, 2011
1 parent 3dacc10 commit 0fd3200
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions mspec/lib/mspec/matchers.rb
Expand Up @@ -32,3 +32,4 @@
require 'mspec/matchers/output'
require 'mspec/matchers/output_to_fd'
require 'mspec/matchers/respond_to'
require 'mspec/matchers/signed_zero'
28 changes: 28 additions & 0 deletions mspec/lib/mspec/matchers/signed_zero.rb
@@ -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
32 changes: 32 additions & 0 deletions mspec/spec/matchers/signed_zero_spec.rb
@@ -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

0 comments on commit 0fd3200

Please sign in to comment.