Permalink
Browse files

added support for 0 to n args in matcher DSL

  • Loading branch information...
1 parent 884d751 commit d2efb4bc2c39c8ff8d7d1bb4cd6bc32b5ca3d001 @dchelimsky committed Mar 16, 2009
View
6 History.txt
@@ -1,3 +1,9 @@
+=== dev
+
+* enhancments
+
+ * added support for 0 to n args in matcher DSL
+
=== Version 1.2.0
WARNINGS:
View
40 features/matchers/create_matcher.feature
@@ -113,3 +113,43 @@ Feature: custom matcher shortcut
And the stdout should match "2 examples, 0 failures"
And the stdout should match "should be multiple of 3"
And the stdout should match "should not be multiple of 4"
+
+ Scenario: with no args
+ Given the following spec:
+ """
+ Spec::Matchers.create :have_7_fingers do
+ match do |thing|
+ thing.fingers.length == 7
+ end
+ end
+
+ class Thing
+ def fingers; (1..7).collect {"finger"}; end
+ end
+
+ describe Thing do
+ it {should have_7_fingers}
+ end
+ """
+ When I run it with the spec command --format specdoc
+ Then the exit code should be 0
+ And the stdout should match "1 example, 0 failures"
+ And the stdout should match "should have 7 fingers"
+
+ Scenario: with multiple args
+ Given the following spec:
+ """
+ Spec::Matchers.create :be_the_sum_of do |a,b,c,d|
+ match do |sum|
+ a + b + c + d == sum
+ end
+ end
+
+ describe 10 do
+ it {should be_the_sum_of(1,2,3,4)}
+ end
+ """
+ When I run it with the spec command --format specdoc
+ Then the exit code should be 0
+ And the stdout should match "1 example, 0 failures"
+ And the stdout should match "should be the sum of 1, 2, 3, and 4"
View
4 lib/spec/dsl/matchers.rb
@@ -2,8 +2,8 @@ module Spec
module DSL
module Matchers
def create(name, &block_passed_to_create)
- define_method name do |expected|
- Spec::Matchers::Matcher.new name, expected, &block_passed_to_create
+ define_method name do |*expected|
+ Spec::Matchers::Matcher.new name, *expected, &block_passed_to_create
end
end
end
View
25 lib/spec/matchers/matcher.rb
@@ -1,21 +1,21 @@
module Spec
module Matchers
class Matcher
- def initialize(name, expected=nil, &block_passed_to_init)
+ def initialize(name, *expected, &declarations)
@name = name
@expected = expected
- @block = block_passed_to_init
+ @declarations = declarations
@messages = {
- :description => lambda {"#{name_to_sentence} #{expected}"},
- :failure_message_for_should => lambda {|actual| "expected #{actual} to #{name_to_sentence} #{expected}"},
- :failure_message_for_should_not => lambda {|actual| "expected #{actual} not to #{name_to_sentence} #{expected}"}
+ :description => lambda {"#{name_to_sentence}#{expected_to_sentence}"},
+ :failure_message_for_should => lambda {|actual| "expected #{actual} to #{name_to_sentence}#{expected_to_sentence}"},
+ :failure_message_for_should_not => lambda {|actual| "expected #{actual} not to #{name_to_sentence}#{expected_to_sentence}"}
}
end
def matches?(actual)
@actual = actual
- instance_exec @expected, &@block
- instance_exec @actual, &@match_block
+ instance_exec(*@expected, &@declarations)
+ instance_exec(@actual, &@match_block)
end
def description(&block)
@@ -45,6 +45,17 @@ def cache_or_call_cached(key, actual=nil, &block)
def name_to_sentence
@name_to_sentence ||= @name.to_s.gsub(/_/,' ')
end
+
+ def expected_to_sentence
+ case @expected.length
+ when 0
+ ""
+ when 1
+ " #{@expected[0]}"
+ else
+ " #{@expected[0...-1].join(', ')}, and #{@expected[-1]}"
+ end
+ end
end
end
View
54 spec/spec/matchers/matcher_spec.rb
@@ -90,8 +90,60 @@ module Matchers
end
end
- context "matching with overrides" do
+ context "with no args" do
+ before(:each) do
+ @matcher = Spec::Matchers::Matcher.new(:matcher_name) do
+ match do |actual|
+ actual == 5
+ end
+ end
+ end
+
+ it "matches" do
+ @matcher.matches?(5).should be_true
+ end
+
+ it "describes" do
+ @matcher.description.should == "matcher name"
+ end
end
+
+ context "with 1 arg" do
+ before(:each) do
+ @matcher = Spec::Matchers::Matcher.new(:matcher_name, 1) do |expected|
+ match do |actual|
+ actual == 5 && expected == 1
+ end
+ end
+ end
+
+ it "matches" do
+ @matcher.matches?(5).should be_true
+ end
+
+ it "describes" do
+ @matcher.description.should == "matcher name 1"
+ end
+ end
+
+ context "with multiple args" do
+ before(:each) do
+ @matcher = Spec::Matchers::Matcher.new(:matcher_name, 1, 2, 3, 4) do |a,b,c,d|
+ match do |sum|
+ a + b + c + d == sum
+ end
+ end
+ end
+
+ it "matches" do
+ @matcher.matches?(10).should be_true
+ end
+
+ it "describes" do
+ @matcher.description.should == "matcher name 1, 2, 3, and 4"
+ end
+ end
+
end
end
end

0 comments on commit d2efb4b

Please sign in to comment.