rspec-expectations 2.11.3 → 2.12.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.
- data/Changelog.md +25 -2
- data/README.md +5 -1
- data/features/built_in_matchers/be.feature +4 -4
- data/features/built_in_matchers/be_within.feature +1 -1
- data/features/built_in_matchers/cover.feature +1 -1
- data/features/built_in_matchers/end_with.feature +2 -2
- data/features/built_in_matchers/equality.feature +5 -5
- data/features/built_in_matchers/exist.feature +1 -1
- data/features/built_in_matchers/expect_change.feature +3 -3
- data/features/built_in_matchers/expect_error.feature +4 -4
- data/features/built_in_matchers/have.feature +2 -2
- data/features/built_in_matchers/include.feature +3 -3
- data/features/built_in_matchers/match.feature +2 -2
- data/features/built_in_matchers/operators.feature +3 -3
- data/features/built_in_matchers/predicates.feature +9 -8
- data/features/built_in_matchers/respond_to.feature +2 -2
- data/features/built_in_matchers/satisfy.feature +1 -1
- data/features/built_in_matchers/start_with.feature +2 -2
- data/features/built_in_matchers/throw_symbol.feature +3 -3
- data/features/built_in_matchers/types.feature +2 -2
- data/features/built_in_matchers/yield.feature +5 -5
- data/features/custom_matchers/access_running_example.feature +3 -3
- data/features/custom_matchers/define_diffable_matcher.feature +1 -1
- data/features/custom_matchers/define_matcher.feature +12 -12
- data/features/custom_matchers/define_matcher_outside_rspec.feature +1 -1
- data/features/custom_matchers/define_matcher_with_fluent_interface.feature +1 -1
- data/features/customized_message.feature +1 -1
- data/features/diffing.feature +4 -4
- data/features/implicit_docstrings.feature +2 -2
- data/features/test_frameworks/test_unit.feature +1 -1
- data/lib/rspec/expectations/differ.rb +35 -1
- data/lib/rspec/expectations/handler.rb +21 -6
- data/lib/rspec/expectations/version.rb +1 -1
- data/lib/rspec/matchers/built_in/be_instance_of.rb +4 -0
- data/lib/rspec/matchers/built_in/include.rb +3 -1
- data/lib/rspec/matchers/built_in/match_array.rb +12 -6
- data/lib/rspec/matchers/built_in/raise_error.rb +17 -7
- data/lib/rspec/matchers/built_in/yield.rb +5 -5
- data/lib/rspec/matchers/configuration.rb +42 -0
- data/lib/rspec/matchers/generated_descriptions.rb +2 -3
- data/spec/rspec/expectations/differ_spec.rb +24 -0
- data/spec/rspec/expectations/handler_spec.rb +40 -40
- data/spec/rspec/expectations/syntax_spec.rb +35 -0
- data/spec/rspec/matchers/be_instance_of_spec.rb +19 -2
- data/spec/rspec/matchers/be_spec.rb +10 -10
- data/spec/rspec/matchers/configuration_spec.rb +155 -123
- data/spec/rspec/matchers/dsl_spec.rb +8 -8
- data/spec/rspec/matchers/have_spec.rb +8 -38
- data/spec/rspec/matchers/include_spec.rb +6 -0
- data/spec/rspec/matchers/match_array_spec.rb +14 -0
- data/spec/rspec/matchers/raise_error_spec.rb +89 -38
- data/spec/rspec/matchers/start_with_end_with_spec.rb +1 -1
- data/spec/rspec/matchers/yield_spec.rb +35 -0
- metadata +70 -78
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module RSpec
|
4
|
+
module Expectations
|
5
|
+
module Syntax
|
6
|
+
describe "the should and should_not expectations" do
|
7
|
+
let(:warner) { ::Kernel }
|
8
|
+
|
9
|
+
describe "#should" do
|
10
|
+
it "prints a warning when the message object isn't a String" do
|
11
|
+
warner.should_receive(:warn).with /ignoring.*message/
|
12
|
+
3.should eq(3), :not_a_string
|
13
|
+
end
|
14
|
+
|
15
|
+
it "doesn't print a warning when message is a String" do
|
16
|
+
warner.should_not_receive(:warn)
|
17
|
+
3.should eq(3), "a string"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "#should_not" do
|
22
|
+
it "prints a warning when the message object isn't a String" do
|
23
|
+
warner.should_receive(:warn).with /ignoring.*message/
|
24
|
+
3.should_not eq(4), :not_a_string
|
25
|
+
end
|
26
|
+
|
27
|
+
it "doesn't print a warning when message is a String" do
|
28
|
+
warner.should_not_receive(:warn)
|
29
|
+
3.should_not eq(4), "a string"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -25,10 +25,27 @@ module RSpec
|
|
25
25
|
matcher.matches?(Numeric)
|
26
26
|
matcher.description.should == "be an instance of Fixnum"
|
27
27
|
end
|
28
|
+
|
29
|
+
context "when expected provides an expanded inspect, e.g. AR::Base" do
|
30
|
+
let(:user_klass) do
|
31
|
+
Class.new do
|
32
|
+
def self.inspect
|
33
|
+
"User(id: integer, name: string)"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
before { stub_const("User", user_klass) }
|
39
|
+
|
40
|
+
it "provides a description including only the class name" do
|
41
|
+
matcher = be_an_instance_of(User)
|
42
|
+
matcher.description.should == "be an instance of User"
|
43
|
+
end
|
44
|
+
end
|
28
45
|
end
|
29
|
-
|
46
|
+
|
30
47
|
describe "actual.should_not #{method}(expected)" do
|
31
|
-
|
48
|
+
|
32
49
|
it "fails with failure message for should_not if actual is instance of expected class" do
|
33
50
|
lambda { "foo".should_not send(method, String) }.should fail_with(%Q{expected "foo" not to be an instance of String})
|
34
51
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe "should be_predicate" do
|
3
|
+
describe "should be_predicate" do
|
4
4
|
it "passes when actual returns true for :predicate?" do
|
5
5
|
actual = stub("actual", :happy? => true)
|
6
6
|
actual.should be_happy
|
@@ -17,20 +17,20 @@ describe "should be_predicate" do
|
|
17
17
|
actual.should be_happy
|
18
18
|
}.should 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
22
|
actual = stub("actual", :happy? => nil)
|
23
23
|
lambda {
|
24
24
|
actual.should be_happy
|
25
25
|
}.should fail_with("expected happy? to return true, got nil")
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
it "fails when actual does not respond to :predicate?" do
|
29
29
|
lambda {
|
30
30
|
Object.new.should be_happy
|
31
31
|
}.should raise_error(NameError, /happy\?/)
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
34
|
it "fails on error other than NameError" do
|
35
35
|
actual = stub("actual")
|
36
36
|
actual.should_receive(:foo?).and_raise("aaaah")
|
@@ -38,7 +38,7 @@ describe "should be_predicate" do
|
|
38
38
|
actual.should be_foo
|
39
39
|
}.should raise_error(/aaaah/)
|
40
40
|
end
|
41
|
-
|
41
|
+
|
42
42
|
it "fails on error other than NameError (with the present tense predicate)" do
|
43
43
|
actual = Object.new
|
44
44
|
actual.should_receive(:foos?).and_raise("aaaah")
|
@@ -53,12 +53,12 @@ describe "should_not be_predicate" do
|
|
53
53
|
actual = stub("actual", :happy? => false)
|
54
54
|
actual.should_not be_happy
|
55
55
|
end
|
56
|
-
|
56
|
+
|
57
57
|
it "passes when actual returns nil for :sym?" do
|
58
58
|
actual = stub("actual", :happy? => nil)
|
59
59
|
actual.should_not be_happy
|
60
60
|
end
|
61
|
-
|
61
|
+
|
62
62
|
it "fails when actual returns true for :sym?" do
|
63
63
|
actual = stub("actual", :happy? => true)
|
64
64
|
lambda {
|
@@ -87,7 +87,7 @@ describe "should be_predicate(*args)" do
|
|
87
87
|
actual.should be_older_than(3)
|
88
88
|
}.should fail_with("expected older_than?(3) to return true, got false")
|
89
89
|
end
|
90
|
-
|
90
|
+
|
91
91
|
it "fails when actual does not respond to :predicate?" do
|
92
92
|
lambda {
|
93
93
|
Object.new.should be_older_than(3)
|
@@ -101,7 +101,7 @@ describe "should_not be_predicate(*args)" do
|
|
101
101
|
actual.should_receive(:older_than?).with(3).and_return(false)
|
102
102
|
actual.should_not be_older_than(3)
|
103
103
|
end
|
104
|
-
|
104
|
+
|
105
105
|
it "fails when actual returns true for :predicate?(*args)" do
|
106
106
|
actual = mock("actual")
|
107
107
|
actual.should_receive(:older_than?).with(3).and_return(true)
|
@@ -444,7 +444,7 @@ describe "be_an_instance_of" do
|
|
444
444
|
it "passes when direct class matches" do
|
445
445
|
5.should be_an_instance_of(Fixnum)
|
446
446
|
end
|
447
|
-
|
447
|
+
|
448
448
|
it "fails when class is higher up hierarchy" do
|
449
449
|
5.should_not be_an_instance_of(Numeric)
|
450
450
|
end
|
@@ -3,11 +3,43 @@ require 'delegate'
|
|
3
3
|
|
4
4
|
module RSpec
|
5
5
|
module Matchers
|
6
|
-
describe ".configuration" do
|
6
|
+
describe "RSpec::Matchers.configuration" do
|
7
7
|
it 'returns a memoized configuration instance' do
|
8
8
|
RSpec::Matchers.configuration.should be_a(RSpec::Matchers::Configuration)
|
9
9
|
RSpec::Matchers.configuration.should be(RSpec::Matchers.configuration)
|
10
10
|
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe Configuration do
|
14
|
+
let(:config) { Configuration.new }
|
15
|
+
|
16
|
+
describe "#backtrace_formatter" do
|
17
|
+
let(:original_backtrace) { %w[ clean-me/a.rb other/file.rb clean-me/b.rb ] }
|
18
|
+
let(:cleaned_backtrace) { %w[ other/file.rb ] }
|
19
|
+
|
20
|
+
let(:formatted_backtrace) do
|
21
|
+
config.backtrace_formatter.format_backtrace(original_backtrace)
|
22
|
+
end
|
23
|
+
|
24
|
+
before do
|
25
|
+
RSpec.configuration.stub(:backtrace_clean_patterns) { [/clean-me/] }
|
26
|
+
end
|
27
|
+
|
28
|
+
it "defaults to rspec-core's backtrace formatter when rspec-core is loaded" do
|
29
|
+
expect(config.backtrace_formatter).to be(RSpec::Core::BacktraceFormatter)
|
30
|
+
expect(formatted_backtrace).to eq(cleaned_backtrace)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "defaults to a null formatter when rspec-core is not loaded" do
|
34
|
+
hide_const("RSpec::Core::BacktraceFormatter")
|
35
|
+
expect(formatted_backtrace).to eq(original_backtrace)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "can be set to another backtrace formatter" do
|
39
|
+
config.backtrace_formatter = stub(:format_backtrace => ['a'])
|
40
|
+
expect(formatted_backtrace).to eq(['a'])
|
41
|
+
end
|
42
|
+
end
|
11
43
|
|
12
44
|
context 'on an interpreter that does not provide BasicObject', :unless => defined?(::BasicObject) do
|
13
45
|
before { RSpec::Expectations::Syntax.disable_should(Delegator) }
|
@@ -22,175 +54,175 @@ module RSpec
|
|
22
54
|
|
23
55
|
it 'provides a means to manually add it Delegator' do
|
24
56
|
instance.should_not respond_to(:delegated?) # because #should is being delegated...
|
25
|
-
|
57
|
+
config.add_should_and_should_not_to Delegator
|
26
58
|
instance.should respond_to(:delegated?) # now it should work!
|
27
59
|
end
|
28
60
|
end
|
29
|
-
end
|
30
|
-
|
31
|
-
shared_examples_for "configuring the expectation syntax" do
|
32
|
-
# We want a sandboxed method that ensures that we wind up with
|
33
|
-
# both syntaxes properly enabled when the example ends.
|
34
|
-
#
|
35
|
-
# On platforms that fork, using a sub process is the easiest,
|
36
|
-
# most robust way to achieve that.
|
37
|
-
#
|
38
|
-
# On jRuby we just re-enable both syntaxes at the end of the example;
|
39
|
-
# however, this is a generally inferior approach because it depends on
|
40
|
-
# the code-under-test working properly; if it doesn't work properly,
|
41
|
-
# it could leave things in a "broken" state where tons of other examples fail.
|
42
|
-
if RUBY_PLATFORM == "java"
|
43
|
-
def sandboxed
|
44
|
-
yield
|
45
|
-
ensure
|
46
|
-
configure_syntax([:should, :expect])
|
47
|
-
end
|
48
|
-
else
|
49
|
-
include InSubProcess
|
50
|
-
alias sandboxed in_sub_process
|
51
|
-
end
|
52
|
-
|
53
|
-
it 'is configured to :should and :expect by default' do
|
54
|
-
configured_syntax.should eq([:should, :expect])
|
55
61
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
62
|
+
shared_examples_for "configuring the expectation syntax" do
|
63
|
+
# We want a sandboxed method that ensures that we wind up with
|
64
|
+
# both syntaxes properly enabled when the example ends.
|
65
|
+
#
|
66
|
+
# On platforms that fork, using a sub process is the easiest,
|
67
|
+
# most robust way to achieve that.
|
68
|
+
#
|
69
|
+
# On jRuby we just re-enable both syntaxes at the end of the example;
|
70
|
+
# however, this is a generally inferior approach because it depends on
|
71
|
+
# the code-under-test working properly; if it doesn't work properly,
|
72
|
+
# it could leave things in a "broken" state where tons of other examples fail.
|
73
|
+
if RUBY_PLATFORM == "java"
|
74
|
+
def sandboxed
|
75
|
+
yield
|
76
|
+
ensure
|
77
|
+
configure_syntax([:should, :expect])
|
78
|
+
end
|
79
|
+
else
|
80
|
+
include InSubProcess
|
81
|
+
alias sandboxed in_sub_process
|
82
|
+
end
|
60
83
|
|
61
|
-
|
62
|
-
|
63
|
-
configure_syntax :should
|
64
|
-
configured_syntax.should eq([:should])
|
84
|
+
it 'is configured to :should and :expect by default' do
|
85
|
+
configured_syntax.should eq([:should, :expect])
|
65
86
|
|
66
87
|
3.should eq(3)
|
67
88
|
3.should_not eq(4)
|
68
|
-
|
89
|
+
expect(3).to eq(3)
|
69
90
|
end
|
70
|
-
end
|
71
91
|
|
72
|
-
|
73
|
-
|
74
|
-
|
92
|
+
it 'can limit the syntax to :should' do
|
93
|
+
sandboxed do
|
94
|
+
configure_syntax :should
|
95
|
+
configured_syntax.should eq([:should])
|
75
96
|
|
76
|
-
|
77
|
-
|
97
|
+
3.should eq(3)
|
98
|
+
3.should_not eq(4)
|
99
|
+
lambda { expect(6).to eq(6) }.should raise_error(NameError)
|
100
|
+
end
|
78
101
|
end
|
79
|
-
end
|
80
102
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
expect(configured_syntax).to eq([:expect])
|
103
|
+
it 'is a no-op when configured to :should twice' do
|
104
|
+
sandboxed do
|
105
|
+
::Kernel.stub(:method_added).and_raise("no methods should be added here")
|
85
106
|
|
86
|
-
|
87
|
-
|
88
|
-
|
107
|
+
configure_syntax :should
|
108
|
+
configure_syntax :should
|
109
|
+
end
|
89
110
|
end
|
90
|
-
end
|
91
111
|
|
92
|
-
|
93
|
-
|
94
|
-
|
112
|
+
it 'can limit the syntax to :expect' do
|
113
|
+
sandboxed do
|
114
|
+
configure_syntax :expect
|
115
|
+
expect(configured_syntax).to eq([:expect])
|
95
116
|
|
96
|
-
|
97
|
-
|
117
|
+
expect(3).to eq(3)
|
118
|
+
expect { 3.should eq(3) }.to raise_error(NameError)
|
119
|
+
expect { 3.should_not eq(3) }.to raise_error(NameError)
|
120
|
+
end
|
98
121
|
end
|
99
|
-
end
|
100
122
|
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
configure_syntax [:should, :expect]
|
105
|
-
configured_syntax.should eq([:should, :expect])
|
123
|
+
it 'is a no-op when configured to :expect twice' do
|
124
|
+
sandboxed do
|
125
|
+
RSpec::Matchers.stub(:method_added).and_raise("no methods should be added here")
|
106
126
|
|
107
|
-
|
108
|
-
|
109
|
-
|
127
|
+
configure_syntax :expect
|
128
|
+
configure_syntax :expect
|
129
|
+
end
|
110
130
|
end
|
111
|
-
end
|
112
131
|
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
132
|
+
it 'can re-enable the :should syntax' do
|
133
|
+
sandboxed do
|
134
|
+
configure_syntax :expect
|
135
|
+
configure_syntax [:should, :expect]
|
136
|
+
configured_syntax.should eq([:should, :expect])
|
118
137
|
|
119
|
-
|
120
|
-
|
121
|
-
|
138
|
+
3.should eq(3)
|
139
|
+
3.should_not eq(4)
|
140
|
+
expect(3).to eq(3)
|
141
|
+
end
|
122
142
|
end
|
123
|
-
end
|
124
|
-
|
125
|
-
it 'does not add the deprecated #should to ExpectationTarget when only :should is enabled' do
|
126
|
-
et = Expectations::ExpectationTarget
|
127
143
|
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
end
|
144
|
+
it 'can re-enable the :expect syntax' do
|
145
|
+
sandboxed do
|
146
|
+
configure_syntax :should
|
147
|
+
configure_syntax [:should, :expect]
|
148
|
+
configured_syntax.should eq([:should, :expect])
|
134
149
|
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
expect(expect(3)).not_to respond_to(:should_not)
|
150
|
+
3.should eq(3)
|
151
|
+
3.should_not eq(4)
|
152
|
+
expect(3).to eq(3)
|
153
|
+
end
|
140
154
|
end
|
141
|
-
end
|
142
155
|
|
143
|
-
|
144
|
-
|
156
|
+
it 'does not add the deprecated #should to ExpectationTarget when only :should is enabled' do
|
157
|
+
et = Expectations::ExpectationTarget
|
145
158
|
|
146
|
-
it 'allows `expect {}.should` to be used' do
|
147
159
|
sandboxed do
|
148
|
-
configure_syntax
|
149
|
-
|
150
|
-
|
160
|
+
configure_syntax :should
|
161
|
+
et.new(Proc.new {}).should be_an(et)
|
162
|
+
et.new(Proc.new {}).should_not be_a(Proc)
|
151
163
|
end
|
152
164
|
end
|
153
165
|
|
154
|
-
it '
|
166
|
+
it 'does not add the deprecated #should to ExpectationTarget when only :expect is enabled' do
|
155
167
|
sandboxed do
|
156
|
-
configure_syntax
|
168
|
+
configure_syntax :expect
|
169
|
+
expect(expect(3)).not_to respond_to(:should)
|
170
|
+
expect(expect(3)).not_to respond_to(:should_not)
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
context 'when both :expect and :should are enabled' do
|
175
|
+
before { RSpec.stub(:warn) }
|
176
|
+
|
177
|
+
it 'allows `expect {}.should` to be used' do
|
178
|
+
sandboxed do
|
179
|
+
configure_syntax [:should, :expect]
|
180
|
+
expect { raise "boom" }.should raise_error("boom")
|
181
|
+
expect { }.should_not raise_error
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
it 'prints a deprecation notice when `expect {}.should` is used' do
|
186
|
+
sandboxed do
|
187
|
+
configure_syntax [:should, :expect]
|
157
188
|
|
158
|
-
|
159
|
-
|
189
|
+
RSpec.should_receive(:warn).with(/please use `expect \{ \}.to.*instead/)
|
190
|
+
expect { raise "boom" }.should raise_error("boom")
|
160
191
|
|
161
|
-
|
162
|
-
|
192
|
+
RSpec.should_receive(:warn).with(/please use `expect \{ \}.to_not.*instead/)
|
193
|
+
expect { }.should_not raise_error
|
194
|
+
end
|
163
195
|
end
|
164
196
|
end
|
165
197
|
end
|
166
|
-
end
|
167
198
|
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
199
|
+
describe "configuring rspec-expectations directly" do
|
200
|
+
it_behaves_like "configuring the expectation syntax" do
|
201
|
+
def configure_syntax(syntax)
|
202
|
+
RSpec::Matchers.configuration.syntax = syntax
|
203
|
+
end
|
173
204
|
|
174
|
-
|
175
|
-
|
205
|
+
def configured_syntax
|
206
|
+
RSpec::Matchers.configuration.syntax
|
207
|
+
end
|
176
208
|
end
|
177
209
|
end
|
178
|
-
end
|
179
210
|
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
211
|
+
describe "configuring using the rspec-core config API" do
|
212
|
+
it_behaves_like "configuring the expectation syntax" do
|
213
|
+
def configure_syntax(syntax)
|
214
|
+
RSpec.configure do |rspec|
|
215
|
+
rspec.expect_with :rspec do |c|
|
216
|
+
c.syntax = syntax
|
217
|
+
end
|
186
218
|
end
|
187
219
|
end
|
188
|
-
end
|
189
220
|
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
221
|
+
def configured_syntax
|
222
|
+
RSpec.configure do |rspec|
|
223
|
+
rspec.expect_with :rspec do |c|
|
224
|
+
return c.syntax
|
225
|
+
end
|
194
226
|
end
|
195
227
|
end
|
196
228
|
end
|