Permalink
Browse files

Refactored modals.

Signed-off-by: Jeremy McAnally <jeremymcanally@gmail.com>
  • Loading branch information...
1 parent aa01e90 commit 822a3ecd61fa38bf0e54e101f39669c37fb1969d @mhennemeyer mhennemeyer committed with Feb 10, 2009
View
@@ -4,6 +4,7 @@
require 'rubygems'
require 'test/unit'
+require 'matchy/expectation_builder'
require 'matchy/modals'
require 'matchy/version'
require 'matchy/matcher_builder'
@@ -15,6 +16,17 @@
require 'matchy/built_in/operator_expectations'
require 'matchy/built_in/change_expectations'
+# Evil test/unit hack.
+# Track the current testcase and
+# provide it to the operator matchers.
+class Test::Unit::TestCase
+ alias_method :old_run_method_aliased_by_matchy_300, :run
+ def run(whatever, *args, &block)
+ $current_test_case = self
+ old_run_method_aliased_by_matchy_300(whatever, *args, &block)
+ end
+end
+
Test::Unit::TestCase.send(:include, Matchy::Expectations::TestCaseExtensions)
include Matchy::DefMatcher
@@ -23,7 +23,7 @@ def initialize(receiver, match)
protected
def pass!
- assert true
+ defined?($current_test_case) ? $current_test_case.assert(true) : (assert true)
end
def fail!(operator)
@@ -0,0 +1,19 @@
+module Matchy
+ module ExpectationBuilder
+ def self.build_expectation(type, exp, obj)
+ if exp
+ match_expectation(exp, (type == :should), obj)
+ else
+ return Matchy::Expectations::OperatorExpectation.new(obj, (type == :should))
+ end
+ end
+
+ def self.match_expectation(expectation, match, obj)
+ if expectation.matches?(obj) != match
+ expectation.fail!(match)
+ else
+ expectation.pass!(match)
+ end
+ end
+ end
+end
@@ -40,7 +40,6 @@ def pass!(which)
alias_method :failure_message, :positive_msg
alias_method :negative_failure_message, :negative_msg
end
- p self
Class.new(&body).new(match_block, self)
end
end
View
@@ -9,11 +9,7 @@ module Modals
# lambda { raise "u r doomed" }.should raise_error
#
def should(expectation = nil)
- if expectation
- match_expectation(expectation, true)
- else
- return Matchy::Expectations::OperatorExpectation.new(self, true)
- end
+ Matchy::ExpectationBuilder.build_expectation(:should, expectation, self)
end
alias :will :should
@@ -27,24 +23,11 @@ def should(expectation = nil)
# lambda { "savd bai da bell" }.should_not raise_error
#
def should_not(expectation = nil)
- if expectation
- match_expectation(expectation, false)
- else
- return Matchy::Expectations::OperatorExpectation.new(self, false)
- end
+ Matchy::ExpectationBuilder.build_expectation(:should_not, expectation, self)
end
alias :will_not :should_not
alias :wont :should_not
-
- protected
- def match_expectation(expectation, match)
- if expectation.matches?(self) != match
- expectation.fail!(match)
- else
- expectation.pass!(match)
- end
- end
end
end
View
@@ -0,0 +1,7 @@
+dir = File.dirname(__FILE__)
+Dir[File.expand_path("#{dir}/*.rb")].uniq.each do |file|
+ if file =~ /\/test_\w+\.rb$/
+ puts file
+ require file
+ end
+end
@@ -0,0 +1,28 @@
+require File.dirname(__FILE__) + '/test_helper.rb'
+
+class TestExpectationBuilder < Test::Unit::TestCase
+
+ def setup
+ @obj = Object.new
+ end
+
+ def test_should
+ exp = Matchy::ExpectationBuilder.build_expectation(:should, nil, @obj)
+ exp.send(:==, @obj)
+ end
+
+ def test_should_fails
+ expect_1 = Matchy::ExpectationBuilder.build_expectation(:should, nil, 1)
+ lambda {expect_1.send(:==, 2)}.should raise_error
+ end
+
+ def test_should_not
+ exp = Matchy::ExpectationBuilder.build_expectation(:should_not, nil, @obj)
+ exp.send(:==, 1)
+ end
+
+ def test_should_not_fails
+ expect_not_1 = Matchy::ExpectationBuilder.build_expectation(:should_not, nil, 1)
+ lambda {expect_not_1.send(:==, 1)}.should raise_error
+ end
+end

0 comments on commit 822a3ec

Please sign in to comment.