dchelimsky / rspec

Behaviour Driven Development framework for Ruby

This URL has Read+Write access

rspec / features / matchers / define_matcher.feature
045fa962 » dchelimsky 2009-04-08 deprecate Spec::Matchers.cr... 1 Feature: define matcher
f0a78ab0 » dchelimsky 2009-03-02 more thorough cucumber feat... 2
3 In order to express my domain clearly in my code examples
4 As an RSpec user
045fa962 » dchelimsky 2009-04-08 deprecate Spec::Matchers.cr... 5 I want a shortcut to define custom matchers
f0a78ab0 » dchelimsky 2009-03-02 more thorough cucumber feat... 6
045fa962 » dchelimsky 2009-04-08 deprecate Spec::Matchers.cr... 7 Scenario: define a matcher with default messages
e3a75241 » bmabey 2009-03-30 converted create_matcher.fe... 8 Given a file named "matcher_with_default_message_spec.rb" with:
f0a78ab0 » dchelimsky 2009-03-02 more thorough cucumber feat... 9 """
045fa962 » dchelimsky 2009-04-08 deprecate Spec::Matchers.cr... 10 Spec::Matchers.define :be_a_multiple_of do |expected|
e14eb28c » dchelimsky 2009-03-02 Fixed bug caught by cucumbe... 11 match do |actual|
f0a78ab0 » dchelimsky 2009-03-02 more thorough cucumber feat... 12 actual % expected == 0
13 end
14 end
15
16 describe 9 do
17 it {should be_a_multiple_of(3)}
18 end
19
20 describe 9 do
21 it {should_not be_a_multiple_of(4)}
22 end
23
24 # fail intentionally to generate expected output
25 describe 9 do
26 it {should be_a_multiple_of(4)}
27 end
28
29 # fail intentionally to generate expected output
30 describe 9 do
31 it {should_not be_a_multiple_of(3)}
32 end
33
34 """
e3a75241 » bmabey 2009-03-30 converted create_matcher.fe... 35 When I run "spec matcher_with_default_message_spec.rb --format specdoc"
f0a78ab0 » dchelimsky 2009-03-02 more thorough cucumber feat... 36 Then the exit code should be 256
37
0b2caa75 » dchelimsky 2009-08-23 stdout should include (in c... 38 And the stdout should include "should be a multiple of 3"
39 And the stdout should include "should not be a multiple of 4"
40 And the stdout should include "should be a multiple of 4 (FAILED - 1)"
41 And the stdout should include "should not be a multiple of 3 (FAILED - 2)"
f0a78ab0 » dchelimsky 2009-03-02 more thorough cucumber feat... 42
0b2caa75 » dchelimsky 2009-08-23 stdout should include (in c... 43 And the stdout should include "4 examples, 2 failures"
44 And the stdout should include "expected 9 to be a multiple of 4"
45 And the stdout should include "expected 9 not to be a multiple of 3"
e3a75241 » bmabey 2009-03-30 converted create_matcher.fe... 46
bac659bc » dchelimsky 2009-03-03 refactor Matcher 47 Scenario: overriding the failure_message_for_should
e3a75241 » bmabey 2009-03-30 converted create_matcher.fe... 48 Given a file named "matcher_with_failure_message_spec.rb" with:
f0a78ab0 » dchelimsky 2009-03-02 more thorough cucumber feat... 49 """
045fa962 » dchelimsky 2009-04-08 deprecate Spec::Matchers.cr... 50 Spec::Matchers.define :be_a_multiple_of do |expected|
e14eb28c » dchelimsky 2009-03-02 Fixed bug caught by cucumbe... 51 match do |actual|
f0a78ab0 » dchelimsky 2009-03-02 more thorough cucumber feat... 52 actual % expected == 0
53 end
819ecf59 » dchelimsky 2009-03-03 simplify matcher DSL 54 failure_message_for_should do |actual|
f0a78ab0 » dchelimsky 2009-03-02 more thorough cucumber feat... 55 "expected that #{actual} would be a multiple of #{expected}"
56 end
57 end
e3a75241 » bmabey 2009-03-30 converted create_matcher.fe... 58
f0a78ab0 » dchelimsky 2009-03-02 more thorough cucumber feat... 59 # fail intentionally to generate expected output
60 describe 9 do
61 it {should be_a_multiple_of(4)}
62 end
63 """
e3a75241 » bmabey 2009-03-30 converted create_matcher.fe... 64 When I run "spec matcher_with_failure_message_spec.rb"
f0a78ab0 » dchelimsky 2009-03-02 more thorough cucumber feat... 65 Then the exit code should be 256
0b2caa75 » dchelimsky 2009-08-23 stdout should include (in c... 66 And the stdout should include "1 example, 1 failure"
67 And the stdout should include "expected that 9 would be a multiple of 4"
e3a75241 » bmabey 2009-03-30 converted create_matcher.fe... 68
bac659bc » dchelimsky 2009-03-03 refactor Matcher 69 Scenario: overriding the failure_message_for_should_not
e3a75241 » bmabey 2009-03-30 converted create_matcher.fe... 70 Given a file named "matcher_with_failure_for_message_spec.rb" with:
f0a78ab0 » dchelimsky 2009-03-02 more thorough cucumber feat... 71 """
045fa962 » dchelimsky 2009-04-08 deprecate Spec::Matchers.cr... 72 Spec::Matchers.define :be_a_multiple_of do |expected|
e14eb28c » dchelimsky 2009-03-02 Fixed bug caught by cucumbe... 73 match do |actual|
f0a78ab0 » dchelimsky 2009-03-02 more thorough cucumber feat... 74 actual % expected == 0
75 end
819ecf59 » dchelimsky 2009-03-03 simplify matcher DSL 76 failure_message_for_should_not do |actual|
f0a78ab0 » dchelimsky 2009-03-02 more thorough cucumber feat... 77 "expected that #{actual} would not be a multiple of #{expected}"
78 end
79 end
e3a75241 » bmabey 2009-03-30 converted create_matcher.fe... 80
f0a78ab0 » dchelimsky 2009-03-02 more thorough cucumber feat... 81 # fail intentionally to generate expected output
82 describe 9 do
83 it {should_not be_a_multiple_of(3)}
84 end
85 """
e3a75241 » bmabey 2009-03-30 converted create_matcher.fe... 86 When I run "spec matcher_with_failure_for_message_spec.rb"
f0a78ab0 » dchelimsky 2009-03-02 more thorough cucumber feat... 87 Then the exit code should be 256
0b2caa75 » dchelimsky 2009-08-23 stdout should include (in c... 88 And the stdout should include "1 example, 1 failure"
89 And the stdout should include "expected that 9 would not be a multiple of 3"
e3a75241 » bmabey 2009-03-30 converted create_matcher.fe... 90
bac659bc » dchelimsky 2009-03-03 refactor Matcher 91 Scenario: overriding the description
e3a75241 » bmabey 2009-03-30 converted create_matcher.fe... 92 Given a file named "matcher_overriding_description_spec.rb" with:
f0a78ab0 » dchelimsky 2009-03-02 more thorough cucumber feat... 93 """
045fa962 » dchelimsky 2009-04-08 deprecate Spec::Matchers.cr... 94 Spec::Matchers.define :be_a_multiple_of do |expected|
e14eb28c » dchelimsky 2009-03-02 Fixed bug caught by cucumbe... 95 match do |actual|
f0a78ab0 » dchelimsky 2009-03-02 more thorough cucumber feat... 96 actual % expected == 0
97 end
98 description do
99 "be multiple of #{expected}"
100 end
101 end
e3a75241 » bmabey 2009-03-30 converted create_matcher.fe... 102
f0a78ab0 » dchelimsky 2009-03-02 more thorough cucumber feat... 103 describe 9 do
104 it {should be_a_multiple_of(3)}
105 end
e3a75241 » bmabey 2009-03-30 converted create_matcher.fe... 106
5190185e » dchelimsky 2009-03-02 refine create matcher cuc f... 107 describe 9 do
108 it {should_not be_a_multiple_of(4)}
109 end
f0a78ab0 » dchelimsky 2009-03-02 more thorough cucumber feat... 110 """
e3a75241 » bmabey 2009-03-30 converted create_matcher.fe... 111 When I run "spec matcher_overriding_description_spec.rb --format specdoc"
f0a78ab0 » dchelimsky 2009-03-02 more thorough cucumber feat... 112 Then the exit code should be 0
0b2caa75 » dchelimsky 2009-08-23 stdout should include (in c... 113 And the stdout should include "2 examples, 0 failures"
114 And the stdout should include "should be multiple of 3"
115 And the stdout should include "should not be multiple of 4"
d2efb4bc » dchelimsky 2009-03-16 added support for 0 to n ar... 116
117 Scenario: with no args
e3a75241 » bmabey 2009-03-30 converted create_matcher.fe... 118 Given a file named "matcher_with_no_args_spec.rb" with:
d2efb4bc » dchelimsky 2009-03-16 added support for 0 to n ar... 119 """
045fa962 » dchelimsky 2009-04-08 deprecate Spec::Matchers.cr... 120 Spec::Matchers.define :have_7_fingers do
d2efb4bc » dchelimsky 2009-03-16 added support for 0 to n ar... 121 match do |thing|
122 thing.fingers.length == 7
123 end
124 end
e3a75241 » bmabey 2009-03-30 converted create_matcher.fe... 125
d2efb4bc » dchelimsky 2009-03-16 added support for 0 to n ar... 126 class Thing
127 def fingers; (1..7).collect {"finger"}; end
128 end
129
130 describe Thing do
131 it {should have_7_fingers}
132 end
133 """
e3a75241 » bmabey 2009-03-30 converted create_matcher.fe... 134 When I run "spec matcher_with_no_args_spec.rb --format specdoc"
d2efb4bc » dchelimsky 2009-03-16 added support for 0 to n ar... 135 Then the exit code should be 0
0b2caa75 » dchelimsky 2009-08-23 stdout should include (in c... 136 And the stdout should include "1 example, 0 failures"
137 And the stdout should include "should have 7 fingers"
e3a75241 » bmabey 2009-03-30 converted create_matcher.fe... 138
d2efb4bc » dchelimsky 2009-03-16 added support for 0 to n ar... 139 Scenario: with multiple args
e3a75241 » bmabey 2009-03-30 converted create_matcher.fe... 140 Given a file named "matcher_with_multiple_args_spec.rb" with:
d2efb4bc » dchelimsky 2009-03-16 added support for 0 to n ar... 141 """
045fa962 » dchelimsky 2009-04-08 deprecate Spec::Matchers.cr... 142 Spec::Matchers.define :be_the_sum_of do |a,b,c,d|
d2efb4bc » dchelimsky 2009-03-16 added support for 0 to n ar... 143 match do |sum|
144 a + b + c + d == sum
145 end
146 end
147
148 describe 10 do
149 it {should be_the_sum_of(1,2,3,4)}
150 end
151 """
e3a75241 » bmabey 2009-03-30 converted create_matcher.fe... 152 When I run "spec matcher_with_multiple_args_spec.rb --format specdoc"
d2efb4bc » dchelimsky 2009-03-16 added support for 0 to n ar... 153 Then the exit code should be 0
0b2caa75 » dchelimsky 2009-08-23 stdout should include (in c... 154 And the stdout should include "1 example, 0 failures"
155 And the stdout should include "should be the sum of 1, 2, 3, and 4"
133c40ae » dchelimsky 2009-04-06 add examples to show helper... 156
157 Scenario: with helper methods
158 Given a file named "matcher_with_internal_helper_spec.rb" with:
159 """
9437a0a4 » dchelimsky 2009-04-08 better name 160 Spec::Matchers.define :have_same_elements_as do |sample|
133c40ae » dchelimsky 2009-04-06 add examples to show helper... 161 match do |actual|
162 similar?(sample, actual)
163 end
164
165 def similar?(a, b)
166 a.sort == b.sort
167 end
168 end
169
170 describe "these two arrays" do
171 specify "should be similar" do
9437a0a4 » dchelimsky 2009-04-08 better name 172 [1,2,3].should have_same_elements_as([2,3,1])
133c40ae » dchelimsky 2009-04-06 add examples to show helper... 173 end
174 end
175 """
176 When I run "spec matcher_with_internal_helper_spec.rb"
177 Then the exit code should be 0
0b2caa75 » dchelimsky 2009-08-23 stdout should include (in c... 178 And the stdout should include "1 example, 0 failures"
045fa962 » dchelimsky 2009-04-08 deprecate Spec::Matchers.cr... 179