rspec-expectations 2.0.0.rc → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -0
- data/History.markdown +11 -0
- data/Rakefile +30 -14
- data/features/README.markdown +14 -8
- data/features/matchers/access_running_example.feature +1 -1
- data/features/matchers/define_diffable_matcher.feature +1 -1
- data/features/matchers/define_matcher.feature +54 -1
- data/features/matchers/define_matcher_outside_rspec.feature +1 -1
- data/features/matchers/define_matcher_with_fluent_interface.feature +8 -11
- data/features/matchers/equality.feature +1 -1
- data/features/matchers/expect_change.feature +1 -1
- data/features/matchers/expect_error.feature +1 -1
- data/features/matchers/have.feature +1 -1
- data/features/matchers/include.feature +136 -0
- data/features/matchers/operators.feature +1 -1
- data/features/matchers/predicates.feature +1 -1
- data/features/matchers/respond_to.feature +78 -0
- data/features/step_definitions/additional_cli_steps.rb +6 -0
- data/lib/rspec/expectations/version.rb +1 -1
- data/lib/rspec/matchers/include.rb +11 -5
- data/lib/rspec/matchers/matcher.rb +25 -3
- data/lib/rspec/matchers/respond_to.rb +23 -9
- data/spec/rspec/matchers/include_spec.rb +318 -65
- data/spec/rspec/matchers/matcher_spec.rb +37 -0
- data/spec/rspec/matchers/respond_to_spec.rb +177 -1
- data/spec/spec_helper.rb +2 -0
- metadata +30 -37
data/Gemfile
CHANGED
data/History.markdown
CHANGED
@@ -1,5 +1,16 @@
|
|
1
1
|
## rspec-expectations release history (incomplete)
|
2
2
|
|
3
|
+
### 2.0.0 / 2010-10-10
|
4
|
+
|
5
|
+
[full changelog](http://github.com/rspec/rspec-expectations/compare/v2.0.0.rc...v2.0.0)
|
6
|
+
|
7
|
+
* Enhancements
|
8
|
+
* Add match_for_should_not method to matcher DSL (Myron Marston)
|
9
|
+
|
10
|
+
* Bug fixes
|
11
|
+
* respond_to matcher works correctly with should_not with multiple methods (Myron Marston)
|
12
|
+
* include matcher works correctly with should_not with multiple values (Myron Marston)
|
13
|
+
|
3
14
|
### 2.0.0.rc / 2010-10-05
|
4
15
|
|
5
16
|
[full changelog](http://github.com/rspec/rspec-expectations/compare/v2.0.0.beta.22...v2.0.0.rc)
|
data/Rakefile
CHANGED
@@ -8,8 +8,6 @@ require 'rspec/core/rake_task'
|
|
8
8
|
require 'rspec/expectations/version'
|
9
9
|
require 'cucumber/rake/task'
|
10
10
|
|
11
|
-
RSpec::Core::RakeTask.new(:spec)
|
12
|
-
|
13
11
|
class Cucumber::Rake::Task::ForkedCucumberRunner
|
14
12
|
# When cucumber shells out, we still need it to run in the context of our
|
15
13
|
# bundle.
|
@@ -18,22 +16,34 @@ class Cucumber::Rake::Task::ForkedCucumberRunner
|
|
18
16
|
end
|
19
17
|
end
|
20
18
|
|
21
|
-
desc "Run all examples using rcov"
|
22
|
-
RSpec::Core::RakeTask.new :rcov => :cleanup_rcov_files do |t|
|
23
|
-
t.rcov = true
|
24
|
-
t.rcov_opts = %[-Ilib -Ispec --exclude "gems/*,features"]
|
25
|
-
t.rcov_opts << %[--text-report --sort coverage --no-html --aggregate coverage.data]
|
26
|
-
end
|
27
|
-
|
28
19
|
task :cleanup_rcov_files do
|
29
20
|
rm_rf 'coverage.data'
|
30
21
|
end
|
31
22
|
|
32
|
-
|
33
|
-
|
34
|
-
t.
|
35
|
-
|
36
|
-
|
23
|
+
desc "Run all examples"
|
24
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
25
|
+
t.rspec_opts = %w[--color]
|
26
|
+
end
|
27
|
+
|
28
|
+
Cucumber::Rake::Task.new(:cucumber)
|
29
|
+
|
30
|
+
namespace :spec do
|
31
|
+
desc "Run all examples using rcov"
|
32
|
+
RSpec::Core::RakeTask.new :rcov => :cleanup_rcov_files do |t|
|
33
|
+
t.rcov = true
|
34
|
+
t.rcov_opts = %[-Ilib -Ispec --exclude "gems/*,features"]
|
35
|
+
t.rcov_opts << %[--text-report --sort coverage --no-html --aggregate coverage.data]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
namespace :cucumber do
|
40
|
+
desc "Run cucumber features using rcov"
|
41
|
+
Cucumber::Rake::Task.new :rcov => :cleanup_rcov_files do |t|
|
42
|
+
t.cucumber_opts = %w{--format progress}
|
43
|
+
t.rcov = true
|
44
|
+
t.rcov_opts = %[-Ilib -Ispec --exclude "gems/*,features"]
|
45
|
+
t.rcov_opts << %[--text-report --sort coverage --aggregate coverage.data]
|
46
|
+
end
|
37
47
|
end
|
38
48
|
|
39
49
|
task :default => [:spec, :cucumber]
|
@@ -45,6 +55,12 @@ Rake::RDocTask.new do |rdoc|
|
|
45
55
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
46
56
|
end
|
47
57
|
|
58
|
+
desc "Push cukes to relishapp using the relish-client-gem"
|
59
|
+
task :relish, :version do |t, args|
|
60
|
+
raise "rake relish[VERSION]" unless args[:version]
|
61
|
+
sh "bundle exec relish --organization rspec --project rspec-expectations -v #{args[:version]} push"
|
62
|
+
end
|
63
|
+
|
48
64
|
task :clobber do
|
49
65
|
rm_rf 'doc'
|
50
66
|
rm_rf 'pkg'
|
data/features/README.markdown
CHANGED
@@ -1,12 +1,18 @@
|
|
1
|
-
|
1
|
+
rspec-expectations is used to set expectations in executable
|
2
|
+
examples:
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
describe Account do
|
5
|
+
it "has a balance of zero when first created" do
|
6
|
+
Account.new.balance.should eq(Money.new(0))
|
7
|
+
end
|
8
|
+
end
|
8
9
|
|
9
10
|
## Issues
|
10
11
|
|
11
|
-
|
12
|
-
|
12
|
+
The documentation for rspec-expectations is a work in progress. We'll be adding
|
13
|
+
Cucumber features over time, and clarifying existing ones. If you have
|
14
|
+
specific features you'd like to see added, find the existing documentation
|
15
|
+
incomplete or confusing, or, better yet, wish to write a missing Cucumber
|
16
|
+
feature yourself, please [submit an
|
17
|
+
issue](http://github.com/rspec/rspec-expectations/issues) or a [pull
|
18
|
+
request](http://github.com/rspec/rspec-expectations).
|
@@ -1,4 +1,4 @@
|
|
1
|
-
Feature:
|
1
|
+
Feature: Define matcher
|
2
2
|
|
3
3
|
In order to express my domain clearly in my code examples
|
4
4
|
As an RSpec user
|
@@ -256,3 +256,56 @@ Feature: define matcher
|
|
256
256
|
|
257
257
|
When I run "rspec scoped_matcher_spec.rb"
|
258
258
|
Then the output should contain "3 examples, 0 failures"
|
259
|
+
|
260
|
+
Scenario: matcher with separate logic for should and should_not
|
261
|
+
Given a file named "matcher_with_separate_should_not_logic_spec.rb" with:
|
262
|
+
"""
|
263
|
+
RSpec::Matchers.define :contain do |*expected|
|
264
|
+
match_for_should do |actual|
|
265
|
+
expected.all? { |e| actual.include?(e) }
|
266
|
+
end
|
267
|
+
|
268
|
+
match_for_should_not do |actual|
|
269
|
+
expected.none? { |e| actual.include?(e) }
|
270
|
+
end
|
271
|
+
end
|
272
|
+
|
273
|
+
describe [1, 2, 3] do
|
274
|
+
it { should contain(1, 2) }
|
275
|
+
it { should_not contain(4, 5, 6) }
|
276
|
+
|
277
|
+
# deliberate failures
|
278
|
+
it { should contain(1, 4) }
|
279
|
+
it { should_not contain(1, 4) }
|
280
|
+
end
|
281
|
+
"""
|
282
|
+
When I run "rspec matcher_with_separate_should_not_logic_spec.rb"
|
283
|
+
Then the output should contain all of these:
|
284
|
+
| 4 examples, 2 failures |
|
285
|
+
| expected [1, 2, 3] to contain 1 and 4 |
|
286
|
+
| expected [1, 2, 3] not to contain 1 and 4 |
|
287
|
+
|
288
|
+
Scenario: use define_method to create a helper method with access to matcher params
|
289
|
+
Given a file named "define_method_spec.rb" with:
|
290
|
+
"""
|
291
|
+
RSpec::Matchers.define :be_a_multiple_of do |expected|
|
292
|
+
define_method :is_multiple? do |actual|
|
293
|
+
actual % expected == 0
|
294
|
+
end
|
295
|
+
match { |actual| is_multiple?(actual) }
|
296
|
+
end
|
297
|
+
|
298
|
+
describe 9 do
|
299
|
+
it { should be_a_multiple_of(3) }
|
300
|
+
it { should_not be_a_multiple_of(4) }
|
301
|
+
|
302
|
+
# deliberate failures
|
303
|
+
it { should be_a_multiple_of(2) }
|
304
|
+
it { should_not be_a_multiple_of(3) }
|
305
|
+
end
|
306
|
+
"""
|
307
|
+
When I run "rspec define_method_spec.rb"
|
308
|
+
Then the output should contain all of these:
|
309
|
+
| 4 examples, 2 failures |
|
310
|
+
| expected 9 to be a multiple of 2 |
|
311
|
+
| expected 9 not to be a multiple of 3 |
|
@@ -1,27 +1,24 @@
|
|
1
|
-
Feature:
|
1
|
+
Feature: Define matcher with fluent interface
|
2
2
|
|
3
|
-
|
4
|
-
As an RSpec user
|
5
|
-
I want to define matchers with fluent interfaces
|
3
|
+
Use the chain() method to define matchers with a fluent interface.
|
6
4
|
|
7
|
-
Scenario:
|
5
|
+
Scenario: chained method with argumetn
|
8
6
|
Given a file named "between_spec.rb" with:
|
9
7
|
"""
|
10
8
|
RSpec::Matchers.define :be_bigger_than do |first|
|
11
|
-
def but_smaller_than(second)
|
12
|
-
@second = second
|
13
|
-
self
|
14
|
-
end
|
15
|
-
|
16
9
|
match do |actual|
|
17
10
|
(actual > first) && (actual < @second)
|
18
11
|
end
|
12
|
+
|
13
|
+
chain :but_smaller_than do |second|
|
14
|
+
@second = second
|
15
|
+
end
|
19
16
|
end
|
20
17
|
|
21
18
|
describe 5 do
|
22
19
|
it { should be_bigger_than(4).but_smaller_than(6) }
|
23
20
|
end
|
24
21
|
"""
|
25
|
-
When I run "rspec
|
22
|
+
When I run "rspec between_spec.rb --format documentation"
|
26
23
|
Then the output should contain "1 example, 0 failures"
|
27
24
|
And the output should contain "should be bigger than 4"
|
@@ -0,0 +1,136 @@
|
|
1
|
+
Feature: include matcher
|
2
|
+
|
3
|
+
Use the include matcher to specify than a collection includes one or more
|
4
|
+
expected objects. This works on any object that responds to #include? (such
|
5
|
+
as a string or array):
|
6
|
+
|
7
|
+
"a string".should include("a")
|
8
|
+
"a string".should include("str")
|
9
|
+
"a string".should include("str", "g")
|
10
|
+
"a string".should_not include("foo")
|
11
|
+
|
12
|
+
[1, 2].should include(1)
|
13
|
+
[1, 2].should include(1, 2)
|
14
|
+
[1, 2].should_not include(17)
|
15
|
+
|
16
|
+
The matcher also provides flexible handling for hashes:
|
17
|
+
|
18
|
+
{:a => 1, :b => 2}.should include(:a)
|
19
|
+
{:a => 1, :b => 2}.should include(:a, :b)
|
20
|
+
{:a => 1, :b => 2}.should include(:a => 1)
|
21
|
+
{:a => 1, :b => 2}.should include(:b => 2, :a => 1)
|
22
|
+
{:a => 1, :b => 2}.should_not include(:c)
|
23
|
+
{:a => 1, :b => 2}.should_not include(:a => 2)
|
24
|
+
{:a => 1, :b => 2}.should_not include(:c => 3)
|
25
|
+
|
26
|
+
Scenario: array usage
|
27
|
+
Given a file named "array_include_matcher_spec.rb" with:
|
28
|
+
"""
|
29
|
+
describe [1, 3, 7] do
|
30
|
+
it { should include(1) }
|
31
|
+
it { should include(3) }
|
32
|
+
it { should include(7) }
|
33
|
+
it { should include(1, 7) }
|
34
|
+
it { should include(1, 3, 7) }
|
35
|
+
it { should_not include(17) }
|
36
|
+
it { should_not include(43, 100) }
|
37
|
+
|
38
|
+
# deliberate failures
|
39
|
+
it { should include(4) }
|
40
|
+
it { should_not include(1) }
|
41
|
+
it { should_not include(3) }
|
42
|
+
it { should_not include(7) }
|
43
|
+
it { should_not include(1, 3, 7) }
|
44
|
+
|
45
|
+
# both of these should fail since it includes 1 but not 9
|
46
|
+
it { should include(1, 9) }
|
47
|
+
it { should_not include(1, 9) }
|
48
|
+
end
|
49
|
+
"""
|
50
|
+
When I run "rspec array_include_matcher_spec.rb"
|
51
|
+
Then the output should contain all of these:
|
52
|
+
| 14 examples, 7 failures |
|
53
|
+
| expected [1, 3, 7] to include 4 |
|
54
|
+
| expected [1, 3, 7] not to include 1 |
|
55
|
+
| expected [1, 3, 7] not to include 3 |
|
56
|
+
| expected [1, 3, 7] not to include 7 |
|
57
|
+
| expected [1, 3, 7] not to include 1, 3, and 7 |
|
58
|
+
| expected [1, 3, 7] to include 1 and 9 |
|
59
|
+
| expected [1, 3, 7] not to include 1 and 9 |
|
60
|
+
|
61
|
+
Scenario: string usage
|
62
|
+
Given a file named "string_include_matcher_spec.rb" with:
|
63
|
+
"""
|
64
|
+
describe "a string" do
|
65
|
+
it { should include("str") }
|
66
|
+
it { should include("a", "str", "ng") }
|
67
|
+
it { should_not include("foo") }
|
68
|
+
it { should_not include("foo", "bar") }
|
69
|
+
|
70
|
+
# deliberate failures
|
71
|
+
it { should include("foo") }
|
72
|
+
it { should_not include("str") }
|
73
|
+
it { should include("str", "foo") }
|
74
|
+
it { should_not include("str", "foo") }
|
75
|
+
end
|
76
|
+
"""
|
77
|
+
When I run "rspec string_include_matcher_spec.rb"
|
78
|
+
Then the output should contain all of these:
|
79
|
+
| 8 examples, 4 failures |
|
80
|
+
| expected "a string" to include "foo" |
|
81
|
+
| expected "a string" not to include "str" |
|
82
|
+
| expected "a string" to include "str" and "foo" |
|
83
|
+
| expected "a string" not to include "str" and "foo" |
|
84
|
+
|
85
|
+
Scenario: hash usage
|
86
|
+
Given a file named "hash_include_matcher_spec.rb" with:
|
87
|
+
"""
|
88
|
+
describe Hash do
|
89
|
+
subject { { :a => 7, :b => 5 } }
|
90
|
+
|
91
|
+
it { should include(:a) }
|
92
|
+
it { should include(:b, :a) }
|
93
|
+
it { should include(:a => 7) }
|
94
|
+
it { should include(:b => 5, :a => 7) }
|
95
|
+
it { should_not include(:c) }
|
96
|
+
it { should_not include(:c, :d) }
|
97
|
+
it { should_not include(:d => 2) }
|
98
|
+
it { should_not include(:a => 5) }
|
99
|
+
it { should_not include(:b => 7, :a => 5) }
|
100
|
+
|
101
|
+
# deliberate failures
|
102
|
+
it { should_not include(:a) }
|
103
|
+
it { should_not include(:b, :a) }
|
104
|
+
it { should_not include(:a => 7) }
|
105
|
+
it { should_not include(:a => 7, :b => 5) }
|
106
|
+
it { should include(:c) }
|
107
|
+
it { should include(:c, :d) }
|
108
|
+
it { should include(:d => 2) }
|
109
|
+
it { should include(:a => 5) }
|
110
|
+
it { should include(:a => 5, :b => 7) }
|
111
|
+
|
112
|
+
# Mixed cases--the hash includes one but not the other.
|
113
|
+
# All 4 of these cases should fail.
|
114
|
+
it { should include(:a, :d) }
|
115
|
+
it { should_not include(:a, :d) }
|
116
|
+
it { should include(:a => 7, :d => 3) }
|
117
|
+
it { should_not include(:a => 7, :d => 3) }
|
118
|
+
end
|
119
|
+
"""
|
120
|
+
When I run "rspec hash_include_matcher_spec.rb"
|
121
|
+
Then the output should contain all of these:
|
122
|
+
| 22 examples, 13 failures |
|
123
|
+
| expected {:a=>7, :b=>5} not to include :a |
|
124
|
+
| expected {:a=>7, :b=>5} not to include :b and :a |
|
125
|
+
| expected {:a=>7, :b=>5} not to include {:a=>7} |
|
126
|
+
| expected {:a=>7, :b=>5} not to include {:a=>7, :b=>5} |
|
127
|
+
| expected {:a=>7, :b=>5} to include :c |
|
128
|
+
| expected {:a=>7, :b=>5} to include :c and :d |
|
129
|
+
| expected {:a=>7, :b=>5} to include {:d=>2} |
|
130
|
+
| expected {:a=>7, :b=>5} to include {:a=>5} |
|
131
|
+
| expected {:a=>7, :b=>5} to include {:a=>5, :b=>7} |
|
132
|
+
| expected {:a=>7, :b=>5} to include :a and :d |
|
133
|
+
| expected {:a=>7, :b=>5} not to include :a and :d |
|
134
|
+
| expected {:a=>7, :b=>5} to include {:a=>7, :d=>3} |
|
135
|
+
| expected {:a=>7, :b=>5} not to include {:a=>7, :d=>3} |
|
136
|
+
|
@@ -0,0 +1,78 @@
|
|
1
|
+
Feature: respond_to matcher
|
2
|
+
|
3
|
+
Use the respond_to matcher to specify details of an object's interface. In
|
4
|
+
its most basic form:
|
5
|
+
|
6
|
+
obj.should respond_to(:foo) # pass if obj.respond_to?(:foo)
|
7
|
+
|
8
|
+
You can specify that an object responds to multiple messages in a single
|
9
|
+
statement with multiple arguments passed to the matcher
|
10
|
+
|
11
|
+
obj.should respond_to(:foo, :bar) # passes if obj.respond_to?(:foo) && obj.respond_to?(:bar)
|
12
|
+
|
13
|
+
If the number of arguments accepted by the method is important to you,
|
14
|
+
you can specify that as well:
|
15
|
+
|
16
|
+
obj.should respond_to(:foo).with(1).argument
|
17
|
+
obj.should respond_to(:bar).with(2).arguments
|
18
|
+
|
19
|
+
Note that this matcher relies entirely upon #respond_to?. If an object
|
20
|
+
dynamically responds to a message via #method_missing, but does not indicate
|
21
|
+
this via #respond_to?, then this matcher will give you false results.
|
22
|
+
|
23
|
+
Scenario: basic usage
|
24
|
+
Given a file named "respond_to_matcher_spec.rb" with:
|
25
|
+
"""
|
26
|
+
describe "a string" do
|
27
|
+
it { should respond_to(:length) }
|
28
|
+
it { should respond_to(:hash, :class, :to_s) }
|
29
|
+
it { should_not respond_to(:to_model) }
|
30
|
+
it { should_not respond_to(:compact, :flatten) }
|
31
|
+
|
32
|
+
# deliberate failures
|
33
|
+
it { should respond_to(:to_model) }
|
34
|
+
it { should respond_to(:compact, :flatten) }
|
35
|
+
it { should_not respond_to(:length) }
|
36
|
+
it { should_not respond_to(:hash, :class, :to_s) }
|
37
|
+
|
38
|
+
# mixed examples--String responds to :length but not :flatten
|
39
|
+
# both specs should fail
|
40
|
+
it { should respond_to(:length, :flatten) }
|
41
|
+
it { should_not respond_to(:length, :flatten) }
|
42
|
+
end
|
43
|
+
"""
|
44
|
+
When I run "rspec respond_to_matcher_spec.rb"
|
45
|
+
Then the output should contain all of these:
|
46
|
+
| 10 examples, 6 failures |
|
47
|
+
| expected "a string" to respond to :to_model |
|
48
|
+
| expected "a string" to respond to :compact, :flatten |
|
49
|
+
| expected "a string" not to respond to :length |
|
50
|
+
| expected "a string" not to respond to :hash, :class, :to_s |
|
51
|
+
| expected "a string" to respond to :flatten |
|
52
|
+
| expected "a string" not to respond to :length |
|
53
|
+
|
54
|
+
Scenario: specify arguments
|
55
|
+
Given a file named "respond_to_matcher_argument_checking_spec.rb" with:
|
56
|
+
"""
|
57
|
+
describe 7 do
|
58
|
+
it { should respond_to(:zero?).with(0).arguments }
|
59
|
+
it { should_not respond_to(:zero?).with(1).argument }
|
60
|
+
|
61
|
+
it { should respond_to(:between?).with(2).arguments }
|
62
|
+
it { should_not respond_to(:between?).with(7).arguments }
|
63
|
+
|
64
|
+
# deliberate failures
|
65
|
+
it { should respond_to(:zero?).with(1).argument }
|
66
|
+
it { should_not respond_to(:zero?).with(0).arguments }
|
67
|
+
|
68
|
+
it { should respond_to(:between?).with(7).arguments }
|
69
|
+
it { should_not respond_to(:between?).with(2).arguments }
|
70
|
+
end
|
71
|
+
"""
|
72
|
+
When I run "rspec respond_to_matcher_argument_checking_spec.rb"
|
73
|
+
Then the output should contain all of these:
|
74
|
+
| 8 examples, 4 failures |
|
75
|
+
| expected 7 to respond to :zero? with 1 argument |
|
76
|
+
| expected 7 not to respond to :zero? with 0 arguments |
|
77
|
+
| expected 7 to respond to :between? with 7 arguments |
|
78
|
+
| expected 7 not to respond to :between? with 2 arguments |
|