rspec-expectations 2.11.3 → 2.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. data/Changelog.md +25 -2
  2. data/README.md +5 -1
  3. data/features/built_in_matchers/be.feature +4 -4
  4. data/features/built_in_matchers/be_within.feature +1 -1
  5. data/features/built_in_matchers/cover.feature +1 -1
  6. data/features/built_in_matchers/end_with.feature +2 -2
  7. data/features/built_in_matchers/equality.feature +5 -5
  8. data/features/built_in_matchers/exist.feature +1 -1
  9. data/features/built_in_matchers/expect_change.feature +3 -3
  10. data/features/built_in_matchers/expect_error.feature +4 -4
  11. data/features/built_in_matchers/have.feature +2 -2
  12. data/features/built_in_matchers/include.feature +3 -3
  13. data/features/built_in_matchers/match.feature +2 -2
  14. data/features/built_in_matchers/operators.feature +3 -3
  15. data/features/built_in_matchers/predicates.feature +9 -8
  16. data/features/built_in_matchers/respond_to.feature +2 -2
  17. data/features/built_in_matchers/satisfy.feature +1 -1
  18. data/features/built_in_matchers/start_with.feature +2 -2
  19. data/features/built_in_matchers/throw_symbol.feature +3 -3
  20. data/features/built_in_matchers/types.feature +2 -2
  21. data/features/built_in_matchers/yield.feature +5 -5
  22. data/features/custom_matchers/access_running_example.feature +3 -3
  23. data/features/custom_matchers/define_diffable_matcher.feature +1 -1
  24. data/features/custom_matchers/define_matcher.feature +12 -12
  25. data/features/custom_matchers/define_matcher_outside_rspec.feature +1 -1
  26. data/features/custom_matchers/define_matcher_with_fluent_interface.feature +1 -1
  27. data/features/customized_message.feature +1 -1
  28. data/features/diffing.feature +4 -4
  29. data/features/implicit_docstrings.feature +2 -2
  30. data/features/test_frameworks/test_unit.feature +1 -1
  31. data/lib/rspec/expectations/differ.rb +35 -1
  32. data/lib/rspec/expectations/handler.rb +21 -6
  33. data/lib/rspec/expectations/version.rb +1 -1
  34. data/lib/rspec/matchers/built_in/be_instance_of.rb +4 -0
  35. data/lib/rspec/matchers/built_in/include.rb +3 -1
  36. data/lib/rspec/matchers/built_in/match_array.rb +12 -6
  37. data/lib/rspec/matchers/built_in/raise_error.rb +17 -7
  38. data/lib/rspec/matchers/built_in/yield.rb +5 -5
  39. data/lib/rspec/matchers/configuration.rb +42 -0
  40. data/lib/rspec/matchers/generated_descriptions.rb +2 -3
  41. data/spec/rspec/expectations/differ_spec.rb +24 -0
  42. data/spec/rspec/expectations/handler_spec.rb +40 -40
  43. data/spec/rspec/expectations/syntax_spec.rb +35 -0
  44. data/spec/rspec/matchers/be_instance_of_spec.rb +19 -2
  45. data/spec/rspec/matchers/be_spec.rb +10 -10
  46. data/spec/rspec/matchers/configuration_spec.rb +155 -123
  47. data/spec/rspec/matchers/dsl_spec.rb +8 -8
  48. data/spec/rspec/matchers/have_spec.rb +8 -38
  49. data/spec/rspec/matchers/include_spec.rb +6 -0
  50. data/spec/rspec/matchers/match_array_spec.rb +14 -0
  51. data/spec/rspec/matchers/raise_error_spec.rb +89 -38
  52. data/spec/rspec/matchers/start_with_end_with_spec.rb +1 -1
  53. data/spec/rspec/matchers/yield_spec.rb +35 -0
  54. 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
- RSpec::Matchers.configuration.add_should_and_should_not_to Delegator
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
- 3.should eq(3)
57
- 3.should_not eq(4)
58
- expect(3).to eq(3)
59
- end
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
- it 'can limit the syntax to :should' do
62
- sandboxed do
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
- lambda { expect(6).to eq(6) }.should raise_error(NameError)
89
+ expect(3).to eq(3)
69
90
  end
70
- end
71
91
 
72
- it 'is a no-op when configured to :should twice' do
73
- sandboxed do
74
- ::Kernel.stub(:method_added).and_raise("no methods should be added here")
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
- configure_syntax :should
77
- configure_syntax :should
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
- it 'can limit the syntax to :expect' do
82
- sandboxed do
83
- configure_syntax :expect
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
- expect(3).to eq(3)
87
- expect { 3.should eq(3) }.to raise_error(NameError)
88
- expect { 3.should_not eq(3) }.to raise_error(NameError)
107
+ configure_syntax :should
108
+ configure_syntax :should
109
+ end
89
110
  end
90
- end
91
111
 
92
- it 'is a no-op when configured to :expect twice' do
93
- sandboxed do
94
- RSpec::Matchers.stub(:method_added).and_raise("no methods should be added here")
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
- configure_syntax :expect
97
- configure_syntax :expect
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
- it 'can re-enable the :should syntax' do
102
- sandboxed do
103
- configure_syntax :expect
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
- 3.should eq(3)
108
- 3.should_not eq(4)
109
- expect(3).to eq(3)
127
+ configure_syntax :expect
128
+ configure_syntax :expect
129
+ end
110
130
  end
111
- end
112
131
 
113
- it 'can re-enable the :expect syntax' do
114
- sandboxed do
115
- configure_syntax :should
116
- configure_syntax [:should, :expect]
117
- configured_syntax.should eq([:should, :expect])
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
- 3.should eq(3)
120
- 3.should_not eq(4)
121
- expect(3).to eq(3)
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
- sandboxed do
129
- configure_syntax :should
130
- et.new(Proc.new {}).should be_an(et)
131
- et.new(Proc.new {}).should_not be_a(Proc)
132
- end
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
- it 'does not add the deprecated #should to ExpectationTarget when only :expect is enabled' do
136
- sandboxed do
137
- configure_syntax :expect
138
- expect(expect(3)).not_to respond_to(:should)
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
- context 'when both :expect and :should are enabled' do
144
- before { RSpec.stub(:warn) }
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 [:should, :expect]
149
- expect { raise "boom" }.should raise_error("boom")
150
- expect { }.should_not raise_error
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 'prints a deprecation notice when `expect {}.should` is used' do
166
+ it 'does not add the deprecated #should to ExpectationTarget when only :expect is enabled' do
155
167
  sandboxed do
156
- configure_syntax [:should, :expect]
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
- RSpec.should_receive(:warn).with(/please use `expect \{ \}.to.*instead/)
159
- expect { raise "boom" }.should raise_error("boom")
189
+ RSpec.should_receive(:warn).with(/please use `expect \{ \}.to.*instead/)
190
+ expect { raise "boom" }.should raise_error("boom")
160
191
 
161
- RSpec.should_receive(:warn).with(/please use `expect \{ \}.to_not.*instead/)
162
- expect { }.should_not raise_error
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
- describe "configuring rspec-expectations directly" do
169
- it_behaves_like "configuring the expectation syntax" do
170
- def configure_syntax(syntax)
171
- RSpec::Matchers.configuration.syntax = syntax
172
- end
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
- def configured_syntax
175
- RSpec::Matchers.configuration.syntax
205
+ def configured_syntax
206
+ RSpec::Matchers.configuration.syntax
207
+ end
176
208
  end
177
209
  end
178
- end
179
210
 
180
- describe "configuring using the rspec-core config API" do
181
- it_behaves_like "configuring the expectation syntax" do
182
- def configure_syntax(syntax)
183
- RSpec.configure do |rspec|
184
- rspec.expect_with :rspec do |c|
185
- c.syntax = syntax
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
- def configured_syntax
191
- RSpec.configure do |rspec|
192
- rspec.expect_with :rspec do |c|
193
- return c.syntax
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