rspec-expectations 2.8.0 → 2.9.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. data/.document +5 -0
  2. data/.yardopts +3 -0
  3. data/Changelog.md +176 -0
  4. data/README.md +2 -13
  5. data/features/custom_matchers/access_running_example.feature +1 -1
  6. data/features/step_definitions/additional_cli_steps.rb +4 -4
  7. data/lib/rspec/expectations/fail_with.rb +3 -3
  8. data/lib/rspec/expectations/handler.rb +3 -5
  9. data/lib/rspec/expectations/version.rb +1 -1
  10. data/lib/rspec/matchers.rb +387 -21
  11. data/lib/rspec/matchers/built_in.rb +33 -0
  12. data/lib/rspec/matchers/built_in/base_matcher.rb +58 -0
  13. data/lib/rspec/matchers/built_in/be.rb +183 -0
  14. data/lib/rspec/matchers/built_in/be_instance_of.rb +13 -0
  15. data/lib/rspec/matchers/built_in/be_kind_of.rb +13 -0
  16. data/lib/rspec/matchers/built_in/be_within.rb +39 -0
  17. data/lib/rspec/matchers/built_in/change.rb +132 -0
  18. data/lib/rspec/matchers/built_in/cover.rb +22 -0
  19. data/lib/rspec/matchers/built_in/eq.rb +26 -0
  20. data/lib/rspec/matchers/built_in/eql.rb +25 -0
  21. data/lib/rspec/matchers/built_in/equal.rb +48 -0
  22. data/lib/rspec/matchers/built_in/exist.rb +28 -0
  23. data/lib/rspec/matchers/built_in/has.rb +47 -0
  24. data/lib/rspec/matchers/built_in/have.rb +107 -0
  25. data/lib/rspec/matchers/built_in/include.rb +52 -0
  26. data/lib/rspec/matchers/built_in/match.rb +13 -0
  27. data/lib/rspec/matchers/built_in/match_array.rb +52 -0
  28. data/lib/rspec/matchers/built_in/raise_error.rb +96 -0
  29. data/lib/rspec/matchers/built_in/respond_to.rb +73 -0
  30. data/lib/rspec/matchers/built_in/satisfy.rb +29 -0
  31. data/lib/rspec/matchers/built_in/throw_symbol.rb +93 -0
  32. data/lib/rspec/matchers/dsl.rb +1 -1
  33. data/lib/rspec/matchers/matcher.rb +263 -233
  34. data/lib/rspec/matchers/method_missing.rb +2 -2
  35. data/lib/rspec/matchers/operator_matcher.rb +19 -20
  36. data/spec/rspec/expectations/handler_spec.rb +1 -1
  37. data/spec/rspec/matchers/base_matcher_spec.rb +1 -2
  38. data/spec/rspec/matchers/change_spec.rb +3 -3
  39. data/spec/rspec/matchers/cover_spec.rb +46 -46
  40. data/spec/rspec/matchers/dsl_spec.rb +36 -3
  41. data/spec/rspec/matchers/have_spec.rb +2 -2
  42. data/spec/rspec/matchers/include_spec.rb +1 -1
  43. data/spec/rspec/matchers/matcher_spec.rb +319 -305
  44. data/spec/rspec/matchers/method_missing_spec.rb +1 -0
  45. data/spec/rspec/matchers/operator_matcher_spec.rb +2 -2
  46. data/spec/rspec/matchers/throw_symbol_spec.rb +103 -105
  47. metadata +93 -39
  48. data/lib/rspec/matchers/base_matcher.rb +0 -56
  49. data/lib/rspec/matchers/be.rb +0 -232
  50. data/lib/rspec/matchers/be_instance_of.rb +0 -24
  51. data/lib/rspec/matchers/be_kind_of.rb +0 -24
  52. data/lib/rspec/matchers/be_within.rb +0 -47
  53. data/lib/rspec/matchers/change.rb +0 -197
  54. data/lib/rspec/matchers/cover.rb +0 -36
  55. data/lib/rspec/matchers/eq.rb +0 -36
  56. data/lib/rspec/matchers/eql.rb +0 -35
  57. data/lib/rspec/matchers/equal.rb +0 -58
  58. data/lib/rspec/matchers/errors.rb +0 -5
  59. data/lib/rspec/matchers/exist.rb +0 -34
  60. data/lib/rspec/matchers/has.rb +0 -44
  61. data/lib/rspec/matchers/have.rb +0 -162
  62. data/lib/rspec/matchers/include.rb +0 -66
  63. data/lib/rspec/matchers/match.rb +0 -21
  64. data/lib/rspec/matchers/match_array.rb +0 -65
  65. data/lib/rspec/matchers/raise_error.rb +0 -116
  66. data/lib/rspec/matchers/respond_to.rb +0 -80
  67. data/lib/rspec/matchers/satisfy.rb +0 -46
  68. data/lib/rspec/matchers/throw_symbol.rb +0 -112
@@ -1,21 +0,0 @@
1
- module RSpec
2
- module Matchers
3
- class Match
4
- include BaseMatcher
5
-
6
- def matches?(actual)
7
- super(actual).match(expected)
8
- end
9
- end
10
-
11
- # Given a Regexp or String, passes if actual.match(pattern)
12
- #
13
- # @example
14
- #
15
- # email.should match(/^([^\s]+)((?:[-a-z0-9]+\.)+[a-z]{2,})$/i)
16
- # email.should match("@example.com")
17
- def match(expected)
18
- Match.new(expected)
19
- end
20
- end
21
- end
@@ -1,65 +0,0 @@
1
- module RSpec
2
- module Matchers
3
- class MatchArray
4
- include RSpec::Matchers::Pretty
5
-
6
- def initialize(expected)
7
- @expected = expected
8
- end
9
-
10
- def matches?(actual)
11
- @actual = actual
12
- @extra_items = difference_between_arrays(@actual, @expected)
13
- @missing_items = difference_between_arrays(@expected, @actual)
14
- @extra_items.empty? & @missing_items.empty?
15
- end
16
-
17
- def failure_message_for_should
18
- message = "expected collection contained: #{safe_sort(@expected).inspect}\n"
19
- message += "actual collection contained: #{safe_sort(@actual).inspect}\n"
20
- message += "the missing elements were: #{safe_sort(@missing_items).inspect}\n" unless @missing_items.empty?
21
- message += "the extra elements were: #{safe_sort(@extra_items).inspect}\n" unless @extra_items.empty?
22
- message
23
- end
24
-
25
- def failure_message_for_should_not
26
- "Matcher does not support should_not"
27
- end
28
-
29
- def description
30
- "contain exactly #{_pretty_print(@expected)}"
31
- end
32
-
33
- private
34
-
35
- def safe_sort(array)
36
- array.sort rescue array
37
- end
38
-
39
- def difference_between_arrays(array_1, array_2)
40
- difference = array_1.dup
41
- array_2.each do |element|
42
- if index = difference.index(element)
43
- difference.delete_at(index)
44
- end
45
- end
46
- difference
47
- end
48
- end
49
-
50
- # Passes if actual contains all of the expected regardless of order.
51
- # This works for collections. Pass in multiple args and it will only
52
- # pass if all args are found in collection.
53
- #
54
- # NOTE: there is no should_not version of array.should =~ other_array
55
- #
56
- # @example
57
- #
58
- # [1,2,3].should =~ [1,2,3] # => would pass
59
- # [1,2,3].should =~ [2,3,1] # => would pass
60
- # [1,2,3,4].should =~ [1,2,3] # => would fail
61
- # [1,2,2,3].should =~ [1,2,3] # => would fail
62
- # [1,2,3].should =~ [1,2,3,4] # => would fail
63
- OperatorMatcher.register(Array, '=~', RSpec::Matchers::MatchArray)
64
- end
65
- end
@@ -1,116 +0,0 @@
1
- module RSpec
2
- module Matchers
3
- class RaiseError
4
- def initialize(expected_error_or_message=Exception, expected_message=nil, &block)
5
- @block = block
6
- @actual_error = nil
7
- case expected_error_or_message
8
- when String, Regexp
9
- @expected_error, @expected_message = Exception, expected_error_or_message
10
- else
11
- @expected_error, @expected_message = expected_error_or_message, expected_message
12
- end
13
- end
14
-
15
- def matches?(given_proc)
16
- @raised_expected_error = false
17
- @with_expected_message = false
18
- @eval_block = false
19
- @eval_block_passed = false
20
- begin
21
- given_proc.call
22
- rescue @expected_error => @actual_error
23
- @raised_expected_error = true
24
- @with_expected_message = verify_message
25
- rescue Exception => @actual_error
26
- # This clause should be empty, but rcov will not report it as covered
27
- # unless something (anything) is executed within the clause
28
- "http://eigenclass.org/hiki.rb?rcov-0.8.0"
29
- end
30
-
31
- unless negative_expectation?
32
- eval_block if @raised_expected_error && @with_expected_message && @block
33
- end
34
- ensure
35
- return (@raised_expected_error & @with_expected_message) ? (@eval_block ? @eval_block_passed : true) : false
36
- end
37
-
38
- def eval_block
39
- @eval_block = true
40
- begin
41
- @block[@actual_error]
42
- @eval_block_passed = true
43
- rescue Exception => err
44
- @actual_error = err
45
- end
46
- end
47
-
48
- def verify_message
49
- case @expected_message
50
- when nil
51
- true
52
- when Regexp
53
- @expected_message =~ @actual_error.message
54
- else
55
- @expected_message == @actual_error.message
56
- end
57
- end
58
-
59
- def failure_message_for_should
60
- @eval_block ? @actual_error.message : "expected #{expected_error}#{given_error}"
61
- end
62
-
63
- def failure_message_for_should_not
64
- "expected no #{expected_error}#{given_error}"
65
- end
66
-
67
- def description
68
- "raise #{expected_error}"
69
- end
70
-
71
- private
72
- def expected_error
73
- case @expected_message
74
- when nil
75
- @expected_error
76
- when Regexp
77
- "#{@expected_error} with message matching #{@expected_message.inspect}"
78
- else
79
- "#{@expected_error} with #{@expected_message.inspect}"
80
- end
81
- end
82
-
83
- def given_error
84
- @actual_error.nil? ? " but nothing was raised" : ", got #{@actual_error.inspect}"
85
- end
86
-
87
- def negative_expectation?
88
- # YES - I'm a bad person... help me find a better way - ryand
89
- caller.first(3).find { |s| s =~ /should_not/ }
90
- end
91
- end
92
-
93
- # With no args, matches if any error is raised.
94
- # With a named error, matches only if that specific error is raised.
95
- # With a named error and messsage specified as a String, matches only if both match.
96
- # With a named error and messsage specified as a Regexp, matches only if both match.
97
- # Pass an optional block to perform extra verifications on the exception matched
98
- #
99
- # @example
100
- #
101
- # lambda { do_something_risky }.should raise_error
102
- # lambda { do_something_risky }.should raise_error(PoorRiskDecisionError)
103
- # lambda { do_something_risky }.should raise_error(PoorRiskDecisionError) { |error| error.data.should == 42 }
104
- # lambda { do_something_risky }.should raise_error(PoorRiskDecisionError, "that was too risky")
105
- # lambda { do_something_risky }.should raise_error(PoorRiskDecisionError, /oo ri/)
106
- #
107
- # lambda { do_something_risky }.should_not raise_error
108
- # lambda { do_something_risky }.should_not raise_error(PoorRiskDecisionError)
109
- # lambda { do_something_risky }.should_not raise_error(PoorRiskDecisionError, "that was too risky")
110
- # lambda { do_something_risky }.should_not raise_error(PoorRiskDecisionError, /oo ri/)
111
- def raise_error(error=Exception, message=nil, &block)
112
- Matchers::RaiseError.new(error, message, &block)
113
- end
114
- alias_method :raise_exception, :raise_error
115
- end
116
- end
@@ -1,80 +0,0 @@
1
- module RSpec
2
- module Matchers
3
- class RespondTo
4
- def initialize(*names)
5
- @names = names
6
- @expected_arity = nil
7
- end
8
-
9
- def matches?(actual)
10
- find_failing_method_names(actual, :reject).empty?
11
- end
12
-
13
- def does_not_match?(actual)
14
- find_failing_method_names(actual, :select).empty?
15
- end
16
-
17
- def failure_message_for_should
18
- "expected #{@actual.inspect} to respond to #{@failing_method_names.collect {|name| name.inspect }.join(', ')}#{with_arity}"
19
- end
20
-
21
- def failure_message_for_should_not
22
- failure_message_for_should.sub(/to respond to/, 'not to respond to')
23
- end
24
-
25
- def description
26
- "respond to #{pp_names}#{with_arity}"
27
- end
28
-
29
- def with(n)
30
- @expected_arity = n
31
- self
32
- end
33
-
34
- def argument
35
- self
36
- end
37
- alias :arguments :argument
38
-
39
- private
40
-
41
- def find_failing_method_names(actual, filter_method)
42
- @actual = actual
43
- @failing_method_names = @names.send(filter_method) do |name|
44
- @actual.respond_to?(name) && matches_arity?(actual, name)
45
- end
46
- end
47
-
48
- def matches_arity?(actual, name)
49
- return true unless @expected_arity
50
-
51
- actual_arity = actual.method(name).arity
52
- if actual_arity < 0
53
- # ~ inverts the one's complement and gives us the number of required args
54
- ~actual_arity <= @expected_arity
55
- else
56
- actual_arity == @expected_arity
57
- end
58
- end
59
-
60
- def with_arity
61
- @expected_arity.nil?? "" :
62
- " with #{@expected_arity} argument#{@expected_arity == 1 ? '' : 's'}"
63
- end
64
-
65
- def pp_names
66
- # Ruby 1.9 returns the same thing for array.to_s as array.inspect, so just use array.inspect here
67
- @names.length == 1 ? "##{@names.first}" : @names.inspect
68
- end
69
- end
70
-
71
- # Matches if the target object responds to all of the names
72
- # provided. Names can be Strings or Symbols.
73
- #
74
- # @example
75
- #
76
- def respond_to(*names)
77
- Matchers::RespondTo.new(*names)
78
- end
79
- end
80
- end
@@ -1,46 +0,0 @@
1
- module RSpec
2
- module Matchers
3
- class Satisfy
4
- def initialize(&block)
5
- @block = block
6
- end
7
-
8
- def matches?(actual, &block)
9
- @block = block if block
10
- @actual = actual
11
- @block.call(actual)
12
- end
13
-
14
- def failure_message_for_should
15
- "expected #{@actual} to satisfy block"
16
- end
17
-
18
- def failure_message_for_should_not
19
- "expected #{@actual} not to satisfy block"
20
- end
21
-
22
- def description
23
- "satisfy block"
24
- end
25
- end
26
-
27
- # Passes if the submitted block returns true. Yields target to the
28
- # block.
29
- #
30
- # Generally speaking, this should be thought of as a last resort when
31
- # you can't find any other way to specify the behaviour you wish to
32
- # specify.
33
- #
34
- # If you do find yourself in such a situation, you could always write
35
- # a custom matcher, which would likely make your specs more expressive.
36
- #
37
- # @example
38
- #
39
- # 5.should satisfy { |n|
40
- # n > 3
41
- # }
42
- def satisfy(&block)
43
- Matchers::Satisfy.new(&block)
44
- end
45
- end
46
- end
@@ -1,112 +0,0 @@
1
- module RSpec
2
- module Matchers
3
- class ThrowSymbol
4
- def initialize(expected_symbol = nil, expected_arg=nil)
5
- @expected_symbol = expected_symbol
6
- @expected_arg = expected_arg
7
- @caught_symbol = @caught_arg = nil
8
- end
9
-
10
- def matches?(given_proc)
11
- begin
12
- if @expected_symbol.nil?
13
- given_proc.call
14
- else
15
- @caught_arg = catch :proc_did_not_throw_anything do
16
- catch @expected_symbol do
17
- given_proc.call
18
- throw :proc_did_not_throw_anything, :nothing_thrown
19
- end
20
- end
21
-
22
- if @caught_arg == :nothing_thrown
23
- @caught_arg = nil
24
- else
25
- @caught_symbol = @expected_symbol
26
- end
27
- end
28
-
29
- # Ruby 1.8 uses NameError with `symbol'
30
- # Ruby 1.9 uses ArgumentError with :symbol
31
- rescue NameError, ArgumentError => e
32
- unless e.message =~ /uncaught throw (`|\:)([a-zA-Z0-9_]*)(')?/
33
- other_exception = e
34
- raise
35
- end
36
- @caught_symbol = $2.to_sym
37
- rescue => other_exception
38
- raise
39
- ensure
40
- unless other_exception
41
- if @expected_symbol.nil?
42
- return !@caught_symbol.nil?
43
- else
44
- if @expected_arg.nil?
45
- return @caught_symbol == @expected_symbol
46
- else
47
- return (@caught_symbol == @expected_symbol) & (@caught_arg == @expected_arg)
48
- end
49
- end
50
- end
51
- end
52
- end
53
-
54
- def failure_message_for_should
55
- "expected #{expected} to be thrown, got #{caught}"
56
- end
57
-
58
- def failure_message_for_should_not
59
- "expected #{expected('no Symbol')}#{' not' if @expected_symbol} to be thrown, got #{caught}"
60
- end
61
-
62
- def description
63
- "throw #{expected}"
64
- end
65
-
66
- private
67
-
68
- def expected(symbol_desc = 'a Symbol')
69
- throw_description(@expected_symbol || symbol_desc, @expected_arg)
70
- end
71
-
72
- def caught
73
- throw_description(@caught_symbol || 'nothing', @caught_arg)
74
- end
75
-
76
- def throw_description(symbol, arg)
77
- symbol_description = symbol.is_a?(String) ? symbol : symbol.inspect
78
-
79
- arg_description = if arg
80
- " with #{arg.inspect}"
81
- elsif @expected_arg && @caught_symbol == @expected_symbol
82
- " with no argument"
83
- else
84
- ""
85
- end
86
-
87
- symbol_description + arg_description
88
- end
89
-
90
- end
91
-
92
- # Given no argument, matches if a proc throws any Symbol.
93
- #
94
- # Given a Symbol, matches if the given proc throws the specified Symbol.
95
- #
96
- # Given a Symbol and an arg, matches if the given proc throws the
97
- # specified Symbol with the specified arg.
98
- #
99
- # @example
100
- #
101
- # lambda { do_something_risky }.should throw_symbol
102
- # lambda { do_something_risky }.should throw_symbol(:that_was_risky)
103
- # lambda { do_something_risky }.should throw_symbol(:that_was_risky, culprit)
104
- #
105
- # lambda { do_something_risky }.should_not throw_symbol
106
- # lambda { do_something_risky }.should_not throw_symbol(:that_was_risky)
107
- # lambda { do_something_risky }.should_not throw_symbol(:that_was_risky, culprit)
108
- def throw_symbol(expected_symbol=nil, expected_arg=nil)
109
- Matchers::ThrowSymbol.new(expected_symbol, expected_arg)
110
- end
111
- end
112
- end