Skip to content

Commit

Permalink
Add description generation for any Has matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Mange authored and dchelimsky committed Mar 21, 2011
1 parent e14e87f commit a7b702d
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 59 deletions.
26 changes: 15 additions & 11 deletions lib/rspec/matchers/has.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,35 +1,39 @@
module RSpec module RSpec
module Matchers module Matchers

class Has class Has

def initialize(expected, *args) def initialize(expected, *args)
@expected, @args = expected, args @expected, @args = expected, args
end end

def matches?(actual) def matches?(actual)
actual.__send__(predicate(@expected), *@args) actual.__send__(predicate(@expected), *@args)
end end

def failure_message_for_should def failure_message_for_should
"expected ##{predicate(@expected)}(#{@args[0].inspect}) to return true, got false" "expected ##{predicate(@expected)}(#{@args[0].inspect}) to return true, got false"
end end

def failure_message_for_should_not def failure_message_for_should_not
"expected ##{predicate(@expected)}(#{@args[0].inspect}) to return false, got true" "expected ##{predicate(@expected)}(#{@args[0].inspect}) to return false, got true"
end end

def description def description
"have key #{@args[0].inspect}" [method_description(@expected), args_description(@args)].compact.join(' ')
end end

private private

def predicate(sym) def predicate(sym)
"#{sym.to_s.sub("have_","has_")}?".to_sym "#{sym.to_s.sub("have_","has_")}?".to_sym
end end


def method_description(method)
method.to_s.gsub('_', ' ')
end

def args_description(args)
return nil if args.empty?
args.map { |arg| arg.inspect }.join(', ')
end
end end

end end
end end
66 changes: 41 additions & 25 deletions spec/rspec/matchers/description_generation_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,42 +9,42 @@
"this".should == "this" "this".should == "this"
RSpec::Matchers.generated_description.should == "should == \"this\"" RSpec::Matchers.generated_description.should == "should == \"this\""
end end

it "should not == expected" do it "should not == expected" do
"this".should_not == "that" "this".should_not == "that"
RSpec::Matchers.generated_description.should == "should not == \"that\"" RSpec::Matchers.generated_description.should == "should not == \"that\""
end end

it "should be empty (arbitrary predicate)" do it "should be empty (arbitrary predicate)" do
[].should be_empty [].should be_empty
RSpec::Matchers.generated_description.should == "should be empty" RSpec::Matchers.generated_description.should == "should be empty"
end end

it "should not be empty (arbitrary predicate)" do it "should not be empty (arbitrary predicate)" do
[1].should_not be_empty [1].should_not be_empty
RSpec::Matchers.generated_description.should == "should not be empty" RSpec::Matchers.generated_description.should == "should not be empty"
end end

it "should be true" do it "should be true" do
true.should be_true true.should be_true
RSpec::Matchers.generated_description.should == "should be true" RSpec::Matchers.generated_description.should == "should be true"
end end

it "should be false" do it "should be false" do
false.should be_false false.should be_false
RSpec::Matchers.generated_description.should == "should be false" RSpec::Matchers.generated_description.should == "should be false"
end end

it "should be nil" do it "should be nil" do
nil.should be_nil nil.should be_nil
RSpec::Matchers.generated_description.should == "should be nil" RSpec::Matchers.generated_description.should == "should be nil"
end end

it "should be > n" do it "should be > n" do
5.should be > 3 5.should be > 3
RSpec::Matchers.generated_description.should == "should be > 3" RSpec::Matchers.generated_description.should == "should be > 3"
end end

it "should be predicate arg1, arg2 and arg3" do it "should be predicate arg1, arg2 and arg3" do
5.0.should be_between(0,10) 5.0.should be_between(0,10)
RSpec::Matchers.generated_description.should == "should be between 0 and 10" RSpec::Matchers.generated_description.should == "should be between 0 and 10"
Expand All @@ -55,42 +55,58 @@
expected.should equal(expected) expected.should equal(expected)
RSpec::Matchers.generated_description.should == "should equal \"expected\"" RSpec::Matchers.generated_description.should == "should equal \"expected\""
end end

it "should_not equal" do it "should_not equal" do
5.should_not equal(37) 5.should_not equal(37)
RSpec::Matchers.generated_description.should == "should not equal 37" RSpec::Matchers.generated_description.should == "should not equal 37"
end end

it "should eql" do it "should eql" do
"string".should eql("string") "string".should eql("string")
RSpec::Matchers.generated_description.should == "should eql \"string\"" RSpec::Matchers.generated_description.should == "should eql \"string\""
end end

it "should not eql" do it "should not eql" do
"a".should_not eql(:a) "a".should_not eql(:a)
RSpec::Matchers.generated_description.should == "should not eql :a" RSpec::Matchers.generated_description.should == "should not eql :a"
end end

it "should have_key" do it "should have_key" do
{:a => "a"}.should have_key(:a) {:a => "a"}.should have_key(:a)
RSpec::Matchers.generated_description.should == "should have key :a" RSpec::Matchers.generated_description.should == "should have key :a"
end end


it "should have_some_method" do
object = Object.new
def object.has_eyes_closed?; true; end

object.should have_eyes_closed
RSpec::Matchers.generated_description.should == 'should have eyes closed'
end

it "should have_some_method(args*)" do
object = Object.new
def object.has_taste_for?(*args); true; end

object.should have_taste_for("blood", "victory")
RSpec::Matchers.generated_description.should == 'should have taste for "blood", "victory"'
end

it "should have n items" do it "should have n items" do
team.should have(3).players team.should have(3).players
RSpec::Matchers.generated_description.should == "should have 3 players" RSpec::Matchers.generated_description.should == "should have 3 players"
end end

it "should have at least n items" do it "should have at least n items" do
team.should have_at_least(2).players team.should have_at_least(2).players
RSpec::Matchers.generated_description.should == "should have at least 2 players" RSpec::Matchers.generated_description.should == "should have at least 2 players"
end end

it "should have at most n items" do it "should have at most n items" do
team.should have_at_most(4).players team.should have_at_most(4).players
RSpec::Matchers.generated_description.should == "should have at most 4 players" RSpec::Matchers.generated_description.should == "should have at most 4 players"
end end

it "should include" do it "should include" do
[1,2,3].should include(3) [1,2,3].should include(3)
RSpec::Matchers.generated_description.should == "should include 3" RSpec::Matchers.generated_description.should == "should include 3"
Expand All @@ -100,42 +116,42 @@
[1,2,3].should =~ [1,2,3] [1,2,3].should =~ [1,2,3]
RSpec::Matchers.generated_description.should == "should contain exactly 1, 2 and 3" RSpec::Matchers.generated_description.should == "should contain exactly 1, 2 and 3"
end end

it "should match" do it "should match" do
"this string".should match(/this string/) "this string".should match(/this string/)
RSpec::Matchers.generated_description.should == "should match /this string/" RSpec::Matchers.generated_description.should == "should match /this string/"
end end

it "should raise_error" do it "should raise_error" do
lambda { raise }.should raise_error lambda { raise }.should raise_error
RSpec::Matchers.generated_description.should == "should raise Exception" RSpec::Matchers.generated_description.should == "should raise Exception"
end end

it "should raise_error with type" do it "should raise_error with type" do
lambda { raise }.should raise_error(RuntimeError) lambda { raise }.should raise_error(RuntimeError)
RSpec::Matchers.generated_description.should == "should raise RuntimeError" RSpec::Matchers.generated_description.should == "should raise RuntimeError"
end end

it "should raise_error with type and message" do it "should raise_error with type and message" do
lambda { raise "there was an error" }.should raise_error(RuntimeError, "there was an error") lambda { raise "there was an error" }.should raise_error(RuntimeError, "there was an error")
RSpec::Matchers.generated_description.should == "should raise RuntimeError with \"there was an error\"" RSpec::Matchers.generated_description.should == "should raise RuntimeError with \"there was an error\""
end end

it "should respond_to" do it "should respond_to" do
[].should respond_to(:insert) [].should respond_to(:insert)
RSpec::Matchers.generated_description.should == "should respond to #insert" RSpec::Matchers.generated_description.should == "should respond to #insert"
end end

it "should throw symbol" do it "should throw symbol" do
lambda { throw :what_a_mess }.should throw_symbol lambda { throw :what_a_mess }.should throw_symbol
RSpec::Matchers.generated_description.should == "should throw a Symbol" RSpec::Matchers.generated_description.should == "should throw a Symbol"
end end

it "should throw symbol (with named symbol)" do it "should throw symbol (with named symbol)" do
lambda { throw :what_a_mess }.should throw_symbol(:what_a_mess) lambda { throw :what_a_mess }.should throw_symbol(:what_a_mess)
RSpec::Matchers.generated_description.should == "should throw :what_a_mess" RSpec::Matchers.generated_description.should == "should throw :what_a_mess"
end end

def team def team
Class.new do Class.new do
def players def players
Expand All @@ -152,7 +168,7 @@ def matches?(ignore); true; end
def failure_message; ""; end def failure_message; ""; end
end.new end.new
end end

it "provides a helpful message when used in a string-less example block" do it "provides a helpful message when used in a string-less example block" do
5.should matcher 5.should matcher
RSpec::Matchers.generated_description.should =~ /When you call.*description method/m RSpec::Matchers.generated_description.should =~ /When you call.*description method/m
Expand Down
4 changes: 2 additions & 2 deletions spec/rspec/matchers/has_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def has_foo?
Object.new.should have_key(:a) Object.new.should have_key(:a)
}.should raise_error(NoMethodError) }.should raise_error(NoMethodError)
end end

it "reraises an exception thrown in #has_sym?(*args)" do it "reraises an exception thrown in #has_sym?(*args)" do
o = Object.new o = Object.new
def o.has_sym?(*args) def o.has_sym?(*args)
Expand Down Expand Up @@ -60,7 +60,7 @@ def has_foo?
Object.new.should have_key(:a) Object.new.should have_key(:a)
}.should raise_error(NoMethodError) }.should raise_error(NoMethodError)
end end

it "reraises an exception thrown in #has_sym?(*args)" do it "reraises an exception thrown in #has_sym?(*args)" do
o = Object.new o = Object.new
def o.has_sym?(*args) def o.has_sym?(*args)
Expand Down
Loading

0 comments on commit a7b702d

Please sign in to comment.