rspec-expectations 2.7.0 → 2.8.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +117 -9
- data/lib/rspec/expectations.rb +24 -16
- data/lib/rspec/expectations/handler.rb +1 -1
- data/lib/rspec/expectations/version.rb +1 -1
- data/lib/rspec/matchers.rb +91 -91
- data/lib/rspec/matchers/base_matcher.rb +41 -0
- data/lib/rspec/matchers/be.rb +31 -12
- data/lib/rspec/matchers/be_instance_of.rb +10 -12
- data/lib/rspec/matchers/be_kind_of.rb +10 -12
- data/lib/rspec/matchers/be_within.rb +36 -26
- data/lib/rspec/matchers/block_aliases.rb +2 -1
- data/lib/rspec/matchers/change.rb +1 -1
- data/lib/rspec/matchers/cover.rb +20 -19
- data/lib/rspec/matchers/eq.rb +22 -32
- data/lib/rspec/matchers/eql.rb +22 -28
- data/lib/rspec/matchers/equal.rb +37 -27
- data/lib/rspec/matchers/exist.rb +26 -18
- data/lib/rspec/matchers/have.rb +22 -28
- data/lib/rspec/matchers/include.rb +45 -37
- data/lib/rspec/matchers/match.rb +10 -10
- data/lib/rspec/matchers/match_array.rb +1 -7
- data/lib/rspec/matchers/matcher.rb +0 -8
- data/lib/rspec/matchers/operator_matcher.rb +0 -2
- data/lib/rspec/matchers/pretty.rb +25 -2
- data/lib/rspec/matchers/raise_error.rb +2 -16
- data/lib/rspec/matchers/respond_to.rb +1 -6
- data/lib/rspec/matchers/satisfy.rb +1 -6
- data/lib/rspec/matchers/throw_symbol.rb +2 -11
- data/spec/rspec/expectations/handler_spec.rb +1 -1
- data/spec/rspec/matchers/change_spec.rb +1 -1
- data/spec/rspec/matchers/description_generation_spec.rb +2 -2
- data/spec/rspec/matchers/eq_spec.rb +1 -1
- data/spec/rspec/matchers/eql_spec.rb +2 -2
- data/spec/rspec/matchers/equal_spec.rb +1 -1
- data/spec/rspec/matchers/include_spec.rb +4 -0
- data/spec/rspec/matchers/raise_error_spec.rb +2 -2
- data/spec/spec_helper.rb +1 -0
- metadata +15 -10
@@ -0,0 +1,41 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Matchers
|
3
|
+
# Used _internally_ as a base class for matchers that ship with
|
4
|
+
# rspec-expectations.
|
5
|
+
#
|
6
|
+
# ### Warning:
|
7
|
+
#
|
8
|
+
# This class is for internal use, and subject to change without notice. We
|
9
|
+
# strongly recommend that you do not base your custom matchers on this
|
10
|
+
# class. If/when this changes, we will announce it and remove this warning.
|
11
|
+
module BaseMatcher
|
12
|
+
include RSpec::Matchers::Pretty
|
13
|
+
|
14
|
+
attr_reader :actual, :expected
|
15
|
+
|
16
|
+
def initialize(expected=nil)
|
17
|
+
@expected = expected
|
18
|
+
end
|
19
|
+
|
20
|
+
def matches?(actual)
|
21
|
+
@actual = actual
|
22
|
+
end
|
23
|
+
|
24
|
+
def failure_message_for_should
|
25
|
+
"expected #{actual.inspect} to #{name_to_sentence}#{expected_to_sentence}"
|
26
|
+
end
|
27
|
+
|
28
|
+
def failure_message_for_should_not
|
29
|
+
"expected #{actual.inspect} not to #{name_to_sentence}#{expected_to_sentence}"
|
30
|
+
end
|
31
|
+
|
32
|
+
def description
|
33
|
+
expected ? "#{name_to_sentence} #{expected.inspect}" : name_to_sentence
|
34
|
+
end
|
35
|
+
|
36
|
+
def diffable?
|
37
|
+
false
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/lib/rspec/matchers/be.rb
CHANGED
@@ -2,34 +2,53 @@ require 'rspec/matchers/dsl'
|
|
2
2
|
|
3
3
|
module RSpec
|
4
4
|
module Matchers
|
5
|
+
class BeTrue
|
6
|
+
include BaseMatcher
|
5
7
|
|
6
|
-
|
7
|
-
|
8
|
-
match do |actual|
|
9
|
-
actual
|
8
|
+
def matches?(actual)
|
9
|
+
super(actual)
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
# Passes if actual is truthy (anything but false or nil)
|
14
|
+
def be_true
|
15
|
+
BeTrue.new
|
16
|
+
end
|
17
|
+
|
18
|
+
class BeFalse
|
19
|
+
include BaseMatcher
|
20
|
+
|
21
|
+
def matches?(actual)
|
22
|
+
!super(actual)
|
16
23
|
end
|
17
24
|
end
|
18
25
|
|
19
|
-
|
20
|
-
|
21
|
-
|
26
|
+
# Passes if actual is falsy (false or nil)
|
27
|
+
def be_false
|
28
|
+
BeFalse.new
|
29
|
+
end
|
30
|
+
|
31
|
+
class BeNil
|
32
|
+
include BaseMatcher
|
33
|
+
|
34
|
+
def matches?(actual)
|
35
|
+
super(actual).nil?
|
22
36
|
end
|
23
37
|
|
24
|
-
failure_message_for_should
|
38
|
+
def failure_message_for_should
|
25
39
|
"expected: nil\n got: #{actual.inspect}"
|
26
40
|
end
|
27
41
|
|
28
|
-
failure_message_for_should_not
|
42
|
+
def failure_message_for_should_not
|
29
43
|
"expected: not nil\n got: nil"
|
30
44
|
end
|
31
45
|
end
|
32
46
|
|
47
|
+
# Passes if actual is nil
|
48
|
+
def be_nil
|
49
|
+
BeNil.new
|
50
|
+
end
|
51
|
+
|
33
52
|
class Be
|
34
53
|
include RSpec::Matchers::Pretty
|
35
54
|
|
@@ -1,24 +1,22 @@
|
|
1
1
|
module RSpec
|
2
2
|
module Matchers
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+
class BeAnInstanceOf
|
4
|
+
include BaseMatcher
|
5
|
+
|
6
|
+
def matches?(actual)
|
7
|
+
super(actual).instance_of?(expected)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
9
11
|
# Passes if actual.instance_of?(expected)
|
10
12
|
#
|
11
|
-
#
|
13
|
+
# @example
|
12
14
|
#
|
13
15
|
# 5.should be_instance_of(Fixnum)
|
14
16
|
# 5.should_not be_instance_of(Numeric)
|
15
17
|
# 5.should_not be_instance_of(Float)
|
16
18
|
def be_an_instance_of(expected)
|
17
|
-
|
18
|
-
match do |actual|
|
19
|
-
actual.instance_of?(_expected_)
|
20
|
-
end
|
21
|
-
end
|
19
|
+
BeAnInstanceOf.new(expected)
|
22
20
|
end
|
23
21
|
|
24
22
|
alias_method :be_instance_of, :be_an_instance_of
|
@@ -1,24 +1,22 @@
|
|
1
1
|
module RSpec
|
2
2
|
module Matchers
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+
class BeAKindOf
|
4
|
+
include BaseMatcher
|
5
|
+
|
6
|
+
def matches?(actual)
|
7
|
+
super(actual).kind_of?(expected)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
9
11
|
# Passes if actual.kind_of?(expected)
|
10
12
|
#
|
11
|
-
#
|
13
|
+
# @example
|
12
14
|
#
|
13
15
|
# 5.should be_kind_of(Fixnum)
|
14
16
|
# 5.should be_kind_of(Numeric)
|
15
17
|
# 5.should_not be_kind_of(Float)
|
16
18
|
def be_a_kind_of(expected)
|
17
|
-
|
18
|
-
match do |actual|
|
19
|
-
actual.kind_of?(_expected_)
|
20
|
-
end
|
21
|
-
end
|
19
|
+
BeAKindOf.new(expected)
|
22
20
|
end
|
23
21
|
|
24
22
|
alias_method :be_kind_of, :be_a_kind_of
|
@@ -1,37 +1,47 @@
|
|
1
1
|
module RSpec
|
2
2
|
module Matchers
|
3
|
-
|
4
|
-
|
5
|
-
# == Examples
|
6
|
-
#
|
7
|
-
# result.should be_within(0.5).of(3.0)
|
8
|
-
# result.should_not be_within(0.5).of(3.0)
|
9
|
-
def be_within(delta)
|
10
|
-
Matcher.new :be_within, delta do |_delta_|
|
11
|
-
chain :of do |_expected_|
|
12
|
-
@_expected = _expected_
|
13
|
-
end
|
3
|
+
class BeWithin
|
4
|
+
include BaseMatcher
|
14
5
|
|
15
|
-
|
16
|
-
unless defined?(@_expected)
|
17
|
-
raise ArgumentError.new("You must set an expected value using #of: be_within(#{_delta_}).of(expected_value)")
|
18
|
-
end
|
19
|
-
(actual - @_expected).abs < _delta_
|
20
|
-
end
|
6
|
+
attr_reader :delta
|
21
7
|
|
22
|
-
|
23
|
-
|
24
|
-
|
8
|
+
def initialize(delta)
|
9
|
+
@delta = delta
|
10
|
+
end
|
25
11
|
|
26
|
-
|
27
|
-
|
12
|
+
def matches?(actual)
|
13
|
+
unless defined?(@expected)
|
14
|
+
raise ArgumentError.new("You must set an expected value using #of: be_within(#{delta}).of(expected_value)")
|
28
15
|
end
|
16
|
+
(super(actual) - expected).abs < delta
|
17
|
+
end
|
29
18
|
|
30
|
-
|
31
|
-
|
32
|
-
|
19
|
+
def of(expected)
|
20
|
+
@expected = expected
|
21
|
+
self
|
22
|
+
end
|
23
|
+
|
24
|
+
def failure_message_for_should
|
25
|
+
"expected #{actual} to #{description}"
|
26
|
+
end
|
27
|
+
|
28
|
+
def failure_message_for_should_not
|
29
|
+
"expected #{actual} not to #{description}"
|
30
|
+
end
|
31
|
+
|
32
|
+
def description
|
33
|
+
"be within #{delta} of #{expected}"
|
33
34
|
end
|
34
35
|
end
|
36
|
+
|
37
|
+
# Passes if actual == expected +/- delta
|
38
|
+
#
|
39
|
+
# @example
|
40
|
+
#
|
41
|
+
# result.should be_within(0.5).of(3.0)
|
42
|
+
# result.should_not be_within(0.5).of(3.0)
|
43
|
+
def be_within(delta)
|
44
|
+
BeWithin.new(delta)
|
45
|
+
end
|
35
46
|
end
|
36
47
|
end
|
37
|
-
|
@@ -7,8 +7,9 @@ module RSpec
|
|
7
7
|
end
|
8
8
|
|
9
9
|
# Extends the submitted block with aliases to and to_not
|
10
|
-
# for should and should_not.
|
10
|
+
# for should and should_not.
|
11
11
|
#
|
12
|
+
# @example
|
12
13
|
# expect { this_block }.to change{this.expression}.from(old_value).to(new_value)
|
13
14
|
# expect { this_block }.to raise_error
|
14
15
|
def expect(&block)
|
data/lib/rspec/matchers/cover.rb
CHANGED
@@ -1,35 +1,36 @@
|
|
1
1
|
module RSpec
|
2
2
|
module Matchers
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
class Cover
|
4
|
+
include BaseMatcher
|
5
|
+
|
6
|
+
def initialize(*expected)
|
7
|
+
super(expected)
|
8
|
+
end
|
9
|
+
|
10
|
+
def matches?(range)
|
11
|
+
expected.all? {|e| super(range).cover?(e)}
|
12
|
+
end
|
13
|
+
|
14
|
+
def does_not_match?(range)
|
15
|
+
@actual = range
|
16
|
+
expected.none? {|e| range.cover?(e)}
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
7
20
|
# Passes if actual covers expected. This works for
|
8
21
|
# Ranges. You can also pass in multiple args
|
9
22
|
# and it will only pass if all args are found in Range.
|
10
23
|
#
|
11
|
-
#
|
24
|
+
# @example
|
12
25
|
# (1..10).should cover(5)
|
13
26
|
# (1..10).should cover(4, 6)
|
14
27
|
# (1..10).should cover(4, 6, 11) # will fail
|
15
28
|
# (1..10).should_not cover(11)
|
16
29
|
# (1..10).should_not cover(5) # will fail
|
17
30
|
#
|
18
|
-
#
|
31
|
+
# ### Warning:: Ruby >= 1.9 only
|
19
32
|
def cover(*values)
|
20
|
-
|
21
|
-
match_for_should do |range|
|
22
|
-
_values.all? &covered_by(range)
|
23
|
-
end
|
24
|
-
|
25
|
-
match_for_should_not do |range|
|
26
|
-
_values.none? &covered_by(range)
|
27
|
-
end
|
28
|
-
|
29
|
-
def covered_by(range)
|
30
|
-
lambda {|value| range.cover?(value)}
|
31
|
-
end
|
32
|
-
end
|
33
|
+
Cover.new(*values)
|
33
34
|
end
|
34
35
|
end
|
35
36
|
end
|
data/lib/rspec/matchers/eq.rb
CHANGED
@@ -1,45 +1,35 @@
|
|
1
1
|
module RSpec
|
2
2
|
module Matchers
|
3
|
+
class Eq
|
4
|
+
include BaseMatcher
|
5
|
+
|
6
|
+
def matches?(actual)
|
7
|
+
super(actual) == expected
|
8
|
+
end
|
9
|
+
|
10
|
+
def failure_message_for_should
|
11
|
+
"\nexpected: #{expected.inspect}\n got: #{actual.inspect}\n\n(compared using ==)\n"
|
12
|
+
end
|
13
|
+
|
14
|
+
def failure_message_for_should_not
|
15
|
+
"\nexpected: value != #{expected.inspect}\n got: #{actual.inspect}\n\n(compared using ==)\n"
|
16
|
+
end
|
17
|
+
|
18
|
+
def diffable?
|
19
|
+
true
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
3
23
|
# Passes if <tt>actual == expected</tt>.
|
4
24
|
#
|
5
25
|
# See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more information about equality in Ruby.
|
6
26
|
#
|
7
|
-
#
|
27
|
+
# @example
|
8
28
|
#
|
9
29
|
# 5.should eq(5)
|
10
30
|
# 5.should_not eq(3)
|
11
31
|
def eq(expected)
|
12
|
-
|
13
|
-
|
14
|
-
diffable
|
15
|
-
|
16
|
-
match do |actual|
|
17
|
-
actual == _expected_
|
18
|
-
end
|
19
|
-
|
20
|
-
failure_message_for_should do |actual|
|
21
|
-
<<-MESSAGE
|
22
|
-
|
23
|
-
expected: #{_expected_.inspect}
|
24
|
-
got: #{actual.inspect}
|
25
|
-
|
26
|
-
(compared using ==)
|
27
|
-
MESSAGE
|
28
|
-
end
|
29
|
-
|
30
|
-
failure_message_for_should_not do |actual|
|
31
|
-
<<-MESSAGE
|
32
|
-
|
33
|
-
expected #{actual.inspect} not to equal #{_expected_.inspect}
|
34
|
-
|
35
|
-
(compared using ==)
|
36
|
-
MESSAGE
|
37
|
-
end
|
38
|
-
|
39
|
-
description do
|
40
|
-
"eq #{_expected_}"
|
41
|
-
end
|
42
|
-
end
|
32
|
+
Eq.new(expected)
|
43
33
|
end
|
44
34
|
end
|
45
35
|
end
|
data/lib/rspec/matchers/eql.rb
CHANGED
@@ -1,41 +1,35 @@
|
|
1
1
|
module RSpec
|
2
2
|
module Matchers
|
3
|
+
class Eql
|
4
|
+
include BaseMatcher
|
5
|
+
|
6
|
+
def matches?(actual)
|
7
|
+
super(actual).eql?(expected)
|
8
|
+
end
|
9
|
+
|
10
|
+
def failure_message_for_should
|
11
|
+
"\nexpected: #{expected.inspect}\n got: #{actual.inspect}\n\n(compared using eql?)\n"
|
12
|
+
end
|
13
|
+
|
14
|
+
def failure_message_for_should_not
|
15
|
+
"\nexpected: value != #{expected.inspect}\n got: #{actual.inspect}\n\n(compared using eql?)\n"
|
16
|
+
end
|
17
|
+
|
18
|
+
def diffable?
|
19
|
+
true
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
3
23
|
# Passes if +actual.eql?(expected)+
|
4
24
|
#
|
5
25
|
# See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more information about equality in Ruby.
|
6
26
|
#
|
7
|
-
#
|
27
|
+
# @example
|
8
28
|
#
|
9
29
|
# 5.should eql(5)
|
10
30
|
# 5.should_not eql(3)
|
11
31
|
def eql(expected)
|
12
|
-
|
13
|
-
|
14
|
-
diffable
|
15
|
-
|
16
|
-
match do |actual|
|
17
|
-
actual.eql?(_expected_)
|
18
|
-
end
|
19
|
-
|
20
|
-
failure_message_for_should do |actual|
|
21
|
-
<<-MESSAGE
|
22
|
-
|
23
|
-
expected #{_expected_.inspect}
|
24
|
-
got #{actual.inspect}
|
25
|
-
|
26
|
-
(compared using eql?)
|
27
|
-
MESSAGE
|
28
|
-
end
|
29
|
-
|
30
|
-
failure_message_for_should_not do |actual|
|
31
|
-
<<-MESSAGE
|
32
|
-
|
33
|
-
expected #{actual.inspect} not to equal #{_expected_.inspect}
|
34
|
-
|
35
|
-
(compared using eql?)
|
36
|
-
MESSAGE
|
37
|
-
end
|
38
|
-
end
|
32
|
+
Eql.new(expected)
|
39
33
|
end
|
40
34
|
end
|
41
35
|
end
|