rspec-expectations 2.13.0 → 2.99.0
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.
- checksums.yaml +15 -0
- data/Changelog.md +204 -23
- data/README.md +43 -87
- data/features/README.md +10 -10
- data/features/built_in_matchers/README.md +41 -41
- data/features/built_in_matchers/be.feature +40 -40
- data/features/built_in_matchers/be_within.feature +3 -3
- data/features/built_in_matchers/expect_change.feature +6 -6
- data/features/built_in_matchers/expect_error.feature +2 -2
- data/features/built_in_matchers/start_with.feature +1 -1
- data/features/built_in_matchers/throw_symbol.feature +11 -11
- data/features/built_in_matchers/yield.feature +18 -3
- data/features/custom_matchers/define_diffable_matcher.feature +1 -1
- data/features/custom_matchers/define_matcher_outside_rspec.feature +6 -6
- data/features/custom_matchers/define_matcher_with_fluent_interface.feature +1 -1
- data/features/customized_message.feature +1 -1
- data/features/diffing.feature +1 -1
- data/features/step_definitions/additional_cli_steps.rb +10 -0
- data/features/support/env.rb +10 -1
- data/features/support/rubinius.rb +6 -0
- data/features/syntax_configuration.feature +3 -0
- data/features/test_frameworks/test_unit.feature +55 -17
- data/lib/rspec/expectations/caller_filter.rb +60 -0
- data/lib/rspec/{matchers → expectations}/configuration.rb +5 -3
- data/lib/rspec/expectations/deprecation.rb +20 -31
- data/lib/rspec/expectations/differ.rb +48 -9
- data/lib/rspec/expectations/expectation_target.rb +81 -15
- data/lib/rspec/expectations/extensions/object.rb +2 -12
- data/lib/rspec/expectations/fail_with.rb +11 -1
- data/lib/rspec/expectations/handler.rb +16 -7
- data/lib/rspec/expectations/syntax.rb +5 -7
- data/lib/rspec/expectations/version.rb +1 -1
- data/lib/rspec/expectations.rb +28 -2
- data/lib/rspec/matchers/be_close.rb +4 -1
- data/lib/rspec/matchers/built_in/base_matcher.rb +17 -10
- data/lib/rspec/matchers/built_in/be.rb +69 -32
- data/lib/rspec/matchers/built_in/be_within.rb +9 -3
- data/lib/rspec/matchers/built_in/change.rb +49 -3
- data/lib/rspec/matchers/built_in/has.rb +40 -7
- data/lib/rspec/matchers/built_in/have.rb +170 -5
- data/lib/rspec/matchers/built_in/include.rb +3 -11
- data/lib/rspec/matchers/built_in/match.rb +5 -0
- data/lib/rspec/matchers/built_in/match_array.rb +1 -1
- data/lib/rspec/matchers/built_in/raise_error.rb +34 -8
- data/lib/rspec/matchers/built_in/respond_to.rb +7 -1
- data/lib/rspec/matchers/built_in/satisfy.rb +7 -1
- data/lib/rspec/matchers/built_in/throw_symbol.rb +10 -2
- data/lib/rspec/matchers/built_in/yield.rb +102 -5
- data/lib/rspec/matchers/built_in.rb +2 -2
- data/lib/rspec/matchers/differentiate_block_method_types.rb +55 -0
- data/lib/rspec/matchers/dsl.rb +2 -1
- data/lib/rspec/matchers/match_aliases.rb +22 -0
- data/lib/rspec/matchers/matcher.rb +131 -10
- data/lib/rspec/matchers/operator_matcher.rb +70 -66
- data/lib/rspec/matchers/pretty.rb +11 -1
- data/lib/rspec/matchers/test_unit_integration.rb +28 -0
- data/lib/rspec/matchers.rb +175 -146
- data/lib/rspec-expectations.rb +5 -0
- data/spec/rspec/{matchers → expectations}/configuration_spec.rb +78 -91
- data/spec/rspec/expectations/differ_spec.rb +65 -6
- data/spec/rspec/expectations/expectation_target_spec.rb +72 -3
- data/spec/rspec/expectations/extensions/kernel_spec.rb +9 -5
- data/spec/rspec/expectations/fail_with_spec.rb +19 -0
- data/spec/rspec/expectations/handler_spec.rb +43 -22
- data/spec/rspec/expectations/syntax_spec.rb +51 -9
- data/spec/rspec/expectations_spec.rb +71 -0
- data/spec/rspec/matchers/base_matcher_spec.rb +33 -18
- data/spec/rspec/matchers/be_close_spec.rb +9 -6
- data/spec/rspec/matchers/be_spec.rb +146 -45
- data/spec/rspec/matchers/be_within_spec.rb +8 -0
- data/spec/rspec/matchers/change_spec.rb +107 -7
- data/spec/rspec/matchers/description_generation_spec.rb +38 -20
- data/spec/rspec/matchers/differentiate_block_method_types_spec.rb +39 -0
- data/spec/rspec/matchers/eq_spec.rb +1 -1
- data/spec/rspec/matchers/equal_spec.rb +26 -0
- data/spec/rspec/matchers/exist_spec.rb +9 -9
- data/spec/rspec/matchers/has_spec.rb +25 -1
- data/spec/rspec/matchers/have_spec.rb +411 -3
- data/spec/rspec/matchers/include_matcher_integration_spec.rb +2 -2
- data/spec/rspec/matchers/include_spec.rb +4 -4
- data/spec/rspec/matchers/match_array_spec.rb +1 -1
- data/spec/rspec/matchers/match_spec.rb +14 -1
- data/spec/rspec/matchers/matcher_spec.rb +213 -24
- data/spec/rspec/matchers/operator_matcher_spec.rb +56 -8
- data/spec/rspec/matchers/pretty_spec.rb +23 -0
- data/spec/rspec/matchers/raise_error_spec.rb +242 -102
- data/spec/rspec/matchers/respond_to_spec.rb +4 -4
- data/spec/rspec/matchers/satisfy_spec.rb +1 -1
- data/spec/rspec/matchers/start_with_end_with_spec.rb +2 -2
- data/spec/rspec/matchers/throw_symbol_spec.rb +14 -14
- data/spec/rspec/matchers/yield_spec.rb +81 -4
- data/spec/spec_helper.rb +5 -3
- data/spec/support/helper_methods.rb +42 -0
- data/spec/support/shared_examples.rb +42 -0
- metadata +40 -44
- data/spec/rspec/matchers/matchers_spec.rb +0 -37
|
@@ -2,24 +2,24 @@ require 'spec_helper'
|
|
|
2
2
|
|
|
3
3
|
describe "expect(...).to be_predicate" do
|
|
4
4
|
it "passes when actual returns true for :predicate?" do
|
|
5
|
-
actual =
|
|
5
|
+
actual = double("actual", :happy? => true)
|
|
6
6
|
expect(actual).to be_happy
|
|
7
7
|
end
|
|
8
8
|
|
|
9
9
|
it "passes when actual returns true for :predicates? (present tense)" do
|
|
10
|
-
actual =
|
|
10
|
+
actual = double("actual", :exists? => true, :exist? => true)
|
|
11
11
|
expect(actual).to be_exist
|
|
12
12
|
end
|
|
13
13
|
|
|
14
14
|
it "fails when actual returns false for :predicate?" do
|
|
15
|
-
actual =
|
|
15
|
+
actual = double("actual", :happy? => false)
|
|
16
16
|
expect {
|
|
17
17
|
expect(actual).to be_happy
|
|
18
18
|
}.to fail_with("expected happy? to return true, got false")
|
|
19
19
|
end
|
|
20
20
|
|
|
21
21
|
it "fails when actual returns false for :predicate?" do
|
|
22
|
-
actual =
|
|
22
|
+
actual = double("actual", :happy? => nil)
|
|
23
23
|
expect {
|
|
24
24
|
expect(actual).to be_happy
|
|
25
25
|
}.to fail_with("expected happy? to return true, got nil")
|
|
@@ -31,8 +31,24 @@ describe "expect(...).to be_predicate" do
|
|
|
31
31
|
}.to raise_error(NameError, /happy\?/)
|
|
32
32
|
end
|
|
33
33
|
|
|
34
|
+
it 'warns of deprecation when :predicate? is private' do
|
|
35
|
+
privately_happy = Class.new do
|
|
36
|
+
private
|
|
37
|
+
def happy?
|
|
38
|
+
true
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
allow(RSpec).to receive(:deprecate)
|
|
42
|
+
expect(RSpec).to receive(:deprecate).with(
|
|
43
|
+
"matching with be_happy on private method happy?",
|
|
44
|
+
:replacement => "`expect(object.send(:happy?)).to be_true` or change the method's visibility to public",
|
|
45
|
+
:call_site => RSpec::Mocks::ArgumentMatchers::RegexpMatcher.new(/#{__FILE__}:#{__LINE__ + 2}/)
|
|
46
|
+
)
|
|
47
|
+
expect(privately_happy.new).to be_happy
|
|
48
|
+
end
|
|
49
|
+
|
|
34
50
|
it "fails on error other than NameError" do
|
|
35
|
-
actual =
|
|
51
|
+
actual = double("actual")
|
|
36
52
|
actual.should_receive(:foo?).and_raise("aaaah")
|
|
37
53
|
expect {
|
|
38
54
|
expect(actual).to be_foo
|
|
@@ -46,21 +62,47 @@ describe "expect(...).to be_predicate" do
|
|
|
46
62
|
expect(actual).to be_foo
|
|
47
63
|
}.to raise_error(/aaaah/)
|
|
48
64
|
end
|
|
65
|
+
|
|
66
|
+
it "does not support operator chaining like a basic `be` matcher does" do
|
|
67
|
+
matcher = be_happy
|
|
68
|
+
value = double(:happy? => false)
|
|
69
|
+
expect(matcher == value).to be false
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
it 'warns of deprecation when actual does not respond to :predicate?' do
|
|
73
|
+
oddly_happy_class = Class.new do
|
|
74
|
+
def method_missing(method)
|
|
75
|
+
return true if method == :happy?
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
expect_deprecation_with_call_site(__FILE__, __LINE__ + 1, /Matching with be_happy on an object that doesn't respond to `happy\?`/)
|
|
79
|
+
expect(oddly_happy_class.new).to be_happy
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
it 'does not warn of deprecation when actual responds to present tense predicate' do
|
|
83
|
+
present_happy_class = Class.new do
|
|
84
|
+
def exists?
|
|
85
|
+
true
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
expect_no_deprecation
|
|
89
|
+
expect(present_happy_class.new).to be_exist
|
|
90
|
+
end
|
|
49
91
|
end
|
|
50
92
|
|
|
51
93
|
describe "expect(...).not_to be_predicate" do
|
|
52
94
|
it "passes when actual returns false for :sym?" do
|
|
53
|
-
actual =
|
|
95
|
+
actual = double("actual", :happy? => false)
|
|
54
96
|
expect(actual).not_to be_happy
|
|
55
97
|
end
|
|
56
98
|
|
|
57
99
|
it "passes when actual returns nil for :sym?" do
|
|
58
|
-
actual =
|
|
100
|
+
actual = double("actual", :happy? => nil)
|
|
59
101
|
expect(actual).not_to be_happy
|
|
60
102
|
end
|
|
61
103
|
|
|
62
104
|
it "fails when actual returns true for :sym?" do
|
|
63
|
-
actual =
|
|
105
|
+
actual = double("actual", :happy? => true)
|
|
64
106
|
expect {
|
|
65
107
|
expect(actual).not_to be_happy
|
|
66
108
|
}.to fail_with("expected happy? to return false, got true")
|
|
@@ -75,13 +117,13 @@ end
|
|
|
75
117
|
|
|
76
118
|
describe "expect(...).to be_predicate(*args)" do
|
|
77
119
|
it "passes when actual returns true for :predicate?(*args)" do
|
|
78
|
-
actual =
|
|
120
|
+
actual = double("actual")
|
|
79
121
|
actual.should_receive(:older_than?).with(3).and_return(true)
|
|
80
122
|
expect(actual).to be_older_than(3)
|
|
81
123
|
end
|
|
82
124
|
|
|
83
125
|
it "fails when actual returns false for :predicate?(*args)" do
|
|
84
|
-
actual =
|
|
126
|
+
actual = double("actual")
|
|
85
127
|
actual.should_receive(:older_than?).with(3).and_return(false)
|
|
86
128
|
expect {
|
|
87
129
|
expect(actual).to be_older_than(3)
|
|
@@ -97,13 +139,13 @@ end
|
|
|
97
139
|
|
|
98
140
|
describe "expect(...).not_to be_predicate(*args)" do
|
|
99
141
|
it "passes when actual returns false for :predicate?(*args)" do
|
|
100
|
-
actual =
|
|
142
|
+
actual = double("actual")
|
|
101
143
|
actual.should_receive(:older_than?).with(3).and_return(false)
|
|
102
144
|
expect(actual).not_to be_older_than(3)
|
|
103
145
|
end
|
|
104
146
|
|
|
105
147
|
it "fails when actual returns true for :predicate?(*args)" do
|
|
106
|
-
actual =
|
|
148
|
+
actual = double("actual")
|
|
107
149
|
actual.should_receive(:older_than?).with(3).and_return(true)
|
|
108
150
|
expect {
|
|
109
151
|
expect(actual).not_to be_older_than(3)
|
|
@@ -119,16 +161,16 @@ end
|
|
|
119
161
|
|
|
120
162
|
describe "expect(...).to be_predicate(&block)" do
|
|
121
163
|
it "passes when actual returns true for :predicate?(&block)" do
|
|
122
|
-
actual =
|
|
123
|
-
delegate =
|
|
164
|
+
actual = double("actual")
|
|
165
|
+
delegate = double("delegate")
|
|
124
166
|
actual.should_receive(:happy?).and_yield
|
|
125
167
|
delegate.should_receive(:check_happy).and_return(true)
|
|
126
168
|
expect(actual).to be_happy { delegate.check_happy }
|
|
127
169
|
end
|
|
128
170
|
|
|
129
171
|
it "fails when actual returns false for :predicate?(&block)" do
|
|
130
|
-
actual =
|
|
131
|
-
delegate =
|
|
172
|
+
actual = double("actual")
|
|
173
|
+
delegate = double("delegate")
|
|
132
174
|
actual.should_receive(:happy?).and_yield
|
|
133
175
|
delegate.should_receive(:check_happy).and_return(false)
|
|
134
176
|
expect {
|
|
@@ -137,7 +179,7 @@ describe "expect(...).to be_predicate(&block)" do
|
|
|
137
179
|
end
|
|
138
180
|
|
|
139
181
|
it "fails when actual does not respond to :predicate?" do
|
|
140
|
-
delegate =
|
|
182
|
+
delegate = double("delegate", :check_happy => true)
|
|
141
183
|
expect {
|
|
142
184
|
expect(Object.new).to be_happy { delegate.check_happy }
|
|
143
185
|
}.to raise_error(NameError)
|
|
@@ -146,16 +188,16 @@ end
|
|
|
146
188
|
|
|
147
189
|
describe "expect(...).not_to be_predicate(&block)" do
|
|
148
190
|
it "passes when actual returns false for :predicate?(&block)" do
|
|
149
|
-
actual =
|
|
150
|
-
delegate =
|
|
191
|
+
actual = double("actual")
|
|
192
|
+
delegate = double("delegate")
|
|
151
193
|
actual.should_receive(:happy?).and_yield
|
|
152
194
|
delegate.should_receive(:check_happy).and_return(false)
|
|
153
195
|
expect(actual).not_to be_happy { delegate.check_happy }
|
|
154
196
|
end
|
|
155
197
|
|
|
156
198
|
it "fails when actual returns true for :predicate?(&block)" do
|
|
157
|
-
actual =
|
|
158
|
-
delegate =
|
|
199
|
+
actual = double("actual")
|
|
200
|
+
delegate = double("delegate")
|
|
159
201
|
actual.should_receive(:happy?).and_yield
|
|
160
202
|
delegate.should_receive(:check_happy).and_return(true)
|
|
161
203
|
expect {
|
|
@@ -164,7 +206,7 @@ describe "expect(...).not_to be_predicate(&block)" do
|
|
|
164
206
|
end
|
|
165
207
|
|
|
166
208
|
it "fails when actual does not respond to :predicate?" do
|
|
167
|
-
delegate =
|
|
209
|
+
delegate = double("delegate", :check_happy => true)
|
|
168
210
|
expect {
|
|
169
211
|
expect(Object.new).not_to be_happy { delegate.check_happy }
|
|
170
212
|
}.to raise_error(NameError)
|
|
@@ -173,16 +215,16 @@ end
|
|
|
173
215
|
|
|
174
216
|
describe "expect(...).to be_predicate(*args, &block)" do
|
|
175
217
|
it "passes when actual returns true for :predicate?(*args, &block)" do
|
|
176
|
-
actual =
|
|
177
|
-
delegate =
|
|
218
|
+
actual = double("actual")
|
|
219
|
+
delegate = double("delegate")
|
|
178
220
|
actual.should_receive(:older_than?).with(3).and_yield(3)
|
|
179
221
|
delegate.should_receive(:check_older_than).with(3).and_return(true)
|
|
180
222
|
expect(actual).to be_older_than(3) { |age| delegate.check_older_than(age) }
|
|
181
223
|
end
|
|
182
224
|
|
|
183
225
|
it "fails when actual returns false for :predicate?(*args, &block)" do
|
|
184
|
-
actual =
|
|
185
|
-
delegate =
|
|
226
|
+
actual = double("actual")
|
|
227
|
+
delegate = double("delegate")
|
|
186
228
|
actual.should_receive(:older_than?).with(3).and_yield(3)
|
|
187
229
|
delegate.should_receive(:check_older_than).with(3).and_return(false)
|
|
188
230
|
expect {
|
|
@@ -191,7 +233,7 @@ describe "expect(...).to be_predicate(*args, &block)" do
|
|
|
191
233
|
end
|
|
192
234
|
|
|
193
235
|
it "fails when actual does not respond to :predicate?" do
|
|
194
|
-
delegate =
|
|
236
|
+
delegate = double("delegate", :check_older_than => true)
|
|
195
237
|
expect {
|
|
196
238
|
expect(Object.new).to be_older_than(3) { |age| delegate.check_older_than(age) }
|
|
197
239
|
}.to raise_error(NameError)
|
|
@@ -200,16 +242,16 @@ end
|
|
|
200
242
|
|
|
201
243
|
describe "expect(...).not_to be_predicate(*args, &block)" do
|
|
202
244
|
it "passes when actual returns false for :predicate?(*args, &block)" do
|
|
203
|
-
actual =
|
|
204
|
-
delegate =
|
|
245
|
+
actual = double("actual")
|
|
246
|
+
delegate = double("delegate")
|
|
205
247
|
actual.should_receive(:older_than?).with(3).and_yield(3)
|
|
206
248
|
delegate.should_receive(:check_older_than).with(3).and_return(false)
|
|
207
249
|
expect(actual).not_to be_older_than(3) { |age| delegate.check_older_than(age) }
|
|
208
250
|
end
|
|
209
251
|
|
|
210
252
|
it "fails when actual returns true for :predicate?(*args, &block)" do
|
|
211
|
-
actual =
|
|
212
|
-
delegate =
|
|
253
|
+
actual = double("actual")
|
|
254
|
+
delegate = double("delegate")
|
|
213
255
|
actual.should_receive(:older_than?).with(3).and_yield(3)
|
|
214
256
|
delegate.should_receive(:check_older_than).with(3).and_return(true)
|
|
215
257
|
expect {
|
|
@@ -218,42 +260,101 @@ describe "expect(...).not_to be_predicate(*args, &block)" do
|
|
|
218
260
|
end
|
|
219
261
|
|
|
220
262
|
it "fails when actual does not respond to :predicate?" do
|
|
221
|
-
delegate =
|
|
263
|
+
delegate = double("delegate", :check_older_than => true)
|
|
222
264
|
expect {
|
|
223
265
|
expect(Object.new).not_to be_older_than(3) { |age| delegate.check_older_than(age) }
|
|
224
266
|
}.to raise_error(NameError)
|
|
225
267
|
end
|
|
226
268
|
end
|
|
227
269
|
|
|
228
|
-
describe "expect(...).to
|
|
270
|
+
describe "expect(...).to be_truthy" do
|
|
229
271
|
it "passes when actual equal?(true)" do
|
|
230
|
-
expect(true).to
|
|
272
|
+
expect(true).to be_truthy
|
|
231
273
|
end
|
|
232
274
|
|
|
233
275
|
it "passes when actual is 1" do
|
|
234
|
-
expect(1).to
|
|
276
|
+
expect(1).to be_truthy
|
|
235
277
|
end
|
|
236
278
|
|
|
237
279
|
it "fails when actual equal?(false)" do
|
|
238
280
|
expect {
|
|
239
|
-
expect(false).to
|
|
240
|
-
}.to fail_with("expected:
|
|
281
|
+
expect(false).to be_truthy
|
|
282
|
+
}.to fail_with("expected: truthy value\n got: false")
|
|
241
283
|
end
|
|
242
284
|
end
|
|
243
285
|
|
|
244
286
|
describe "expect(...).to be_false" do
|
|
245
|
-
it "
|
|
287
|
+
it "is deprecated" do
|
|
288
|
+
expect_deprecation_with_call_site(__FILE__, __LINE__ + 1, /be_false/)
|
|
289
|
+
expect(false).to be_false
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
it "has the correct call site in the deprecation message" do
|
|
293
|
+
expect_deprecation_with_call_site(__FILE__, __LINE__ + 1)
|
|
294
|
+
expect(false).to be_false
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
it "still passes" do
|
|
298
|
+
allow(RSpec).to receive(:deprecate)
|
|
246
299
|
expect(false).to be_false
|
|
247
300
|
end
|
|
248
301
|
|
|
302
|
+
it "still fails" do
|
|
303
|
+
allow(RSpec).to receive(:deprecate)
|
|
304
|
+
expect {
|
|
305
|
+
expect(true).to be_false
|
|
306
|
+
}.to fail_with(/expected: falsey value/)
|
|
307
|
+
end
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
describe "expect(...).to be_true" do
|
|
311
|
+
it "is deprecated" do
|
|
312
|
+
expect_deprecation_with_call_site(__FILE__, __LINE__ + 1, /be_true/)
|
|
313
|
+
expect(true).to be_true
|
|
314
|
+
end
|
|
315
|
+
|
|
316
|
+
it "still passes" do
|
|
317
|
+
allow(RSpec).to receive(:deprecate)
|
|
318
|
+
expect(true).to be_true
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
it "still fails" do
|
|
322
|
+
allow(RSpec).to receive(:deprecate)
|
|
323
|
+
expect {
|
|
324
|
+
expect(false).to be_true
|
|
325
|
+
}.to fail_with(/expected: truthy value/)
|
|
326
|
+
end
|
|
327
|
+
end
|
|
328
|
+
|
|
329
|
+
describe "expect(...).to be_falsey" do
|
|
330
|
+
it "passes when actual equal?(false)" do
|
|
331
|
+
expect(false).to be_falsey
|
|
332
|
+
end
|
|
333
|
+
|
|
249
334
|
it "passes when actual equal?(nil)" do
|
|
250
|
-
expect(nil).to
|
|
335
|
+
expect(nil).to be_falsey
|
|
251
336
|
end
|
|
252
337
|
|
|
253
338
|
it "fails when actual equal?(true)" do
|
|
254
339
|
expect {
|
|
255
|
-
expect(true).to
|
|
256
|
-
}.to fail_with("expected:
|
|
340
|
+
expect(true).to be_falsey
|
|
341
|
+
}.to fail_with("expected: falsey value\n got: true")
|
|
342
|
+
end
|
|
343
|
+
end
|
|
344
|
+
|
|
345
|
+
describe "expect(...).to be_falsy" do
|
|
346
|
+
it "passes when actual equal?(false)" do
|
|
347
|
+
expect(false).to be_falsy
|
|
348
|
+
end
|
|
349
|
+
|
|
350
|
+
it "passes when actual equal?(nil)" do
|
|
351
|
+
expect(nil).to be_falsy
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
it "fails when actual equal?(true)" do
|
|
355
|
+
expect {
|
|
356
|
+
expect(true).to be_falsy
|
|
357
|
+
}.to fail_with("expected: falsey value\n got: true")
|
|
257
358
|
end
|
|
258
359
|
end
|
|
259
360
|
|
|
@@ -465,10 +566,10 @@ end
|
|
|
465
566
|
|
|
466
567
|
describe "'expect(...).to be' with operator" do
|
|
467
568
|
it "includes 'be' in the description" do
|
|
468
|
-
expect((be > 6).description).to match
|
|
469
|
-
expect((be >= 6).description).to match
|
|
470
|
-
expect((be <= 6).description).to match
|
|
471
|
-
expect((be < 6).description).to match
|
|
569
|
+
expect((be > 6).description).to match(/be > 6/)
|
|
570
|
+
expect((be >= 6).description).to match(/be >= 6/)
|
|
571
|
+
expect((be <= 6).description).to match(/be <= 6/)
|
|
572
|
+
expect((be < 6).description).to match(/be < 6/)
|
|
472
573
|
end
|
|
473
574
|
end
|
|
474
575
|
|
|
@@ -27,6 +27,14 @@ module RSpec
|
|
|
27
27
|
expect(5.5).to be_within(0.5).of(5.0)
|
|
28
28
|
end
|
|
29
29
|
|
|
30
|
+
it "passes with integer arguments that are near each other" do
|
|
31
|
+
expect(1.0001).to be_within(5).percent_of(1)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
it "passes with negative arguments" do
|
|
35
|
+
expect(-1.0001).to be_within(5).percent_of(-1)
|
|
36
|
+
end
|
|
37
|
+
|
|
30
38
|
it "fails when actual < (expected - delta)" do
|
|
31
39
|
expect {
|
|
32
40
|
expect(4.49).to be_within(0.5).of(5.0)
|
|
@@ -28,6 +28,20 @@ describe "expect { ... }.to change(actual, message)" do
|
|
|
28
28
|
end
|
|
29
29
|
end
|
|
30
30
|
|
|
31
|
+
it "can specify the change of a variable's class" do
|
|
32
|
+
val = nil
|
|
33
|
+
|
|
34
|
+
expect {
|
|
35
|
+
val = 42
|
|
36
|
+
}.to change { val.class }.from(NilClass).to(Fixnum)
|
|
37
|
+
|
|
38
|
+
expect {
|
|
39
|
+
expect {
|
|
40
|
+
val = "string"
|
|
41
|
+
}.to change { val.class }.from(Fixnum).to(NilClass)
|
|
42
|
+
}.to fail_with(/but is now String/)
|
|
43
|
+
end
|
|
44
|
+
|
|
31
45
|
context "with boolean values" do
|
|
32
46
|
before(:each) do
|
|
33
47
|
@instance = SomethingExpected.new
|
|
@@ -72,6 +86,17 @@ describe "expect { ... }.to change(actual, message)" do
|
|
|
72
86
|
expect {@instance.some_value << 1}.to change(@instance, :some_value)
|
|
73
87
|
end
|
|
74
88
|
|
|
89
|
+
it "fails when a predicate on the actual fails" do
|
|
90
|
+
expect do
|
|
91
|
+
expect {@instance.some_value << 1}.to change { @instance.some_value }.to be_empty
|
|
92
|
+
end.to fail_with(/result should have been changed to/)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
it "passes when a predicate on the actual passes" do
|
|
96
|
+
@instance.some_value = [1]
|
|
97
|
+
expect {@instance.some_value.pop}.to change { @instance.some_value }.to be_empty
|
|
98
|
+
end
|
|
99
|
+
|
|
75
100
|
it "fails when actual is not modified by the block" do
|
|
76
101
|
expect do
|
|
77
102
|
expect {}.to change(@instance, :some_value)
|
|
@@ -156,19 +181,20 @@ describe "expect { ... }.not_to change(actual, message)" do
|
|
|
156
181
|
end
|
|
157
182
|
|
|
158
183
|
it "passes when actual is not modified by the block" do
|
|
159
|
-
expect { }.
|
|
184
|
+
expect { }.not_to change(@instance, :some_value)
|
|
160
185
|
end
|
|
161
186
|
|
|
162
187
|
it "fails when actual is not modified by the block" do
|
|
163
188
|
expect do
|
|
164
|
-
expect {@instance.some_value = 6}.
|
|
189
|
+
expect {@instance.some_value = 6}.not_to change(@instance, :some_value)
|
|
165
190
|
end.to fail_with("some_value should not have changed, but did change from 5 to 6")
|
|
166
191
|
end
|
|
167
192
|
end
|
|
168
193
|
|
|
169
194
|
describe "expect { ... }.to change { block }" do
|
|
170
195
|
o = SomethingExpected.new
|
|
171
|
-
|
|
196
|
+
o.some_value = 1
|
|
197
|
+
it_behaves_like "an RSpec matcher", :valid_value => lambda { o.some_value += 1 },
|
|
172
198
|
:invalid_value => lambda { } do
|
|
173
199
|
let(:matcher) { change { o.some_value } }
|
|
174
200
|
end
|
|
@@ -206,22 +232,96 @@ describe "expect { ... }.not_to change { block }" do
|
|
|
206
232
|
end
|
|
207
233
|
|
|
208
234
|
it "passes when actual is modified by the block" do
|
|
209
|
-
expect {}.
|
|
235
|
+
expect {}.not_to change{ @instance.some_value }
|
|
210
236
|
end
|
|
211
237
|
|
|
212
238
|
it "fails when actual is not modified by the block" do
|
|
213
239
|
expect do
|
|
214
|
-
expect {@instance.some_value = 6}.
|
|
240
|
+
expect {@instance.some_value = 6}.not_to change { @instance.some_value }
|
|
215
241
|
end.to fail_with("result should not have changed, but did change from 5 to 6")
|
|
216
242
|
end
|
|
217
243
|
|
|
218
244
|
it "warns if passed a block using do/end instead of {}" do
|
|
219
245
|
expect do
|
|
220
|
-
expect {}.
|
|
246
|
+
expect {}.not_to change do; end
|
|
221
247
|
end.to raise_error(SyntaxError, /block passed to should or should_not/)
|
|
222
248
|
end
|
|
223
249
|
end
|
|
224
250
|
|
|
251
|
+
describe "expect { ... }.not_to change { }.from" do
|
|
252
|
+
context 'when the value starts at the from value' do
|
|
253
|
+
it 'passes when the value does not change' do
|
|
254
|
+
k = 5
|
|
255
|
+
expect { }.not_to change { k }.from(5)
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
it 'fails when the value does change' do
|
|
259
|
+
expect {
|
|
260
|
+
k = 5
|
|
261
|
+
expect { k += 1 }.not_to change { k }.from(5)
|
|
262
|
+
}.to fail_with(/but did change from 5 to 6/)
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
it 'does not issue a deprecation warning' do
|
|
266
|
+
expect(RSpec.configuration.reporter).not_to receive(:deprecation)
|
|
267
|
+
k = 5
|
|
268
|
+
expect { }.not_to change { k }.from(5)
|
|
269
|
+
end
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
context 'when the value starts at a different value' do
|
|
273
|
+
before { allow_deprecation }
|
|
274
|
+
|
|
275
|
+
it 'passes when the value does not change' do
|
|
276
|
+
k = 6
|
|
277
|
+
expect { }.not_to change { k }.from(5)
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
it 'passes when the value does change' do
|
|
281
|
+
k = 6
|
|
282
|
+
expect { k += 1 }.not_to change { k }.from(5)
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
it 'issues a deprecation warning' do
|
|
286
|
+
expect_deprecation_with_call_site(__FILE__, __LINE__ + 2, /#{Regexp.escape("expect { }.not_to change { }.from()")}/)
|
|
287
|
+
k = 6
|
|
288
|
+
expect { }.not_to change { k }.from(5)
|
|
289
|
+
end
|
|
290
|
+
end
|
|
291
|
+
end
|
|
292
|
+
|
|
293
|
+
describe "expect { ... }.not_to change { }.to" do
|
|
294
|
+
it 'issues a deprecation warning' do
|
|
295
|
+
expect_deprecation_with_call_site(__FILE__, __LINE__ + 1, /#{Regexp.escape("expect { }.not_to change { }.to()")}/)
|
|
296
|
+
expect {
|
|
297
|
+
}.not_to change { }.to(3)
|
|
298
|
+
end
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
describe "expect { ... }.not_to change { }.by" do
|
|
302
|
+
it 'issues a deprecation warning' do
|
|
303
|
+
expect_deprecation_with_call_site(__FILE__, __LINE__ + 1, /#{Regexp.escape("expect { }.not_to change { }.by()")}/)
|
|
304
|
+
expect {
|
|
305
|
+
}.not_to change { }.by(3)
|
|
306
|
+
end
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
describe "expect { ... }.not_to change { }.by_at_least" do
|
|
310
|
+
it 'issues a deprecation warning' do
|
|
311
|
+
expect_deprecation_with_call_site(__FILE__, __LINE__ + 1, /#{Regexp.escape("expect { }.not_to change { }.by_at_least()")}/)
|
|
312
|
+
expect {
|
|
313
|
+
}.not_to change { }.by_at_least(3)
|
|
314
|
+
end
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
describe "expect { ... }.not_to change { }.by_at_most" do
|
|
318
|
+
it 'issues a deprecation warning' do
|
|
319
|
+
expect_deprecation_with_call_site(__FILE__, __LINE__ + 1, /#{Regexp.escape("expect { }.not_to change { }.by_at_most()")}/)
|
|
320
|
+
expect {
|
|
321
|
+
}.not_to change { }.by_at_most(3)
|
|
322
|
+
end
|
|
323
|
+
end
|
|
324
|
+
|
|
225
325
|
describe "expect { ... }.to change(actual, message).by(expected)" do
|
|
226
326
|
before(:each) do
|
|
227
327
|
@instance = SomethingExpected.new
|
|
@@ -537,6 +637,6 @@ describe RSpec::Matchers::BuiltIn::Change do
|
|
|
537
637
|
|
|
538
638
|
expect {
|
|
539
639
|
expect { @instance.some_value = "cat" }.to change(@instance, :some_value)
|
|
540
|
-
}.
|
|
640
|
+
}.not_to raise_error
|
|
541
641
|
end
|
|
542
642
|
end
|
|
@@ -25,14 +25,14 @@ describe "Matchers should be able to generate their own descriptions" do
|
|
|
25
25
|
expect(RSpec::Matchers.generated_description).to eq "should not be empty"
|
|
26
26
|
end
|
|
27
27
|
|
|
28
|
-
it "expect(...).to be
|
|
29
|
-
expect(true).to
|
|
30
|
-
expect(RSpec::Matchers.generated_description).to eq "should be
|
|
28
|
+
it "expect(...).to be truthy" do
|
|
29
|
+
expect(true).to be_truthy
|
|
30
|
+
expect(RSpec::Matchers.generated_description).to eq "should be truthy"
|
|
31
31
|
end
|
|
32
32
|
|
|
33
|
-
it "expect(...).to be
|
|
34
|
-
expect(false).to
|
|
35
|
-
expect(RSpec::Matchers.generated_description).to eq "should be
|
|
33
|
+
it "expect(...).to be falsey" do
|
|
34
|
+
expect(false).to be_falsey
|
|
35
|
+
expect(RSpec::Matchers.generated_description).to eq "should be falsey"
|
|
36
36
|
end
|
|
37
37
|
|
|
38
38
|
it "expect(...).to be nil" do
|
|
@@ -92,29 +92,47 @@ describe "Matchers should be able to generate their own descriptions" do
|
|
|
92
92
|
expect(RSpec::Matchers.generated_description).to eq 'should have taste for "wine", "cheese"'
|
|
93
93
|
end
|
|
94
94
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
expect(RSpec::Matchers.generated_description).to eq "should have 3 players"
|
|
98
|
-
end
|
|
95
|
+
context "the deprecated collection cardinality matchers" do
|
|
96
|
+
before { allow_deprecation }
|
|
99
97
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
98
|
+
it "expect(...).to have n items" do
|
|
99
|
+
expect(team).to have(3).players
|
|
100
|
+
expect(RSpec::Matchers.generated_description).to eq "should have 3 players"
|
|
101
|
+
end
|
|
104
102
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
103
|
+
it "expect(...).to have at least n items" do
|
|
104
|
+
expect(team).to have_at_least(2).players
|
|
105
|
+
expect(RSpec::Matchers.generated_description).to eq "should have at least 2 players"
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
it "expect(...).to have at most n items" do
|
|
109
|
+
expect(team).to have_at_most(4).players
|
|
110
|
+
expect(RSpec::Matchers.generated_description).to eq "should have at most 4 players"
|
|
111
|
+
end
|
|
108
112
|
end
|
|
109
113
|
|
|
110
|
-
it "expect(...).to include" do
|
|
114
|
+
it "expect(...).to include(x)" do
|
|
111
115
|
expect([1,2,3]).to include(3)
|
|
112
116
|
expect(RSpec::Matchers.generated_description).to eq "should include 3"
|
|
113
117
|
end
|
|
114
118
|
|
|
119
|
+
it "expect(...).to include(x) when x responds to description but is not a matcher" do
|
|
120
|
+
obj = double(:description => "description", :inspect => "inspect")
|
|
121
|
+
expect([obj]).to include(obj)
|
|
122
|
+
expect(RSpec::Matchers.generated_description).to eq "should include inspect"
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
it "expect(...).to include(x) when x responds to description and is a matcher" do
|
|
126
|
+
matcher = double(:description => "description",
|
|
127
|
+
:matches? => true,
|
|
128
|
+
:failure_message_for_should => "")
|
|
129
|
+
expect([matcher]).to include(matcher)
|
|
130
|
+
expect(RSpec::Matchers.generated_description).to eq "should include description"
|
|
131
|
+
end
|
|
132
|
+
|
|
115
133
|
it "expect(array).not_to match_array [1,2,3]" do
|
|
116
134
|
expect([1,2,3]).to match_array [1,2,3]
|
|
117
|
-
expect(RSpec::Matchers.generated_description).to eq "should contain exactly 1, 2 and 3"
|
|
135
|
+
expect(RSpec::Matchers.generated_description).to eq "should contain exactly 1, 2, and 3"
|
|
118
136
|
end
|
|
119
137
|
|
|
120
138
|
it "expect(...).to match" do
|
|
@@ -171,6 +189,6 @@ describe "a Matcher with no description" do
|
|
|
171
189
|
|
|
172
190
|
it "provides a helpful message when used in a string-less example block" do
|
|
173
191
|
expect(5).to matcher
|
|
174
|
-
expect(RSpec::Matchers.generated_description).to match
|
|
192
|
+
expect(RSpec::Matchers.generated_description).to match(/When you call.*description method/m)
|
|
175
193
|
end
|
|
176
194
|
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
module RSpec
|
|
4
|
+
module Matchers
|
|
5
|
+
describe DifferentiateBlockMethodTypes do
|
|
6
|
+
let(:differentiator) do
|
|
7
|
+
DifferentiateBlockMethodTypes.new do
|
|
8
|
+
def some_instance_method_1; end
|
|
9
|
+
def self.some_singleton_method_1; end
|
|
10
|
+
define_method(:some_instance_method_2) { }
|
|
11
|
+
|
|
12
|
+
if RUBY_VERSION.to_f > 1.8
|
|
13
|
+
define_singleton_method(:some_singleton_method_2) { }
|
|
14
|
+
else
|
|
15
|
+
def self.some_singleton_method_2; end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it 'differentiates singleton method defs from instance method defs' do
|
|
21
|
+
expect(differentiator.instance_methods).to eq([:some_instance_method_1, :some_instance_method_2])
|
|
22
|
+
expect(differentiator.singleton_methods).to eq([:some_singleton_method_1, :some_singleton_method_2])
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it 'passes the given args through to the block' do
|
|
26
|
+
expect { |b|
|
|
27
|
+
DifferentiateBlockMethodTypes.new(1, 2, &b)
|
|
28
|
+
}.to yield_with_args(1, 2)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it 'ignores unrecognized DSL methods called in the block' do
|
|
32
|
+
expect {
|
|
33
|
+
DifferentiateBlockMethodTypes.new { foo.bar; some_dsl { nested } }
|
|
34
|
+
}.not_to raise_error
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|