rspec-expectations 3.0.0.beta2 → 3.0.0.rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,135 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module RSpec::Matchers::BuiltIn
4
- describe ThrowSymbol do
5
- it_behaves_like("an RSpec matcher", :valid_value => lambda { throw :foo },
6
- :invalid_value => lambda { }) do
7
- let(:matcher) { throw_symbol(:foo) }
8
- end
9
-
10
- describe "with no args" do
11
- before(:each) { @matcher = throw_symbol }
12
-
13
- it "matches if any Symbol is thrown" do
14
- expect(@matcher.matches?(lambda{ throw :sym })).to be_truthy
15
- end
16
- it "matches if any Symbol is thrown with an arg" do
17
- expect(@matcher.matches?(lambda{ throw :sym, "argument" })).to be_truthy
18
- end
19
- it "does not match if no Symbol is thrown" do
20
- expect(@matcher.matches?(lambda{ })).to be_falsey
21
- end
22
- it "provides a failure message" do
23
- @matcher.matches?(lambda{})
24
- expect(@matcher.failure_message).to eq "expected a Symbol to be thrown, got nothing"
25
- end
26
- it "provides a negative failure message" do
27
- @matcher.matches?(lambda{ throw :sym})
28
- expect(@matcher.failure_message_when_negated).to eq "expected no Symbol to be thrown, got :sym"
29
- end
30
- end
31
-
32
- describe "with a symbol" do
33
- before(:each) { @matcher = throw_symbol(:sym) }
34
-
35
- it "matches if correct Symbol is thrown" do
36
- expect(@matcher.matches?(lambda{ throw :sym })).to be_truthy
37
- end
38
- it "matches if correct Symbol is thrown with an arg" do
39
- expect(@matcher.matches?(lambda{ throw :sym, "argument" })).to be_truthy
40
- end
41
- it "does not match if no Symbol is thrown" do
42
- expect(@matcher.matches?(lambda{ })).to be_falsey
43
- end
44
- it "does not match if correct Symbol is thrown" do
45
- expect(@matcher.matches?(lambda{ throw :other_sym })).to be_falsey
46
- end
47
- it "provides a failure message when no Symbol is thrown" do
48
- @matcher.matches?(lambda{})
49
- expect(@matcher.failure_message).to eq "expected :sym to be thrown, got nothing"
50
- end
51
- it "provides a failure message when wrong Symbol is thrown" do
52
- @matcher.matches?(lambda{ throw :other_sym })
53
- expect(@matcher.failure_message).to eq "expected :sym to be thrown, got :other_sym"
54
- end
55
- it "provides a negative failure message" do
56
- @matcher.matches?(lambda{ throw :sym })
57
- expect(@matcher.failure_message_when_negated).to eq "expected :sym not to be thrown, got :sym"
58
- end
59
- it "only matches NameErrors raised by uncaught throws" do
60
- expect {
61
- expect(@matcher.matches?(lambda{ sym })).to be_falsey
62
- }.to raise_error(NameError)
63
- end
64
- end
65
-
66
- describe "with a symbol and an arg" do
67
- before(:each) { @matcher = throw_symbol(:sym, "a") }
68
-
69
- it "matches if correct Symbol and args are thrown" do
70
- expect(@matcher.matches?(lambda{ throw :sym, "a" })).to be_truthy
71
- end
72
- it "does not match if nothing is thrown" do
73
- expect(@matcher.matches?(lambda{ })).to be_falsey
74
- end
75
- it "does not match if other Symbol is thrown" do
76
- expect(@matcher.matches?(lambda{ throw :other_sym, "a" })).to be_falsey
77
- end
78
- it "does not match if no arg is thrown" do
79
- expect(@matcher.matches?(lambda{ throw :sym })).to be_falsey
80
- end
81
- it "does not match if wrong arg is thrown" do
82
- expect(@matcher.matches?(lambda{ throw :sym, "b" })).to be_falsey
83
- end
84
- it "provides a failure message when no Symbol is thrown" do
85
- @matcher.matches?(lambda{})
86
- expect(@matcher.failure_message).to eq %q[expected :sym with "a" to be thrown, got nothing]
87
- end
88
- it "provides a failure message when wrong Symbol is thrown" do
89
- @matcher.matches?(lambda{ throw :other_sym })
90
- expect(@matcher.failure_message).to eq %q[expected :sym with "a" to be thrown, got :other_sym]
91
- end
92
- it "provides a failure message when wrong arg is thrown" do
93
- @matcher.matches?(lambda{ throw :sym, "b" })
94
- expect(@matcher.failure_message).to eq %q[expected :sym with "a" to be thrown, got :sym with "b"]
95
- end
96
- it "provides a failure message when no arg is thrown" do
97
- @matcher.matches?(lambda{ throw :sym })
98
- expect(@matcher.failure_message).to eq %q[expected :sym with "a" to be thrown, got :sym with no argument]
99
- end
100
- it "provides a negative failure message" do
101
- @matcher.matches?(lambda{ throw :sym })
102
- expect(@matcher.failure_message_when_negated).to eq %q[expected :sym with "a" not to be thrown, got :sym with no argument]
103
- end
104
- it "only matches NameErrors raised by uncaught throws" do
105
- expect {
106
- expect(@matcher.matches?(lambda{ sym })).to be_falsey
107
- }.to raise_error(NameError)
108
- end
109
- it "raises other errors" do
110
- expect {
111
- @matcher.matches?(lambda { raise "Boom" })
112
- }.to raise_error(/Boom/)
113
- end
114
- end
115
-
116
- describe "composing with other matchers" do
117
- it 'passes when the matcher matches the thrown arg' do
118
- expect {
119
- throw :foo, "bar"
120
- }.to throw_symbol(:foo, a_string_matching(/bar/))
121
- end
122
-
123
- it 'fails when the matcher does not match the thrown arg' do
124
- expect {
125
- expect { throw :foo, "bar" }.to throw_symbol(:foo, a_string_matching(/foo/))
126
- }.to fail_with('expected :foo with a string matching /foo/ to be thrown, got :foo with "bar"')
127
- end
128
-
129
- it 'provides a description' do
130
- description = throw_symbol(:foo, a_string_matching(/bar/)).description
131
- expect(description).to eq("throw :foo with a string matching /bar/")
132
- end
133
- end
134
- end
135
- end
@@ -1,627 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module YieldHelpers
4
- # these helpers are prefixed with an underscore to prevent
5
- # collisions with the matchers (some of which have the same names)
6
- def _dont_yield
7
- end
8
-
9
- def _yield_with_no_args
10
- yield
11
- end
12
-
13
- def _yield_with_args(*args)
14
- yield(*args)
15
- end
16
- end
17
-
18
- class InstanceEvaler
19
-
20
- def yield_with_no_args(&block)
21
- instance_exec(&block)
22
- end
23
-
24
- def yield_with_args(*args, &block)
25
- instance_exec(*args, &block)
26
- end
27
-
28
- def each_arg(*args, &block)
29
- args.each do |arg|
30
- instance_exec(arg, &block)
31
- end
32
- end
33
- end
34
-
35
- describe "yield_control matcher" do
36
- include YieldHelpers
37
- extend YieldHelpers
38
-
39
- it_behaves_like "an RSpec matcher",
40
- :valid_value => lambda { |b| _yield_with_no_args(&b) },
41
- :invalid_value => lambda { |b| _dont_yield(&b) } do
42
- let(:matcher) { yield_control }
43
- end
44
-
45
- it 'has a description' do
46
- expect(yield_control.description).to eq("yield control")
47
- end
48
-
49
- describe "expect {...}.to yield_control" do
50
- it 'passes if the block yields, regardless of the number of yielded arguments' do
51
- expect { |b| _yield_with_no_args(&b) }.to yield_control
52
- expect { |b| _yield_with_args(1, 2, &b) }.to yield_control
53
- end
54
-
55
- it 'passes if the block yields using instance_exec' do
56
- expect { |b| InstanceEvaler.new.yield_with_no_args(&b) }.to yield_control
57
- end
58
-
59
- it 'fails if the block does not yield' do
60
- expect {
61
- expect { |b| _dont_yield(&b) }.to yield_control
62
- }.to fail_with(/expected given block to yield control/)
63
- end
64
-
65
- it 'does not return a meaningful value from the block' do
66
- val = nil
67
- expect { |b| val = _yield_with_args(&b) }.to yield_control
68
- expect(val).to be_nil
69
- end
70
-
71
- context "with exact count" do
72
- it 'fails if the block yields wrong number of times' do
73
- expect {
74
- expect { |b| [1, 2, 3].each(&b) }.to yield_control.twice
75
- }.to fail_with(/expected given block to yield control twice/)
76
-
77
- expect {
78
- expect { |b| [1, 2].each(&b) }.to yield_control.exactly(3).times
79
- }.to fail_with(/expected given block to yield control 3 times/)
80
- end
81
-
82
- it 'passes if the block yields the specified number of times' do
83
- expect { |b| [1].each(&b) }.to yield_control.once
84
- expect { |b| [1, 2].each(&b) }.to yield_control.twice
85
- expect { |b| [1, 2, 3].each(&b) }.to yield_control.exactly(3).times
86
- end
87
- end
88
-
89
- context "with at_least count" do
90
- it 'passes if the block yields the given number of times' do
91
- expect { |b| [1, 2].each(&b) }.to yield_control.at_least(2).times
92
- expect { |b| [1, 2, 3].each(&b) }.to yield_control.at_least(3).times
93
- end
94
-
95
- it 'passes if the block yields more times' do
96
- expect { |b| [1, 2, 3].each(&b) }.to yield_control.at_least(2).times
97
- expect { |b| [1, 2, 3, 4].each(&b) }.to yield_control.at_least(3).times
98
- end
99
-
100
- it 'allows :once and :twice to be passed as counts' do
101
- expect { |b| [1].each(&b) }.to yield_control.at_least(:once)
102
- expect { |b| [1, 2].each(&b) }.to yield_control.at_least(:once)
103
-
104
- expect {
105
- expect { |b| [].each(&b) }.to yield_control.at_least(:once)
106
- }.to fail_with(/at least once/)
107
-
108
- expect { |b| [1, 2].each(&b) }.to yield_control.at_least(:twice)
109
- expect { |b| [1, 2, 3].each(&b) }.to yield_control.at_least(:twice)
110
-
111
- expect {
112
- expect { |b| [1].each(&b) }.to yield_control.at_least(:twice)
113
- }.to fail_with(/at least twice/)
114
- end
115
-
116
- it 'fails if the block yields too few times' do
117
- expect {
118
- expect { |b| _yield_with_no_args(&b) }.to yield_control.at_least(2).times
119
- }.to fail_with(/expected given block to yield control at least twice/)
120
- end
121
- end
122
-
123
- context "with at_most count" do
124
- it 'passes if the block yields the given number of times' do
125
- expect { |b| [1, 2].each(&b) }.to yield_control.at_most(2).times
126
- expect { |b| [1, 2, 3].each(&b) }.to yield_control.at_most(3).times
127
- end
128
-
129
- it 'passes if the block yields fewer times' do
130
- expect { |b| [1, 2].each(&b) }.to yield_control.at_most(3).times
131
- end
132
-
133
- it 'allows :once and :twice to be passed as counts' do
134
- expect { |b| [1].each(&b) }.to yield_control.at_most(:once)
135
-
136
- expect {
137
- expect { |b| [1, 2].each(&b) }.to yield_control.at_most(:once)
138
- }.to fail_with(/expected given block to yield control at most once/)
139
-
140
- expect { |b| [1, 2].each(&b) }.to yield_control.at_most(:twice)
141
-
142
- expect {
143
- expect { |b| [1, 2, 3].each(&b) }.to yield_control.at_most(:twice)
144
- }.to fail_with(/expected given block to yield control at most twice/)
145
- end
146
-
147
- it 'fails if the block yields too many times' do
148
- expect {
149
- expect { |b| [1, 2, 3].each(&b) }.to yield_control.at_most(2).times
150
- }.to fail_with(/expected given block to yield control at most twice/)
151
- end
152
- end
153
- end
154
-
155
- describe "expect {...}.not_to yield_control" do
156
- it 'passes if the block does not yield' do
157
- expect { |b| _dont_yield(&b) }.not_to yield_control
158
- end
159
-
160
- it 'fails if the block does yield' do
161
- expect {
162
- expect { |b| _yield_with_no_args(&b) }.not_to yield_control
163
- }.to fail_with(/expected given block not to yield control/)
164
- end
165
-
166
- it 'fails if the expect block does not accept an argument' do
167
- expect {
168
- expect { }.not_to yield_control
169
- }.to raise_error(/expect block must accept an argument/)
170
- end
171
-
172
- it 'raises an error if the expect block arg is not passed to a method as a block' do
173
- expect {
174
- expect { |b| }.not_to yield_control
175
- }.to raise_error(/must pass the argument.*as a block/)
176
- end
177
- end
178
- end
179
-
180
- describe "yield_with_no_args matcher" do
181
- include YieldHelpers
182
- extend YieldHelpers
183
-
184
- it_behaves_like "an RSpec matcher",
185
- :valid_value => lambda { |b| _yield_with_no_args(&b) },
186
- :invalid_value => lambda { |b| _dont_yield(&b) } do
187
- let(:matcher) { yield_with_no_args }
188
- end
189
-
190
- it 'has a description' do
191
- expect(yield_with_no_args.description).to eq("yield with no args")
192
- end
193
-
194
- it 'does not return a meaningful value from the block' do
195
- val = nil
196
- expect { |b| val = _yield_with_no_args(&b) }.to yield_with_no_args
197
- expect(val).to be_nil
198
- end
199
-
200
- describe "expect {...}.to yield_with_no_args" do
201
- it 'passes if the block yields with no args' do
202
- expect { |b| _yield_with_no_args(&b) }.to yield_with_no_args
203
- end
204
-
205
- it 'passes if the block yields with no args using instance_exec' do
206
- expect { |b| InstanceEvaler.new.yield_with_no_args(&b) }.to yield_with_no_args
207
- end
208
-
209
- it 'fails if the block does not yield' do
210
- expect {
211
- expect { |b| _dont_yield(&b) }.to yield_with_no_args
212
- }.to fail_with(/expected given block to yield with no arguments, but did not yield/)
213
- end
214
-
215
- it 'fails if the block yields with args' do
216
- expect {
217
- expect { |b| _yield_with_args(1, &b) }.to yield_with_no_args
218
- }.to fail_with(/expected given block to yield with no arguments, but yielded with arguments/)
219
- end
220
-
221
- it 'fails if the block yields with arg false' do
222
- expect {
223
- expect { |b| _yield_with_args(false, &b) }.to yield_with_no_args
224
- }.to fail_with(/expected given block to yield with no arguments, but yielded with arguments/)
225
- end
226
-
227
- it 'raises an error if it yields multiple times' do
228
- expect {
229
- expect { |b| [1, 2].each(&b) }.to yield_with_no_args
230
- }.to raise_error(/not designed.*yields multiple times/)
231
- end
232
- end
233
-
234
- describe "expect {...}.not_to yield_with_no_args" do
235
- it "passes if the block does not yield" do
236
- expect { |b| _dont_yield(&b) }.not_to yield_with_no_args
237
- end
238
-
239
- it "passes if the block yields with args" do
240
- expect { |b| _yield_with_args(1, &b) }.not_to yield_with_no_args
241
- end
242
-
243
- it "fails if the block yields with no args" do
244
- expect {
245
- expect { |b| _yield_with_no_args(&b) }.not_to yield_with_no_args
246
- }.to fail_with(/expected given block not to yield with no arguments, but did/)
247
- end
248
-
249
- it 'fails if the expect block does not accept an argument' do
250
- expect {
251
- expect { }.not_to yield_with_no_args
252
- }.to raise_error(/expect block must accept an argument/)
253
- end
254
-
255
- it 'raises an error if the expect block arg is not passed to a method as a block' do
256
- expect {
257
- expect { |b| }.not_to yield_with_no_args
258
- }.to raise_error(/must pass the argument.*as a block/)
259
- end
260
- end
261
- end
262
-
263
- describe "yield_with_args matcher" do
264
- include YieldHelpers
265
- extend YieldHelpers
266
-
267
- it_behaves_like "an RSpec matcher",
268
- :valid_value => lambda { |b| _yield_with_args(1, &b) },
269
- :invalid_value => lambda { |b| _dont_yield(&b) } do
270
- let(:matcher) { yield_with_args }
271
- end
272
-
273
- it 'has a description' do
274
- expect(yield_with_args.description).to eq("yield with args")
275
- expect(yield_with_args(1, 3).description).to eq("yield with args(1, 3)")
276
- expect(yield_with_args(false).description).to eq("yield with args(false)")
277
- end
278
-
279
- it 'does not return a meaningful value from the block' do
280
- val = nil
281
- expect { |b| val = _yield_with_args(1, &b) }.to yield_with_args(1)
282
- expect(val).to be_nil
283
- end
284
-
285
- describe "expect {...}.to yield_with_args" do
286
- it 'passes if the block yields with arguments' do
287
- expect { |b| _yield_with_args(1, &b) }.to yield_with_args
288
- end
289
-
290
- it 'fails if the block does not yield' do
291
- expect {
292
- expect { |b| _dont_yield(&b) }.to yield_with_args
293
- }.to fail_with(/expected given block to yield with arguments, but did not yield/)
294
- end
295
-
296
- it 'fails if the block yields with no arguments' do
297
- expect {
298
- expect { |b| _yield_with_no_args(&b) }.to yield_with_args
299
- }.to fail_with(/expected given block to yield with arguments, but yielded with no arguments/)
300
- end
301
-
302
- it 'raises an error if it yields multiple times' do
303
- expect {
304
- expect { |b| [1, 2].each(&b) }.to yield_with_args
305
- }.to raise_error(/not designed.*yields multiple times/)
306
- end
307
- end
308
-
309
- describe "expect {...}.not_to yield_with_args" do
310
- it 'fails if the block yields with arguments' do
311
- expect {
312
- expect { |b| _yield_with_args(1, &b) }.not_to yield_with_args
313
- }.to fail_with(/expected given block not to yield with arguments, but did/)
314
- end
315
-
316
- it 'passes if the block does not yield' do
317
- expect { |b| _dont_yield(&b) }.not_to yield_with_args
318
- end
319
-
320
- it 'passes if the block yields with no arguments' do
321
- expect { |b| _yield_with_no_args(&b) }.not_to yield_with_args
322
- end
323
-
324
- it 'fails if the expect block does not accept an argument' do
325
- expect {
326
- expect { }.not_to yield_with_args
327
- }.to raise_error(/expect block must accept an argument/)
328
- end
329
-
330
- it 'raises an error if the expect block arg is not passed to a method as a block' do
331
- expect {
332
- expect { |b| }.not_to yield_with_args
333
- }.to raise_error(/must pass the argument.*as a block/)
334
- end
335
- end
336
-
337
- describe "expect {...}.to yield_with_args(3, 17)" do
338
- it 'passes if the block yields with the given arguments' do
339
- expect { |b| _yield_with_args(3, 17, &b) }.to yield_with_args(3, 17)
340
- end
341
-
342
- it 'passes if the block yields with the given arguments using instance_exec' do
343
- expect { |b| InstanceEvaler.new.yield_with_args(3, 17, &b) }.to yield_with_args(3, 17)
344
- end
345
-
346
- it 'fails if the block does not yield' do
347
- expect {
348
- expect { |b| _dont_yield(&b) }.to yield_with_args(3, 17)
349
- }.to fail_with(/expected given block to yield with arguments, but did not yield/)
350
- end
351
-
352
- it 'fails if the block yields with no arguments' do
353
- expect {
354
- expect { |b| _yield_with_no_args(&b) }.to yield_with_args(3, 17)
355
- }.to fail_with(/expected given block to yield with arguments, but yielded with unexpected arguments/)
356
- end
357
-
358
- it 'fails if the block yields with different arguments' do
359
- expect {
360
- expect { |b| _yield_with_args("a", "b", &b) }.to yield_with_args("a", "c")
361
- }.to fail_with(/expected given block to yield with arguments, but yielded with unexpected arguments/)
362
- end
363
- end
364
-
365
- describe "expect {...}.to yield_with_args(matcher, matcher)" do
366
- it 'passes when the matchers match the args' do
367
- expect { |b|
368
- _yield_with_args(1.1, "food", &b)
369
- }.to yield_with_args(a_value_within(0.2).of(1), a_string_matching(/foo/))
370
- end
371
-
372
- it 'provides a description' do
373
- description = yield_with_args(a_value_within(0.2).of(1), a_string_matching(/foo/)).description
374
- expect(description).to eq("yield with args(a value within 0.2 of 1, a string matching /foo/)")
375
- end
376
-
377
- it 'fails with a useful error message when the matchers do not match the args' do
378
- expect {
379
- expect { |b|
380
- _yield_with_args(2.1, "food", &b)
381
- }.to yield_with_args(a_value_within(0.2).of(1), a_string_matching(/foo/))
382
- }.to fail_with(dedent <<-EOS)
383
- |expected given block to yield with arguments, but yielded with unexpected arguments
384
- |expected: [(a value within 0.2 of 1), (a string matching /foo/)]
385
- | got: [2.1, "food"]
386
- EOS
387
- end
388
- end
389
-
390
- describe "expect {...}.not_to yield_with_args(3, 17)" do
391
- it 'passes if the block yields with different arguments' do
392
- expect { |b| _yield_with_args("a", "b", &b) }.not_to yield_with_args("a", "c")
393
- end
394
-
395
- it 'fails if the block yields with the given arguments' do
396
- expect {
397
- expect { |b| _yield_with_args("a", "b", &b) }.not_to yield_with_args("a", "b")
398
- }.to fail_with(/expected given block not to yield with arguments, but yielded with expected arguments/)
399
- end
400
- end
401
-
402
- describe "expect {...}.not_to yield_with_args(matcher, matcher)" do
403
- it 'passes when the matchers do not match the args' do
404
- expect { |b|
405
- _yield_with_args(2.1, "food", &b)
406
- }.not_to yield_with_args(a_value_within(0.2).of(1), a_string_matching(/foo/))
407
- end
408
-
409
- it 'fails with a useful error message when the matchers do not match the args' do
410
- expect {
411
- expect { |b|
412
- _yield_with_args(1.1, "food", &b)
413
- }.not_to yield_with_args(a_value_within(0.2).of(1), a_string_matching(/foo/))
414
- }.to fail_with(dedent <<-EOS)
415
- |expected given block not to yield with arguments, but yielded with expected arguments
416
- |expected not: [(a value within 0.2 of 1), (a string matching /foo/)]
417
- | got: [1.1, "food"]
418
- EOS
419
- end
420
- end
421
-
422
- describe "expect {...}.to yield_with_args( false )" do
423
- it 'passes if the block yields with the given arguments' do
424
- expect { |b| _yield_with_args(false, &b) }.to yield_with_args(false)
425
- end
426
-
427
- it 'passes if the block yields with the given arguments using instance_exec' do
428
- expect { |b| InstanceEvaler.new.yield_with_args(false, &b) }.to yield_with_args(false)
429
- end
430
-
431
- it 'fails if the block does not yield' do
432
- expect {
433
- expect { |b| _dont_yield(&b) }.to yield_with_args(false)
434
- }.to fail_with(/expected given block to yield with arguments, but did not yield/)
435
- end
436
-
437
- it 'fails if the block yields with no arguments' do
438
- expect {
439
- expect { |b| _yield_with_no_args(&b) }.to yield_with_args(false)
440
- }.to fail_with(/expected given block to yield with arguments, but yielded with unexpected arguments/)
441
- end
442
-
443
- it 'fails if the block yields with different arguments' do
444
- expect {
445
- expect { |b| _yield_with_args(false, &b) }.to yield_with_args(true)
446
- }.to fail_with(/expected given block to yield with arguments, but yielded with unexpected arguments/)
447
- end
448
- end
449
-
450
- describe "expect {...}.to yield_with_args(/reg/, /ex/)" do
451
- it "passes if the block yields strings matching the regexes" do
452
- expect { |b| _yield_with_args("regular", "expression", &b) }.to yield_with_args(/reg/, /ex/)
453
- end
454
-
455
- it "fails if the block yields strings that do not match the regexes" do
456
- expect {
457
- expect { |b| _yield_with_args("no", "match", &b) }.to yield_with_args(/reg/, /ex/)
458
- }.to fail_with(/expected given block to yield with arguments, but yielded with unexpected arguments/)
459
- end
460
- end
461
-
462
- describe "expect {...}.to yield_with_args(String, Fixnum)" do
463
- it "passes if the block yields objects of the given types" do
464
- expect { |b| _yield_with_args("string", 15, &b) }.to yield_with_args(String, Fixnum)
465
- end
466
-
467
- it "passes if the block yields the given types" do
468
- expect { |b| _yield_with_args(String, Fixnum, &b) }.to yield_with_args(String, Fixnum)
469
- end
470
-
471
- it "fails if the block yields objects of different types" do
472
- expect {
473
- expect { |b| _yield_with_args(15, "string", &b) }.to yield_with_args(String, Fixnum)
474
- }.to fail_with(/expected given block to yield with arguments, but yielded with unexpected arguments/)
475
- end
476
- end
477
- end
478
-
479
- describe "yield_successive_args matcher" do
480
- include YieldHelpers
481
- extend YieldHelpers
482
-
483
- it_behaves_like "an RSpec matcher",
484
- :valid_value => lambda { |b| [1, 2].each(&b) },
485
- :invalid_value => lambda { |b| _dont_yield(&b) } do
486
- let(:matcher) { yield_successive_args(1, 2) }
487
- end
488
-
489
- it 'has a description' do
490
- expect(yield_successive_args(1, 3).description).to eq("yield successive args(1, 3)")
491
- expect(yield_successive_args([:a, 1], [:b, 2]).description).to eq("yield successive args([:a, 1], [:b, 2])")
492
- end
493
-
494
- it 'does not return a meaningful value from the block' do
495
- val = nil
496
- expect { |b| val = _yield_with_args(1, &b) }.to yield_successive_args(1)
497
- expect(val).to be_nil
498
- end
499
-
500
- describe "expect {...}.to yield_successive_args([:a, 1], [:b, 2])" do
501
- it 'passes when the block successively yields the given args' do
502
- expect { |b| [ [:a, 1], [:b, 2] ].each(&b) }.to yield_successive_args([:a, 1], [:b, 2])
503
- end
504
-
505
- it 'fails when the block does not yield that many times' do
506
- expect {
507
- expect { |b| [[:a, 1]].each(&b) }.to yield_successive_args([:a, 1], [:b, 2])
508
- }.to fail_with(/but yielded with unexpected arguments/)
509
- end
510
-
511
- it 'fails when the block yields the right number of times but with different arguments' do
512
- expect {
513
- expect { |b| [ [:a, 1], [:b, 3] ].each(&b) }.to yield_successive_args([:a, 1], [:b, 2])
514
- }.to fail_with(/but yielded with unexpected arguments/)
515
- end
516
- end
517
-
518
- describe "expect {...}.to yield_successive_args(1, 2, 3)" do
519
- it 'passes when the block successively yields the given args' do
520
- expect { |b| [1, 2, 3].each(&b) }.to yield_successive_args(1, 2, 3)
521
- end
522
-
523
- it 'passes when the block successively yields the given args using instance_exec' do
524
- expect { |b| InstanceEvaler.new.each_arg(1, 2, 3, &b) }.to yield_successive_args(1, 2, 3)
525
- end
526
-
527
- it 'fails when the block does not yield the expected args' do
528
- expect {
529
- expect { |b| [1, 2, 4].each(&b) }.to yield_successive_args([:a, 1], [:b, 2])
530
- }.to fail_with(/but yielded with unexpected arguments/)
531
- end
532
- end
533
-
534
- describe "expect {...}.to yield_successive_args(matcher, matcher)" do
535
- it 'passes when the successively yielded args match the matchers' do
536
- expect { |b|
537
- %w[ food barn ].each(&b)
538
- }.to yield_successive_args(a_string_matching(/foo/), a_string_matching(/bar/))
539
- end
540
-
541
- it 'fails when the successively yielded args do not match the matchers' do
542
- expect {
543
- expect { |b|
544
- %w[ barn food ].each(&b)
545
- }.to yield_successive_args(a_string_matching(/foo/), a_string_matching(/bar/))
546
- }.to fail_with(dedent <<-EOS)
547
- |expected given block to yield successively with arguments, but yielded with unexpected arguments
548
- |expected: [(a string matching /foo/), (a string matching /bar/)]
549
- | got: ["barn", "food"]
550
- EOS
551
- end
552
-
553
- it 'provides a description' do
554
- description = yield_successive_args(a_string_matching(/foo/), a_string_matching(/bar/)).description
555
- expect(description).to eq("yield successive args(a string matching /foo/, a string matching /bar/)")
556
- end
557
- end
558
-
559
- describe "expect {...}.not_to yield_successive_args(1, 2, 3)" do
560
- it 'passes when the block does not yield' do
561
- expect { |b| _dont_yield(&b) }.not_to yield_successive_args(1, 2, 3)
562
- end
563
-
564
- it 'passes when the block yields the wrong number of times' do
565
- expect { |b| [1, 2].each(&b) }.not_to yield_successive_args(1, 2, 3)
566
- end
567
-
568
- it 'passes when the block yields the wrong arguments' do
569
- expect { |b| [1, 2, 4].each(&b) }.not_to yield_successive_args(1, 2, 3)
570
- end
571
-
572
- it 'fails when the block yields the given arguments' do
573
- expect {
574
- expect { |b| [1, 2, 3].each(&b) }.not_to yield_successive_args(1, 2, 3)
575
- }.to fail_with(/expected given block not to yield successively/)
576
- end
577
-
578
- it 'fails if the expect block does not accept an argument' do
579
- expect {
580
- expect { }.not_to yield_successive_args(1, 2, 3)
581
- }.to raise_error(/expect block must accept an argument/)
582
- end
583
-
584
- it 'raises an error if the expect block arg is not passed to a method as a block' do
585
- expect {
586
- expect { |b| }.not_to yield_successive_args(1, 2, 3)
587
- }.to raise_error(/must pass the argument.*as a block/)
588
- end
589
- end
590
-
591
- describe "expect {...}.not_to yield_successive_args(matcher, matcher)" do
592
- it 'passes when the successively yielded args match the matchers' do
593
- expect { |b|
594
- %w[ barn food ].each(&b)
595
- }.not_to yield_successive_args(a_string_matching(/foo/), a_string_matching(/bar/))
596
- end
597
-
598
- it 'fails when the successively yielded args do not match the matchers' do
599
- expect {
600
- expect { |b|
601
- %w[ food barn ].each(&b)
602
- }.not_to yield_successive_args(a_string_matching(/foo/), a_string_matching(/bar/))
603
- }.to fail_with(dedent <<-EOS)
604
- |expected given block not to yield successively with arguments, but yielded with expected arguments
605
- |expected not: [(a string matching /foo/), (a string matching /bar/)]
606
- | got: ["food", "barn"]
607
- EOS
608
- end
609
- end
610
-
611
- describe "expect {...}.to yield_successive_args(String, Fixnum)" do
612
- it "passes if the block successively yields objects of the given types" do
613
- expect { |b| ["string", 15].each(&b) }.to yield_successive_args(String, Fixnum)
614
- end
615
-
616
- it "passes if the block yields the given types" do
617
- expect { |b| [String, Fixnum].each(&b) }.to yield_successive_args(String, Fixnum)
618
- end
619
-
620
- it "fails if the block yields objects of different types" do
621
- expect {
622
- expect { |b| [15, "string"].each(&b) }.to yield_successive_args(String, Fixnum)
623
- }.to fail_with(/expected given block to yield successively with arguments/)
624
- end
625
- end
626
- end
627
-