rspec-expectations 3.0.0.beta2 → 3.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (135) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +2 -2
  4. data/.yardopts +0 -1
  5. data/Changelog.md +115 -35
  6. data/README.md +2 -2
  7. data/lib/rspec/expectations.rb +13 -8
  8. data/lib/rspec/{matchers → expectations}/configuration.rb +38 -13
  9. data/lib/rspec/expectations/expectation_target.rb +72 -8
  10. data/lib/rspec/expectations/fail_with.rb +10 -52
  11. data/lib/rspec/expectations/handler.rb +9 -11
  12. data/lib/rspec/expectations/syntax.rb +37 -35
  13. data/lib/rspec/expectations/version.rb +1 -1
  14. data/lib/rspec/matchers.rb +60 -9
  15. data/lib/rspec/matchers/aliased_matcher.rb +6 -0
  16. data/lib/rspec/matchers/built_in.rb +9 -1
  17. data/lib/rspec/matchers/built_in/all.rb +78 -0
  18. data/lib/rspec/matchers/built_in/base_matcher.rb +39 -1
  19. data/lib/rspec/matchers/built_in/be.rb +117 -42
  20. data/lib/rspec/matchers/built_in/be_between.rb +22 -0
  21. data/lib/rspec/matchers/built_in/be_instance_of.rb +11 -3
  22. data/lib/rspec/matchers/built_in/be_kind_of.rb +5 -0
  23. data/lib/rspec/matchers/built_in/be_within.rb +26 -6
  24. data/lib/rspec/matchers/built_in/change.rb +89 -13
  25. data/lib/rspec/matchers/built_in/compound.rb +19 -3
  26. data/lib/rspec/matchers/built_in/contain_exactly.rb +17 -6
  27. data/lib/rspec/matchers/built_in/cover.rb +3 -0
  28. data/lib/rspec/matchers/built_in/eq.rb +20 -5
  29. data/lib/rspec/matchers/built_in/eql.rb +15 -3
  30. data/lib/rspec/matchers/built_in/equal.rb +23 -6
  31. data/lib/rspec/matchers/built_in/exist.rb +74 -10
  32. data/lib/rspec/matchers/built_in/has.rb +58 -3
  33. data/lib/rspec/matchers/built_in/include.rb +16 -1
  34. data/lib/rspec/matchers/built_in/match.rb +14 -4
  35. data/lib/rspec/matchers/built_in/operators.rb +16 -0
  36. data/lib/rspec/matchers/built_in/output.rb +47 -5
  37. data/lib/rspec/matchers/built_in/raise_error.rb +40 -23
  38. data/lib/rspec/matchers/built_in/respond_to.rb +37 -16
  39. data/lib/rspec/matchers/built_in/satisfy.rb +15 -0
  40. data/lib/rspec/matchers/built_in/start_and_end_with.rb +29 -14
  41. data/lib/rspec/matchers/built_in/throw_symbol.rb +32 -3
  42. data/lib/rspec/matchers/built_in/yield.rb +148 -44
  43. data/lib/rspec/matchers/composable.rb +48 -7
  44. data/lib/rspec/matchers/dsl.rb +45 -17
  45. data/lib/rspec/matchers/generated_descriptions.rb +7 -0
  46. data/lib/rspec/matchers/matcher_delegator.rb +6 -2
  47. data/lib/rspec/matchers/pretty.rb +15 -19
  48. metadata +33 -236
  49. metadata.gz.sig +0 -0
  50. data/features/README.md +0 -48
  51. data/features/Upgrade.md +0 -53
  52. data/features/built_in_matchers/README.md +0 -96
  53. data/features/built_in_matchers/be.feature +0 -175
  54. data/features/built_in_matchers/be_within.feature +0 -48
  55. data/features/built_in_matchers/comparisons.feature +0 -97
  56. data/features/built_in_matchers/contain_exactly.feature +0 -46
  57. data/features/built_in_matchers/cover.feature +0 -47
  58. data/features/built_in_matchers/end_with.feature +0 -48
  59. data/features/built_in_matchers/equality.feature +0 -136
  60. data/features/built_in_matchers/exist.feature +0 -45
  61. data/features/built_in_matchers/expect_change.feature +0 -59
  62. data/features/built_in_matchers/expect_error.feature +0 -144
  63. data/features/built_in_matchers/include.feature +0 -126
  64. data/features/built_in_matchers/match.feature +0 -51
  65. data/features/built_in_matchers/output.feature +0 -70
  66. data/features/built_in_matchers/predicates.feature +0 -161
  67. data/features/built_in_matchers/respond_to.feature +0 -84
  68. data/features/built_in_matchers/satisfy.feature +0 -33
  69. data/features/built_in_matchers/start_with.feature +0 -48
  70. data/features/built_in_matchers/throw_symbol.feature +0 -91
  71. data/features/built_in_matchers/types.feature +0 -116
  72. data/features/built_in_matchers/yield.feature +0 -161
  73. data/features/composing_matchers.feature +0 -250
  74. data/features/compound_expectations.feature +0 -45
  75. data/features/custom_matchers/access_running_example.feature +0 -53
  76. data/features/custom_matchers/define_diffable_matcher.feature +0 -27
  77. data/features/custom_matchers/define_matcher.feature +0 -340
  78. data/features/custom_matchers/define_matcher_outside_rspec.feature +0 -34
  79. data/features/custom_matchers/define_matcher_with_fluent_interface.feature +0 -24
  80. data/features/customized_message.feature +0 -39
  81. data/features/diffing.feature +0 -85
  82. data/features/implicit_docstrings.feature +0 -52
  83. data/features/step_definitions/additional_cli_steps.rb +0 -22
  84. data/features/support/env.rb +0 -21
  85. data/features/support/rubinius.rb +0 -6
  86. data/features/syntax_configuration.feature +0 -71
  87. data/features/test_frameworks/minitest.feature +0 -44
  88. data/lib/rspec-expectations.rb +0 -1
  89. data/lib/rspec/expectations/diff_presenter.rb +0 -141
  90. data/lib/rspec/expectations/differ.rb +0 -44
  91. data/lib/rspec/expectations/encoded_string.rb +0 -56
  92. data/spec/rspec/expectations/diff_presenter_spec.rb +0 -249
  93. data/spec/rspec/expectations/encoded_string_spec.rb +0 -74
  94. data/spec/rspec/expectations/expectation_target_spec.rb +0 -82
  95. data/spec/rspec/expectations/extensions/kernel_spec.rb +0 -67
  96. data/spec/rspec/expectations/fail_with_spec.rb +0 -114
  97. data/spec/rspec/expectations/handler_spec.rb +0 -205
  98. data/spec/rspec/expectations/minitest_integration_spec.rb +0 -27
  99. data/spec/rspec/expectations/syntax_spec.rb +0 -89
  100. data/spec/rspec/expectations_spec.rb +0 -12
  101. data/spec/rspec/matchers/aliased_matcher_spec.rb +0 -48
  102. data/spec/rspec/matchers/aliases_spec.rb +0 -449
  103. data/spec/rspec/matchers/built_in/base_matcher_spec.rb +0 -83
  104. data/spec/rspec/matchers/built_in/be_between_spec.rb +0 -159
  105. data/spec/rspec/matchers/built_in/be_instance_of_spec.rb +0 -63
  106. data/spec/rspec/matchers/built_in/be_kind_of_spec.rb +0 -41
  107. data/spec/rspec/matchers/built_in/be_spec.rb +0 -592
  108. data/spec/rspec/matchers/built_in/be_within_spec.rb +0 -141
  109. data/spec/rspec/matchers/built_in/change_spec.rb +0 -808
  110. data/spec/rspec/matchers/built_in/compound_spec.rb +0 -292
  111. data/spec/rspec/matchers/built_in/contain_exactly_spec.rb +0 -441
  112. data/spec/rspec/matchers/built_in/cover_spec.rb +0 -69
  113. data/spec/rspec/matchers/built_in/eq_spec.rb +0 -156
  114. data/spec/rspec/matchers/built_in/eql_spec.rb +0 -41
  115. data/spec/rspec/matchers/built_in/equal_spec.rb +0 -106
  116. data/spec/rspec/matchers/built_in/exist_spec.rb +0 -124
  117. data/spec/rspec/matchers/built_in/has_spec.rb +0 -161
  118. data/spec/rspec/matchers/built_in/include_spec.rb +0 -540
  119. data/spec/rspec/matchers/built_in/match_spec.rb +0 -102
  120. data/spec/rspec/matchers/built_in/operators_spec.rb +0 -252
  121. data/spec/rspec/matchers/built_in/output_spec.rb +0 -165
  122. data/spec/rspec/matchers/built_in/raise_error_spec.rb +0 -461
  123. data/spec/rspec/matchers/built_in/respond_to_spec.rb +0 -292
  124. data/spec/rspec/matchers/built_in/satisfy_spec.rb +0 -44
  125. data/spec/rspec/matchers/built_in/start_and_end_with_spec.rb +0 -253
  126. data/spec/rspec/matchers/built_in/throw_symbol_spec.rb +0 -135
  127. data/spec/rspec/matchers/built_in/yield_spec.rb +0 -627
  128. data/spec/rspec/matchers/configuration_spec.rb +0 -213
  129. data/spec/rspec/matchers/description_generation_spec.rb +0 -191
  130. data/spec/rspec/matchers/dsl_spec.rb +0 -895
  131. data/spec/rspec/matchers/legacy_spec.rb +0 -101
  132. data/spec/rspec/matchers_spec.rb +0 -74
  133. data/spec/spec_helper.rb +0 -85
  134. data/spec/support/matchers.rb +0 -22
  135. data/spec/support/shared_examples.rb +0 -35
@@ -1,19 +1,29 @@
1
1
  module RSpec
2
2
  module Matchers
3
3
  module BuiltIn
4
+ # @api private
5
+ # Provides the implementation for `match`.
6
+ # Not intended to be instantiated directly.
4
7
  class Match < BaseMatcher
5
- def match(expected, actual)
6
- return true if values_match?(expected, actual)
7
- actual.match(expected) if actual.respond_to?(:match)
8
- end
9
8
 
9
+ # @api private
10
+ # @return [String]
10
11
  def description
11
12
  "match #{surface_descriptions_in(expected).inspect}"
12
13
  end
13
14
 
15
+ # @api private
16
+ # @return [Boolean]
14
17
  def diffable?
15
18
  true
16
19
  end
20
+
21
+ private
22
+
23
+ def match(expected, actual)
24
+ return true if values_match?(expected, actual)
25
+ actual.match(expected) if actual.respond_to?(:match)
26
+ end
17
27
  end
18
28
  end
19
29
  end
@@ -3,21 +3,29 @@ require 'rspec/support'
3
3
  module RSpec
4
4
  module Matchers
5
5
  module BuiltIn
6
+ # @api private
7
+ # Provides the implementation for operator matchers.
8
+ # Not intended to be instantiated directly.
9
+ # Only available for use with `should`.
6
10
  class OperatorMatcher
7
11
  class << self
12
+ # @private
8
13
  def registry
9
14
  @registry ||= {}
10
15
  end
11
16
 
17
+ # @private
12
18
  def register(klass, operator, matcher)
13
19
  registry[klass] ||= {}
14
20
  registry[klass][operator] = matcher
15
21
  end
16
22
 
23
+ # @private
17
24
  def unregister(klass, operator)
18
25
  registry[klass] && registry[klass].delete(operator)
19
26
  end
20
27
 
28
+ # @private
21
29
  def get(klass, operator)
22
30
  klass.ancestors.each { |ancestor|
23
31
  matcher = registry[ancestor] && registry[ancestor][operator]
@@ -34,6 +42,7 @@ module RSpec
34
42
  @actual = actual
35
43
  end
36
44
 
45
+ # @private
37
46
  def self.use_custom_matcher_or_delegate(operator)
38
47
  define_method(operator) do |expected|
39
48
  if uses_generic_implementation_of?(operator) && matcher = OperatorMatcher.get(@actual.class, operator)
@@ -57,10 +66,13 @@ module RSpec
57
66
  use_custom_matcher_or_delegate operator
58
67
  end
59
68
 
69
+ # @private
60
70
  def fail_with_message(message)
61
71
  RSpec::Expectations.fail_with(message, @expected, @actual)
62
72
  end
63
73
 
74
+ # @api private
75
+ # @return [String]
64
76
  def description
65
77
  "#{@operator} #{@expected.inspect}"
66
78
  end
@@ -80,6 +92,8 @@ module RSpec
80
92
  end
81
93
  end
82
94
 
95
+ # @private
96
+ # Handles operator matcher for `should`.
83
97
  class PositiveOperatorMatcher < OperatorMatcher
84
98
  def __delegate_operator(actual, operator, expected)
85
99
  if actual.__send__(operator, expected)
@@ -92,6 +106,8 @@ module RSpec
92
106
  end
93
107
  end
94
108
 
109
+ # @private
110
+ # Handles operator matcher for `should_not`.
95
111
  class NegativeOperatorMatcher < OperatorMatcher
96
112
  def __delegate_operator(actual, operator, expected)
97
113
  return false unless actual.__send__(operator, expected)
@@ -3,6 +3,9 @@ require 'stringio'
3
3
  module RSpec
4
4
  module Matchers
5
5
  module BuiltIn
6
+ # @api private
7
+ # Provides the implementation for `output`.
8
+ # Not intended to be instantiated directly.
6
9
  class Output < BaseMatcher
7
10
  def initialize(expected)
8
11
  @expected = expected
@@ -10,29 +13,44 @@ module RSpec
10
13
  end
11
14
 
12
15
  def matches?(block)
16
+ @block = block
17
+ return false unless Proc === block
13
18
  @actual = @stream_capturer.capture(block)
14
-
15
19
  @expected ? values_match?(@expected, @actual) : captured?
16
20
  end
17
21
 
22
+ def does_not_match?(block)
23
+ !matches?(block) && Proc === block
24
+ end
25
+
26
+ # @api public
27
+ # Tells the matcher to match against stdout.
18
28
  def to_stdout
19
29
  @stream_capturer = CaptureStdout
20
30
  self
21
31
  end
22
32
 
33
+ # @api public
34
+ # Tells the matcher to match against stderr.
23
35
  def to_stderr
24
36
  @stream_capturer = CaptureStderr
25
37
  self
26
38
  end
27
39
 
40
+ # @api private
41
+ # @return [String]
28
42
  def failure_message
29
- "expected block to #{description}, #{actual_description}"
43
+ "expected block to #{description}, but #{positive_failure_reason}"
30
44
  end
31
45
 
46
+ # @api private
47
+ # @return [String]
32
48
  def failure_message_when_negated
33
- "expected block to not #{description}, but did"
49
+ "expected block to not #{description}, but #{negative_failure_reason}"
34
50
  end
35
51
 
52
+ # @api private
53
+ # @return [String]
36
54
  def description
37
55
  if @expected
38
56
  "output #{description_of @expected} to #{@stream_capturer.name}"
@@ -41,21 +59,43 @@ module RSpec
41
59
  end
42
60
  end
43
61
 
62
+ # @api private
63
+ # @return [Boolean]
44
64
  def diffable?
45
65
  true
46
66
  end
47
67
 
68
+ # @api private
69
+ # Indicates this matcher matches against a block.
70
+ # @return [True]
71
+ def supports_block_expectations?
72
+ true
73
+ end
74
+
48
75
  private
49
76
 
50
77
  def captured?
51
78
  @actual.length > 0
52
79
  end
53
80
 
54
- def actual_description
55
- @expected ? "but output #{captured? ? @actual.inspect : 'nothing'}" : "but did not"
81
+ def positive_failure_reason
82
+ return "was not a block" unless Proc === @block
83
+ return "output #{actual_output_description}" if @expected
84
+ "did not"
85
+ end
86
+
87
+ def negative_failure_reason
88
+ return "was not a block" unless Proc === @block
89
+ "output #{actual_output_description}"
90
+ end
91
+
92
+ def actual_output_description
93
+ return "nothing" unless captured?
94
+ @actual.inspect
56
95
  end
57
96
  end
58
97
 
98
+ # @private
59
99
  module NullCapture
60
100
  def self.name
61
101
  "some stream"
@@ -66,6 +106,7 @@ module RSpec
66
106
  end
67
107
  end
68
108
 
109
+ # @private
69
110
  module CaptureStdout
70
111
  def self.name
71
112
  'stdout'
@@ -85,6 +126,7 @@ module RSpec
85
126
  end
86
127
  end
87
128
 
129
+ # @private
88
130
  module CaptureStderr
89
131
  def self.name
90
132
  'stderr'
@@ -1,6 +1,9 @@
1
1
  module RSpec
2
2
  module Matchers
3
3
  module BuiltIn
4
+ # @api private
5
+ # Provides the implementation for `raise_error`.
6
+ # Not intended to be instantiated directly.
4
7
  class RaiseError
5
8
  include Composable
6
9
 
@@ -15,23 +18,24 @@ module RSpec
15
18
  end
16
19
  end
17
20
 
21
+ # @api public
22
+ # Specifies the expected error message.
18
23
  def with_message(expected_message)
19
24
  raise_message_already_set if @expected_message
20
25
  @expected_message = expected_message
21
26
  self
22
27
  end
23
28
 
29
+ # @private
24
30
  def matches?(given_proc, negative_expectation = false, &block)
31
+ @given_proc = given_proc
25
32
  @block ||= block
26
33
  @raised_expected_error = false
27
34
  @with_expected_message = false
28
35
  @eval_block = false
29
36
  @eval_block_passed = false
30
37
 
31
- unless given_proc.respond_to?(:call)
32
- ::Kernel.warn "`raise_error` was called with non-proc object #{given_proc.inspect}"
33
- return false
34
- end
38
+ return false unless Proc === given_proc
35
39
 
36
40
  begin
37
41
  given_proc.call
@@ -49,6 +53,37 @@ module RSpec
49
53
  expectation_matched?
50
54
  end
51
55
 
56
+ # @private
57
+ def does_not_match?(given_proc)
58
+ prevent_invalid_expectations
59
+ !matches?(given_proc, :negative_expectation) && Proc === given_proc
60
+ end
61
+
62
+ # @private
63
+ def supports_block_expectations?
64
+ true
65
+ end
66
+
67
+ # @api private
68
+ # @return [String]
69
+ def failure_message
70
+ @eval_block ? @actual_error.message : "expected #{expected_error}#{given_error}"
71
+ end
72
+
73
+ # @api private
74
+ # @return [String]
75
+ def failure_message_when_negated
76
+ "expected no #{expected_error}#{given_error}"
77
+ end
78
+
79
+ # @api private
80
+ # @return [String]
81
+ def description
82
+ "raise #{expected_error}"
83
+ end
84
+
85
+ private
86
+
52
87
  def expectation_matched?
53
88
  error_and_message_match? && block_matches?
54
89
  end
@@ -61,11 +96,6 @@ module RSpec
61
96
  @eval_block ? @eval_block_passed : true
62
97
  end
63
98
 
64
- def does_not_match?(given_proc)
65
- prevent_invalid_expectations
66
- !matches?(given_proc, :negative_expectation)
67
- end
68
-
69
99
  def eval_block
70
100
  @eval_block = true
71
101
  begin
@@ -81,20 +111,6 @@ module RSpec
81
111
  values_match?(@expected_message, @actual_error.message)
82
112
  end
83
113
 
84
- def failure_message
85
- @eval_block ? @actual_error.message : "expected #{expected_error}#{given_error}"
86
- end
87
-
88
- def failure_message_when_negated
89
- "expected no #{expected_error}#{given_error}"
90
- end
91
-
92
- def description
93
- "raise #{expected_error}"
94
- end
95
-
96
- private
97
-
98
114
  def prevent_invalid_expectations
99
115
  if (expecting_specific_exception? || @expected_message)
100
116
  what_to_raise = if expecting_specific_exception? && @expected_message
@@ -126,6 +142,7 @@ module RSpec
126
142
  end
127
143
 
128
144
  def given_error
145
+ return " but was not given a block" unless Proc === @given_proc
129
146
  return " but nothing was raised" unless @actual_error
130
147
 
131
148
  backtrace = format_backtrace(@actual_error.backtrace)
@@ -1,6 +1,9 @@
1
1
  module RSpec
2
2
  module Matchers
3
3
  module BuiltIn
4
+ # @api private
5
+ # Provides the implementation for `respond_to`.
6
+ # Not intended to be instantiated directly.
4
7
  class RespondTo
5
8
  include Composable
6
9
 
@@ -9,37 +12,60 @@ module RSpec
9
12
  @expected_arity = nil
10
13
  end
11
14
 
15
+ # @api public
16
+ # Specifies the number of expected arguments.
17
+ #
18
+ # @example
19
+ # expect(obj).to respond_to(:message).with(3).arguments
20
+ def with(n)
21
+ @expected_arity = n
22
+ self
23
+ end
24
+
25
+ # @api public
26
+ # No-op. Intended to be used as syntactic sugar when using `with`.
27
+ #
28
+ # @example
29
+ # expect(obj).to respond_to(:message).with(3).arguments
30
+ def argument
31
+ self
32
+ end
33
+ alias :arguments :argument
34
+
35
+ # @private
12
36
  def matches?(actual)
13
37
  find_failing_method_names(actual, :reject).empty?
14
38
  end
15
39
 
40
+ # @private
16
41
  def does_not_match?(actual)
17
42
  find_failing_method_names(actual, :select).empty?
18
43
  end
19
44
 
45
+ # @api private
46
+ # @return [String]
20
47
  def failure_message
21
48
  "expected #{@actual.inspect} to respond to #{@failing_method_names.collect {|name| name.inspect }.join(', ')}#{with_arity}"
22
49
  end
23
50
 
51
+ # @api private
52
+ # @return [String]
24
53
  def failure_message_when_negated
25
54
  failure_message.sub(/to respond to/, 'not to respond to')
26
55
  end
27
56
 
57
+ # @api private
58
+ # @return [String]
28
59
  def description
29
60
  "respond to #{pp_names}#{with_arity}"
30
61
  end
31
62
 
32
- def with(n)
33
- @expected_arity = n
34
- self
63
+ # @private
64
+ def supports_block_expectations?
65
+ false
35
66
  end
36
67
 
37
- def argument
38
- self
39
- end
40
- alias :arguments :argument
41
-
42
- private
68
+ private
43
69
 
44
70
  def find_failing_method_names(actual, filter_method)
45
71
  @actual = actual
@@ -51,13 +77,8 @@ module RSpec
51
77
  def matches_arity?(actual, name)
52
78
  return true unless @expected_arity
53
79
 
54
- actual_arity = actual.method(name).arity
55
- if actual_arity < 0
56
- # ~ inverts the one's complement and gives us the number of required args
57
- ~actual_arity <= @expected_arity
58
- else
59
- actual_arity == @expected_arity
60
- end
80
+ signature = Support::MethodSignature.new(actual.method(name))
81
+ Support::MethodSignatureVerifier.new(signature, Array.new(@expected_arity)).valid?
61
82
  end
62
83
 
63
84
  def with_arity
@@ -1,6 +1,9 @@
1
1
  module RSpec
2
2
  module Matchers
3
3
  module BuiltIn
4
+ # @api private
5
+ # Provides the implementation for `satisfy`.
6
+ # Not intended to be instantiated directly.
4
7
  class Satisfy
5
8
  include Composable
6
9
 
@@ -8,23 +11,35 @@ module RSpec
8
11
  @block = block
9
12
  end
10
13
 
14
+ # @private
11
15
  def matches?(actual, &block)
12
16
  @block = block if block
13
17
  @actual = actual
14
18
  @block.call(actual)
15
19
  end
16
20
 
21
+ # @api private
22
+ # @return [String]
17
23
  def failure_message
18
24
  "expected #{@actual} to satisfy block"
19
25
  end
20
26
 
27
+ # @api private
28
+ # @return [String]
21
29
  def failure_message_when_negated
22
30
  "expected #{@actual} not to satisfy block"
23
31
  end
24
32
 
33
+ # @api private
34
+ # @return [String]
25
35
  def description
26
36
  "satisfy block"
27
37
  end
38
+
39
+ # @private
40
+ def supports_block_expectations?
41
+ false
42
+ end
28
43
  end
29
44
  end
30
45
  end