Skip to content

Commit

Permalink
Refactored all matchers to use Matchy::MatcherBuilder#build_matcher().
Browse files Browse the repository at this point in the history
Signed-off-by: Jeremy McAnally <jeremymcanally@gmail.com>
  • Loading branch information
mhennemeyer authored and jm committed Feb 11, 2009
1 parent 07e1378 commit 79bd961
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 316 deletions.
51 changes: 11 additions & 40 deletions lib/matchy/built_in/enumerable_expectations.rb
@@ -1,44 +1,7 @@
module Matchy
module Expectations
class IncludeExpectation < Base
def matches?(receiver)
@receiver = receiver
@expected.each do |o|
return false unless receiver.include?(o)
end

true
end

def failure_message
"Expected #{@receiver.inspect} to include #{@expected.inspect}."
end

def negative_failure_message
"Expected #{@receiver.inspect} to not include #{@expected.inspect}."
end
end

class ExcludeExpectation < Base
def matches?(receiver)
@receiver = receiver
@expected.each do |o|
return false unless !receiver.include?(o)
end

true
end

def failure_message
"Expected #{@receiver.inspect} to exclude #{@expected.inspect}."
end

def negative_failure_message
"Expected #{@receiver.inspect} to not exclude #{@expected.inspect}."
end
end

module TestCaseExtensions

# Calls +include?+ on the receiver for any object. You can also provide
# multiple arguments to see if all of them are included.
#
Expand All @@ -49,7 +12,11 @@ module TestCaseExtensions
# ['a', 'b', 'c'].should include('a', 'c')
#
def include(*obj)
Matchy::Expectations::IncludeExpectation.new(obj, self)
build_matcher(:include, obj) do |given, matcher, args|
matcher.positive_msg = "Expected #{given.inspect} to include #{args.inspect}."
matcher.negative_msg = "Expected #{given.inspect} to not include #{args.inspect}."
args.inject(true) {|m,o| m && given.include?(o) }
end
end

# Expects the receiver to exclude the given object(s). You can provide
Expand All @@ -62,7 +29,11 @@ def include(*obj)
# ['a', 'b', 'c'].should exclude('e', 'f', 'g')
#
def exclude(*obj)
Matchy::Expectations::ExcludeExpectation.new(obj, self)
build_matcher(:exlude, obj) do |given, matcher, args|
matcher.positive_msg = "Expected #{given.inspect} to exclude #{args.inspect}."
matcher.negative_msg = "Expected #{given.inspect} to not exclude #{args.inspect}."
args.inject(true) {|m,o| m && !given.include?(o) }
end
end
end
end
Expand Down
102 changes: 36 additions & 66 deletions lib/matchy/built_in/error_expectations.rb
@@ -1,67 +1,5 @@
module Matchy
module Expectations
class RaiseErrorExpectation < Base
def initialize(expected, test_case)
@error = nil
super
end

def matches?(receiver)
@receiver = receiver
begin
receiver.call
return false
rescue StandardError => e
@error = e
return false unless e.class.ancestors.include?(@expected)

return true
end
end

def failure_message
extra = ""
if @error
extra = "but #{@error.class.name} was raised instead"
else
extra = "but none was raised"
end

"Expected #{@receiver.inspect} to raise #{@expected.name}, #{extra}."
end

def negative_failure_message
"Expected #{@receiver.inspect} to not raise #{@expected.name}."
end
end

class ThrowSymbolExpectation < Base
def initialize(expected, test_case)
@thrown_symbol = nil
super
end

def matches?(receiver)
@receiver = receiver
begin
receiver.call
rescue NameError => e
raise e unless e.message =~ /uncaught throw/
@thrown_symbol = e.name.to_sym
ensure
return @expected == @thrown_symbol
end
end

def failure_message
"Expected #{@receiver.inspect} to throw :#{@expected}, but #{@thrown_symbol ? ':' + @thrown_symbol.to_s + ' was thrown instead' : 'no symbol was thrown'}."
end

def negative_failure_message
"Expected #{@receiver.inspect} to not throw :#{@expected}."
end
end

module TestCaseExtensions
# Expects a lambda to raise an error. You can specify the error or leave it blank to encompass
# any error.
Expand All @@ -71,8 +9,23 @@ module TestCaseExtensions
# lambda { raise "FAILURE." }.should raise_error
# lambda { puts i_dont_exist }.should raise_error(NameError)
#
def raise_error(obj = StandardError)
Matchy::Expectations::RaiseErrorExpectation.new(obj, self)
def raise_error(*obj)
build_matcher(:raise_error, obj) do |given, matcher, args|
raised = false
error = nil
begin
given.call
rescue StandardError => e
raised = true
error = e
end
extra = "but none was raised"
extra = "but #{error.class.name} was raised instead" if error
expected_error = args[0] || StandardError
matcher.positive_msg = "Expected #{given.inspect} to raise #{expected_error.name}, #{extra}."
matcher.negative_msg = "Expected #{given.inspect} to not raise #{expected_error.name}."
raised && error.class.ancestors.include?(expected_error)
end
end

# Expects a lambda to throw an error.
Expand All @@ -82,8 +35,25 @@ def raise_error(obj = StandardError)
# lambda { throw :thing }.should throw_symbol(:thing)
# lambda { "not this time" }.should_not throw_symbol(:hello)
#
def throw_symbol(obj)
Matchy::Expectations::ThrowSymbolExpectation.new(obj, self)
def throw_symbol(*obj)
build_matcher(:throw_symbol, obj) do |given, matcher, args|
raised = false
thrown_symbol = nil
begin
given.call
rescue NameError => e
raise e unless e.message =~ /uncaught throw/
raised = true
thrown_symbol = e.name.to_sym
end
expected = args[0]
matcher.positive_msg = <<-END
Expected #{given.inspect} to throw :#{expected}, but \
#{thrown_symbol ? ':' + thrown_symbol.to_s + ' was thrown instead' : 'no symbol was thrown'}.
END
matcher.negative_msg = "Expected #{given.inspect} to not throw :#{expected}."
expected == thrown_symbol
end
end
end
end
Expand Down
68 changes: 7 additions & 61 deletions lib/matchy/built_in/operator_expectations.rb
Expand Up @@ -7,72 +7,18 @@ module Expectations
# 13.should == 13
# "hello".length.should_not == 2
#
class OperatorExpectation < Base
class OperatorExpectation < Base
OPERATORS = ['==', '===', '=~', '>', '>=', '<', '<=']

def initialize(receiver, match)
@receiver = receiver
@match = match
end

def ==(expected)
@expected = expected
if @receiver.send(:==, expected) == @match
pass!
else
fail!("==")
end
end

def ===(expected)
@expected = expected
if @receiver.send(:===, expected) == @match
pass!
else
fail!("===")
end
end

def =~(expected)
@expected = expected
if @receiver.send(:=~, expected).nil? != @match
pass!
else
fail!("=~")
end
end

def >(expected)
@expected = expected
if @receiver.send(:>, expected) == @match
pass!
else
fail!(">")
end
end

def <(expected)
@expected = expected
if @receiver.send(:<, expected) == @match
pass!
else
fail!("<")
end
end

def >=(expected)
@expected = expected
if @receiver.send(:>=, expected) == @match
pass!
else
fail!(">=")
end
end

def <=(expected)
@expected = expected
if @receiver.send(:<=, expected) == @match
pass!
else
fail!("<=")
OPERATORS.each do |op|
define_method(op) do |expected|
@expected = expected
(@receiver.send(op,expected) ? true : false) == @match ? pass! : fail!(op)
end
end

Expand Down

0 comments on commit 79bd961

Please sign in to comment.