Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Generated descriptions in the indicative

  • Loading branch information...
commit c44fee682dc17f4f3ffd33a0e55510b64e0cbe6f 1 parent 48e8fe5
@rentalcustard authored
View
14 features/implicit_docstrings.feature
@@ -20,9 +20,9 @@ Feature: implicit docstrings
When I run `rspec ./implicit_docstrings_spec.rb -fdoc`
- Then the output should contain "should be < 5"
- And the output should contain "should include 2"
- And the output should contain "should respond to #size"
+ Then the output should contain "is < 5"
+ And the output should contain "includes 2"
+ And the output should contain "responds to #size"
Scenario: run failing examples
Given a file named "failing_implicit_docstrings_spec.rb" with:
@@ -46,7 +46,7 @@ Feature: implicit docstrings
When I run `rspec ./failing_implicit_docstrings_spec.rb -fdoc`
- Then the output should contain "should equal 2"
- And the output should contain "should be > 5"
- And the output should contain "should include 4"
- And the output should contain "should not respond to #size"
+ Then the output should contain "equals 2"
+ And the output should contain "is > 5"
+ And the output should contain "includes 4"
+ And the output should contain "does not respond to #size"
View
15 lib/rspec/matchers/be.rb
@@ -53,6 +53,11 @@ def description
"be"
end
+ def docstrings
+ {:positive => "is",
+ :negative => "is not"}
+ end
+
[:==, :<, :<=, :>=, :>, :===].each do |operator|
define_method operator do |operand|
BeComparedTo.new(operand, operator)
@@ -114,6 +119,11 @@ def description
"be #{@operator} #{expected_to_sentence}#{args_to_sentence}"
end
+ def docstrings
+ {:positive => "is #{@operator} #{expected_to_sentence}#{args_to_sentence}",
+ :negative => "is not #{@operator} #{expected_to_sentence}#{args_to_sentence}"}
+ end
+
end
class BePredicate < Be
@@ -151,6 +161,11 @@ def description
"#{prefix_to_sentence}#{expected_to_sentence}#{args_to_sentence}"
end
+ def docstrings
+ {:positive => "is #{expected_to_sentence}#{args_to_sentence}",
+ :negative => "is not #{expected_to_sentence}#{args_to_sentence}"}
+ end
+
private
def predicate
View
4 lib/rspec/matchers/eq.rb
@@ -43,6 +43,10 @@ def eq(expected)
description do
"== #{_expected_}"
end
+
+ docstrings do
+ {:positive => "== #{_expected_}"}
+ end
end
end
end
View
5 lib/rspec/matchers/equal.rb
@@ -47,6 +47,11 @@ def inspect_object(o)
MESSAGE
end
+
+ docstrings do
+ {:positive => "equals #{_expected_.inspect}",
+ :negative => "does not equal #{_expected_.inspect}"}
+ end
end
end
end
View
5 lib/rspec/matchers/has.rb
@@ -21,6 +21,11 @@ def description
[method_description(@expected), args_description(@args)].compact.join(' ')
end
+ def docstrings
+ {:positive => description.sub(/have/, "has"),
+ :negative => "not #{description}"}
+ end
+
private
def predicate(sym)
"#{sym.to_s.sub("have_","has_")}?".to_sym
View
5 lib/rspec/matchers/have.rb
@@ -66,6 +66,11 @@ def failure_message_for_should_not
def description
"have #{relative_expectation} #{@collection_name}"
end
+
+ def docstrings
+ {:positive => "has #{relative_expectation} #{@collection_name}",
+ :negative => "does not have #{relative_expectation} #{@collection_name}"}
+ end
def respond_to?(sym)
@expected.respond_to?(sym) || super
View
4 lib/rspec/matchers/match_array.rb
@@ -31,6 +31,10 @@ def description
"contain exactly #{_pretty_print(@expected)}"
end
+ def docstrings
+ {:positive => "contains exactly #{_pretty_print(@expected)}"}
+ end
+
private
def safe_sort(array)
View
10 lib/rspec/matchers/matcher.rb
@@ -15,6 +15,8 @@ def initialize(name, *expected, &declarations)
@match_for_should_not_block = nil
@messages = {
+ :docstrings => lambda { {:positive => "#{name_to_indicative_sentence}#{expected_to_sentence}",
+ :negative => "does not #{name_to_sentence}#{expected_to_sentence}".sub("not is", "is not") } },
:description => lambda {"#{name_to_sentence}#{expected_to_sentence}"},
:failure_message_for_should => lambda {|actual| "expected #{actual.inspect} to #{name_to_sentence}#{expected_to_sentence}"},
:failure_message_for_should_not => lambda {|actual| "expected #{actual.inspect} not to #{name_to_sentence}#{expected_to_sentence}"}
@@ -91,6 +93,10 @@ def description(&block)
cache_or_call_cached(:description, &block)
end
+ def docstrings(&block)
+ cache_or_call_cached(:docstrings, &block)
+ end
+
#Used internally by objects returns by +should+ and +should_not+.
def diffable?
@diffable
@@ -151,6 +157,10 @@ def name_to_sentence
split_words(@name)
end
+ def name_to_indicative_sentence
+ to_indicative(name_to_sentence)
+ end
+
def expected_to_sentence
to_sentence(@expected)
end
View
5 lib/rspec/matchers/operator_matcher.rb
@@ -51,6 +51,11 @@ def fail_with_message(message)
def description
"#{@operator} #{@expected.inspect}"
end
+
+ def docstrings
+ {:positive => description,
+ :negative => "does not #{description}"}
+ end
private
View
14 lib/rspec/matchers/pretty.rb
@@ -19,6 +19,18 @@ def to_sentence(words)
end
end
+ def to_indicative(infinitive)
+ changed = false
+ { /be/ => "is",
+ /match/ => "matches"}.each_pair do |pattern, substitution|
+ if infinitive =~ pattern
+ infinitive.sub!(pattern, substitution)
+ changed = true
+ end
+ end
+ changed ? infinitive : infinitive.to_s + 's'
+ end
+
def _pretty_print(array)
result = ""
array.each_with_index do |item, index|
@@ -34,4 +46,4 @@ def _pretty_print(array)
end
end
end
-end
+end
View
5 lib/rspec/matchers/raise_error.rb
@@ -67,6 +67,11 @@ def failure_message_for_should_not
def description
"raise #{expected_error}"
end
+
+ def docstrings
+ {:positive => "raises #{expected_error}",
+ :negative => "does not raise #{expected_error}"}
+ end
private
def expected_error
View
5 lib/rspec/matchers/respond_to.rb
@@ -27,6 +27,11 @@ def description
"respond to #{pp_names}#{with_arity}"
end
+ def docstrings
+ {:positive => "responds to #{pp_names}#{with_arity}",
+ :negative => "does not respond to #{pp_names}#{with_arity}"}
+ end
+
def with(n)
@expected_arity = n
self
View
5 lib/rspec/matchers/throw_symbol.rb
@@ -63,6 +63,11 @@ def failure_message_for_should_not
def description
"throw #{expected}"
end
+
+ def docstrings
+ {:positive => "throws #{expected}",
+ :negative => "does not throw #{expected}"}
+ end
private
View
66 spec/rspec/matchers/description_generation_spec.rb
@@ -7,73 +7,78 @@
it "should == expected" do
"this".should == "this"
- RSpec::Matchers.generated_description.should == "should == \"this\""
+ RSpec::Matchers.generated_description.should == "== \"this\""
end
it "should not == expected" do
"this".should_not == "that"
- RSpec::Matchers.generated_description.should == "should not == \"that\""
+ RSpec::Matchers.generated_description.should == "does not == \"that\""
end
it "should be empty (arbitrary predicate)" do
[].should be_empty
- RSpec::Matchers.generated_description.should == "should be empty"
+ RSpec::Matchers.generated_description.should == "is empty"
end
it "should not be empty (arbitrary predicate)" do
[1].should_not be_empty
- RSpec::Matchers.generated_description.should == "should not be empty"
+ RSpec::Matchers.generated_description.should == "is not empty"
end
it "should be true" do
true.should be_true
- RSpec::Matchers.generated_description.should == "should be true"
+ RSpec::Matchers.generated_description.should == "is true"
end
it "should be false" do
false.should be_false
- RSpec::Matchers.generated_description.should == "should be false"
+ RSpec::Matchers.generated_description.should == "is false"
end
it "should be nil" do
nil.should be_nil
- RSpec::Matchers.generated_description.should == "should be nil"
+ RSpec::Matchers.generated_description.should == "is nil"
end
it "should be > n" do
5.should be > 3
- RSpec::Matchers.generated_description.should == "should be > 3"
+ RSpec::Matchers.generated_description.should == "is > 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"
+ RSpec::Matchers.generated_description.should == "is between 0 and 10"
+ end
+
+ it "should not be predicate arg1, arg2 and arg3" do
+ 5.0.should_not be_between(0, 1)
+ RSpec::Matchers.generated_description.should == "is not between 0 and 1"
end
it "should equal" do
expected = "expected"
expected.should equal(expected)
- RSpec::Matchers.generated_description.should == "should equal \"expected\""
+ RSpec::Matchers.generated_description.should == "equals \"expected\""
end
it "should_not equal" do
5.should_not equal(37)
- RSpec::Matchers.generated_description.should == "should not equal 37"
+ RSpec::Matchers.generated_description.should == "does not equal 37"
end
it "should eql" do
"string".should eql("string")
- RSpec::Matchers.generated_description.should == "should eql \"string\""
+ RSpec::Matchers.generated_description.should == "eqls \"string\""
end
it "should not eql" do
"a".should_not eql(:a)
- RSpec::Matchers.generated_description.should == "should not eql :a"
+ RSpec::Matchers.generated_description.should == "does not eql :a"
end
it "should have_key" do
{:a => "a"}.should have_key(:a)
- RSpec::Matchers.generated_description.should == "should have key :a"
+ RSpec::Matchers.generated_description.should == "has key :a"
end
it "should have_some_method" do
@@ -81,7 +86,7 @@
def object.has_eyes_closed?; true; end
object.should have_eyes_closed
- RSpec::Matchers.generated_description.should == 'should have eyes closed'
+ RSpec::Matchers.generated_description.should == 'has eyes closed'
end
it "should have_some_method(args*)" do
@@ -89,67 +94,72 @@ def object.has_eyes_closed?; true; end
def object.has_taste_for?(*args); true; end
object.should have_taste_for("wine", "cheese")
- RSpec::Matchers.generated_description.should == 'should have taste for "wine", "cheese"'
+ RSpec::Matchers.generated_description.should == 'has taste for "wine", "cheese"'
end
it "should have n items" do
team.should have(3).players
- RSpec::Matchers.generated_description.should == "should have 3 players"
+ RSpec::Matchers.generated_description.should == "has 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"
+ RSpec::Matchers.generated_description.should == "has 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"
+ RSpec::Matchers.generated_description.should == "has at most 4 players"
end
it "should include" do
[1,2,3].should include(3)
- RSpec::Matchers.generated_description.should == "should include 3"
+ RSpec::Matchers.generated_description.should == "includes 3"
end
it "array.should =~ [1,2,3]" do
[1,2,3].should =~ [1,2,3]
- RSpec::Matchers.generated_description.should == "should contain exactly 1, 2 and 3"
+ RSpec::Matchers.generated_description.should == "contains 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/"
+ RSpec::Matchers.generated_description.should == "matches /this string/"
end
it "should raise_error" do
lambda { raise }.should raise_error
- RSpec::Matchers.generated_description.should == "should raise Exception"
+ RSpec::Matchers.generated_description.should == "raises Exception"
end
it "should raise_error with type" do
lambda { raise }.should raise_error(RuntimeError)
- RSpec::Matchers.generated_description.should == "should raise RuntimeError"
+ RSpec::Matchers.generated_description.should == "raises 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\""
+ RSpec::Matchers.generated_description.should == "raises RuntimeError with \"there was an error\""
+ end
+
+ it "should not raise_error with type and message" do
+ lambda { "Innocuous" }.should_not raise_error(RuntimeError, "there was an error")
+ RSpec::Matchers.generated_description.should == "does not 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"
+ RSpec::Matchers.generated_description.should == "responds 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"
+ RSpec::Matchers.generated_description.should == "throws 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"
+ RSpec::Matchers.generated_description.should == "throws :what_a_mess"
end
def team
Please sign in to comment.
Something went wrong with that request. Please try again.