Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Generated descriptions in the indicative

  • Loading branch information...
commit c44fee682dc17f4f3ffd33a0e55510b64e0cbe6f 1 parent 48e8fe5
Tom Stuart authored
14  features/implicit_docstrings.feature
@@ -20,9 +20,9 @@ Feature: implicit docstrings
20 20
 
21 21
     When I run `rspec ./implicit_docstrings_spec.rb -fdoc`
22 22
 
23  
-    Then the output should contain "should be < 5"
24  
-    And the output should contain "should include 2"
25  
-    And the output should contain "should respond to #size"
  23
+    Then the output should contain "is < 5"
  24
+    And the output should contain "includes 2"
  25
+    And the output should contain "responds to #size"
26 26
 
27 27
   Scenario: run failing examples
28 28
     Given a file named "failing_implicit_docstrings_spec.rb" with:
@@ -46,7 +46,7 @@ Feature: implicit docstrings
46 46
 
47 47
     When I run `rspec ./failing_implicit_docstrings_spec.rb -fdoc`
48 48
 
49  
-    Then the output should contain "should equal 2"
50  
-    And the output should contain "should be > 5"
51  
-    And the output should contain "should include 4"
52  
-    And the output should contain "should not respond to #size"
  49
+    Then the output should contain "equals 2"
  50
+    And the output should contain "is > 5"
  51
+    And the output should contain "includes 4"
  52
+    And the output should contain "does not respond to #size"
15  lib/rspec/matchers/be.rb
@@ -53,6 +53,11 @@ def description
53 53
         "be"
54 54
       end
55 55
 
  56
+      def docstrings
  57
+        {:positive => "is",
  58
+         :negative => "is not"}
  59
+      end
  60
+
56 61
       [:==, :<, :<=, :>=, :>, :===].each do |operator|
57 62
         define_method operator do |operand|
58 63
           BeComparedTo.new(operand, operator)
@@ -114,6 +119,11 @@ def description
114 119
         "be #{@operator} #{expected_to_sentence}#{args_to_sentence}"
115 120
       end
116 121
 
  122
+      def docstrings
  123
+        {:positive => "is #{@operator} #{expected_to_sentence}#{args_to_sentence}",
  124
+         :negative => "is not #{@operator} #{expected_to_sentence}#{args_to_sentence}"}
  125
+      end
  126
+
117 127
     end
118 128
 
119 129
     class BePredicate < Be
@@ -151,6 +161,11 @@ def description
151 161
         "#{prefix_to_sentence}#{expected_to_sentence}#{args_to_sentence}"
152 162
       end
153 163
 
  164
+      def docstrings
  165
+        {:positive => "is #{expected_to_sentence}#{args_to_sentence}",
  166
+         :negative => "is not #{expected_to_sentence}#{args_to_sentence}"}
  167
+      end
  168
+
154 169
     private
155 170
 
156 171
       def predicate
4  lib/rspec/matchers/eq.rb
@@ -43,6 +43,10 @@ def eq(expected)
43 43
         description do
44 44
           "== #{_expected_}"
45 45
         end
  46
+
  47
+        docstrings do
  48
+          {:positive => "== #{_expected_}"}
  49
+        end
46 50
       end
47 51
     end
48 52
   end
5  lib/rspec/matchers/equal.rb
@@ -47,6 +47,11 @@ def inspect_object(o)
47 47
 
48 48
 MESSAGE
49 49
         end
  50
+
  51
+        docstrings do
  52
+          {:positive => "equals #{_expected_.inspect}",
  53
+           :negative => "does not equal #{_expected_.inspect}"}
  54
+        end
50 55
       end
51 56
     end
52 57
   end
5  lib/rspec/matchers/has.rb
@@ -21,6 +21,11 @@ def description
21 21
         [method_description(@expected), args_description(@args)].compact.join(' ')
22 22
       end
23 23
 
  24
+      def docstrings
  25
+        {:positive => description.sub(/have/, "has"),
  26
+          :negative => "not #{description}"}
  27
+      end
  28
+
24 29
     private
25 30
       def predicate(sym)
26 31
         "#{sym.to_s.sub("have_","has_")}?".to_sym
5  lib/rspec/matchers/have.rb
@@ -66,6 +66,11 @@ def failure_message_for_should_not
66 66
       def description
67 67
         "have #{relative_expectation} #{@collection_name}"
68 68
       end
  69
+
  70
+      def docstrings
  71
+        {:positive => "has #{relative_expectation} #{@collection_name}",
  72
+         :negative => "does not have #{relative_expectation} #{@collection_name}"}
  73
+      end
69 74
       
70 75
       def respond_to?(sym)
71 76
         @expected.respond_to?(sym) || super
4  lib/rspec/matchers/match_array.rb
@@ -31,6 +31,10 @@ def description
31 31
         "contain exactly #{_pretty_print(@expected)}"
32 32
       end
33 33
 
  34
+      def docstrings
  35
+        {:positive => "contains exactly #{_pretty_print(@expected)}"}
  36
+      end
  37
+
34 38
       private
35 39
 
36 40
         def safe_sort(array)
10  lib/rspec/matchers/matcher.rb
@@ -15,6 +15,8 @@ def initialize(name, *expected, &declarations)
15 15
         @match_for_should_not_block = nil
16 16
 
17 17
         @messages = {
  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") } },
18 20
           :description => lambda {"#{name_to_sentence}#{expected_to_sentence}"},
19 21
           :failure_message_for_should => lambda {|actual| "expected #{actual.inspect} to #{name_to_sentence}#{expected_to_sentence}"},
20 22
           :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)
91 93
         cache_or_call_cached(:description, &block)
92 94
       end
93 95
 
  96
+      def docstrings(&block)
  97
+        cache_or_call_cached(:docstrings, &block)
  98
+      end
  99
+
94 100
       #Used internally by objects returns by +should+ and +should_not+.
95 101
       def diffable?
96 102
         @diffable
@@ -151,6 +157,10 @@ def name_to_sentence
151 157
         split_words(@name)
152 158
       end
153 159
 
  160
+      def name_to_indicative_sentence
  161
+        to_indicative(name_to_sentence)
  162
+      end
  163
+
154 164
       def expected_to_sentence
155 165
         to_sentence(@expected)
156 166
       end
5  lib/rspec/matchers/operator_matcher.rb
@@ -51,6 +51,11 @@ def fail_with_message(message)
51 51
       def description
52 52
         "#{@operator} #{@expected.inspect}"
53 53
       end
  54
+
  55
+      def docstrings
  56
+        {:positive => description,
  57
+         :negative => "does not #{description}"}
  58
+      end
54 59
       
55 60
     private
56 61
       
14  lib/rspec/matchers/pretty.rb
@@ -19,6 +19,18 @@ def to_sentence(words)
19 19
         end
20 20
       end
21 21
 
  22
+      def to_indicative(infinitive)
  23
+        changed = false
  24
+        { /be/ => "is",
  25
+          /match/ => "matches"}.each_pair do |pattern, substitution|
  26
+          if infinitive =~ pattern
  27
+            infinitive.sub!(pattern, substitution)
  28
+            changed = true
  29
+          end
  30
+        end
  31
+        changed ? infinitive : infinitive.to_s + 's'
  32
+      end
  33
+
22 34
       def _pretty_print(array)
23 35
         result = ""
24 36
         array.each_with_index do |item, index|
@@ -34,4 +46,4 @@ def _pretty_print(array)
34 46
       end
35 47
     end
36 48
   end
37  
-end
  49
+end
5  lib/rspec/matchers/raise_error.rb
@@ -67,6 +67,11 @@ def failure_message_for_should_not
67 67
       def description
68 68
         "raise #{expected_error}"
69 69
       end
  70
+
  71
+      def docstrings
  72
+        {:positive => "raises #{expected_error}",
  73
+         :negative => "does not raise #{expected_error}"}
  74
+      end
70 75
       
71 76
       private
72 77
         def expected_error
5  lib/rspec/matchers/respond_to.rb
@@ -27,6 +27,11 @@ def description
27 27
         "respond to #{pp_names}#{with_arity}"
28 28
       end
29 29
       
  30
+      def docstrings
  31
+        {:positive => "responds to #{pp_names}#{with_arity}",
  32
+         :negative => "does not respond to #{pp_names}#{with_arity}"}        
  33
+      end
  34
+
30 35
       def with(n)
31 36
         @expected_arity = n
32 37
         self
5  lib/rspec/matchers/throw_symbol.rb
@@ -63,6 +63,11 @@ def failure_message_for_should_not
63 63
       def description
64 64
         "throw #{expected}"
65 65
       end
  66
+
  67
+      def docstrings
  68
+        {:positive => "throws #{expected}",
  69
+         :negative => "does not throw #{expected}"}
  70
+      end
66 71
       
67 72
       private
68 73
 
66  spec/rspec/matchers/description_generation_spec.rb
@@ -7,73 +7,78 @@
7 7
 
8 8
   it "should == expected" do
9 9
     "this".should == "this"
10  
-    RSpec::Matchers.generated_description.should == "should == \"this\""
  10
+    RSpec::Matchers.generated_description.should == "== \"this\""
11 11
   end
12 12
 
13 13
   it "should not == expected" do
14 14
     "this".should_not == "that"
15  
-    RSpec::Matchers.generated_description.should == "should not == \"that\""
  15
+    RSpec::Matchers.generated_description.should == "does not == \"that\""
16 16
   end
17 17
 
18 18
   it "should be empty (arbitrary predicate)" do
19 19
     [].should be_empty
20  
-    RSpec::Matchers.generated_description.should == "should be empty"
  20
+    RSpec::Matchers.generated_description.should == "is empty"
21 21
   end
22 22
 
23 23
   it "should not be empty (arbitrary predicate)" do
24 24
     [1].should_not be_empty
25  
-    RSpec::Matchers.generated_description.should == "should not be empty"
  25
+    RSpec::Matchers.generated_description.should == "is not empty"
26 26
   end
27 27
 
28 28
   it "should be true" do
29 29
     true.should be_true
30  
-    RSpec::Matchers.generated_description.should == "should be true"
  30
+    RSpec::Matchers.generated_description.should == "is true"
31 31
   end
32 32
 
33 33
   it "should be false" do
34 34
     false.should be_false
35  
-    RSpec::Matchers.generated_description.should == "should be false"
  35
+    RSpec::Matchers.generated_description.should == "is false"
36 36
   end
37 37
 
38 38
   it "should be nil" do
39 39
     nil.should be_nil
40  
-    RSpec::Matchers.generated_description.should == "should be nil"
  40
+    RSpec::Matchers.generated_description.should == "is nil"
41 41
   end
42 42
 
43 43
   it "should be > n" do
44 44
     5.should be > 3
45  
-    RSpec::Matchers.generated_description.should == "should be > 3"
  45
+    RSpec::Matchers.generated_description.should == "is > 3"
46 46
   end
47 47
 
48 48
   it "should be predicate arg1, arg2 and arg3" do
49 49
     5.0.should be_between(0,10)
50  
-    RSpec::Matchers.generated_description.should == "should be between 0 and 10"
  50
+    RSpec::Matchers.generated_description.should == "is between 0 and 10"
  51
+  end
  52
+
  53
+  it "should not be predicate arg1, arg2 and arg3" do
  54
+    5.0.should_not be_between(0, 1)
  55
+    RSpec::Matchers.generated_description.should == "is not between 0 and 1"
51 56
   end
52 57
 
53 58
   it "should equal" do
54 59
     expected = "expected"
55 60
     expected.should equal(expected)
56  
-    RSpec::Matchers.generated_description.should == "should equal \"expected\""
  61
+    RSpec::Matchers.generated_description.should == "equals \"expected\""
57 62
   end
58 63
 
59 64
   it "should_not equal" do
60 65
     5.should_not equal(37)
61  
-    RSpec::Matchers.generated_description.should == "should not equal 37"
  66
+    RSpec::Matchers.generated_description.should == "does not equal 37"
62 67
   end
63 68
 
64 69
   it "should eql" do
65 70
     "string".should eql("string")
66  
-    RSpec::Matchers.generated_description.should == "should eql \"string\""
  71
+    RSpec::Matchers.generated_description.should == "eqls \"string\""
67 72
   end
68 73
 
69 74
   it "should not eql" do
70 75
     "a".should_not eql(:a)
71  
-    RSpec::Matchers.generated_description.should == "should not eql :a"
  76
+    RSpec::Matchers.generated_description.should == "does not eql :a"
72 77
   end
73 78
 
74 79
   it "should have_key" do
75 80
     {:a => "a"}.should have_key(:a)
76  
-    RSpec::Matchers.generated_description.should == "should have key :a"
  81
+    RSpec::Matchers.generated_description.should == "has key :a"
77 82
   end
78 83
 
79 84
   it "should have_some_method" do
@@ -81,7 +86,7 @@
81 86
     def object.has_eyes_closed?; true; end
82 87
 
83 88
     object.should have_eyes_closed
84  
-    RSpec::Matchers.generated_description.should == 'should have eyes closed'
  89
+    RSpec::Matchers.generated_description.should == 'has eyes closed'
85 90
   end
86 91
 
87 92
   it "should have_some_method(args*)" do
@@ -89,67 +94,72 @@ def object.has_eyes_closed?; true; end
89 94
     def object.has_taste_for?(*args); true; end
90 95
 
91 96
     object.should have_taste_for("wine", "cheese")
92  
-    RSpec::Matchers.generated_description.should == 'should have taste for "wine", "cheese"'
  97
+    RSpec::Matchers.generated_description.should == 'has taste for "wine", "cheese"'
93 98
   end
94 99
 
95 100
   it "should have n items" do
96 101
     team.should have(3).players
97  
-    RSpec::Matchers.generated_description.should == "should have 3 players"
  102
+    RSpec::Matchers.generated_description.should == "has 3 players"
98 103
   end
99 104
 
100 105
   it "should have at least n items" do
101 106
     team.should have_at_least(2).players
102  
-    RSpec::Matchers.generated_description.should == "should have at least 2 players"
  107
+    RSpec::Matchers.generated_description.should == "has at least 2 players"
103 108
   end
104 109
 
105 110
   it "should have at most n items" do
106 111
     team.should have_at_most(4).players
107  
-    RSpec::Matchers.generated_description.should == "should have at most 4 players"
  112
+    RSpec::Matchers.generated_description.should == "has at most 4 players"
108 113
   end
109 114
 
110 115
   it "should include" do
111 116
     [1,2,3].should include(3)
112  
-    RSpec::Matchers.generated_description.should == "should include 3"
  117
+    RSpec::Matchers.generated_description.should == "includes 3"
113 118
   end
114 119
 
115 120
   it "array.should =~ [1,2,3]" do
116 121
     [1,2,3].should =~ [1,2,3]
117  
-    RSpec::Matchers.generated_description.should == "should contain exactly 1, 2 and 3"
  122
+    RSpec::Matchers.generated_description.should == "contains exactly 1, 2 and 3"
118 123
   end
119 124
 
120 125
   it "should match" do
121 126
     "this string".should match(/this string/)
122  
-    RSpec::Matchers.generated_description.should == "should match /this string/"
  127
+    RSpec::Matchers.generated_description.should == "matches /this string/"
123 128
   end
124 129
 
125 130
   it "should raise_error" do
126 131
     lambda { raise }.should raise_error
127  
-    RSpec::Matchers.generated_description.should == "should raise Exception"
  132
+    RSpec::Matchers.generated_description.should == "raises Exception"
128 133
   end
129 134
 
130 135
   it "should raise_error with type" do
131 136
     lambda { raise }.should raise_error(RuntimeError)
132  
-    RSpec::Matchers.generated_description.should == "should raise RuntimeError"
  137
+    RSpec::Matchers.generated_description.should == "raises RuntimeError"
133 138
   end
134 139
 
135 140
   it "should raise_error with type and message" do
136 141
     lambda { raise "there was an error" }.should raise_error(RuntimeError, "there was an error")
137  
-    RSpec::Matchers.generated_description.should == "should raise RuntimeError with \"there was an error\""
  142
+    RSpec::Matchers.generated_description.should == "raises RuntimeError with \"there was an error\""
  143
+  end
  144
+
  145
+  it "should not raise_error with type and message" do
  146
+    lambda { "Innocuous" }.should_not raise_error(RuntimeError, "there was an error")
  147
+    RSpec::Matchers.generated_description.should == "does not raise RuntimeError with \"there was an error\""
138 148
   end
139 149
 
140 150
   it "should respond_to" do
141 151
     [].should respond_to(:insert)
142  
-    RSpec::Matchers.generated_description.should == "should respond to #insert"
  152
+    RSpec::Matchers.generated_description.should == "responds to #insert"
143 153
   end
144 154
 
145 155
   it "should throw symbol" do
146 156
     lambda { throw :what_a_mess }.should throw_symbol
147  
-    RSpec::Matchers.generated_description.should == "should throw a Symbol"
  157
+    RSpec::Matchers.generated_description.should == "throws a Symbol"
148 158
   end
149 159
 
150 160
   it "should throw symbol (with named symbol)" do
151 161
     lambda { throw :what_a_mess }.should throw_symbol(:what_a_mess)
152  
-    RSpec::Matchers.generated_description.should == "should throw :what_a_mess"
  162
+    RSpec::Matchers.generated_description.should == "throws :what_a_mess"
153 163
   end
154 164
 
155 165
   def team

0 notes on commit c44fee6

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