Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Improved default docstrings

  • Loading branch information...
commit 0b14d7f5b2a20531413b18a0cc12121948875f8b 1 parent d9a2c8f
@rentalcustard authored
View
19 features/custom_matchers/define_matcher.feature
@@ -37,8 +37,8 @@ Feature: define matcher
When I run `rspec ./matcher_with_default_message_spec.rb --format documentation`
Then the exit status should not be 0
- And the output should contain "should be a multiple of 3"
- And the output should contain "should not be a multiple of 4"
+ And the output should contain "is a multiple of 3"
+ And the output should contain "is not a multiple of 4"
And the output should contain "Failure/Error: it {should be_a_multiple_of(4)}"
And the output should contain "Failure/Error: it {should_not be_a_multiple_of(3)}"
@@ -94,7 +94,7 @@ Feature: define matcher
And the stdout should contain "1 example, 1 failure"
And the stdout should contain "expected that 9 would not be a multiple of 3"
- Scenario: overriding the description
+ Scenario: overriding the docstrings
Given a file named "matcher_overriding_description_spec.rb" with:
"""
require 'rspec/expectations'
@@ -103,8 +103,9 @@ Feature: define matcher
match do |actual|
actual % expected == 0
end
- description do
- "be multiple of #{expected}"
+ docstrings do
+ {:positive => "is multiple of #{expected}",
+ :negative => "is not multiple of #{expected}"}
end
end
@@ -119,8 +120,8 @@ Feature: define matcher
When I run `rspec ./matcher_overriding_description_spec.rb --format documentation`
Then the exit status should be 0
And the stdout should contain "2 examples, 0 failures"
- And the stdout should contain "should be multiple of 3"
- And the stdout should contain "should not be multiple of 4"
+ And the stdout should contain "is multiple of 3"
+ And the stdout should contain "is not multiple of 4"
Scenario: with no args
Given a file named "matcher_with_no_args_spec.rb" with:
@@ -144,7 +145,7 @@ Feature: define matcher
When I run `rspec ./matcher_with_no_args_spec.rb --format documentation`
Then the exit status should be 0
And the stdout should contain "1 example, 0 failures"
- And the stdout should contain "should have 7 fingers"
+ And the stdout should contain "has 7 fingers"
Scenario: with multiple args
Given a file named "matcher_with_multiple_args_spec.rb" with:
@@ -164,7 +165,7 @@ Feature: define matcher
When I run `rspec ./matcher_with_multiple_args_spec.rb --format documentation`
Then the exit status should be 0
And the stdout should contain "1 example, 0 failures"
- And the stdout should contain "should be the sum of 1, 2, 3, and 4"
+ And the stdout should contain "is the sum of 1, 2, 3, and 4"
Scenario: with helper methods
Given a file named "matcher_with_internal_helper_spec.rb" with:
View
11 lib/rspec/matchers/matcher.rb
@@ -16,7 +16,7 @@ def initialize(name, *expected, &declarations)
@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") } },
+ :negative => "#{name_to_indicative_sentence(true)}#{expected_to_sentence}"} },
: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}"}
@@ -158,8 +158,13 @@ def name_to_sentence
split_words(@name)
end
- def name_to_indicative_sentence
- to_indicative(name_to_sentence)
+ def name_to_indicative_sentence(negated=false)
+ if negated
+ indicative = to_indicative(name_to_sentence)
+ indicative =~ /is\s/ ? indicative.sub("is", "is not") : "does not #{name_to_sentence}"
+ else
+ to_indicative(name_to_sentence)
+ end
end
def expected_to_sentence
View
1  lib/rspec/matchers/pretty.rb
@@ -22,6 +22,7 @@ def to_sentence(words)
def to_indicative(infinitive)
changed = false
{ /be/ => "is",
+ /have/ => "has",
/match/ => "matches"}.each_pair do |pattern, substitution|
if infinitive =~ pattern
infinitive.sub!(pattern, substitution)
Please sign in to comment.
Something went wrong with that request. Please try again.