rspec-expectations 2.8.0.rc1 → 2.8.0.rc2
Sign up to get free protection for your applications and to get access to all the features.
- data/License.txt +23 -0
- data/README.md +71 -45
- data/features/built_in_matchers/README.md +22 -5
- data/lib/rspec/expectations/fail_with.rb +3 -3
- data/lib/rspec/expectations/version.rb +1 -1
- data/lib/rspec/matchers/base_matcher.rb +16 -1
- data/lib/rspec/matchers/dsl.rb +2 -1
- data/lib/rspec/matchers/matcher.rb +149 -38
- data/spec/rspec/matchers/base_matcher_spec.rb +53 -0
- data/spec/rspec/matchers/dsl_spec.rb +0 -17
- data/spec/rspec/matchers/matcher_spec.rb +39 -39
- metadata +16 -11
data/License.txt
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
(The MIT License)
|
2
|
+
|
3
|
+
Copyright (c) 2006 David Chelimsky, The RSpec Development Team
|
4
|
+
Copyright (c) 2005 Steven Baker
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
7
|
+
a copy of this software and associated documentation files (the
|
8
|
+
"Software"), to deal in the Software without restriction, including
|
9
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
10
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
11
|
+
permit persons to whom the Software is furnished to do so, subject to
|
12
|
+
the following conditions:
|
13
|
+
|
14
|
+
The above copyright notice and this permission notice shall be
|
15
|
+
included in all copies or substantial portions of the Software.
|
16
|
+
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
18
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
19
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
20
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
21
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
22
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
23
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -22,19 +22,21 @@ Minitest, or Cucumber, you can install it directly:
|
|
22
22
|
|
23
23
|
Here's an example using rspec-core:
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
25
|
+
```ruby
|
26
|
+
describe Order do
|
27
|
+
it "sums the prices of the items in its line items" do
|
28
|
+
order = Order.new
|
29
|
+
order.add_entry(LineItem.new(:item => Item.new(
|
30
|
+
:price => Money.new(1.11, :USD)
|
31
|
+
)))
|
32
|
+
order.add_entry(LineItem.new(:item => Item.new(
|
33
|
+
:price => Money.new(2.22, :USD),
|
34
|
+
:quantity => 2
|
35
|
+
)))
|
36
|
+
order.total.should eq(Money.new(5.55, :USD))
|
37
|
+
end
|
38
|
+
end
|
39
|
+
```
|
38
40
|
|
39
41
|
The `describe` and `it` methods come from rspec-core. The `Order`, `LineItem`,
|
40
42
|
and `Item` classes would be from _your_ code. The last line of the example
|
@@ -48,73 +50,97 @@ the example passes. If not, it fails with a message like:
|
|
48
50
|
|
49
51
|
### Equivalence
|
50
52
|
|
51
|
-
|
52
|
-
|
53
|
-
|
53
|
+
```ruby
|
54
|
+
actual.should eq(expected) # passes if actual == expected
|
55
|
+
actual.should == expected # passes if actual == expected
|
56
|
+
actual.should eql(expected) # passes if actual.eql?(expected)
|
57
|
+
```
|
54
58
|
|
55
59
|
### Identity
|
56
60
|
|
57
|
-
|
58
|
-
|
61
|
+
```ruby
|
62
|
+
actual.should be(expected) # passes if actual.equal?(expected)
|
63
|
+
actual.should equal(expected) # passes if actual.equal?(expected)
|
64
|
+
```
|
59
65
|
|
60
66
|
### Comparisons
|
61
67
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
68
|
+
```ruby
|
69
|
+
actual.should be > expected
|
70
|
+
actual.should be >= expected
|
71
|
+
actual.should be <= expected
|
72
|
+
actual.should be < expected
|
73
|
+
actual.should be_within(delta).of(expected)
|
74
|
+
```
|
67
75
|
|
68
76
|
### Regular expressions
|
69
77
|
|
70
|
-
|
71
|
-
|
78
|
+
```ruby
|
79
|
+
actual.should =~ /expression/
|
80
|
+
actual.should match(/expression/)
|
81
|
+
```
|
72
82
|
|
73
83
|
### Types/classes
|
74
84
|
|
75
|
-
|
76
|
-
|
85
|
+
```ruby
|
86
|
+
actual.should be_an_instance_of(expected)
|
87
|
+
actual.should be_a_kind_of(expected)
|
88
|
+
```
|
77
89
|
|
78
90
|
### Truthiness
|
79
91
|
|
80
|
-
|
81
|
-
|
82
|
-
|
92
|
+
```ruby
|
93
|
+
actual.should be_true # passes if actual is truthy (not nil or false)
|
94
|
+
actual.should be_false # passes if actual is falsy (nil or false)
|
95
|
+
actual.should be_nil # passes if actual is nil
|
96
|
+
```
|
83
97
|
|
84
98
|
### Expecting errors
|
85
99
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
100
|
+
```ruby
|
101
|
+
expect { ... }.to raise_error
|
102
|
+
expect { ... }.to raise_error(ErrorClass)
|
103
|
+
expect { ... }.to raise_error("message")
|
104
|
+
expect { ... }.to raise_error(ErrorClass, "message")
|
105
|
+
```
|
90
106
|
|
91
107
|
### Expecting throws
|
92
108
|
|
93
|
-
|
94
|
-
|
95
|
-
|
109
|
+
```ruby
|
110
|
+
expect { ... }.to throw_symbol
|
111
|
+
expect { ... }.to throw_symbol(:symbol)
|
112
|
+
expect { ... }.to throw_symbol(:symbol, 'value')
|
113
|
+
```
|
96
114
|
|
97
115
|
### Predicate matchers
|
98
116
|
|
99
|
-
|
100
|
-
|
117
|
+
```ruby
|
118
|
+
actual.should be_xxx # passes if actual.xxx?
|
119
|
+
actual.should have_xxx(:arg) # passes if actual.has_xxx?(:arg)
|
120
|
+
```
|
101
121
|
|
102
122
|
See [RSpec::Matchers](../RSpec/Matchers) for more about predicate matchers.
|
103
123
|
|
104
124
|
### Ranges (Ruby >= 1.9 only)
|
105
125
|
|
106
|
-
|
126
|
+
```ruby
|
127
|
+
(1..10).should cover(3)
|
128
|
+
```
|
107
129
|
|
108
130
|
### Collection membership
|
109
131
|
|
110
|
-
|
132
|
+
```ruby
|
133
|
+
actual.should include(expected)
|
134
|
+
```
|
111
135
|
|
112
136
|
#### Examples
|
113
137
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
138
|
+
```ruby
|
139
|
+
[1,2,3].should include(1)
|
140
|
+
[1,2,3].should include(1, 2)
|
141
|
+
{:a => 'b'}.should include(:a => 'b')
|
142
|
+
"this string".should include("is str")
|
143
|
+
```
|
118
144
|
|
119
145
|
## Learn more
|
120
146
|
|
@@ -1,16 +1,33 @@
|
|
1
|
+
# Built-in Matchers
|
2
|
+
|
3
|
+
Here is a list of matchers that ship with rspec-expectations. Each matcher
|
4
|
+
can be used with `should` or `should_not` e.g.
|
5
|
+
|
6
|
+
result.should eq(3)
|
7
|
+
list.should_not be_empty
|
8
|
+
|
1
9
|
## Object identity
|
2
10
|
|
3
|
-
actual.should
|
11
|
+
actual.should be(expected) # passes if actual.equal?(expected)
|
4
12
|
|
5
13
|
## Object equivalence
|
6
14
|
|
7
|
-
actual.should
|
8
|
-
actual.should eql(expected) # passes if actual.eql?(expected)
|
15
|
+
actual.should eq(expected) # passes if actual == expected
|
9
16
|
|
10
17
|
## Optional APIs for identity/equivalence
|
11
18
|
|
12
|
-
actual.should
|
13
|
-
actual.should
|
19
|
+
actual.should == expected # passes if actual == expected
|
20
|
+
actual.should eql(expected) # passes if actual.eql?(expected)
|
21
|
+
actual.should equal(expected) # passes if actual.equal?(expected)
|
22
|
+
|
23
|
+
# NOTE: this can't work in Ruby 1.8, so we don't support it at all:
|
24
|
+
# actual.should != expected
|
25
|
+
# The reason is that Ruby 1.8 parses it as:
|
26
|
+
# !(actual.should.==(expected)),
|
27
|
+
# so by the time RSpec sees it it has no way to know that it's
|
28
|
+
# been negated. Use either of these instead:
|
29
|
+
# actual.should_not eq(expected)
|
30
|
+
# actual.should_not == expected
|
14
31
|
|
15
32
|
## Comparisons
|
16
33
|
|
@@ -11,7 +11,7 @@ module RSpec
|
|
11
11
|
# @param [Object] expected
|
12
12
|
# @param [Object] actual
|
13
13
|
#
|
14
|
-
# Adds a diff to the failure message when
|
14
|
+
# Adds a diff to the failure message when `expected` and `actual` are
|
15
15
|
# both present.
|
16
16
|
def fail_with(message, expected=nil, actual=nil)
|
17
17
|
if !message
|
@@ -22,10 +22,10 @@ module RSpec
|
|
22
22
|
if actual && expected
|
23
23
|
if all_strings?(actual, expected)
|
24
24
|
if any_multiline_strings?(actual, expected)
|
25
|
-
message << "\nDiff:" <<
|
25
|
+
message << "\nDiff:" << differ.diff_as_string(actual, expected)
|
26
26
|
end
|
27
27
|
elsif no_procs?(actual, expected) && no_numbers?(actual, expected)
|
28
|
-
message << "\nDiff:" <<
|
28
|
+
message << "\nDiff:" << differ.diff_as_object(actual, expected)
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
@@ -1,5 +1,7 @@
|
|
1
1
|
module RSpec
|
2
2
|
module Matchers
|
3
|
+
# @api private
|
4
|
+
#
|
3
5
|
# Used _internally_ as a base class for matchers that ship with
|
4
6
|
# rspec-expectations.
|
5
7
|
#
|
@@ -11,7 +13,7 @@ module RSpec
|
|
11
13
|
module BaseMatcher
|
12
14
|
include RSpec::Matchers::Pretty
|
13
15
|
|
14
|
-
attr_reader :actual, :expected
|
16
|
+
attr_reader :actual, :expected, :rescued_exception
|
15
17
|
|
16
18
|
def initialize(expected=nil)
|
17
19
|
@expected = expected
|
@@ -21,6 +23,15 @@ module RSpec
|
|
21
23
|
@actual = actual
|
22
24
|
end
|
23
25
|
|
26
|
+
def match_unless_raises(exception=Exception)
|
27
|
+
begin
|
28
|
+
yield
|
29
|
+
true
|
30
|
+
rescue exception => @rescued_exception
|
31
|
+
false
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
24
35
|
def failure_message_for_should
|
25
36
|
"expected #{actual.inspect} to #{name_to_sentence}#{expected_to_sentence}"
|
26
37
|
end
|
@@ -36,6 +47,10 @@ module RSpec
|
|
36
47
|
def diffable?
|
37
48
|
false
|
38
49
|
end
|
50
|
+
|
51
|
+
def ==(other)
|
52
|
+
matches?(other)
|
53
|
+
end
|
39
54
|
end
|
40
55
|
end
|
41
56
|
end
|
data/lib/rspec/matchers/dsl.rb
CHANGED
@@ -4,9 +4,10 @@ module RSpec
|
|
4
4
|
# Defines a custom matcher.
|
5
5
|
# @see RSpec::Matchers
|
6
6
|
def define(name, &declarations)
|
7
|
+
matcher = RSpec::Matchers::Matcher.new(name, &declarations)
|
7
8
|
define_method name do |*expected|
|
8
9
|
$matcher_execution_context = self
|
9
|
-
|
10
|
+
matcher.for_expected *expected
|
10
11
|
end
|
11
12
|
end
|
12
13
|
|
@@ -1,16 +1,20 @@
|
|
1
1
|
module RSpec
|
2
2
|
module Matchers
|
3
|
+
# Provides the context in which the block passed to RSpec::Matchers.define
|
4
|
+
# will be evaluated.
|
3
5
|
class Matcher
|
4
6
|
include RSpec::Matchers::Extensions::InstanceEvalWithArgs
|
5
7
|
include RSpec::Matchers::Pretty
|
6
8
|
include RSpec::Matchers
|
7
9
|
|
8
10
|
attr_reader :expected, :actual, :rescued_exception
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
@
|
13
|
-
@
|
11
|
+
|
12
|
+
# @api private
|
13
|
+
def initialize(name, &declarations)
|
14
|
+
@name = name
|
15
|
+
@declarations = declarations
|
16
|
+
@actual = nil
|
17
|
+
@diffable = false
|
14
18
|
@expected_exception, @rescued_exception = nil, nil
|
15
19
|
@match_for_should_not_block = nil
|
16
20
|
|
@@ -19,12 +23,19 @@ module RSpec
|
|
19
23
|
:failure_message_for_should => lambda {|actual| "expected #{actual.inspect} to #{name_to_sentence}#{expected_to_sentence}"},
|
20
24
|
:failure_message_for_should_not => lambda {|actual| "expected #{actual.inspect} not to #{name_to_sentence}#{expected_to_sentence}"}
|
21
25
|
}
|
26
|
+
end
|
27
|
+
|
28
|
+
# @api private
|
29
|
+
def for_expected(*expected)
|
30
|
+
@expected = expected
|
22
31
|
making_declared_methods_public do
|
23
|
-
instance_eval_with_args(*@expected,
|
32
|
+
instance_eval_with_args(*@expected, &@declarations)
|
24
33
|
end
|
34
|
+
self
|
25
35
|
end
|
26
|
-
|
27
|
-
#
|
36
|
+
|
37
|
+
# @api private
|
38
|
+
# Used internally by +should+ and +should_not+.
|
28
39
|
def matches?(actual)
|
29
40
|
@actual = actual
|
30
41
|
if @expected_exception
|
@@ -43,65 +54,142 @@ module RSpec
|
|
43
54
|
end
|
44
55
|
end
|
45
56
|
|
46
|
-
#
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
end
|
61
|
-
|
62
|
-
#
|
57
|
+
# Stores the block that is used to determine whether this matcher passes
|
58
|
+
# or fails. The block should return a boolean value. When the matcher is
|
59
|
+
# passed to `should` and the block returns `true`, then the expectation
|
60
|
+
# passes. Similarly, when the matcher is passed to `should_not` and the
|
61
|
+
# block returns `false`, then the expectation passes.
|
62
|
+
#
|
63
|
+
# Use `match_for_should` when used in conjuntion with
|
64
|
+
# `match_for_should_not`.
|
65
|
+
#
|
66
|
+
# @example
|
67
|
+
#
|
68
|
+
# RSpec::Matchers.define :be_even do
|
69
|
+
# match do |actual|
|
70
|
+
# actual.even?
|
71
|
+
# end
|
72
|
+
# end
|
73
|
+
#
|
74
|
+
# 4.should be_even # passes
|
75
|
+
# 3.should_not be_even # passes
|
76
|
+
# 3.should be_even # fails
|
77
|
+
# 4.should_not be_even # fails
|
78
|
+
#
|
79
|
+
# @yield [Object] actual the actual value (or receiver of should)
|
63
80
|
def match(&block)
|
64
81
|
@match_block = block
|
65
82
|
end
|
66
|
-
alias match_for_should match
|
67
83
|
|
68
|
-
|
84
|
+
alias_method :match_for_should, :match
|
85
|
+
|
86
|
+
# Use this to define the block for a negative expectation (`should_not`)
|
87
|
+
# when the positive and negative forms require different handling. This
|
88
|
+
# is rarely necessary, but can be helpful, for example, when specifying
|
89
|
+
# asynchronous processes that require different timeouts.
|
90
|
+
#
|
91
|
+
# @yield [Object] actual the actual value (or receiver of should)
|
69
92
|
def match_for_should_not(&block)
|
70
93
|
@match_for_should_not_block = block
|
71
94
|
end
|
72
95
|
|
73
|
-
#
|
96
|
+
# Use this instead of `match` when the block will raise an exception
|
97
|
+
# rather than returning false to indicate a failure.
|
98
|
+
#
|
99
|
+
# @example
|
100
|
+
#
|
101
|
+
# RSpec::Matchers.define :accept_as_valid do |candidate_address|
|
102
|
+
# match_unless_raises ValidationException do |validator|
|
103
|
+
# validator.validate(candidate_address)
|
104
|
+
# end
|
105
|
+
# end
|
106
|
+
#
|
107
|
+
# email_validator.should accept_as_valid("person@company.com")
|
74
108
|
def match_unless_raises(exception=Exception, &block)
|
75
109
|
@expected_exception = exception
|
76
110
|
match(&block)
|
77
111
|
end
|
78
112
|
|
79
|
-
#
|
113
|
+
# Customize the failure messsage to use when this matcher is invoked with
|
114
|
+
# `should`. Only use this when the message generated by default doesn't
|
115
|
+
# suit your needs.
|
116
|
+
#
|
117
|
+
# @example
|
118
|
+
#
|
119
|
+
# RSpec::Matchers.define :have_strength do |expected|
|
120
|
+
# match { ... }
|
121
|
+
#
|
122
|
+
# failure_message_for_should do |actual|
|
123
|
+
# "Expected strength of #{expected}, but had #{actual.strength}"
|
124
|
+
# end
|
125
|
+
# end
|
126
|
+
#
|
127
|
+
# @yield [Object] actual the actual object
|
80
128
|
def failure_message_for_should(&block)
|
81
129
|
cache_or_call_cached(:failure_message_for_should, &block)
|
82
130
|
end
|
83
131
|
|
84
|
-
#
|
132
|
+
# Customize the failure messsage to use when this matcher is invoked with
|
133
|
+
# `should_not`. Only use this when the message generated by default
|
134
|
+
# doesn't suit your needs.
|
135
|
+
#
|
136
|
+
# @example
|
137
|
+
#
|
138
|
+
# RSpec::Matchers.define :have_strength do |expected|
|
139
|
+
# match { ... }
|
140
|
+
#
|
141
|
+
# failure_message_for_should_not do |actual|
|
142
|
+
# "Expected not to have strength of #{expected}, but did"
|
143
|
+
# end
|
144
|
+
# end
|
145
|
+
#
|
146
|
+
# @yield [Object] actual the actual object
|
147
|
+
# @yield [Object] actual the actual object
|
85
148
|
def failure_message_for_should_not(&block)
|
86
149
|
cache_or_call_cached(:failure_message_for_should_not, &block)
|
87
150
|
end
|
88
151
|
|
89
|
-
|
152
|
+
|
153
|
+
# Customize the description to use for one-liners. Only use this when
|
154
|
+
# the description generated by default doesn't suit your needs.
|
155
|
+
#
|
156
|
+
# @example
|
157
|
+
#
|
158
|
+
# RSpec::Matchers.define :qualify_for do |expected|
|
159
|
+
# match { ... }
|
160
|
+
#
|
161
|
+
# description do
|
162
|
+
# "qualify for #{expected}"
|
163
|
+
# end
|
164
|
+
# end
|
90
165
|
def description(&block)
|
91
166
|
cache_or_call_cached(:description, &block)
|
92
167
|
end
|
93
168
|
|
94
|
-
#
|
95
|
-
|
96
|
-
@diffable
|
97
|
-
end
|
98
|
-
|
99
|
-
# See RSpec::Matchers
|
169
|
+
# Tells the matcher to diff the actual and expected values in the failure
|
170
|
+
# message.
|
100
171
|
def diffable
|
101
172
|
@diffable = true
|
102
173
|
end
|
103
174
|
|
104
|
-
#
|
175
|
+
# Convenience for defining methods on this matcher to create a fluent
|
176
|
+
# interface. The trick about fluent interfaces is that each method must
|
177
|
+
# return self in order to chain methods together. `chain` handles that
|
178
|
+
# for you.
|
179
|
+
#
|
180
|
+
# @example
|
181
|
+
#
|
182
|
+
# RSpec::Matchers.define :have_errors_on do |key|
|
183
|
+
# chain :with do |message|
|
184
|
+
# @message = message
|
185
|
+
# end
|
186
|
+
#
|
187
|
+
# match do |actual|
|
188
|
+
# actual.errors[key] == @message
|
189
|
+
# end
|
190
|
+
# end
|
191
|
+
#
|
192
|
+
# minor.should have_errors_on(:age).with("Not old enough to participate")
|
105
193
|
def chain(method, &block)
|
106
194
|
define_method method do |*args|
|
107
195
|
block.call(*args)
|
@@ -109,8 +197,31 @@ module RSpec
|
|
109
197
|
end
|
110
198
|
end
|
111
199
|
|
200
|
+
# @api private
|
201
|
+
# Used internally by objects returns by +should+ and +should_not+.
|
202
|
+
def diffable?
|
203
|
+
@diffable
|
204
|
+
end
|
205
|
+
|
206
|
+
# @api private
|
207
|
+
# Used internally by +should_not+
|
208
|
+
def does_not_match?(actual)
|
209
|
+
@actual = actual
|
210
|
+
@match_for_should_not_block ?
|
211
|
+
instance_eval_with_args(actual, &@match_for_should_not_block) :
|
212
|
+
!matches?(actual)
|
213
|
+
end
|
214
|
+
|
112
215
|
private
|
113
216
|
|
217
|
+
def include(*args)
|
218
|
+
singleton_class.__send__(:include, *args)
|
219
|
+
end
|
220
|
+
|
221
|
+
def define_method(name, &block)
|
222
|
+
singleton_class.__send__(:define_method, name, &block)
|
223
|
+
end
|
224
|
+
|
114
225
|
def method_missing(method, *args, &block)
|
115
226
|
if $matcher_execution_context.respond_to?(method)
|
116
227
|
$matcher_execution_context.send method, *args, &block
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module RSpec::Matchers
|
2
|
+
|
3
|
+
describe BaseMatcher do
|
4
|
+
describe "#match_unless_raises" do
|
5
|
+
let(:matcher) do
|
6
|
+
Class.new do
|
7
|
+
include BaseMatcher
|
8
|
+
end.new
|
9
|
+
end
|
10
|
+
|
11
|
+
it "returns true if there are no errors" do
|
12
|
+
matcher.match_unless_raises {}.should be_true
|
13
|
+
end
|
14
|
+
|
15
|
+
it "returns false if there is an error" do
|
16
|
+
matcher.match_unless_raises { raise }.should be_false
|
17
|
+
end
|
18
|
+
|
19
|
+
it "returns false if the submitted error is raised" do
|
20
|
+
matcher.match_unless_raises(RuntimeError){ raise "foo" }.should be_false
|
21
|
+
end
|
22
|
+
|
23
|
+
it "re-raises any error other than the one specified" do
|
24
|
+
expect do
|
25
|
+
matcher.match_unless_raises(ArgumentError){ raise "foo" }
|
26
|
+
end.to raise_error
|
27
|
+
end
|
28
|
+
|
29
|
+
it "stores the rescued exception for use in messages" do
|
30
|
+
matcher.match_unless_raises(RuntimeError){ raise "foo" }
|
31
|
+
matcher.rescued_exception.should be_a(RuntimeError)
|
32
|
+
matcher.rescued_exception.message.should eq("foo")
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "#==" do
|
38
|
+
it "responds the same way as matches?" do
|
39
|
+
matcher = Class.new do
|
40
|
+
include BaseMatcher
|
41
|
+
def matches?(actual)
|
42
|
+
actual == expected
|
43
|
+
end
|
44
|
+
end
|
45
|
+
matcher.new(3).matches?(3).should be_true
|
46
|
+
matcher.new(3).should eq(3)
|
47
|
+
|
48
|
+
matcher.new(3).matches?(4).should be_false
|
49
|
+
matcher.new(3).should_not eq(4)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -3,23 +3,6 @@ require 'spec_helper'
|
|
3
3
|
module RSpec
|
4
4
|
module Matchers
|
5
5
|
module DSL
|
6
|
-
describe "#define" do
|
7
|
-
it "creates a method that initializes a new matcher with the submitted name and expected arg" do
|
8
|
-
# FIXME - this expects new to be called, but we need something
|
9
|
-
# more robust - that expects new to be called with a specific
|
10
|
-
# block (lambda, proc, whatever)
|
11
|
-
mod = Module.new
|
12
|
-
mod.extend RSpec::Matchers::DSL
|
13
|
-
mod.define(:foo)
|
14
|
-
|
15
|
-
obj = Object.new
|
16
|
-
obj.extend mod
|
17
|
-
|
18
|
-
RSpec::Matchers::Matcher.should_receive(:new).with(:foo, 3) { stub('matcher').as_null_object }
|
19
|
-
|
20
|
-
obj.foo(3)
|
21
|
-
end
|
22
|
-
end
|
23
6
|
end
|
24
7
|
end
|
25
8
|
end
|
@@ -27,7 +27,7 @@ module RSpec
|
|
27
27
|
RSpec::Matchers::Matcher.new(:be_a_greeting) do
|
28
28
|
include MatcherHelperModule
|
29
29
|
match { |actual| actual == greeting }
|
30
|
-
end
|
30
|
+
end.for_expected
|
31
31
|
end
|
32
32
|
|
33
33
|
it "has access to the module's methods" do
|
@@ -45,7 +45,7 @@ module RSpec
|
|
45
45
|
it 'allows multiple modules to be included at once' do
|
46
46
|
m = RSpec::Matchers::Matcher.new(:multiple_modules) do
|
47
47
|
include Enumerable, Comparable
|
48
|
-
end
|
48
|
+
end.for_expected
|
49
49
|
m.should be_a(Enumerable)
|
50
50
|
m.should be_a(Comparable)
|
51
51
|
end
|
@@ -53,11 +53,11 @@ module RSpec
|
|
53
53
|
|
54
54
|
context "without overrides" do
|
55
55
|
before(:each) do
|
56
|
-
@matcher = RSpec::Matchers::Matcher.new(:be_a_multiple_of
|
56
|
+
@matcher = RSpec::Matchers::Matcher.new(:be_a_multiple_of) do |multiple|
|
57
57
|
match do |actual|
|
58
58
|
actual % multiple == 0
|
59
59
|
end
|
60
|
-
end
|
60
|
+
end.for_expected(3)
|
61
61
|
end
|
62
62
|
|
63
63
|
it "provides a default description" do
|
@@ -77,7 +77,7 @@ module RSpec
|
|
77
77
|
|
78
78
|
context "with separate match logic for should and should not" do
|
79
79
|
let(:matcher) do
|
80
|
-
RSpec::Matchers::Matcher.new(:to_be_composed_of
|
80
|
+
RSpec::Matchers::Matcher.new(:to_be_composed_of) do |a, b|
|
81
81
|
match_for_should do |actual|
|
82
82
|
actual == a * b
|
83
83
|
end
|
@@ -85,7 +85,7 @@ module RSpec
|
|
85
85
|
match_for_should_not do |actual|
|
86
86
|
actual == a + b
|
87
87
|
end
|
88
|
-
end
|
88
|
+
end.for_expected(7, 11)
|
89
89
|
end
|
90
90
|
|
91
91
|
it "invokes the match_for_should block for #matches?" do
|
@@ -105,9 +105,9 @@ module RSpec
|
|
105
105
|
end
|
106
106
|
|
107
107
|
it "allows helper methods to be defined with #define_method to have access to matcher parameters" do
|
108
|
-
matcher = RSpec::Matchers::Matcher.new(:name
|
108
|
+
matcher = RSpec::Matchers::Matcher.new(:name) do |a, b|
|
109
109
|
define_method(:sum) { a + b }
|
110
|
-
end
|
110
|
+
end.for_expected(3,4)
|
111
111
|
|
112
112
|
matcher.sum.should == 7
|
113
113
|
end
|
@@ -118,19 +118,19 @@ module RSpec
|
|
118
118
|
end
|
119
119
|
|
120
120
|
it "is diffable when told to be" do
|
121
|
-
matcher = RSpec::Matchers::Matcher.new(:name) { diffable }
|
121
|
+
matcher = RSpec::Matchers::Matcher.new(:name) { diffable }.for_expected
|
122
122
|
matcher.should be_diffable
|
123
123
|
end
|
124
124
|
|
125
125
|
it "provides expected" do
|
126
|
-
matcher = RSpec::Matchers::Matcher.new(:name
|
126
|
+
matcher = RSpec::Matchers::Matcher.new(:name) {}.for_expected('expected string')
|
127
127
|
matcher.expected.should == ['expected string']
|
128
128
|
end
|
129
129
|
|
130
130
|
it "provides actual" do
|
131
|
-
matcher = RSpec::Matchers::Matcher.new(:name
|
131
|
+
matcher = RSpec::Matchers::Matcher.new(:name) do
|
132
132
|
match {|actual|}
|
133
|
-
end
|
133
|
+
end.for_expected('expected string')
|
134
134
|
|
135
135
|
matcher.matches?('actual string')
|
136
136
|
|
@@ -139,27 +139,27 @@ module RSpec
|
|
139
139
|
|
140
140
|
context "wrapping another expectation (should == ...)" do
|
141
141
|
it "returns true if the wrapped expectation passes" do
|
142
|
-
matcher = RSpec::Matchers::Matcher.new(:name
|
142
|
+
matcher = RSpec::Matchers::Matcher.new(:name) do |expected|
|
143
143
|
match do |actual|
|
144
144
|
actual.should == expected
|
145
145
|
end
|
146
|
-
end
|
146
|
+
end.for_expected('value')
|
147
147
|
matcher.matches?('value').should be_true
|
148
148
|
end
|
149
149
|
|
150
150
|
it "returns false if the wrapped expectation fails" do
|
151
|
-
matcher = RSpec::Matchers::Matcher.new(:name
|
151
|
+
matcher = RSpec::Matchers::Matcher.new(:name) do |expected|
|
152
152
|
match do |actual|
|
153
153
|
actual.should == expected
|
154
154
|
end
|
155
|
-
end
|
155
|
+
end.for_expected('value')
|
156
156
|
matcher.matches?('other value').should be_false
|
157
157
|
end
|
158
158
|
end
|
159
159
|
|
160
160
|
context "with overrides" do
|
161
161
|
before(:each) do
|
162
|
-
@matcher = RSpec::Matchers::Matcher.new(:be_boolean
|
162
|
+
@matcher = RSpec::Matchers::Matcher.new(:be_boolean) do |boolean|
|
163
163
|
match do |actual|
|
164
164
|
actual
|
165
165
|
end
|
@@ -172,7 +172,7 @@ module RSpec
|
|
172
172
|
failure_message_for_should_not do |actual|
|
173
173
|
"expected #{actual} not to be the boolean #{boolean}"
|
174
174
|
end
|
175
|
-
end
|
175
|
+
end.for_expected(true)
|
176
176
|
end
|
177
177
|
|
178
178
|
it "does not hide result of match block when true" do
|
@@ -204,16 +204,16 @@ module RSpec
|
|
204
204
|
match do |actual|
|
205
205
|
actual == 5
|
206
206
|
end
|
207
|
-
end
|
207
|
+
end.for_expected
|
208
208
|
matcher.matches?(5).should be_true
|
209
209
|
end
|
210
210
|
|
211
211
|
it "exposes arg submitted through #new to matcher block" do
|
212
|
-
matcher = RSpec::Matchers::Matcher.new(:ignore
|
212
|
+
matcher = RSpec::Matchers::Matcher.new(:ignore) do |expected|
|
213
213
|
match do |actual|
|
214
214
|
actual > expected
|
215
215
|
end
|
216
|
-
end
|
216
|
+
end.for_expected(4)
|
217
217
|
matcher.matches?(5).should be_true
|
218
218
|
end
|
219
219
|
end
|
@@ -224,7 +224,7 @@ module RSpec
|
|
224
224
|
match do |actual|
|
225
225
|
actual == 5
|
226
226
|
end
|
227
|
-
end
|
227
|
+
end.for_expected
|
228
228
|
end
|
229
229
|
|
230
230
|
it "matches" do
|
@@ -238,11 +238,11 @@ module RSpec
|
|
238
238
|
|
239
239
|
context "with 1 arg" do
|
240
240
|
before(:each) do
|
241
|
-
@matcher = RSpec::Matchers::Matcher.new(:matcher_name
|
241
|
+
@matcher = RSpec::Matchers::Matcher.new(:matcher_name) do |expected|
|
242
242
|
match do |actual|
|
243
243
|
actual == 5 && expected == 1
|
244
244
|
end
|
245
|
-
end
|
245
|
+
end.for_expected(1)
|
246
246
|
end
|
247
247
|
|
248
248
|
it "matches" do
|
@@ -256,11 +256,11 @@ module RSpec
|
|
256
256
|
|
257
257
|
context "with multiple args" do
|
258
258
|
before(:each) do
|
259
|
-
@matcher = RSpec::Matchers::Matcher.new(:matcher_name
|
259
|
+
@matcher = RSpec::Matchers::Matcher.new(:matcher_name) do |a,b,c,d|
|
260
260
|
match do |sum|
|
261
261
|
a + b + c + d == sum
|
262
262
|
end
|
263
|
-
end
|
263
|
+
end.for_expected(1,2,3,4)
|
264
264
|
end
|
265
265
|
|
266
266
|
it "matches" do
|
@@ -273,7 +273,7 @@ module RSpec
|
|
273
273
|
end
|
274
274
|
|
275
275
|
it "supports helper methods" do
|
276
|
-
matcher = RSpec::Matchers::Matcher.new(:be_similar_to
|
276
|
+
matcher = RSpec::Matchers::Matcher.new(:be_similar_to) do |sample|
|
277
277
|
match do |actual|
|
278
278
|
similar?(sample, actual)
|
279
279
|
end
|
@@ -281,7 +281,7 @@ module RSpec
|
|
281
281
|
def similar?(a, b)
|
282
282
|
a.sort == b.sort
|
283
283
|
end
|
284
|
-
end
|
284
|
+
end.for_expected([1,2,3])
|
285
285
|
|
286
286
|
matcher.matches?([2,3,1]).should be_true
|
287
287
|
end
|
@@ -291,23 +291,23 @@ module RSpec
|
|
291
291
|
def second_word
|
292
292
|
self
|
293
293
|
end
|
294
|
-
end
|
294
|
+
end.for_expected
|
295
295
|
|
296
296
|
matcher.second_word.should == matcher
|
297
297
|
end
|
298
298
|
|
299
299
|
it "treats method missing normally for undeclared methods" do
|
300
|
-
matcher = RSpec::Matchers::Matcher.new(:ignore) { }
|
300
|
+
matcher = RSpec::Matchers::Matcher.new(:ignore) { }.for_expected
|
301
301
|
expect { matcher.non_existent_method }.to raise_error(NoMethodError)
|
302
302
|
end
|
303
303
|
|
304
304
|
it "has access to other matchers" do
|
305
|
-
matcher = RSpec::Matchers::Matcher.new(:ignore
|
305
|
+
matcher = RSpec::Matchers::Matcher.new(:ignore) do |expected|
|
306
306
|
match do |actual|
|
307
307
|
extend RSpec::Matchers
|
308
308
|
actual.should eql(5 + expected)
|
309
309
|
end
|
310
|
-
end
|
310
|
+
end.for_expected(3)
|
311
311
|
|
312
312
|
matcher.matches?(8).should be_true
|
313
313
|
end
|
@@ -323,12 +323,12 @@ module RSpec
|
|
323
323
|
end
|
324
324
|
let(:matcher) do
|
325
325
|
m = mod
|
326
|
-
RSpec::Matchers::Matcher.new :equal
|
326
|
+
RSpec::Matchers::Matcher.new :equal do |expected|
|
327
327
|
extend m
|
328
328
|
match_unless_raises UnexpectedError do
|
329
329
|
assert_equal expected, actual
|
330
330
|
end
|
331
|
-
end
|
331
|
+
end.for_expected(4)
|
332
332
|
end
|
333
333
|
|
334
334
|
context "with passing assertion" do
|
@@ -352,11 +352,11 @@ module RSpec
|
|
352
352
|
|
353
353
|
context "with an unexpected error" do
|
354
354
|
let(:matcher) do
|
355
|
-
RSpec::Matchers::Matcher.new :foo
|
355
|
+
RSpec::Matchers::Matcher.new :foo do |expected|
|
356
356
|
match_unless_raises SyntaxError do |actual|
|
357
357
|
raise "unexpected exception"
|
358
358
|
end
|
359
|
-
end
|
359
|
+
end.for_expected(:bar)
|
360
360
|
end
|
361
361
|
|
362
362
|
it "raises the error" do
|
@@ -374,15 +374,15 @@ module RSpec
|
|
374
374
|
@expected_value = expected_value
|
375
375
|
end
|
376
376
|
match { |actual| actual == @expected_value }
|
377
|
-
end
|
377
|
+
end.for_expected
|
378
378
|
|
379
379
|
matcher.expecting('value').matches?('value').should be_true
|
380
380
|
matcher.expecting('value').matches?('other value').should be_false
|
381
381
|
end
|
382
382
|
|
383
383
|
it "prevents name collisions on chainable methods from different matchers" do
|
384
|
-
m1 = RSpec::Matchers::Matcher.new(:m1) { chain(:foo) { raise "foo in m1" } }
|
385
|
-
m2 = RSpec::Matchers::Matcher.new(:m2) { chain(:foo) { raise "foo in m2" } }
|
384
|
+
m1 = RSpec::Matchers::Matcher.new(:m1) { chain(:foo) { raise "foo in m1" } }.for_expected
|
385
|
+
m2 = RSpec::Matchers::Matcher.new(:m2) { chain(:foo) { raise "foo in m2" } }.for_expected
|
386
386
|
|
387
387
|
expect { m1.foo }.to raise_error("foo in m1")
|
388
388
|
expect { m2.foo }.to raise_error("foo in m2")
|
metadata
CHANGED
@@ -1,24 +1,24 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec-expectations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 15424209
|
5
5
|
prerelease: 6
|
6
6
|
segments:
|
7
7
|
- 2
|
8
8
|
- 8
|
9
9
|
- 0
|
10
10
|
- rc
|
11
|
-
-
|
12
|
-
version: 2.8.0.
|
11
|
+
- 2
|
12
|
+
version: 2.8.0.rc2
|
13
13
|
platform: ruby
|
14
14
|
authors:
|
15
|
+
- Steven Baker
|
15
16
|
- David Chelimsky
|
16
|
-
- Chad Humphries
|
17
17
|
autorequire:
|
18
18
|
bindir: bin
|
19
19
|
cert_chain: []
|
20
20
|
|
21
|
-
date: 2011-
|
21
|
+
date: 2011-12-20 00:00:00 Z
|
22
22
|
dependencies:
|
23
23
|
- !ruby/object:Gem::Dependency
|
24
24
|
version_requirements: &id001 !ruby/object:Gem::Requirement
|
@@ -32,18 +32,19 @@ dependencies:
|
|
32
32
|
- 1
|
33
33
|
- 2
|
34
34
|
version: 1.1.2
|
35
|
-
requirement: *id001
|
36
|
-
type: :runtime
|
37
35
|
prerelease: false
|
36
|
+
requirement: *id001
|
38
37
|
name: diff-lcs
|
38
|
+
type: :runtime
|
39
39
|
description: rspec expectations (should[_not] and matchers)
|
40
|
-
email:
|
40
|
+
email: rspec-users@rubyforge.org
|
41
41
|
executables: []
|
42
42
|
|
43
43
|
extensions: []
|
44
44
|
|
45
45
|
extra_rdoc_files:
|
46
46
|
- README.md
|
47
|
+
- License.txt
|
47
48
|
files:
|
48
49
|
- lib/rspec-expectations.rb
|
49
50
|
- lib/rspec/expectations.rb
|
@@ -89,6 +90,7 @@ files:
|
|
89
90
|
- lib/rspec/matchers/respond_to.rb
|
90
91
|
- lib/rspec/matchers/satisfy.rb
|
91
92
|
- lib/rspec/matchers/throw_symbol.rb
|
93
|
+
- License.txt
|
92
94
|
- README.md
|
93
95
|
- features/README.markdown
|
94
96
|
- features/Upgrade.md
|
@@ -124,6 +126,7 @@ files:
|
|
124
126
|
- spec/rspec/expectations/extensions/kernel_spec.rb
|
125
127
|
- spec/rspec/expectations/fail_with_spec.rb
|
126
128
|
- spec/rspec/expectations/handler_spec.rb
|
129
|
+
- spec/rspec/matchers/base_matcher_spec.rb
|
127
130
|
- spec/rspec/matchers/be_close_spec.rb
|
128
131
|
- spec/rspec/matchers/be_instance_of_spec.rb
|
129
132
|
- spec/rspec/matchers/be_kind_of_spec.rb
|
@@ -156,8 +159,8 @@ files:
|
|
156
159
|
- spec/support/matchers.rb
|
157
160
|
- spec/support/ruby_version.rb
|
158
161
|
homepage: http://github.com/rspec/rspec-expectations
|
159
|
-
licenses:
|
160
|
-
|
162
|
+
licenses:
|
163
|
+
- MIT
|
161
164
|
post_install_message:
|
162
165
|
rdoc_options:
|
163
166
|
- --charset=UTF-8
|
@@ -189,7 +192,7 @@ rubyforge_project: rspec
|
|
189
192
|
rubygems_version: 1.8.11
|
190
193
|
signing_key:
|
191
194
|
specification_version: 3
|
192
|
-
summary: rspec-expectations-2.8.0.
|
195
|
+
summary: rspec-expectations-2.8.0.rc2
|
193
196
|
test_files:
|
194
197
|
- features/README.markdown
|
195
198
|
- features/Upgrade.md
|
@@ -225,6 +228,7 @@ test_files:
|
|
225
228
|
- spec/rspec/expectations/extensions/kernel_spec.rb
|
226
229
|
- spec/rspec/expectations/fail_with_spec.rb
|
227
230
|
- spec/rspec/expectations/handler_spec.rb
|
231
|
+
- spec/rspec/matchers/base_matcher_spec.rb
|
228
232
|
- spec/rspec/matchers/be_close_spec.rb
|
229
233
|
- spec/rspec/matchers/be_instance_of_spec.rb
|
230
234
|
- spec/rspec/matchers/be_kind_of_spec.rb
|
@@ -256,3 +260,4 @@ test_files:
|
|
256
260
|
- spec/support/classes.rb
|
257
261
|
- spec/support/matchers.rb
|
258
262
|
- spec/support/ruby_version.rb
|
263
|
+
has_rdoc:
|