Permalink
Browse files

Add description generation for any Has matcher

  • Loading branch information...
1 parent e14e87f commit a7b702d759de587ec169041b52eb557125aace26 @Mange Mange committed with dchelimsky Mar 21, 2011
@@ -1,35 +1,39 @@
module RSpec
module Matchers
-
class Has
-
def initialize(expected, *args)
@expected, @args = expected, args
end
-
+
def matches?(actual)
actual.__send__(predicate(@expected), *@args)
end
-
+
def failure_message_for_should
"expected ##{predicate(@expected)}(#{@args[0].inspect}) to return true, got false"
end
-
+
def failure_message_for_should_not
"expected ##{predicate(@expected)}(#{@args[0].inspect}) to return false, got true"
end
-
+
def description
- "have key #{@args[0].inspect}"
+ [method_description(@expected), args_description(@args)].compact.join(' ')
end
-
+
private
-
def predicate(sym)
"#{sym.to_s.sub("have_","has_")}?".to_sym
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
@@ -9,42 +9,42 @@
"this".should == "this"
RSpec::Matchers.generated_description.should == "should == \"this\""
end
-
+
it "should not == expected" do
"this".should_not == "that"
RSpec::Matchers.generated_description.should == "should not == \"that\""
end
-
+
it "should be empty (arbitrary predicate)" do
[].should be_empty
RSpec::Matchers.generated_description.should == "should be empty"
end
-
+
it "should not be empty (arbitrary predicate)" do
[1].should_not be_empty
RSpec::Matchers.generated_description.should == "should not be empty"
end
-
+
it "should be true" do
true.should be_true
RSpec::Matchers.generated_description.should == "should be true"
end
-
+
it "should be false" do
false.should be_false
RSpec::Matchers.generated_description.should == "should be false"
end
-
+
it "should be nil" do
nil.should be_nil
RSpec::Matchers.generated_description.should == "should be nil"
end
-
+
it "should be > n" do
5.should be > 3
RSpec::Matchers.generated_description.should == "should be > 3"
end
-
+
it "should be predicate arg1, arg2 and arg3" do
5.0.should be_between(0,10)
RSpec::Matchers.generated_description.should == "should be between 0 and 10"
@@ -55,42 +55,58 @@
expected.should equal(expected)
RSpec::Matchers.generated_description.should == "should equal \"expected\""
end
-
+
it "should_not equal" do
5.should_not equal(37)
RSpec::Matchers.generated_description.should == "should not equal 37"
end
-
+
it "should eql" do
"string".should eql("string")
RSpec::Matchers.generated_description.should == "should eql \"string\""
end
-
+
it "should not eql" do
"a".should_not eql(:a)
RSpec::Matchers.generated_description.should == "should not eql :a"
end
-
+
it "should have_key" do
{:a => "a"}.should have_key(:a)
RSpec::Matchers.generated_description.should == "should have key :a"
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
team.should have(3).players
RSpec::Matchers.generated_description.should == "should have 3 players"
end
-
+
it "should have at least n items" do
team.should have_at_least(2).players
RSpec::Matchers.generated_description.should == "should have at least 2 players"
end
-
+
it "should have at most n items" do
team.should have_at_most(4).players
RSpec::Matchers.generated_description.should == "should have at most 4 players"
end
-
+
it "should include" do
[1,2,3].should include(3)
RSpec::Matchers.generated_description.should == "should include 3"
@@ -100,42 +116,42 @@
[1,2,3].should =~ [1,2,3]
RSpec::Matchers.generated_description.should == "should contain exactly 1, 2 and 3"
end
-
+
it "should match" do
"this string".should match(/this string/)
RSpec::Matchers.generated_description.should == "should match /this string/"
end
-
+
it "should raise_error" do
lambda { raise }.should raise_error
RSpec::Matchers.generated_description.should == "should raise Exception"
end
-
+
it "should raise_error with type" do
lambda { raise }.should raise_error(RuntimeError)
RSpec::Matchers.generated_description.should == "should raise RuntimeError"
end
-
+
it "should raise_error with type and message" do
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\""
end
-
+
it "should respond_to" do
[].should respond_to(:insert)
RSpec::Matchers.generated_description.should == "should respond to #insert"
end
-
+
it "should throw symbol" do
lambda { throw :what_a_mess }.should throw_symbol
RSpec::Matchers.generated_description.should == "should throw a Symbol"
end
-
+
it "should throw symbol (with named symbol)" do
lambda { throw :what_a_mess }.should throw_symbol(:what_a_mess)
RSpec::Matchers.generated_description.should == "should throw :what_a_mess"
end
-
+
def team
Class.new do
def players
@@ -152,7 +168,7 @@ def matches?(ignore); true; end
def failure_message; ""; end
end.new
end
-
+
it "provides a helpful message when used in a string-less example block" do
5.should matcher
RSpec::Matchers.generated_description.should =~ /When you call.*description method/m
@@ -26,7 +26,7 @@ def has_foo?
Object.new.should have_key(:a)
}.should raise_error(NoMethodError)
end
-
+
it "reraises an exception thrown in #has_sym?(*args)" do
o = Object.new
def o.has_sym?(*args)
@@ -60,7 +60,7 @@ def has_foo?
Object.new.should have_key(:a)
}.should raise_error(NoMethodError)
end
-
+
it "reraises an exception thrown in #has_sym?(*args)" do
o = Object.new
def o.has_sym?(*args)
Oops, something went wrong.

0 comments on commit a7b702d

Please sign in to comment.