Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Improved default docstrings

  • Loading branch information...
commit 0b14d7f5b2a20531413b18a0cc12121948875f8b 1 parent d9a2c8f
Tom Stuart authored
19 features/custom_matchers/define_matcher.feature
@@ -37,8 +37,8 @@ Feature: define matcher
37 37 When I run `rspec ./matcher_with_default_message_spec.rb --format documentation`
38 38 Then the exit status should not be 0
39 39
40   - And the output should contain "should be a multiple of 3"
41   - And the output should contain "should not be a multiple of 4"
  40 + And the output should contain "is a multiple of 3"
  41 + And the output should contain "is not a multiple of 4"
42 42 And the output should contain "Failure/Error: it {should be_a_multiple_of(4)}"
43 43 And the output should contain "Failure/Error: it {should_not be_a_multiple_of(3)}"
44 44
@@ -94,7 +94,7 @@ Feature: define matcher
94 94 And the stdout should contain "1 example, 1 failure"
95 95 And the stdout should contain "expected that 9 would not be a multiple of 3"
96 96
97   - Scenario: overriding the description
  97 + Scenario: overriding the docstrings
98 98 Given a file named "matcher_overriding_description_spec.rb" with:
99 99 """
100 100 require 'rspec/expectations'
@@ -103,8 +103,9 @@ Feature: define matcher
103 103 match do |actual|
104 104 actual % expected == 0
105 105 end
106   - description do
107   - "be multiple of #{expected}"
  106 + docstrings do
  107 + {:positive => "is multiple of #{expected}",
  108 + :negative => "is not multiple of #{expected}"}
108 109 end
109 110 end
110 111
@@ -119,8 +120,8 @@ Feature: define matcher
119 120 When I run `rspec ./matcher_overriding_description_spec.rb --format documentation`
120 121 Then the exit status should be 0
121 122 And the stdout should contain "2 examples, 0 failures"
122   - And the stdout should contain "should be multiple of 3"
123   - And the stdout should contain "should not be multiple of 4"
  123 + And the stdout should contain "is multiple of 3"
  124 + And the stdout should contain "is not multiple of 4"
124 125
125 126 Scenario: with no args
126 127 Given a file named "matcher_with_no_args_spec.rb" with:
@@ -144,7 +145,7 @@ Feature: define matcher
144 145 When I run `rspec ./matcher_with_no_args_spec.rb --format documentation`
145 146 Then the exit status should be 0
146 147 And the stdout should contain "1 example, 0 failures"
147   - And the stdout should contain "should have 7 fingers"
  148 + And the stdout should contain "has 7 fingers"
148 149
149 150 Scenario: with multiple args
150 151 Given a file named "matcher_with_multiple_args_spec.rb" with:
@@ -164,7 +165,7 @@ Feature: define matcher
164 165 When I run `rspec ./matcher_with_multiple_args_spec.rb --format documentation`
165 166 Then the exit status should be 0
166 167 And the stdout should contain "1 example, 0 failures"
167   - And the stdout should contain "should be the sum of 1, 2, 3, and 4"
  168 + And the stdout should contain "is the sum of 1, 2, 3, and 4"
168 169
169 170 Scenario: with helper methods
170 171 Given a file named "matcher_with_internal_helper_spec.rb" with:
11 lib/rspec/matchers/matcher.rb
@@ -16,7 +16,7 @@ def initialize(name, *expected, &declarations)
16 16
17 17 @messages = {
18 18 :docstrings => lambda { {:positive => "#{name_to_indicative_sentence}#{expected_to_sentence}",
19   - :negative => "does not #{name_to_sentence}#{expected_to_sentence}".sub("not is", "is not") } },
  19 + :negative => "#{name_to_indicative_sentence(true)}#{expected_to_sentence}"} },
20 20 :description => lambda {"#{name_to_sentence}#{expected_to_sentence}"},
21 21 :failure_message_for_should => lambda {|actual| "expected #{actual.inspect} to #{name_to_sentence}#{expected_to_sentence}"},
22 22 :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
158 158 split_words(@name)
159 159 end
160 160
161   - def name_to_indicative_sentence
162   - to_indicative(name_to_sentence)
  161 + def name_to_indicative_sentence(negated=false)
  162 + if negated
  163 + indicative = to_indicative(name_to_sentence)
  164 + indicative =~ /is\s/ ? indicative.sub("is", "is not") : "does not #{name_to_sentence}"
  165 + else
  166 + to_indicative(name_to_sentence)
  167 + end
163 168 end
164 169
165 170 def expected_to_sentence
1  lib/rspec/matchers/pretty.rb
@@ -22,6 +22,7 @@ def to_sentence(words)
22 22 def to_indicative(infinitive)
23 23 changed = false
24 24 { /be/ => "is",
  25 + /have/ => "has",
25 26 /match/ => "matches"}.each_pair do |pattern, substitution|
26 27 if infinitive =~ pattern
27 28 infinitive.sub!(pattern, substitution)

0 comments on commit 0b14d7f

Please sign in to comment.
Something went wrong with that request. Please try again.