rspec-expectations 3.0.0.beta2 → 3.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (135) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +2 -2
  4. data/.yardopts +0 -1
  5. data/Changelog.md +115 -35
  6. data/README.md +2 -2
  7. data/lib/rspec/expectations.rb +13 -8
  8. data/lib/rspec/{matchers → expectations}/configuration.rb +38 -13
  9. data/lib/rspec/expectations/expectation_target.rb +72 -8
  10. data/lib/rspec/expectations/fail_with.rb +10 -52
  11. data/lib/rspec/expectations/handler.rb +9 -11
  12. data/lib/rspec/expectations/syntax.rb +37 -35
  13. data/lib/rspec/expectations/version.rb +1 -1
  14. data/lib/rspec/matchers.rb +60 -9
  15. data/lib/rspec/matchers/aliased_matcher.rb +6 -0
  16. data/lib/rspec/matchers/built_in.rb +9 -1
  17. data/lib/rspec/matchers/built_in/all.rb +78 -0
  18. data/lib/rspec/matchers/built_in/base_matcher.rb +39 -1
  19. data/lib/rspec/matchers/built_in/be.rb +117 -42
  20. data/lib/rspec/matchers/built_in/be_between.rb +22 -0
  21. data/lib/rspec/matchers/built_in/be_instance_of.rb +11 -3
  22. data/lib/rspec/matchers/built_in/be_kind_of.rb +5 -0
  23. data/lib/rspec/matchers/built_in/be_within.rb +26 -6
  24. data/lib/rspec/matchers/built_in/change.rb +89 -13
  25. data/lib/rspec/matchers/built_in/compound.rb +19 -3
  26. data/lib/rspec/matchers/built_in/contain_exactly.rb +17 -6
  27. data/lib/rspec/matchers/built_in/cover.rb +3 -0
  28. data/lib/rspec/matchers/built_in/eq.rb +20 -5
  29. data/lib/rspec/matchers/built_in/eql.rb +15 -3
  30. data/lib/rspec/matchers/built_in/equal.rb +23 -6
  31. data/lib/rspec/matchers/built_in/exist.rb +74 -10
  32. data/lib/rspec/matchers/built_in/has.rb +58 -3
  33. data/lib/rspec/matchers/built_in/include.rb +16 -1
  34. data/lib/rspec/matchers/built_in/match.rb +14 -4
  35. data/lib/rspec/matchers/built_in/operators.rb +16 -0
  36. data/lib/rspec/matchers/built_in/output.rb +47 -5
  37. data/lib/rspec/matchers/built_in/raise_error.rb +40 -23
  38. data/lib/rspec/matchers/built_in/respond_to.rb +37 -16
  39. data/lib/rspec/matchers/built_in/satisfy.rb +15 -0
  40. data/lib/rspec/matchers/built_in/start_and_end_with.rb +29 -14
  41. data/lib/rspec/matchers/built_in/throw_symbol.rb +32 -3
  42. data/lib/rspec/matchers/built_in/yield.rb +148 -44
  43. data/lib/rspec/matchers/composable.rb +48 -7
  44. data/lib/rspec/matchers/dsl.rb +45 -17
  45. data/lib/rspec/matchers/generated_descriptions.rb +7 -0
  46. data/lib/rspec/matchers/matcher_delegator.rb +6 -2
  47. data/lib/rspec/matchers/pretty.rb +15 -19
  48. metadata +33 -236
  49. metadata.gz.sig +0 -0
  50. data/features/README.md +0 -48
  51. data/features/Upgrade.md +0 -53
  52. data/features/built_in_matchers/README.md +0 -96
  53. data/features/built_in_matchers/be.feature +0 -175
  54. data/features/built_in_matchers/be_within.feature +0 -48
  55. data/features/built_in_matchers/comparisons.feature +0 -97
  56. data/features/built_in_matchers/contain_exactly.feature +0 -46
  57. data/features/built_in_matchers/cover.feature +0 -47
  58. data/features/built_in_matchers/end_with.feature +0 -48
  59. data/features/built_in_matchers/equality.feature +0 -136
  60. data/features/built_in_matchers/exist.feature +0 -45
  61. data/features/built_in_matchers/expect_change.feature +0 -59
  62. data/features/built_in_matchers/expect_error.feature +0 -144
  63. data/features/built_in_matchers/include.feature +0 -126
  64. data/features/built_in_matchers/match.feature +0 -51
  65. data/features/built_in_matchers/output.feature +0 -70
  66. data/features/built_in_matchers/predicates.feature +0 -161
  67. data/features/built_in_matchers/respond_to.feature +0 -84
  68. data/features/built_in_matchers/satisfy.feature +0 -33
  69. data/features/built_in_matchers/start_with.feature +0 -48
  70. data/features/built_in_matchers/throw_symbol.feature +0 -91
  71. data/features/built_in_matchers/types.feature +0 -116
  72. data/features/built_in_matchers/yield.feature +0 -161
  73. data/features/composing_matchers.feature +0 -250
  74. data/features/compound_expectations.feature +0 -45
  75. data/features/custom_matchers/access_running_example.feature +0 -53
  76. data/features/custom_matchers/define_diffable_matcher.feature +0 -27
  77. data/features/custom_matchers/define_matcher.feature +0 -340
  78. data/features/custom_matchers/define_matcher_outside_rspec.feature +0 -34
  79. data/features/custom_matchers/define_matcher_with_fluent_interface.feature +0 -24
  80. data/features/customized_message.feature +0 -39
  81. data/features/diffing.feature +0 -85
  82. data/features/implicit_docstrings.feature +0 -52
  83. data/features/step_definitions/additional_cli_steps.rb +0 -22
  84. data/features/support/env.rb +0 -21
  85. data/features/support/rubinius.rb +0 -6
  86. data/features/syntax_configuration.feature +0 -71
  87. data/features/test_frameworks/minitest.feature +0 -44
  88. data/lib/rspec-expectations.rb +0 -1
  89. data/lib/rspec/expectations/diff_presenter.rb +0 -141
  90. data/lib/rspec/expectations/differ.rb +0 -44
  91. data/lib/rspec/expectations/encoded_string.rb +0 -56
  92. data/spec/rspec/expectations/diff_presenter_spec.rb +0 -249
  93. data/spec/rspec/expectations/encoded_string_spec.rb +0 -74
  94. data/spec/rspec/expectations/expectation_target_spec.rb +0 -82
  95. data/spec/rspec/expectations/extensions/kernel_spec.rb +0 -67
  96. data/spec/rspec/expectations/fail_with_spec.rb +0 -114
  97. data/spec/rspec/expectations/handler_spec.rb +0 -205
  98. data/spec/rspec/expectations/minitest_integration_spec.rb +0 -27
  99. data/spec/rspec/expectations/syntax_spec.rb +0 -89
  100. data/spec/rspec/expectations_spec.rb +0 -12
  101. data/spec/rspec/matchers/aliased_matcher_spec.rb +0 -48
  102. data/spec/rspec/matchers/aliases_spec.rb +0 -449
  103. data/spec/rspec/matchers/built_in/base_matcher_spec.rb +0 -83
  104. data/spec/rspec/matchers/built_in/be_between_spec.rb +0 -159
  105. data/spec/rspec/matchers/built_in/be_instance_of_spec.rb +0 -63
  106. data/spec/rspec/matchers/built_in/be_kind_of_spec.rb +0 -41
  107. data/spec/rspec/matchers/built_in/be_spec.rb +0 -592
  108. data/spec/rspec/matchers/built_in/be_within_spec.rb +0 -141
  109. data/spec/rspec/matchers/built_in/change_spec.rb +0 -808
  110. data/spec/rspec/matchers/built_in/compound_spec.rb +0 -292
  111. data/spec/rspec/matchers/built_in/contain_exactly_spec.rb +0 -441
  112. data/spec/rspec/matchers/built_in/cover_spec.rb +0 -69
  113. data/spec/rspec/matchers/built_in/eq_spec.rb +0 -156
  114. data/spec/rspec/matchers/built_in/eql_spec.rb +0 -41
  115. data/spec/rspec/matchers/built_in/equal_spec.rb +0 -106
  116. data/spec/rspec/matchers/built_in/exist_spec.rb +0 -124
  117. data/spec/rspec/matchers/built_in/has_spec.rb +0 -161
  118. data/spec/rspec/matchers/built_in/include_spec.rb +0 -540
  119. data/spec/rspec/matchers/built_in/match_spec.rb +0 -102
  120. data/spec/rspec/matchers/built_in/operators_spec.rb +0 -252
  121. data/spec/rspec/matchers/built_in/output_spec.rb +0 -165
  122. data/spec/rspec/matchers/built_in/raise_error_spec.rb +0 -461
  123. data/spec/rspec/matchers/built_in/respond_to_spec.rb +0 -292
  124. data/spec/rspec/matchers/built_in/satisfy_spec.rb +0 -44
  125. data/spec/rspec/matchers/built_in/start_and_end_with_spec.rb +0 -253
  126. data/spec/rspec/matchers/built_in/throw_symbol_spec.rb +0 -135
  127. data/spec/rspec/matchers/built_in/yield_spec.rb +0 -627
  128. data/spec/rspec/matchers/configuration_spec.rb +0 -213
  129. data/spec/rspec/matchers/description_generation_spec.rb +0 -191
  130. data/spec/rspec/matchers/dsl_spec.rb +0 -895
  131. data/spec/rspec/matchers/legacy_spec.rb +0 -101
  132. data/spec/rspec/matchers_spec.rb +0 -74
  133. data/spec/spec_helper.rb +0 -85
  134. data/spec/support/matchers.rb +0 -22
  135. data/spec/support/shared_examples.rb +0 -35
@@ -1,250 +0,0 @@
1
- Feature: Composing Matchers
2
-
3
- RSpec's matchers are designed to be composable so that you can
4
- combine them to express the exact details of what you expect
5
- but nothing more. This can help you avoid writing over-specified
6
- brittle specs, by using a matcher in place of an exact value to
7
- specify only the essential aspects of what you expect.
8
-
9
- The following matchers accept matchers as arguments:
10
-
11
- * `change { }.by(matcher)`
12
- * `change { }.from(matcher).to(matcher)`
13
- * `contain_exactly(matcher, matcher, matcher)`
14
- * `end_with(matcher, matcher)`
15
- * `include(matcher, matcher)`
16
- * `include(:key => matcher, :other => matcher)`
17
- * `match(arbitrary_nested_structure_with_matchers)`
18
- * `output(matcher).to_stdout`
19
- * `output(matcher).to_stderr`
20
- * `raise_error(ErrorClass, matcher)`
21
- * `start_with(matcher, matcher)`
22
- * `throw_symbol(:sym, matcher)`
23
- * `yield_with_args(matcher, matcher)`
24
- * `yield_successive_args(matcher, matcher)`
25
-
26
- Note that many built-in matchers do not accept matcher arguments
27
- because they have precise semantics that do not allow for a matcher
28
- argument. For example, `equal(some_object)` is designed to pass only
29
- if the actual and expected arguments are references to the same object.
30
- It would not make sense to support a matcher argument here.
31
-
32
- All of RSpec's built-in matchers have one or more aliases that allow
33
- you to use a noun-phrase rather than verb form since they read better
34
- as composed arguments. They also provide customized failure output so
35
- that the failure message reads better as well.
36
-
37
- A full list of these aliases is out of scope here, but here are some
38
- of the aliases used below:
39
-
40
- * `be < 2` => `a_value < 2`
41
- * `be > 2` => `a_value > 2`
42
- * `be_an_instance_of` => `an_instance_of`
43
- * `be_within` => `a_value_within`
44
- * `contain_exactly` => `a_collection_containing_exactly`
45
- * `end_with` => `a_string_ending_with`, `ending_with`
46
- * `match` => `a_string_matching`
47
- * `start_with` => `a_string_starting_with`
48
-
49
- For a full list, see the API docs for the `RSpec::Matchers` module.
50
-
51
- Scenario: Composing matchers with `change`
52
- Given a file named "change_spec.rb" with:
53
- """
54
- describe "Passing matchers to `change`" do
55
- specify "you can pass a matcher to `by`" do
56
- k = 0
57
- expect { k += 1.05 }.to change { k }.
58
- by( a_value_within(0.1).of(1.0) )
59
- end
60
-
61
- specify "you can pass matchers to `from` and `to" do
62
- s = "food"
63
- expect { s = "barn" }.to change { s }.
64
- from( a_string_matching(/foo/) ).
65
- to( a_string_matching(/bar/) )
66
- end
67
- end
68
- """
69
- When I run `rspec change_spec.rb`
70
- Then the examples should all pass
71
-
72
- Scenario: Composing matchers with `contain_exactly`
73
- Given a file named "contain_exactly_spec.rb" with:
74
- """
75
- describe "Passing matchers to `contain_exactly`" do
76
- specify "you can pass matchers in place of exact values" do
77
- expect(["barn", 2.45]).to contain_exactly(
78
- a_value_within(0.1).of(2.5),
79
- a_string_starting_with("bar")
80
- )
81
- end
82
- end
83
- """
84
- When I run `rspec contain_exactly_spec.rb`
85
- Then the examples should all pass
86
-
87
- Scenario: Composing matchers with `end_with`
88
- Given a file named "end_with_spec.rb" with:
89
- """
90
- describe "Passing matchers to `end_with`" do
91
- specify "you can pass matchers in place of exact values" do
92
- expect(["barn", "food", 2.45]).to end_with(
93
- a_string_matching("foo"),
94
- a_value > 2
95
- )
96
- end
97
- end
98
- """
99
- When I run `rspec end_with_spec.rb`
100
- Then the examples should all pass
101
-
102
- Scenario: Composing matchers with `include`
103
- Given a file named "include_spec.rb" with:
104
- """
105
- describe "Passing matchers to `contain_exactly`" do
106
- specify "you can use matchers in place of array values" do
107
- expect(["barn", 2.45]).to include( a_string_starting_with("bar") )
108
- end
109
-
110
- specify "you can use matchers in place of hash values" do
111
- expect(:a => "food", :b => "good").to include(:a => a_string_matching(/foo/))
112
- end
113
-
114
- specify "you can use matchers in place of hash keys" do
115
- expect("food" => "is good").to include( a_string_matching(/foo/) )
116
- end
117
- end
118
- """
119
- When I run `rspec include_spec.rb`
120
- Then the examples should all pass
121
-
122
- Scenario: Composing matchers with `match`:
123
- Given a file named "match_spec.rb" with:
124
- """
125
- describe "Passing matchers to `match`" do
126
- specify "you can match nested data structures against matchers" do
127
- hash = {
128
- :a => {
129
- :b => ["foo", 5],
130
- :c => { :d => 2.05 }
131
- }
132
- }
133
-
134
- expect(hash).to match(
135
- :a => {
136
- :b => a_collection_containing_exactly(
137
- a_string_starting_with("f"),
138
- an_instance_of(Fixnum)
139
- ),
140
- :c => { :d => (a_value < 3) }
141
- }
142
- )
143
- end
144
- end
145
- """
146
- When I run `rspec match_spec.rb`
147
- Then the examples should all pass
148
-
149
- Scenario: Composing matchers with `output`
150
- Given a file named "output_spec.rb" with:
151
- """
152
- describe "Passing matchers to `output`" do
153
- specify "you can pass a matcher in place of the output (to_stdout)" do
154
- expect {
155
- print 'foo'
156
- }.to output(a_string_starting_with('f')).to_stdout
157
- end
158
- specify "you can pass a matcher in place of the output (to_stderr)" do
159
- expect {
160
- warn 'foo'
161
- }.to output(a_string_starting_with('f')).to_stderr
162
- end
163
- end
164
- """
165
- When I run `rspec output_spec.rb`
166
- Then the examples should all pass
167
-
168
- Scenario: Composing matchers with `raise_error`
169
- Given a file named "raise_error_spec.rb" with:
170
- """
171
- describe "Passing matchers to `raise_error`" do
172
- specify "you can pass a matcher in place of the message" do
173
- expect {
174
- raise RuntimeError, "this goes boom"
175
- }.to raise_error(RuntimeError, a_string_ending_with("boom"))
176
- end
177
- end
178
- """
179
- When I run `rspec raise_error_spec.rb`
180
- Then the examples should all pass
181
-
182
- Scenario: Composing matchers with `start_with`
183
- Given a file named "start_with_spec.rb" with:
184
- """
185
- describe "Passing matchers to `start_with`" do
186
- specify "you can pass matchers in place of exact values" do
187
- expect(["barn", "food", 2.45]).to start_with(
188
- a_string_matching("bar"),
189
- a_string_matching("foo")
190
- )
191
- end
192
- end
193
- """
194
- When I run `rspec start_with_spec.rb`
195
- Then the examples should all pass
196
-
197
- Scenario: Composing matchers with `throw_symbol`
198
- Given a file named "throw_symbol_spec.rb" with:
199
- """
200
- describe "Passing matchers to `throw_symbol`" do
201
- specify "you can pass a matcher in place of a throw arg" do
202
- expect {
203
- throw :pi, Math::PI
204
- }.to throw_symbol(:pi, a_value_within(0.01).of(3.14))
205
- end
206
- end
207
- """
208
- When I run `rspec throw_symbol_spec.rb`
209
- Then the examples should all pass
210
-
211
- Scenario: Composing matchers with `yield_with_args`
212
- Given a file named "yield_with_args_spec.rb" with:
213
- """
214
- describe "Passing matchers to `yield_with_args`" do
215
- specify "you can pass matchers in place of the args" do
216
- expect { |probe|
217
- "food".tap(&probe)
218
- }.to yield_with_args(a_string_matching(/foo/))
219
- end
220
- end
221
- """
222
- When I run `rspec yield_with_args_spec.rb`
223
- Then the examples should all pass
224
-
225
- Scenario: Composing matchers with `yield_successive_args`
226
- Given a file named "yield_successive_args_spec.rb" with:
227
- """
228
- describe "Passing matchers to `yield_successive_args`" do
229
- specify "you can pass matchers in place of the args" do
230
- expect { |probe|
231
- [1, 2, 3].each(&probe)
232
- }.to yield_successive_args(a_value < 2, 2, a_value > 2)
233
- end
234
- end
235
- """
236
- When I run `rspec yield_successive_args_spec.rb`
237
- Then the examples should all pass
238
-
239
- Scenario: Composing matchers using a compound `and` expression
240
- Given a file named "include_spec.rb" with:
241
- """
242
- describe "Passing a compound matcher expression to `include`" do
243
- example do
244
- expect(["food", "drink"]).to include( a_string_starting_with("f").and ending_with("d"))
245
- end
246
- end
247
- """
248
- When I run `rspec include_spec.rb`
249
- Then the examples should all pass
250
-
@@ -1,45 +0,0 @@
1
- Feature: Compound Expectations
2
-
3
- Matchers can be composed using `and` or `or` to make compound expectations.
4
-
5
- Scenario: Use `and` to chain expectations
6
- Given a file named "compound_and_matcher_spec.rb" with:
7
- """ruby
8
- describe "A compound `and` matcher" do
9
- let(:string) { "foo bar bazz" }
10
-
11
- it "passes when both are true" do
12
- expect(string).to start_with("foo").and end_with("bazz")
13
- end
14
-
15
- it "fails when the first matcher fails" do
16
- expect(string).to start_with("bar").and end_with("bazz")
17
- end
18
-
19
- it "fails when the second matcher fails" do
20
- expect(string).to start_with("foo").and end_with("bar")
21
- end
22
- end
23
- """
24
- When I run `rspec compound_and_matcher_spec.rb`
25
- Then the output should contain "3 examples, 2 failures"
26
-
27
- Scenario: Use `or` to chain expectations
28
- Given a file named "stoplight_spec.rb" with:
29
- """ruby
30
- class StopLight
31
- def color
32
- %w[ green yellow red ].shuffle.first
33
- end
34
- end
35
-
36
- describe StopLight, "#color" do
37
- it "is green, yellow or red" do
38
- light = StopLight.new
39
- expect(light.color).to eq("green").or eq("yellow").or eq("red")
40
- end
41
- end
42
- """
43
- When I run `rspec stoplight_spec.rb`
44
- Then the example should pass
45
-
@@ -1,53 +0,0 @@
1
- Feature: access running example
2
-
3
- In order to take advantage of services that are available
4
- in my examples when I'm writing matchers
5
- As a spec author
6
- I want to call methods on the running example
7
-
8
- If the method exists in the context of the example, it gets
9
- called. If not, a NoMethodError is raised on the Matcher itself
10
- (not the example).
11
-
12
- Scenario: call method defined on example from matcher
13
- Given a file named "example_spec.rb" with:
14
- """ruby
15
- RSpec::Matchers.define :bar do
16
- match do |_|
17
- foo == "foo"
18
- end
19
- end
20
-
21
- describe "something" do
22
- def foo
23
- "foo"
24
- end
25
-
26
- it "does something" do
27
- expect("foo").to bar
28
- end
29
- end
30
- """
31
- When I run `rspec ./example_spec.rb`
32
- Then the output should contain "1 example, 0 failures"
33
-
34
- Scenario: call method _not_ defined on example from matcher
35
- Given a file named "example_spec.rb" with:
36
- """ruby
37
- RSpec::Matchers.define :bar do
38
- match do |_|
39
- foo == "foo"
40
- end
41
- end
42
-
43
- describe "something" do
44
- it "does something" do
45
- expect("foo").to bar
46
- end
47
- end
48
- """
49
- When I run `rspec ./example_spec.rb`
50
- Then the output should contain "1 example, 1 failure"
51
- And the output should match /undefined.*method/
52
- And the output should contain "RSpec::Matchers::DSL::Matcher"
53
- And the output should not contain "ExampleGroup"
@@ -1,27 +0,0 @@
1
- Feature: define diffable matcher
2
-
3
- When a matcher is defined as diffable, and the --diff
4
- flag is set, the output will include a diff of the submitted
5
- objects.
6
-
7
- @wip
8
- Scenario: define a diffable matcher
9
- Given a file named "diffable_matcher_spec.rb" with:
10
- """ruby
11
- RSpec::Matchers.define :be_just_like do |expected|
12
- match do |actual|
13
- actual == expected
14
- end
15
-
16
- diffable
17
- end
18
-
19
- describe "this" do
20
- it {should be_just_like("that")}
21
- end
22
- """
23
- When I run `rspec ./diffable_matcher_spec.rb --diff`
24
- Then the exit status should not be 0
25
-
26
- And the output should contain "should be just like that"
27
- And the output should contain "Diff:\n@@ -1,2 +1,2 @@\n-that\n+this"
@@ -1,340 +0,0 @@
1
- Feature: define matcher
2
-
3
- In order to express my domain clearly in my code examples
4
- As an RSpec user
5
- I want a shortcut to define custom matchers
6
-
7
- Scenario: define a matcher with default messages
8
- Given a file named "matcher_with_default_message_spec.rb" with:
9
- """ruby
10
- require 'rspec/expectations'
11
-
12
- RSpec::Matchers.define :be_a_multiple_of do |expected|
13
- match do |actual|
14
- actual % expected == 0
15
- end
16
- end
17
-
18
- describe 9 do
19
- it {should be_a_multiple_of(3)}
20
- end
21
-
22
- describe 9 do
23
- it {should_not be_a_multiple_of(4)}
24
- end
25
-
26
- # fail intentionally to generate expected output
27
- describe 9 do
28
- it {should be_a_multiple_of(4)}
29
- end
30
-
31
- # fail intentionally to generate expected output
32
- describe 9 do
33
- it {should_not be_a_multiple_of(3)}
34
- end
35
-
36
- """
37
- When I run `rspec ./matcher_with_default_message_spec.rb --format documentation`
38
- Then the exit status should not be 0
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"
42
- And the output should contain "Failure/Error: it {should be_a_multiple_of(4)}"
43
- And the output should contain "Failure/Error: it {should_not be_a_multiple_of(3)}"
44
-
45
- And the output should contain "4 examples, 2 failures"
46
- And the output should contain "expected 9 to be a multiple of 4"
47
- And the output should contain "expected 9 not to be a multiple of 3"
48
-
49
- Scenario: overriding the failure_message
50
- Given a file named "matcher_with_failure_message_spec.rb" with:
51
- """ruby
52
- require 'rspec/expectations'
53
-
54
- RSpec::Matchers.define :be_a_multiple_of do |expected|
55
- match do |actual|
56
- actual % expected == 0
57
- end
58
- failure_message do |actual|
59
- "expected that #{actual} would be a multiple of #{expected}"
60
- end
61
- end
62
-
63
- # fail intentionally to generate expected output
64
- describe 9 do
65
- it {should be_a_multiple_of(4)}
66
- end
67
- """
68
- When I run `rspec ./matcher_with_failure_message_spec.rb`
69
- Then the exit status should not be 0
70
- And the stdout should contain "1 example, 1 failure"
71
- And the stdout should contain "expected that 9 would be a multiple of 4"
72
-
73
- Scenario: overriding the failure_message_when_negated
74
- Given a file named "matcher_with_failure_for_message_spec.rb" with:
75
- """ruby
76
- require 'rspec/expectations'
77
-
78
- RSpec::Matchers.define :be_a_multiple_of do |expected|
79
- match do |actual|
80
- actual % expected == 0
81
- end
82
- failure_message_when_negated do |actual|
83
- "expected that #{actual} would not be a multiple of #{expected}"
84
- end
85
- end
86
-
87
- # fail intentionally to generate expected output
88
- describe 9 do
89
- it {should_not be_a_multiple_of(3)}
90
- end
91
- """
92
- When I run `rspec ./matcher_with_failure_for_message_spec.rb`
93
- Then the exit status should not be 0
94
- And the stdout should contain "1 example, 1 failure"
95
- And the stdout should contain "expected that 9 would not be a multiple of 3"
96
-
97
- Scenario: overriding the description
98
- Given a file named "matcher_overriding_description_spec.rb" with:
99
- """ruby
100
- require 'rspec/expectations'
101
-
102
- RSpec::Matchers.define :be_a_multiple_of do |expected|
103
- match do |actual|
104
- actual % expected == 0
105
- end
106
- description do
107
- "be multiple of #{expected}"
108
- end
109
- end
110
-
111
- describe 9 do
112
- it {should be_a_multiple_of(3)}
113
- end
114
-
115
- describe 9 do
116
- it {should_not be_a_multiple_of(4)}
117
- end
118
- """
119
- When I run `rspec ./matcher_overriding_description_spec.rb --format documentation`
120
- Then the exit status should be 0
121
- 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"
124
-
125
- Scenario: with no args
126
- Given a file named "matcher_with_no_args_spec.rb" with:
127
- """ruby
128
- require 'rspec/expectations'
129
-
130
- RSpec::Matchers.define :have_7_fingers do
131
- match do |thing|
132
- thing.fingers.length == 7
133
- end
134
- end
135
-
136
- class Thing
137
- def fingers; (1..7).collect {"finger"}; end
138
- end
139
-
140
- describe Thing do
141
- it {should have_7_fingers}
142
- end
143
- """
144
- When I run `rspec ./matcher_with_no_args_spec.rb --format documentation`
145
- Then the exit status should be 0
146
- And the stdout should contain "1 example, 0 failures"
147
- And the stdout should contain "should have 7 fingers"
148
-
149
- Scenario: with multiple args
150
- Given a file named "matcher_with_multiple_args_spec.rb" with:
151
- """ruby
152
- require 'rspec/expectations'
153
-
154
- RSpec::Matchers.define :be_the_sum_of do |a,b,c,d|
155
- match do |sum|
156
- a + b + c + d == sum
157
- end
158
- end
159
-
160
- describe 10 do
161
- it {should be_the_sum_of(1,2,3,4)}
162
- end
163
- """
164
- When I run `rspec ./matcher_with_multiple_args_spec.rb --format documentation`
165
- Then the exit status should be 0
166
- 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
-
169
- Scenario: with helper methods
170
- Given a file named "matcher_with_internal_helper_spec.rb" with:
171
- """ruby
172
- require 'rspec/expectations'
173
-
174
- RSpec::Matchers.define :have_same_elements_as do |sample|
175
- match do |actual|
176
- similar?(sample, actual)
177
- end
178
-
179
- def similar?(a, b)
180
- a.sort == b.sort
181
- end
182
- end
183
-
184
- describe "these two arrays" do
185
- specify "should be similar" do
186
- expect([1,2,3]).to have_same_elements_as([2,3,1])
187
- end
188
- end
189
- """
190
- When I run `rspec ./matcher_with_internal_helper_spec.rb`
191
- Then the exit status should be 0
192
- And the stdout should contain "1 example, 0 failures"
193
-
194
- Scenario: scoped in a module
195
- Given a file named "scoped_matcher_spec.rb" with:
196
- """ruby
197
- require 'rspec/expectations'
198
-
199
- module MyHelpers
200
- extend RSpec::Matchers::DSL
201
-
202
- matcher :be_just_like do |expected|
203
- match {|actual| actual == expected}
204
- end
205
- end
206
-
207
- describe "group with MyHelpers" do
208
- include MyHelpers
209
- it "has access to the defined matcher" do
210
- expect(5).to be_just_like(5)
211
- end
212
- end
213
-
214
- describe "group without MyHelpers" do
215
- it "does not have access to the defined matcher" do
216
- expect do
217
- expect(5).to be_just_like(5)
218
- end.to raise_exception
219
- end
220
- end
221
- """
222
-
223
- When I run `rspec ./scoped_matcher_spec.rb`
224
- Then the stdout should contain "2 examples, 0 failures"
225
-
226
- Scenario: scoped in an example group
227
- Given a file named "scoped_matcher_spec.rb" with:
228
- """ruby
229
- require 'rspec/expectations'
230
-
231
- describe "group with matcher" do
232
- matcher :be_just_like do |expected|
233
- match {|actual| actual == expected}
234
- end
235
-
236
- it "has access to the defined matcher" do
237
- expect(5).to be_just_like(5)
238
- end
239
-
240
- describe "nested group" do
241
- it "has access to the defined matcher" do
242
- expect(5).to be_just_like(5)
243
- end
244
- end
245
-
246
- end
247
-
248
- describe "group without matcher" do
249
- it "does not have access to the defined matcher" do
250
- expect do
251
- expect(5).to be_just_like(5)
252
- end.to raise_exception
253
- end
254
- end
255
- """
256
-
257
- When I run `rspec scoped_matcher_spec.rb`
258
- Then the output should contain "3 examples, 0 failures"
259
-
260
- Scenario: matcher with separate logic for should and should_not
261
- Given a file named "matcher_with_separate_should_not_logic_spec.rb" with:
262
- """ruby
263
- RSpec::Matchers.define :contain do |*expected|
264
- match do |actual|
265
- expected.all? { |e| actual.include?(e) }
266
- end
267
-
268
- match_when_negated do |actual|
269
- expected.none? { |e| actual.include?(e) }
270
- end
271
- end
272
-
273
- describe [1, 2, 3] do
274
- it { should contain(1, 2) }
275
- it { should_not contain(4, 5, 6) }
276
-
277
- # deliberate failures
278
- it { should contain(1, 4) }
279
- it { should_not contain(1, 4) }
280
- end
281
- """
282
- When I run `rspec matcher_with_separate_should_not_logic_spec.rb`
283
- Then the output should contain all of these:
284
- | 4 examples, 2 failures |
285
- | expected [1, 2, 3] to contain 1 and 4 |
286
- | expected [1, 2, 3] not to contain 1 and 4 |
287
-
288
- Scenario: use define_method to create a helper method with access to matcher params
289
- Given a file named "define_method_spec.rb" with:
290
- """ruby
291
- RSpec::Matchers.define :be_a_multiple_of do |expected|
292
- define_method :is_multiple? do |actual|
293
- actual % expected == 0
294
- end
295
- match { |actual| is_multiple?(actual) }
296
- end
297
-
298
- describe 9 do
299
- it { should be_a_multiple_of(3) }
300
- it { should_not be_a_multiple_of(4) }
301
-
302
- # deliberate failures
303
- it { should be_a_multiple_of(2) }
304
- it { should_not be_a_multiple_of(3) }
305
- end
306
- """
307
- When I run `rspec define_method_spec.rb`
308
- Then the output should contain all of these:
309
- | 4 examples, 2 failures |
310
- | expected 9 to be a multiple of 2 |
311
- | expected 9 not to be a multiple of 3 |
312
-
313
- Scenario: include a module with helper methods in the matcher
314
- Given a file named "include_module_spec.rb" with:
315
- """ruby
316
- module MatcherHelpers
317
- def is_multiple?(actual, expected)
318
- actual % expected == 0
319
- end
320
- end
321
-
322
- RSpec::Matchers.define :be_a_multiple_of do |expected|
323
- include MatcherHelpers
324
- match { |actual| is_multiple?(actual, expected) }
325
- end
326
-
327
- describe 9 do
328
- it { should be_a_multiple_of(3) }
329
- it { should_not be_a_multiple_of(4) }
330
-
331
- # deliberate failures
332
- it { should be_a_multiple_of(2) }
333
- it { should_not be_a_multiple_of(3) }
334
- end
335
- """
336
- When I run `rspec include_module_spec.rb`
337
- Then the output should contain all of these:
338
- | 4 examples, 2 failures |
339
- | expected 9 to be a multiple of 2 |
340
- | expected 9 not to be a multiple of 3 |