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.
Files changed (38) hide show
  1. data/README.md +117 -9
  2. data/lib/rspec/expectations.rb +24 -16
  3. data/lib/rspec/expectations/handler.rb +1 -1
  4. data/lib/rspec/expectations/version.rb +1 -1
  5. data/lib/rspec/matchers.rb +91 -91
  6. data/lib/rspec/matchers/base_matcher.rb +41 -0
  7. data/lib/rspec/matchers/be.rb +31 -12
  8. data/lib/rspec/matchers/be_instance_of.rb +10 -12
  9. data/lib/rspec/matchers/be_kind_of.rb +10 -12
  10. data/lib/rspec/matchers/be_within.rb +36 -26
  11. data/lib/rspec/matchers/block_aliases.rb +2 -1
  12. data/lib/rspec/matchers/change.rb +1 -1
  13. data/lib/rspec/matchers/cover.rb +20 -19
  14. data/lib/rspec/matchers/eq.rb +22 -32
  15. data/lib/rspec/matchers/eql.rb +22 -28
  16. data/lib/rspec/matchers/equal.rb +37 -27
  17. data/lib/rspec/matchers/exist.rb +26 -18
  18. data/lib/rspec/matchers/have.rb +22 -28
  19. data/lib/rspec/matchers/include.rb +45 -37
  20. data/lib/rspec/matchers/match.rb +10 -10
  21. data/lib/rspec/matchers/match_array.rb +1 -7
  22. data/lib/rspec/matchers/matcher.rb +0 -8
  23. data/lib/rspec/matchers/operator_matcher.rb +0 -2
  24. data/lib/rspec/matchers/pretty.rb +25 -2
  25. data/lib/rspec/matchers/raise_error.rb +2 -16
  26. data/lib/rspec/matchers/respond_to.rb +1 -6
  27. data/lib/rspec/matchers/satisfy.rb +1 -6
  28. data/lib/rspec/matchers/throw_symbol.rb +2 -11
  29. data/spec/rspec/expectations/handler_spec.rb +1 -1
  30. data/spec/rspec/matchers/change_spec.rb +1 -1
  31. data/spec/rspec/matchers/description_generation_spec.rb +2 -2
  32. data/spec/rspec/matchers/eq_spec.rb +1 -1
  33. data/spec/rspec/matchers/eql_spec.rb +2 -2
  34. data/spec/rspec/matchers/equal_spec.rb +1 -1
  35. data/spec/rspec/matchers/include_spec.rb +4 -0
  36. data/spec/rspec/matchers/raise_error_spec.rb +2 -2
  37. data/spec/spec_helper.rb +1 -0
  38. 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
@@ -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
- # @method be_true
7
- RSpec::Matchers.define :be_true do
8
- match do |actual|
9
- actual
8
+ def matches?(actual)
9
+ super(actual)
10
10
  end
11
11
  end
12
12
 
13
- RSpec::Matchers.define :be_false do
14
- match do |actual|
15
- !actual
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
- RSpec::Matchers.define :be_nil do
20
- match do |actual|
21
- actual.nil?
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 do |actual|
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 do
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
- # :call-seq:
4
- # should be_instance_of(expected)
5
- # should be_an_instance_of(expected)
6
- # should_not be_instance_of(expected)
7
- # should_not be_an_instance_of(expected)
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
- # == Examples
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
- Matcher.new :be_an_instance_of, expected do |_expected_|
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
- # :call-seq:
4
- # should be_kind_of(expected)
5
- # should be_a_kind_of(expected)
6
- # should_not be_kind_of(expected)
7
- # should_not be_a_kind_of(expected)
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
- # == Examples
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
- Matcher.new :be_a_kind_of, expected do |_expected_|
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
- # Passes if actual == expected +/- delta
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
- match do |actual|
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
- failure_message_for_should do |actual|
23
- "expected #{actual} to #{description}"
24
- end
8
+ def initialize(delta)
9
+ @delta = delta
10
+ end
25
11
 
26
- failure_message_for_should_not do |actual|
27
- "expected #{actual} not to #{description}"
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
- description do
31
- "be within #{_delta_} of #{@_expected}"
32
- end
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. Allows expectations like this:
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)
@@ -141,7 +141,7 @@ MESSAGE
141
141
  # do/end, as <tt>{ ... }</tt> binds to the +change+ method, whereas do/end
142
142
  # would errantly bind to the +should+ or +should_not+ method.
143
143
  #
144
- # == Examples
144
+ # @example
145
145
  #
146
146
  # lambda {
147
147
  # team.add_player(player)
@@ -1,35 +1,36 @@
1
1
  module RSpec
2
2
  module Matchers
3
- # :call-seq:
4
- # should cover(expected)
5
- # should_not cover(expected)
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
- # == Examples
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
- # == Warning: Ruby >= 1.9 only
31
+ # ### Warning:: Ruby >= 1.9 only
19
32
  def cover(*values)
20
- Matcher.new :cover, *values do |*_values|
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
@@ -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
- # == Examples
27
+ # @example
8
28
  #
9
29
  # 5.should eq(5)
10
30
  # 5.should_not eq(3)
11
31
  def eq(expected)
12
- Matcher.new :eq, expected do |_expected_|
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
@@ -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
- # == Examples
27
+ # @example
8
28
  #
9
29
  # 5.should eql(5)
10
30
  # 5.should_not eql(3)
11
31
  def eql(expected)
12
- Matcher.new :eql, expected do |_expected_|
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