rspec-expectations 2.6.0 → 2.7.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +2 -2
- data/features/built_in_matchers/README.md +7 -5
- data/features/built_in_matchers/be.feature +1 -1
- data/features/built_in_matchers/cover.feature +1 -1
- data/features/built_in_matchers/expect_error.feature +59 -26
- data/features/built_in_matchers/have.feature +2 -2
- data/features/built_in_matchers/predicates.feature +1 -1
- data/features/step_definitions/additional_cli_steps.rb +1 -1
- data/features/support/env.rb +1 -1
- data/lib/rspec/expectations.rb +0 -2
- data/lib/rspec/expectations/deprecation.rb +6 -4
- data/lib/rspec/expectations/errors.rb +4 -7
- data/lib/rspec/expectations/extensions.rb +1 -0
- data/lib/rspec/expectations/extensions/array.rb +2 -0
- data/lib/rspec/expectations/extensions/kernel.rb +18 -44
- data/lib/rspec/expectations/{backward_compatibility.rb → extensions/object.rb} +5 -3
- data/lib/rspec/expectations/fail_with.rb +8 -5
- data/lib/rspec/expectations/version.rb +5 -4
- data/lib/rspec/matchers.rb +77 -73
- data/lib/rspec/matchers/be.rb +42 -51
- data/lib/rspec/matchers/be_within.rb +1 -1
- data/lib/rspec/matchers/change.rb +5 -13
- data/lib/rspec/matchers/dsl.rb +2 -1
- data/lib/rspec/matchers/eq.rb +3 -3
- data/lib/rspec/matchers/extensions/{instance_exec.rb → instance_eval_with_args.rb} +15 -7
- data/lib/rspec/matchers/has.rb +11 -6
- data/lib/rspec/matchers/have.rb +36 -19
- data/lib/rspec/matchers/match_array.rb +1 -1
- data/lib/rspec/matchers/matcher.rb +5 -5
- data/spec/rspec/matchers/change_spec.rb +38 -0
- data/spec/rspec/matchers/description_generation_spec.rb +32 -32
- data/spec/rspec/matchers/eq_spec.rb +2 -2
- data/spec/rspec/matchers/has_spec.rb +33 -1
- data/spec/rspec/matchers/have_spec.rb +64 -7
- data/spec/rspec/matchers/match_array_spec.rb +0 -3
- data/spec/rspec/matchers/operator_matcher_spec.rb +10 -4
- data/spec/rspec/matchers/raise_error_spec.rb +6 -6
- data/spec/support/classes.rb +21 -10
- metadata +51 -62
- data/.document +0 -5
- data/.gitignore +0 -10
- data/.travis.yml +0 -7
- data/Gemfile +0 -40
- data/Guardfile +0 -5
- data/License.txt +0 -22
- data/Rakefile +0 -81
- data/cucumber.yml +0 -10
- data/features/.nav +0 -29
- data/features/Changelog.md +0 -101
- data/rspec-expectations.gemspec +0 -27
- data/specs.watchr +0 -57
data/README.md
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
rspec-expectations adds `should` and `should_not` to every object and includes
|
4
4
|
RSpec::Matchers, a library of standard matchers.
|
5
5
|
|
6
|
-
[![build status](
|
6
|
+
[![build status](https://secure.travis-ci.org/rspec/rspec-expectations.png)](http://travis-ci.org/rspec/rspec-expectations)
|
7
7
|
|
8
8
|
## Documentation
|
9
9
|
|
@@ -21,7 +21,7 @@ tracker](https://github.com/rspec/rspec-expectations/issues).
|
|
21
21
|
## Install
|
22
22
|
|
23
23
|
gem install rspec # for rspec-core, rspec-expectations, rspec-mocks
|
24
|
-
gem install rspec-
|
24
|
+
gem install rspec-expectations # for rspec-expectations only
|
25
25
|
|
26
26
|
## Matchers
|
27
27
|
|
@@ -29,10 +29,10 @@
|
|
29
29
|
|
30
30
|
## Truthiness and existentialism
|
31
31
|
|
32
|
-
actual.should be_true # passes if actual is
|
33
|
-
actual.should be_false # passes if actual is nil or false
|
32
|
+
actual.should be_true # passes if actual is truthy (not nil or false)
|
33
|
+
actual.should be_false # passes if actual is falsy (nil or false)
|
34
34
|
actual.should be_nil # passes if actual is nil
|
35
|
-
actual.should be # passes if actual is not nil
|
35
|
+
actual.should be # passes if actual is truthy (not nil or false)
|
36
36
|
|
37
37
|
## Expecting errors
|
38
38
|
|
@@ -49,11 +49,13 @@
|
|
49
49
|
|
50
50
|
## Predicate matchers
|
51
51
|
|
52
|
-
actual.should be_xxx
|
52
|
+
actual.should be_xxx # passes if actual.xxx?
|
53
|
+
actual.should have_xxx(:arg) # passes if actual.has_xxx?(:arg)
|
53
54
|
|
54
|
-
###
|
55
|
+
### Examples
|
55
56
|
|
56
57
|
[].should be_empty # passes because [].empty? returns true
|
58
|
+
{ :a => 1 }.should have_key(:a) # passes because the hash has the key :a
|
57
59
|
|
58
60
|
## Collection membership
|
59
61
|
|
@@ -5,7 +5,7 @@ Feature: "be" matchers
|
|
5
5
|
obj.should be_true # passes if obj is truthy (not nil or false)
|
6
6
|
obj.should be_false # passes if obj is falsy (nil or false)
|
7
7
|
obj.should be_nil # passes if obj is nil
|
8
|
-
obj.should be # passes if obj is not nil
|
8
|
+
obj.should be # passes if obj is truthy (not nil or false)
|
9
9
|
|
10
10
|
Scenario: be_true matcher
|
11
11
|
Given a file named "be_true_spec.rb" with:
|
@@ -28,7 +28,7 @@ Feature: cover matcher
|
|
28
28
|
it { should_not cover(8) }
|
29
29
|
it { should_not cover(4, 6, 8) }
|
30
30
|
|
31
|
-
# both of these should fail since it covers
|
31
|
+
# both of these should fail since it covers 5 but not 11
|
32
32
|
it { should cover(5, 11) }
|
33
33
|
it { should_not cover(5, 11) }
|
34
34
|
end
|
@@ -15,32 +15,65 @@ Feature: raise_error matcher
|
|
15
15
|
In addition to the basic form, above, there are a number of ways to specify
|
16
16
|
details of an error/exception:
|
17
17
|
|
18
|
+
expect { raise "oops" }.to raise_error
|
19
|
+
expect { raise "oops" }.to raise_error(RuntimeError)
|
20
|
+
expect { raise "oops" }.to raise_error("oops")
|
21
|
+
expect { raise "oops" }.to raise_error(/op/)
|
22
|
+
expect { raise "oops" }.to raise_error(RuntimeError, "oops")
|
23
|
+
expect { raise "oops" }.to raise_error(RuntimeError, /op/)
|
24
|
+
|
18
25
|
Scenario: expect any error
|
19
|
-
Given a file named "
|
26
|
+
Given a file named "example_spec" with:
|
20
27
|
"""
|
21
|
-
describe "calling a method
|
28
|
+
describe "calling a missing method" do
|
22
29
|
it "raises" do
|
23
30
|
expect { Object.new.foo }.to raise_error
|
24
31
|
end
|
25
32
|
end
|
26
33
|
"""
|
27
|
-
When I run `rspec
|
34
|
+
When I run `rspec example_spec`
|
28
35
|
Then the example should pass
|
29
36
|
|
30
37
|
Scenario: expect specific error
|
31
|
-
Given a file named "
|
38
|
+
Given a file named "example_spec" with:
|
32
39
|
"""
|
33
|
-
describe "calling a method
|
40
|
+
describe "calling a missing method" do
|
34
41
|
it "raises" do
|
35
42
|
expect { Object.new.foo }.to raise_error(NameError)
|
36
43
|
end
|
37
44
|
end
|
38
45
|
"""
|
39
|
-
When I run `rspec
|
46
|
+
When I run `rspec example_spec`
|
47
|
+
Then the example should pass
|
48
|
+
|
49
|
+
Scenario: match message with a string
|
50
|
+
Given a file named "example_spec.rb" with:
|
51
|
+
"""
|
52
|
+
describe "matching error message with string" do
|
53
|
+
it "matches the error message" do
|
54
|
+
expect { raise StandardError, 'this message exactly'}.
|
55
|
+
to raise_error('this message exactly')
|
56
|
+
end
|
57
|
+
end
|
58
|
+
"""
|
59
|
+
When I run `rspec example_spec.rb`
|
60
|
+
Then the example should pass
|
61
|
+
|
62
|
+
Scenario: match message with a regexp
|
63
|
+
Given a file named "example_spec.rb" with:
|
64
|
+
"""
|
65
|
+
describe "matching error message with regex" do
|
66
|
+
it "matches the error message" do
|
67
|
+
expect { raise StandardError, "my message" }.
|
68
|
+
to raise_error(/my mess/)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
"""
|
72
|
+
When I run `rspec example_spec.rb`
|
40
73
|
Then the example should pass
|
41
74
|
|
42
|
-
Scenario:
|
43
|
-
Given a file named "
|
75
|
+
Scenario: match type + message with string
|
76
|
+
Given a file named "example_spec.rb" with:
|
44
77
|
"""
|
45
78
|
describe "matching error message with string" do
|
46
79
|
it "matches the error message" do
|
@@ -49,11 +82,11 @@ Feature: raise_error matcher
|
|
49
82
|
end
|
50
83
|
end
|
51
84
|
"""
|
52
|
-
When I run `rspec
|
85
|
+
When I run `rspec example_spec.rb`
|
53
86
|
Then the example should pass
|
54
87
|
|
55
|
-
Scenario:
|
56
|
-
Given a file named "
|
88
|
+
Scenario: match type + message with regexp
|
89
|
+
Given a file named "example_spec.rb" with:
|
57
90
|
"""
|
58
91
|
describe "matching error message with regex" do
|
59
92
|
it "matches the error message" do
|
@@ -62,11 +95,11 @@ Feature: raise_error matcher
|
|
62
95
|
end
|
63
96
|
end
|
64
97
|
"""
|
65
|
-
When I run `rspec
|
98
|
+
When I run `rspec example_spec.rb`
|
66
99
|
Then the example should pass
|
67
100
|
|
68
101
|
Scenario: set expectations on error object passed to block
|
69
|
-
Given a file named "
|
102
|
+
Given a file named "example_spec" with:
|
70
103
|
"""
|
71
104
|
describe "#foo" do
|
72
105
|
it "raises NameError" do
|
@@ -76,30 +109,30 @@ Feature: raise_error matcher
|
|
76
109
|
end
|
77
110
|
end
|
78
111
|
"""
|
79
|
-
When I run `rspec
|
112
|
+
When I run `rspec example_spec`
|
80
113
|
Then the example should pass
|
81
114
|
|
82
|
-
Scenario: expect no
|
83
|
-
Given a file named "
|
115
|
+
Scenario: expect no occurence of a specific error
|
116
|
+
Given a file named "example_spec" with:
|
84
117
|
"""
|
85
|
-
describe "#
|
118
|
+
describe Object, "#public_instance_methods" do
|
86
119
|
it "does not raise" do
|
87
|
-
expect { Object.
|
120
|
+
expect { Object.public_instance_methods }.
|
121
|
+
to_not raise_error(NameError)
|
88
122
|
end
|
89
123
|
end
|
90
124
|
"""
|
91
|
-
When I run `rspec
|
125
|
+
When I run `rspec example_spec`
|
92
126
|
Then the example should pass
|
93
|
-
|
94
|
-
Scenario: expect no
|
95
|
-
Given a file named "
|
127
|
+
|
128
|
+
Scenario: expect no error at all
|
129
|
+
Given a file named "example_spec" with:
|
96
130
|
"""
|
97
|
-
describe
|
131
|
+
describe "#to_s" do
|
98
132
|
it "does not raise" do
|
99
|
-
expect { Object.
|
100
|
-
to_not raise_error(NameError)
|
133
|
+
expect { Object.new.to_s }.to_not raise_error
|
101
134
|
end
|
102
135
|
end
|
103
136
|
"""
|
104
|
-
When I run `rspec
|
137
|
+
When I run `rspec example_spec`
|
105
138
|
Then the example should pass
|
@@ -4,8 +4,8 @@ Feature: have(n).items matcher
|
|
4
4
|
size of a collection. There are three basic forms:
|
5
5
|
|
6
6
|
* collection.should have(x).items
|
7
|
-
* collection.should
|
8
|
-
* collection.should
|
7
|
+
* collection.should have\_at\_least(x).items
|
8
|
+
* collection.should have\_at\_most(x).items
|
9
9
|
|
10
10
|
In addition, #have_exactly is provided as an alias to #have.
|
11
11
|
|
@@ -101,7 +101,7 @@ Feature: predicate matchers
|
|
101
101
|
"""
|
102
102
|
When I run `rspec should_not_have_all_string_keys_spec.rb`
|
103
103
|
Then the output should contain "2 examples, 1 failure"
|
104
|
-
And the output should contain "expected #has_all_string_keys?
|
104
|
+
And the output should contain "expected #has_all_string_keys? to return false, got true"
|
105
105
|
|
106
106
|
Scenario: matcher arguments are passed on to the predicate method
|
107
107
|
Given a file named "predicate_matcher_argument_spec.rb" with:
|
data/features/support/env.rb
CHANGED
data/lib/rspec/expectations.rb
CHANGED
@@ -5,11 +5,9 @@ require 'rspec/expectations/errors'
|
|
5
5
|
require 'rspec/expectations/deprecation'
|
6
6
|
require 'rspec/expectations/handler'
|
7
7
|
require 'rspec/expectations/version'
|
8
|
-
require 'rspec/expectations/backward_compatibility'
|
9
8
|
require 'rspec/expectations/differ'
|
10
9
|
|
11
10
|
module RSpec
|
12
|
-
|
13
11
|
# RSpec::Expectations lets you set expectations on your objects.
|
14
12
|
#
|
15
13
|
# result.should == 37
|
@@ -1,9 +1,9 @@
|
|
1
1
|
module RSpec
|
2
|
-
|
3
|
-
# This is defined in rspec-core, but we can't assume it's loaded since
|
4
|
-
# rspec-expectations should be usable w/o rspec-core.
|
5
2
|
unless respond_to?(:deprecate)
|
6
3
|
class << self
|
4
|
+
# Used internally by RSpec to display standard deprecation warnings.
|
5
|
+
# This is also defined in rspec-core, but we can't assume it's loaded
|
6
|
+
# since rspec-expectations should be usable w/o rspec-core.
|
7
7
|
def deprecate(method, alternate_method=nil, version=nil)
|
8
8
|
version_string = version ? "rspec-#{version}" : "a future version of RSpec"
|
9
9
|
|
@@ -27,10 +27,12 @@ ADDITIONAL
|
|
27
27
|
warn_deprecation(message)
|
28
28
|
end
|
29
29
|
|
30
|
+
# Used internally by RSpec to display custom deprecation warnings. This
|
31
|
+
# is also defined in rspec-core, but we can't assume it's loaded since
|
32
|
+
# rspec-expectations should be usable w/o rspec-core.
|
30
33
|
def warn_deprecation(message)
|
31
34
|
send :warn, message
|
32
35
|
end
|
33
36
|
end
|
34
37
|
end
|
35
38
|
end
|
36
|
-
|
@@ -1,12 +1,9 @@
|
|
1
1
|
module RSpec
|
2
2
|
module Expectations
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
end.compact.first
|
8
|
-
|
9
|
-
class ExpectationNotMetError < superclass
|
3
|
+
if defined?(Test::Unit::AssertionFailedError)
|
4
|
+
class ExpectationNotMetError < Test::Unit::AssertionFailedError; end
|
5
|
+
else
|
6
|
+
class ExpectationNotMetError < ::StandardError; end
|
10
7
|
end
|
11
8
|
end
|
12
9
|
end
|
@@ -1,51 +1,25 @@
|
|
1
1
|
module Kernel
|
2
|
-
#
|
3
|
-
#
|
4
|
-
# should
|
5
|
-
# should
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
# receiver.should == expected #any value
|
12
|
-
# => Passes if (receiver == expected)
|
13
|
-
#
|
14
|
-
# receiver.should === expected #any value
|
15
|
-
# => Passes if (receiver === expected)
|
16
|
-
#
|
17
|
-
# receiver.should =~ regexp
|
18
|
-
# => Passes if (receiver =~ regexp)
|
19
|
-
#
|
20
|
-
# See RSpec::Matchers for more information about matchers
|
21
|
-
#
|
22
|
-
# == Warning
|
23
|
-
#
|
24
|
-
# NOTE that this does NOT support receiver.should != expected.
|
25
|
-
# Instead, use receiver.should_not == expected
|
2
|
+
# Passes if +matcher+ returns true. Available on every +Object+.
|
3
|
+
# @example
|
4
|
+
# actual.should eq(expected)
|
5
|
+
# actual.should be > 4
|
6
|
+
# @param [Matcher]
|
7
|
+
# matcher
|
8
|
+
# @param [String] message optional message to display when the expectation fails
|
9
|
+
# @return [Boolean] true if the expectation succeeds (else raises)
|
10
|
+
# @see RSpec::Matchers
|
26
11
|
def should(matcher=nil, message=nil, &block)
|
27
12
|
RSpec::Expectations::PositiveExpectationHandler.handle_matcher(self, matcher, message, &block)
|
28
13
|
end
|
29
|
-
|
30
|
-
#
|
31
|
-
#
|
32
|
-
# should_not
|
33
|
-
#
|
34
|
-
#
|
35
|
-
#
|
36
|
-
#
|
37
|
-
#
|
38
|
-
#
|
39
|
-
# receiver.should_not == expected
|
40
|
-
# => Passes unless (receiver == expected)
|
41
|
-
#
|
42
|
-
# receiver.should_not === expected
|
43
|
-
# => Passes unless (receiver === expected)
|
44
|
-
#
|
45
|
-
# receiver.should_not =~ regexp
|
46
|
-
# => Passes unless (receiver =~ regexp)
|
47
|
-
#
|
48
|
-
# See RSpec::Matchers for more information about matchers
|
14
|
+
|
15
|
+
# Passes if +matcher+ returns false. Available on every +Object+.
|
16
|
+
# @example
|
17
|
+
# actual.should_not eq(expected)
|
18
|
+
# @param [Matcher]
|
19
|
+
# matcher
|
20
|
+
# @param [String] message optional message to display when the expectation fails
|
21
|
+
# @return [Boolean] false if the negative expectation succeeds (else raises)
|
22
|
+
# @see RSpec::Matchers
|
49
23
|
def should_not(matcher=nil, message=nil, &block)
|
50
24
|
RSpec::Expectations::NegativeExpectationHandler.handle_matcher(self, matcher, message, &block)
|
51
25
|
end
|
@@ -1,7 +1,8 @@
|
|
1
|
-
# Cucumber 0.7 includes Rspec::Expectations
|
2
1
|
module RSpec
|
3
2
|
module Expectations
|
4
|
-
module
|
3
|
+
module DeprecatedConstants
|
4
|
+
# Displays deprecation warning when it captures Rspec and Spec. Otherwise
|
5
|
+
# delegates to super.
|
5
6
|
def const_missing(name)
|
6
7
|
case name
|
7
8
|
when :Rspec, :Spec
|
@@ -28,10 +29,11 @@ WARNING
|
|
28
29
|
end
|
29
30
|
end
|
30
31
|
|
32
|
+
# @deprecated (no replacement)
|
31
33
|
def differ=(ignore)
|
32
34
|
RSpec.deprecate("RSpec::Expectations.differ=(differ)", "nothing at all (diffing is now automatic and no longer configurable)")
|
33
35
|
end
|
34
36
|
end
|
35
37
|
end
|
36
38
|
|
37
|
-
|
39
|
+
extend RSpec::Expectations::DeprecatedConstants
|
@@ -1,16 +1,19 @@
|
|
1
1
|
module RSpec
|
2
2
|
module Expectations
|
3
3
|
class << self
|
4
|
+
# @private
|
4
5
|
def differ
|
5
6
|
@differ ||= Differ.new
|
6
7
|
end
|
7
8
|
|
8
|
-
#
|
9
|
+
# Raises an RSpec::Expectations::ExpectationNotMetError with message.
|
10
|
+
# @param [String] message
|
11
|
+
# @param [Object] expected
|
12
|
+
# @param [Object] actual
|
9
13
|
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
|
13
|
-
def fail_with(message, expected=nil, actual=nil) # :nodoc:
|
14
|
+
# Adds a diff to the failure message when +expected+ and +actual+ are
|
15
|
+
# both present.
|
16
|
+
def fail_with(message, expected=nil, actual=nil)
|
14
17
|
if !message
|
15
18
|
raise ArgumentError, "Failure message is nil. Does your matcher define the " +
|
16
19
|
"appropriate failure_message_for_* method to return a string?"
|