Permalink
Browse files

Fixed bug caught by cucumber scenarios (CUCUMBER FTW)

  • Loading branch information...
dchelimsky committed Mar 3, 2009
1 parent e32b7b4 commit e14eb28cfbe4ee85b77f88b078275c1dad4f617d
View
@@ -1,2 +1,2 @@
-default: --require features features
+default: --require features features/matchers
pending: --require features features-pending
@@ -7,8 +7,8 @@ Feature: custom matcher shortcut
Scenario: create a matcher with default messages
Given the following spec:
"""
- Spec::Matchers.create :be_a_multiple_of do |actual, expected|
- match do
+ Spec::Matchers.create :be_a_multiple_of do |expected|
+ match do |actual|
actual % expected == 0
end
end
@@ -42,16 +42,16 @@ Feature: custom matcher shortcut
And the stdout should match "4 examples, 2 failures"
And the stdout should match "expected 9 to be a multiple of 4"
- And the stdout should match "expected 9 to not be a multiple of 3"
+ And the stdout should match "expected 9 not to be a multiple of 3"
Scenario: override the failure_message_for(:should)
Given the following spec:
"""
- Spec::Matchers.create :be_a_multiple_of do |actual, expected|
- match do
+ Spec::Matchers.create :be_a_multiple_of do |expected|
+ match do |actual|
actual % expected == 0
end
- failure_message_for(:should) do
+ failure_message_for(:should) do |actual|
"expected that #{actual} would be a multiple of #{expected}"
end
end
@@ -69,11 +69,11 @@ Feature: custom matcher shortcut
Scenario: override the failure_message_for(:should_not)
Given the following spec:
"""
- Spec::Matchers.create :be_a_multiple_of do |actual, expected|
- match do
+ Spec::Matchers.create :be_a_multiple_of do |expected|
+ match do |actual|
actual % expected == 0
end
- failure_message_for(:should_not) do
+ failure_message_for(:should_not) do |actual|
"expected that #{actual} would not be a multiple of #{expected}"
end
end
@@ -91,8 +91,8 @@ Feature: custom matcher shortcut
Scenario: override the description
Given the following spec:
"""
- Spec::Matchers.create :be_a_multiple_of do |actual, expected|
- match do
+ Spec::Matchers.create :be_a_multiple_of do |expected|
+ match do |actual|
actual % expected == 0
end
description do
View
@@ -2,10 +2,8 @@ module Spec
module DSL
module Matchers
def create(name, &block_passed_to_create)
- self.class.class_eval do
- define_method name do |expected|
- Spec::Matchers::Matcher.new name, expected, &block_passed_to_create
- end
+ define_method name do |expected|
+ Spec::Matchers::Matcher.new name, expected, &block_passed_to_create
end
end
end
@@ -19,6 +19,7 @@ def initialize(name, expected=nil, &block_passed_to_init)
def matches?(actual)
@actual = actual
instance_exec @expected, &@block
+ instance_exec actual, &@match_block
end
def description(&block)
@@ -34,7 +35,7 @@ def negative_failure_message
end
def match(&block_passed_to_match)
- instance_exec @actual, &block_passed_to_match
+ @match_block = block_passed_to_match
end
def failure_message_for(should_or_should_not, &block)
@@ -0,0 +1,25 @@
+require File.dirname(__FILE__) + '/../../spec_helper'
+
+module Spec
+ module DSL
+ module Matchers
+ describe "#create" do
+ it "creates a method that initializes a new matcher with the submitted name and expected arg" do
+ # FIXME - this expects new to be called, but we need something
+ # more robust - that expects new to be called with a specific
+ # block (lambda, proc, whatever)
+ mod = Module.new
+ mod.extend Spec::DSL::Matchers
+ mod.create(:foo)
+
+ obj = Object.new
+ obj.extend mod
+
+ Spec::Matchers::Matcher.should_receive(:new).with(:foo, 3)
+
+ obj.foo(3)
+ end
+ end
+ end
+ end
+end
@@ -1,29 +1,9 @@
require File.dirname(__FILE__) + '/../../spec_helper'
-
-
-
module Spec
module Matchers
- describe "#create" do
- context "telling #create what I want it to do" do
- it "returns true if I say so" do
- # FIXME - this expects new to be called, but we need something
- # more robust - that expects new to be called with a specific
- # block (lambda, proc, whatever)
- mod = Module.new
- mod.extend Spec::DSL::Matchers
- mod.create(:foo)
-
- Spec::Matchers::Matcher.should_receive(:new).with(:foo, 3)
-
- mod.foo(3)
- end
- end
- end
-
describe Spec::Matchers::Matcher do
- context "defaults" do
+ context "without overrides" do
before(:each) do
@matcher = Spec::Matchers::Matcher.new(:be_a_multiple_of, 3) do |multiple|
match do |actual|
@@ -48,48 +28,48 @@ module Matchers
end
end
- context "overrides" do
- it "overrides the description" do
- matcher = Spec::Matchers::Matcher.new(:be_a_multiple_of, 3) do |multiple|
+ context "with overrides" do
+ before(:each) do
+ @matcher = Spec::Matchers::Matcher.new(:be_boolean, true) do |boolean|
match do |actual|
- actual % multiple == 0
+ actual
end
description do
- "be a multiple of #{multiple}, dude"
+ "be the boolean #{boolean}"
+ end
+ failure_message_for(:should) do |actual|
+ "expected #{actual} to be the boolean #{boolean}"
+ end
+ failure_message_for(:should_not) do |actual|
+ "expected #{actual} not to be the boolean #{boolean}"
end
end
- matcher.matches?(0)
- matcher.description.should == "be a multiple of 3, dude"
+ end
+
+ it "does not hide result of match block when true" do
+ @matcher.matches?(true).should be_true
+ end
+
+ it "does not hide result of match block when false" do
+ @matcher.matches?(false).should be_false
+ end
+
+ it "overrides the description" do
+ @matcher.matches?(true)
+ @matcher.description.should == "be the boolean true"
end
it "overrides the failure message for #should" do
- matcher = Spec::Matchers::Matcher.new(:be_a_multiple_of, 3) do |multiple|
- match do |actual|
- actual % multiple == 0
- end
- failure_message_for(:should) do |actual|
- "expected #{actual} to be a multiple of #{multiple}, dude"
- end
- end
- matcher.matches?(8)
- matcher.failure_message.should == "expected 8 to be a multiple of 3, dude"
+ @matcher.matches?(false)
+ @matcher.failure_message.should == "expected false to be the boolean true"
end
it "overrides the failure message for #should_not" do
- matcher = Spec::Matchers::Matcher.new(:be_a_multiple_of, 3) do |multiple|
- match do |actual|
- actual % multiple == 0
- end
- failure_message_for(:should_not) do |actual|
- "expected #{actual} not to be a multiple of #{multiple}, dude"
- end
- end
- matcher.matches?(9)
- matcher.negative_failure_message.should == "expected 9 not to be a multiple of 3, dude"
+ @matcher.matches?(true)
+ @matcher.negative_failure_message.should == "expected true not to be the boolean true"
end
end
-
context "#new" do
it "passes matches? arg to match block" do
matcher = Spec::Matchers::Matcher.new(:ignore) do
@@ -109,6 +89,9 @@ module Matchers
matcher.matches?(5).should be_true
end
end
+
+ context "matching with overrides" do
+ end
end
end
end

0 comments on commit e14eb28

Please sign in to comment.