dchelimsky-rspec 1.1.11 → 1.1.11.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (103) hide show
  1. data/.autotest +4 -0
  2. data/History.txt +19 -3
  3. data/Manifest.txt +6 -1
  4. data/examples/failing/README.txt +7 -0
  5. data/examples/failing/diffing_spec.rb +36 -0
  6. data/examples/failing/failing_autogenerated_docstrings_example.rb +19 -0
  7. data/examples/failing/failure_in_setup.rb +10 -0
  8. data/examples/failing/failure_in_teardown.rb +10 -0
  9. data/examples/failing/mocking_example.rb +40 -0
  10. data/examples/failing/mocking_with_flexmock.rb +26 -0
  11. data/examples/failing/mocking_with_mocha.rb +25 -0
  12. data/examples/failing/mocking_with_rr.rb +27 -0
  13. data/examples/failing/partial_mock_example.rb +20 -0
  14. data/examples/failing/predicate_example.rb +34 -0
  15. data/examples/failing/raising_example.rb +47 -0
  16. data/examples/failing/spec_helper.rb +3 -0
  17. data/examples/failing/syntax_error_example.rb +7 -0
  18. data/examples/failing/team_spec.rb +44 -0
  19. data/examples/failing/timeout_behaviour.rb +7 -0
  20. data/examples/passing/custom_formatter.rb +1 -1
  21. data/examples/passing/simple_matcher_example.rb +31 -0
  22. data/lib/autotest/rspec.rb +1 -1
  23. data/lib/spec/dsl/main.rb +82 -0
  24. data/lib/spec/dsl.rb +1 -0
  25. data/lib/spec/example/before_and_after_hooks.rb +2 -1
  26. data/lib/spec/example/configuration.rb +1 -1
  27. data/lib/spec/example/example_group.rb +1 -0
  28. data/lib/spec/example/example_group_factory.rb +2 -1
  29. data/lib/spec/example/example_group_methods.rb +40 -25
  30. data/lib/spec/example/example_methods.rb +19 -8
  31. data/lib/spec/example.rb +15 -17
  32. data/lib/spec/expectations/handler.rb +23 -28
  33. data/lib/spec/expectations/wrap_expectation.rb +56 -0
  34. data/lib/spec/expectations.rb +22 -18
  35. data/lib/spec/extensions.rb +0 -1
  36. data/lib/spec/interop/test/unit/testcase.rb +19 -17
  37. data/lib/spec/matchers/be_close.rb +6 -22
  38. data/lib/spec/matchers/eql.rb +7 -25
  39. data/lib/spec/matchers/equal.rb +6 -24
  40. data/lib/spec/matchers/errors.rb +5 -0
  41. data/lib/spec/matchers/exist.rb +8 -14
  42. data/lib/spec/matchers/generated_descriptions.rb +48 -0
  43. data/lib/spec/matchers/has.rb +12 -28
  44. data/lib/spec/matchers/include.rb +12 -9
  45. data/lib/spec/matchers/match.rb +8 -27
  46. data/lib/spec/matchers/method_missing.rb +9 -0
  47. data/lib/spec/matchers/operator_matcher.rb +23 -46
  48. data/lib/spec/matchers/raise_error.rb +4 -8
  49. data/lib/spec/matchers/respond_to.rb +2 -1
  50. data/lib/spec/matchers/throw_symbol.rb +9 -3
  51. data/lib/spec/mocks/message_expectation.rb +0 -5
  52. data/lib/spec/mocks/proxy.rb +12 -10
  53. data/lib/spec/rake/spectask.rb +4 -6
  54. data/lib/spec/runner/class_and_arguments_parser.rb +7 -9
  55. data/lib/spec/runner/command_line.rb +6 -8
  56. data/lib/spec/runner/formatter/base_text_formatter.rb +3 -2
  57. data/lib/spec/runner/option_parser.rb +4 -6
  58. data/lib/spec/runner/spec_parser.rb +5 -5
  59. data/lib/spec/runner.rb +37 -39
  60. data/lib/spec/story/runner/story_runner.rb +10 -6
  61. data/lib/spec/story/runner.rb +40 -42
  62. data/lib/spec/story/world.rb +66 -70
  63. data/lib/spec/version.rb +3 -1
  64. data/lib/spec.rb +21 -19
  65. data/rspec.gemspec +15 -6
  66. data/spec/autotest/autotest_helper.rb +2 -2
  67. data/spec/autotest/discover_spec.rb +4 -15
  68. data/spec/autotest/failed_results_re_spec.rb +24 -0
  69. data/spec/autotest/rspec_spec.rb +0 -37
  70. data/spec/spec/dsl/main_spec.rb +65 -0
  71. data/spec/spec/example/example_group_class_definition_spec.rb +6 -2
  72. data/spec/spec/example/example_group_factory_spec.rb +2 -1
  73. data/spec/spec/example/example_group_methods_spec.rb +138 -141
  74. data/spec/spec/example/example_group_spec.rb +3 -5
  75. data/spec/spec/example/example_methods_spec.rb +60 -23
  76. data/spec/spec/expectations/wrap_expectation_spec.rb +30 -0
  77. data/spec/spec/interop/test/unit/spec_spec.rb +1 -5
  78. data/spec/spec/interop/test/unit/test_unit_spec_helper.rb +4 -0
  79. data/spec/spec/matchers/be_close_spec.rb +12 -10
  80. data/spec/spec/matchers/description_generation_spec.rb +1 -1
  81. data/spec/spec/matchers/eql_spec.rb +7 -6
  82. data/spec/spec/matchers/equal_spec.rb +7 -6
  83. data/spec/spec/matchers/has_spec.rb +1 -1
  84. data/spec/spec/matchers/have_spec.rb +23 -18
  85. data/spec/spec/matchers/include_spec.rb +24 -0
  86. data/spec/spec/matchers/matcher_methods_spec.rb +1 -13
  87. data/spec/spec/matchers/throw_symbol_spec.rb +3 -3
  88. data/spec/spec/mocks/bug_report_11545_spec.rb +4 -5
  89. data/spec/spec/mocks/bug_report_496.rb +11 -9
  90. data/spec/spec/mocks/bug_report_600_spec.rb +22 -0
  91. data/spec/spec/runner/formatter/nested_text_formatter_spec.rb +1 -1
  92. data/spec/spec/runner/formatter/progress_bar_formatter_spec.rb +4 -14
  93. data/spec/spec/runner/formatter/specdoc_formatter_spec.rb +1 -1
  94. data/spec/spec/runner/options_spec.rb +1 -1
  95. data/spec/spec/runner/spec_parser_spec.rb +76 -80
  96. data/spec/spec_helper.rb +4 -0
  97. data/stories/example_groups/autogenerated_docstrings +4 -4
  98. data/stories/interop/test_but_not_test_unit +14 -0
  99. data/stories/resources/helpers/story_helper.rb +1 -1
  100. data/stories/resources/test/spec_including_test_but_not_unit.rb +11 -0
  101. data/story_server/prototype/javascripts/prototype.js +1 -1
  102. metadata +34 -53
  103. data/lib/spec/extensions/metaclass.rb +0 -7
@@ -3,28 +3,46 @@ module Spec
3
3
 
4
4
  module ExampleGroupMethods
5
5
  include Spec::Example::BeforeAndAfterHooks
6
+
7
+ def self.matcher_class
8
+ @matcher_class
9
+ end
10
+
11
+ def self.matcher_class=(matcher_class)
12
+ @matcher_class = matcher_class
13
+ end
6
14
 
7
- class << self
8
- attr_accessor :matcher_class
9
-
10
- def description_text(*args)
11
- args.inject("") do |result, arg|
12
- result << " " unless (result == "" || arg.to_s =~ /^(\s|\.|#)/)
13
- result << arg.to_s
14
- end
15
+ def self.description_text(*args)
16
+ args.inject("") do |result, arg|
17
+ result << " " unless (result == "" || arg.to_s =~ /^(\s|\.|#)/)
18
+ result << arg.to_s
15
19
  end
16
20
  end
17
21
 
18
22
  attr_reader :description_text, :description_options, :spec_path
19
23
  alias :options :description_options
20
24
 
25
+ # Provides the backtrace up to where this example_group was declared.
26
+ def backtrace
27
+ @backtrace
28
+ end
29
+
30
+ # Deprecated - use +backtrace()+
31
+ def example_group_backtrace
32
+ Kernel.warn <<-WARNING
33
+ ExampleGroupMethods#example_group_backtrace is deprecated and will be removed
34
+ from a future version. Please use ExampleGroupMethods#backtrace instead.
35
+ WARNING
36
+ backtrace
37
+ end
38
+
21
39
  def description_args
22
40
  @description_args ||= []
23
41
  end
24
42
 
25
43
  def inherited(klass)
26
44
  super
27
- klass.register {}
45
+ klass.register
28
46
  Spec::Runner.register_at_exit_hook
29
47
  end
30
48
 
@@ -46,11 +64,12 @@ module Spec
46
64
  args << {} unless Hash === args.last
47
65
  if example_group_block
48
66
  options = args.last
49
- options[:spec_path] = eval("caller(0)[1]", example_group_block) unless options[:spec_path]
67
+ # Ruby 1.9 - the next line uses example_group_block.binding instead of example_group_block
68
+ options[:spec_path] = eval("caller(0)[1]", example_group_block.binding) unless options[:spec_path]
50
69
  if options[:shared]
51
70
  create_shared_example_group(*args, &example_group_block)
52
71
  else
53
- create_nested_example_group(*args, &example_group_block)
72
+ create_subclass(*args, &example_group_block)
54
73
  end
55
74
  else
56
75
  set_description(*args)
@@ -58,11 +77,11 @@ module Spec
58
77
  end
59
78
  alias :context :describe
60
79
 
61
- def create_shared_example_group(*args, &example_group_block)
80
+ def create_shared_example_group(*args, &example_group_block) # :nodoc:
62
81
  SharedExampleGroup.register(*args, &example_group_block)
63
82
  end
64
83
 
65
- def create_nested_example_group(*args, &example_group_block)
84
+ def create_subclass(*args, &example_group_block) # :nodoc:
66
85
  self.subclass("Subclass") do
67
86
  set_description(*args)
68
87
  module_eval(&example_group_block)
@@ -164,13 +183,13 @@ module Spec
164
183
  #
165
184
  # See +ExampleMethods#should+ for more information about this approach.
166
185
  def subject(&block)
167
- if block
168
- @_subject_block = block
169
- else
170
- @_subject_block.call if @_subject_block
171
- end
186
+ @_subject_block = block
172
187
  end
173
-
188
+
189
+ def subject_block
190
+ @_subject_block || lambda {nil}
191
+ end
192
+
174
193
  def description_parts #:nodoc:
175
194
  parts = []
176
195
  each_ancestor_example_group_class do |example_group_class|
@@ -184,6 +203,7 @@ module Spec
184
203
  @description_args = args
185
204
  @description_options = options
186
205
  @description_text = ExampleGroupMethods.description_text(*args)
206
+ @backtrace = caller(1)
187
207
  @spec_path = File.expand_path(options[:spec_path]) if options[:spec_path]
188
208
  self
189
209
  end
@@ -206,8 +226,7 @@ module Spec
206
226
  @after_each_parts = nil
207
227
  end
208
228
 
209
- def register(&registration_binding_block)
210
- @registration_binding_block = registration_binding_block
229
+ def register
211
230
  Spec::Runner.options.add_example_group self
212
231
  end
213
232
 
@@ -215,10 +234,6 @@ module Spec
215
234
  Spec::Runner.options.remove_example_group self
216
235
  end
217
236
 
218
- def registration_backtrace
219
- eval("caller", @registration_binding_block)
220
- end
221
-
222
237
  def run_before_each(example)
223
238
  each_ancestor_example_group_class do |example_group_class|
224
239
  example.eval_each_fail_fast(example_group_class.before_each_parts)
@@ -4,9 +4,10 @@ module Spec
4
4
 
5
5
  extend ModuleReopeningFix
6
6
 
7
- def subject # :nodoc:
8
- @subject ||= (instance_variable_get(subject_variable_name) || self.class.subject ||
9
- (described_class ? described_class.new : nil))
7
+ def subject # :nodoc: this is somewhat experimental
8
+ @subject ||= ( instance_variable_get(subject_variable_name) ||
9
+ instance_eval(&self.class.subject_block) ||
10
+ (described_class ? described_class.new : nil) )
10
11
  end
11
12
 
12
13
  # When +should+ is called with no explicit receiver, the call is
@@ -63,20 +64,20 @@ module Spec
63
64
  success = execution_error.nil? || ExamplePendingError === execution_error
64
65
  end
65
66
 
66
- def instance_variable_hash
67
+ def instance_variable_hash # :nodoc:
67
68
  instance_variables.inject({}) do |variable_hash, variable_name|
68
69
  variable_hash[variable_name] = instance_variable_get(variable_name)
69
70
  variable_hash
70
71
  end
71
72
  end
72
73
 
73
- def eval_each_fail_fast(examples) #:nodoc:
74
+ def eval_each_fail_fast(examples) # :nodoc:
74
75
  examples.each do |example|
75
76
  instance_eval(&example)
76
77
  end
77
78
  end
78
79
 
79
- def eval_each_fail_slow(examples) #:nodoc:
80
+ def eval_each_fail_slow(examples) # :nodoc:
80
81
  first_exception = nil
81
82
  examples.each do |example|
82
83
  begin
@@ -109,12 +110,22 @@ module Spec
109
110
  end
110
111
  end
111
112
 
112
- def eval_block
113
+ def eval_block # :nodoc:
113
114
  instance_eval(&@_implementation)
114
115
  end
115
116
 
117
+ # Provides the backtrace up to where this example was declared.
118
+ def backtrace
119
+ @_backtrace
120
+ end
121
+
122
+ # Deprecated - use +backtrace()+
116
123
  def implementation_backtrace
117
- eval("caller", @_implementation)
124
+ Kernel.warn <<-WARNING
125
+ ExampleMethods#implementation_backtrace is deprecated and will be removed
126
+ from a future version. Please use ExampleMethods#backtrace instead.
127
+ WARNING
128
+ backtrace
118
129
  end
119
130
 
120
131
  private
data/lib/spec/example.rb CHANGED
@@ -1,26 +1,24 @@
1
1
  module Spec
2
2
  module Example
3
- class << self
4
- def args_and_options(*args)
5
- with_options_from(args) do |options|
6
- return args, options
7
- end
3
+ def self.args_and_options(*args)
4
+ with_options_from(args) do |options|
5
+ return args, options
8
6
  end
7
+ end
9
8
 
10
- def scope_from(*args)
11
- args[0] || :each
12
- end
9
+ def self.scope_from(*args)
10
+ args[0] || :each
11
+ end
13
12
 
14
- def scope_and_options(*args)
15
- args, options = args_and_options(*args)
16
- return scope_from(*args), options
17
- end
13
+ def self.scope_and_options(*args)
14
+ args, options = args_and_options(*args)
15
+ return scope_from(*args), options
16
+ end
18
17
 
19
- private
20
-
21
- def with_options_from(args)
22
- yield Hash === args.last ? args.pop : {} if block_given?
23
- end
18
+ private
19
+
20
+ def self.with_options_from(args)
21
+ yield Hash === args.last ? args.pop : {} if block_given?
24
22
  end
25
23
  end
26
24
  end
@@ -3,50 +3,45 @@ module Spec
3
3
  class InvalidMatcherError < ArgumentError; end
4
4
 
5
5
  class ExpectationMatcherHandler
6
- class << self
7
- def handle_matcher(actual, matcher, &block)
8
- ::Spec::Matchers.last_should = "should"
9
- return Spec::Matchers::PositiveOperatorMatcher.new(actual) if matcher.nil?
6
+ def self.handle_matcher(actual, matcher, &block)
7
+ ::Spec::Matchers.last_should = "should"
8
+ return Spec::Matchers::PositiveOperatorMatcher.new(actual) if matcher.nil?
10
9
 
11
- unless matcher.respond_to?(:matches?)
12
- raise InvalidMatcherError, "Expected a matcher, got #{matcher.inspect}."
13
- end
14
-
15
- match = matcher.matches?(actual, &block)
16
- ::Spec::Matchers.last_matcher = matcher
17
- Spec::Expectations.fail_with(matcher.failure_message) unless match
18
- match
10
+ unless matcher.respond_to?(:matches?)
11
+ raise InvalidMatcherError, "Expected a matcher, got #{matcher.inspect}."
19
12
  end
13
+
14
+ match = matcher.matches?(actual, &block)
15
+ ::Spec::Matchers.last_matcher = matcher
16
+ Spec::Expectations.fail_with(matcher.failure_message) unless match
17
+ match
20
18
  end
21
19
  end
22
20
 
23
21
  class NegativeExpectationMatcherHandler
24
- class << self
25
- def handle_matcher(actual, matcher, &block)
26
- ::Spec::Matchers.last_should = "should not"
27
- return Spec::Matchers::NegativeOperatorMatcher.new(actual) if matcher.nil?
28
-
29
- unless matcher.respond_to?(:matches?)
30
- raise InvalidMatcherError, "Expected a matcher, got #{matcher.inspect}."
31
- end
22
+ def self.handle_matcher(actual, matcher, &block)
23
+ ::Spec::Matchers.last_should = "should not"
24
+ return Spec::Matchers::NegativeOperatorMatcher.new(actual) if matcher.nil?
25
+
26
+ unless matcher.respond_to?(:matches?)
27
+ raise InvalidMatcherError, "Expected a matcher, got #{matcher.inspect}."
28
+ end
32
29
 
33
- unless matcher.respond_to?(:negative_failure_message)
34
- Spec::Expectations.fail_with(
30
+ unless matcher.respond_to?(:negative_failure_message)
31
+ Spec::Expectations.fail_with(
35
32
  <<-EOF
36
33
  Matcher does not support should_not.
37
34
  See Spec::Matchers for more information
38
35
  about matchers.
39
36
  EOF
40
37
  )
41
- end
42
- match = matcher.matches?(actual, &block)
43
- ::Spec::Matchers.last_matcher = matcher
44
- Spec::Expectations.fail_with(matcher.negative_failure_message) if match
45
- match
46
38
  end
39
+ match = matcher.matches?(actual, &block)
40
+ ::Spec::Matchers.last_matcher = matcher
41
+ Spec::Expectations.fail_with(matcher.negative_failure_message) if match
42
+ match
47
43
  end
48
44
  end
49
-
50
45
  end
51
46
  end
52
47
 
@@ -0,0 +1,56 @@
1
+ module Spec
2
+ module Matchers
3
+
4
+ # wraps an expectation in a block that will return true if the
5
+ # expectation passes and false if it fails (without bubbling up
6
+ # the failure).
7
+ #
8
+ # This is intended to be used in the context of a simple matcher,
9
+ # and is especially useful for wrapping multiple expectations or
10
+ # one or more assertions from test/unit extensions when running
11
+ # with test/unit.
12
+ #
13
+ # == Examples
14
+ #
15
+ # def eat_cheese(cheese)
16
+ # simple_matcher do |mouse, matcher|
17
+ # matcher.negative_failure_message = "expected #{mouse} not to eat cheese"
18
+ # wrap_expectation do |matcher|
19
+ # assert_eats_cheese(mouse)
20
+ # end
21
+ # end
22
+ # end
23
+ #
24
+ # describe Mouse do
25
+ # it "eats cheese" do
26
+ # Mouse.new.should eat_cheese
27
+ # end
28
+ # end
29
+ #
30
+ # You might be wondering "why would I do this if I could just say"
31
+ # assert_eats_cheese?", a fair question, indeed. You might prefer
32
+ # to replace the word assert with something more aligned with the
33
+ # rest of your code examples. You are using rspec, after all.
34
+ #
35
+ # The other benefit you get is that you can use the negative version
36
+ # of the matcher:
37
+ #
38
+ # describe Cat do
39
+ # it "does not eat cheese" do
40
+ # Cat.new.should_not eat_cheese
41
+ # end
42
+ # end
43
+ #
44
+ # So in the event there is no assert_does_not_eat_cheese available,
45
+ # you're all set!
46
+ def wrap_expectation(matcher, &block)
47
+ begin
48
+ block.call(matcher)
49
+ return true
50
+ rescue Exception => e
51
+ matcher.failure_message = e.message
52
+ return false
53
+ end
54
+ end
55
+ end
56
+ end
@@ -31,27 +31,31 @@ module Spec
31
31
  # RSpec ships with a standard set of useful matchers, and writing your own
32
32
  # matchers is quite simple. See Spec::Matchers for details.
33
33
  module Expectations
34
- class << self
35
- attr_accessor :differ
34
+ def self.differ
35
+ @differ
36
+ end
37
+
38
+ def self.differ=(differ)
39
+ @differ = differ
40
+ end
36
41
 
37
- # raises a Spec::Expectations::ExpectationNotMetError with message
38
- #
39
- # When a differ has been assigned and fail_with is passed
40
- # <code>expected</code> and <code>target</code>, passes them
41
- # to the differ to append a diff message to the failure message.
42
- def fail_with(message, expected=nil, target=nil) # :nodoc:
43
- if Array === message && message.length == 3
44
- message, expected, target = message[0], message[1], message[2]
45
- end
46
- unless (differ.nil? || expected.nil? || target.nil?)
47
- if expected.is_a?(String)
48
- message << "\nDiff:" << self.differ.diff_as_string(target.to_s, expected)
49
- elsif !target.is_a?(Proc)
50
- message << "\nDiff:" << self.differ.diff_as_object(target, expected)
51
- end
42
+ # raises a Spec::Expectations::ExpectationNotMetError with message
43
+ #
44
+ # When a differ has been assigned and fail_with is passed
45
+ # <code>expected</code> and <code>target</code>, passes them
46
+ # to the differ to append a diff message to the failure message.
47
+ def self.fail_with(message, expected=nil, target=nil) # :nodoc:
48
+ if Array === message && message.length == 3
49
+ message, expected, target = message[0], message[1], message[2]
50
+ end
51
+ unless (differ.nil? || expected.nil? || target.nil?)
52
+ if expected.is_a?(String)
53
+ message << "\nDiff:" << self.differ.diff_as_string(target.to_s, expected)
54
+ elsif !target.is_a?(Proc)
55
+ message << "\nDiff:" << self.differ.diff_as_object(target, expected)
52
56
  end
53
- Kernel::raise(Spec::Expectations::ExpectationNotMetError.new(message))
54
57
  end
58
+ Kernel::raise(Spec::Expectations::ExpectationNotMetError.new(message))
55
59
  end
56
60
  end
57
61
  end
@@ -1,2 +1 @@
1
1
  require 'spec/extensions/class'
2
- require 'spec/extensions/metaclass'
@@ -23,26 +23,24 @@ module Test
23
23
  extend Spec::Example::ExampleGroupMethods
24
24
  include Spec::Example::ExampleMethods
25
25
 
26
- before(:each) {setup}
27
- after(:each) {teardown}
28
-
29
- class << self
30
- def suite
31
- Test::Unit::TestSuiteAdapter.new(self)
32
- end
26
+ def self.suite
27
+ Test::Unit::TestSuiteAdapter.new(self)
28
+ end
33
29
 
34
- def example_method?(method_name)
35
- should_method?(method_name) || test_method?(method_name)
36
- end
30
+ def self.example_method?(method_name)
31
+ should_method?(method_name) || test_method?(method_name)
32
+ end
37
33
 
38
- def test_method?(method_name)
39
- method_name =~ /^test[_A-Z]./ && (
40
- instance_method(method_name).arity == 0 ||
41
- instance_method(method_name).arity == -1
42
- )
43
- end
34
+ def self.test_method?(method_name)
35
+ method_name =~ /^test[_A-Z]./ && (
36
+ instance_method(method_name).arity == 0 ||
37
+ instance_method(method_name).arity == -1
38
+ )
44
39
  end
45
40
 
41
+ before(:each) {setup}
42
+ after(:each) {teardown}
43
+
46
44
  def initialize(defined_description, options={}, &implementation)
47
45
  @_defined_description = defined_description
48
46
 
@@ -51,9 +49,13 @@ module Test
51
49
  @_implementation = implementation || pending_implementation
52
50
 
53
51
  @_result = ::Test::Unit::TestResult.new
54
- # @method_name is important to set here because it "complies" with Test::Unit's interface.
52
+ # @method_name is important to set here because it complies with Test::Unit's interface.
55
53
  # Some Test::Unit extensions depend on @method_name being present.
56
54
  @method_name = @_defined_description
55
+
56
+ # TODO - this is necessary to run single examples in rspec-rails, but I haven't
57
+ # found a good way to write a failing example just within rspec core
58
+ @_backtrace = caller
57
59
  end
58
60
 
59
61
  def run(ignore_this_argument=nil)
@@ -1,37 +1,21 @@
1
1
  module Spec
2
2
  module Matchers
3
3
 
4
- class BeClose #:nodoc:
5
- def initialize(expected, delta)
6
- @expected = expected
7
- @delta = delta
8
- end
9
-
10
- def matches?(given)
11
- @given = given
12
- (@given - @expected).abs < @delta
13
- end
14
-
15
- def failure_message
16
- "expected #{@expected} +/- (< #{@delta}), got #{@given}"
17
- end
18
-
19
- def description
20
- "be close to #{@expected} (within +- #{@delta})"
21
- end
22
- end
23
-
24
4
  # :call-seq:
25
5
  # should be_close(expected, delta)
26
6
  # should_not be_close(expected, delta)
27
7
  #
28
- # Passes if given == expected +/- delta
8
+ # Passes if actual == expected +/- delta
29
9
  #
30
10
  # == Example
31
11
  #
32
12
  # result.should be_close(3.0, 0.5)
33
13
  def be_close(expected, delta)
34
- Matchers::BeClose.new(expected, delta)
14
+ simple_matcher do |actual, matcher|
15
+ matcher.failure_message = "expected #{expected} +/- (< #{delta}), got #{actual}"
16
+ matcher.description = "be close to #{expected} (within +- #{delta})"
17
+ (actual - expected).abs < delta
18
+ end
35
19
  end
36
20
  end
37
21
  end
@@ -1,34 +1,11 @@
1
1
  module Spec
2
2
  module Matchers
3
3
 
4
- class Eql #:nodoc:
5
- def initialize(expected)
6
- @expected = expected
7
- end
8
-
9
- def matches?(given)
10
- @given = given
11
- @given.eql?(@expected)
12
- end
13
-
14
- def failure_message
15
- return "expected #{@expected.inspect}, got #{@given.inspect} (using .eql?)", @expected, @given
16
- end
17
-
18
- def negative_failure_message
19
- return "expected #{@given.inspect} not to equal #{@expected.inspect} (using .eql?)", @expected, @given
20
- end
21
-
22
- def description
23
- "eql #{@expected.inspect}"
24
- end
25
- end
26
-
27
4
  # :call-seq:
28
5
  # should eql(expected)
29
6
  # should_not eql(expected)
30
7
  #
31
- # Passes if given and expected are of equal value, but not necessarily the same object.
8
+ # Passes if actual and expected are of equal value, but not necessarily the same object.
32
9
  #
33
10
  # See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more information about equality in Ruby.
34
11
  #
@@ -37,7 +14,12 @@ module Spec
37
14
  # 5.should eql(5)
38
15
  # 5.should_not eql(3)
39
16
  def eql(expected)
40
- Matchers::Eql.new(expected)
17
+ simple_matcher do |actual, matcher|
18
+ matcher.failure_message = "expected #{expected.inspect}, got #{actual.inspect} (using .eql?)", expected, actual
19
+ matcher.negative_failure_message = "expected #{actual.inspect} not to equal #{expected.inspect} (using .eql?)", expected, actual
20
+ matcher.description = "eql #{expected.inspect}"
21
+ actual.eql?(expected)
22
+ end
41
23
  end
42
24
  end
43
25
  end
@@ -1,29 +1,6 @@
1
1
  module Spec
2
2
  module Matchers
3
3
 
4
- class Equal #:nodoc:
5
- def initialize(expected)
6
- @expected = expected
7
- end
8
-
9
- def matches?(given)
10
- @given = given
11
- @given.equal?(@expected)
12
- end
13
-
14
- def failure_message
15
- return "expected #{@expected.inspect}, got #{@given.inspect} (using .equal?)", @expected, @given
16
- end
17
-
18
- def negative_failure_message
19
- return "expected #{@given.inspect} not to equal #{@expected.inspect} (using .equal?)", @expected, @given
20
- end
21
-
22
- def description
23
- "equal #{@expected.inspect}"
24
- end
25
- end
26
-
27
4
  # :call-seq:
28
5
  # should equal(expected)
29
6
  # should_not equal(expected)
@@ -37,7 +14,12 @@ module Spec
37
14
  # 5.should equal(5) #Fixnums are equal
38
15
  # "5".should_not equal("5") #Strings that look the same are not the same object
39
16
  def equal(expected)
40
- Matchers::Equal.new(expected)
17
+ simple_matcher do |actual, matcher|
18
+ matcher.failure_message = "expected #{expected.inspect}, got #{actual.inspect} (using .equal?)", expected, actual
19
+ matcher.negative_failure_message = "expected #{actual.inspect} not to equal #{expected.inspect} (using .equal?)", expected, actual
20
+ matcher.description = "equal #{expected.inspect}"
21
+ actual.equal?(expected)
22
+ end
41
23
  end
42
24
  end
43
25
  end
@@ -0,0 +1,5 @@
1
+ module Spec
2
+ module Matchers
3
+ class MatcherError < StandardError; end
4
+ end
5
+ end
@@ -1,22 +1,16 @@
1
1
  module Spec
2
2
  module Matchers
3
- class Exist
4
- def matches?(given)
5
- @given = given
6
- @given.exist?
7
- end
8
- def failure_message
9
- "expected #{@given.inspect} to exist, but it doesn't."
10
- end
11
- def negative_failure_message
12
- "expected #{@given.inspect} to not exist, but it does."
13
- end
14
- end
15
3
  # :call-seq:
16
4
  # should exist
17
5
  # should_not exist
18
6
  #
19
- # Passes if given.exist?
20
- def exist; Exist.new; end
7
+ # Passes if actual.exist?
8
+ def exist
9
+ simple_matcher do |actual, matcher|
10
+ matcher.failure_message = "expected #{actual.inspect} to exist, but it doesn't."
11
+ matcher.negative_failure_message = "expected #{actual.inspect} to not exist, but it does."
12
+ actual.exist?
13
+ end
14
+ end
21
15
  end
22
16
  end