rspec 1.2.2 → 1.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (138) hide show
  1. data/History.rdoc +31 -1
  2. data/Manifest.txt +14 -12
  3. data/Rakefile +1 -1
  4. data/Upgrade.rdoc +43 -3
  5. data/examples/failing/failing_implicit_docstrings_example.rb +5 -5
  6. data/examples/passing/implicit_docstrings_example.rb +3 -3
  7. data/features/before_and_after_blocks/before_and_after_blocks.feature +20 -21
  8. data/features/example_groups/example_group_with_should_methods.feature +20 -8
  9. data/features/example_groups/implicit_docstrings.feature +36 -20
  10. data/features/example_groups/nested_groups.feature +25 -10
  11. data/features/extensions/custom_example_group.feature +19 -0
  12. data/features/formatters/custom_formatter.feature +30 -0
  13. data/features/heckle/heckle.feature +7 -7
  14. data/features/interop/examples_and_tests_together.feature +63 -14
  15. data/features/interop/rspec_output.feature +25 -0
  16. data/features/interop/test_but_not_test_unit.feature +19 -7
  17. data/features/interop/test_case_with_should_methods.feature +37 -8
  18. data/features/matchers/{create_matcher.feature → define_matcher.feature} +54 -30
  19. data/features/matchers/{create_matcher_outside_rspec.feature → define_matcher_outside_rspec.feature} +7 -7
  20. data/features/mock_framework_integration/use_flexmock.feature +3 -3
  21. data/features/mock_framework_integration/use_mocha.feature +3 -3
  22. data/features/mock_framework_integration/use_rr.feature +3 -3
  23. data/features/mocks/mix_stubs_and_mocks.feature +4 -4
  24. data/features/pending/pending_examples.feature +18 -18
  25. data/features/runner/specify_line_number.feature +32 -0
  26. data/features/step_definitions/running_rspec_steps.rb +41 -0
  27. data/features/subject/explicit_subject.feature +4 -4
  28. data/features/subject/implicit_subject.feature +4 -4
  29. data/features/support/env.rb +64 -11
  30. data/features/support/matchers/smart_match.rb +10 -35
  31. data/lib/spec.rb +1 -0
  32. data/lib/spec/deprecation.rb +40 -0
  33. data/lib/spec/dsl/main.rb +10 -5
  34. data/lib/spec/example.rb +1 -27
  35. data/lib/spec/example/args_and_options.rb +27 -0
  36. data/lib/spec/example/before_and_after_hooks.rb +30 -21
  37. data/lib/spec/example/errors.rb +8 -18
  38. data/lib/spec/example/example_group_factory.rb +0 -4
  39. data/lib/spec/example/example_group_methods.rb +27 -41
  40. data/lib/spec/example/example_group_proxy.rb +12 -22
  41. data/lib/spec/example/example_methods.rb +8 -18
  42. data/lib/spec/example/example_proxy.rb +4 -4
  43. data/lib/spec/example/pending.rb +3 -4
  44. data/lib/spec/example/predicate_matchers.rb +1 -1
  45. data/lib/spec/example/subject.rb +2 -0
  46. data/lib/spec/expectations.rb +1 -26
  47. data/lib/spec/expectations/extensions.rb +1 -1
  48. data/lib/spec/expectations/extensions/kernel.rb +52 -0
  49. data/lib/spec/expectations/handler.rb +21 -10
  50. data/lib/spec/matchers/be.rb +3 -3
  51. data/lib/spec/matchers/be_close.rb +17 -21
  52. data/lib/spec/matchers/be_instance_of.rb +7 -26
  53. data/lib/spec/matchers/be_kind_of.rb +7 -26
  54. data/lib/spec/matchers/dsl.rb +9 -4
  55. data/lib/spec/matchers/eql.rb +24 -25
  56. data/lib/spec/matchers/equal.rb +25 -25
  57. data/lib/spec/matchers/exist.rb +5 -21
  58. data/lib/spec/matchers/include.rb +22 -44
  59. data/lib/spec/matchers/match.rb +5 -25
  60. data/lib/spec/matchers/match_array.rb +8 -4
  61. data/lib/spec/matchers/matcher.rb +13 -2
  62. data/lib/spec/matchers/pretty.rb +1 -0
  63. data/lib/spec/matchers/simple_matcher.rb +2 -2
  64. data/lib/spec/mocks/mock.rb +10 -10
  65. data/lib/spec/mocks/spec_methods.rb +1 -1
  66. data/lib/spec/runner/configuration.rb +53 -42
  67. data/lib/spec/{expectations → runner}/differs/default.rb +0 -0
  68. data/lib/spec/{expectations → runner}/differs/load-diff-lcs.rb +0 -0
  69. data/lib/spec/runner/drb_command_line.rb +1 -1
  70. data/lib/spec/runner/example_group_runner.rb +0 -12
  71. data/lib/spec/runner/formatter/base_formatter.rb +77 -30
  72. data/lib/spec/runner/formatter/base_text_formatter.rb +19 -24
  73. data/lib/spec/runner/formatter/failing_example_groups_formatter.rb +1 -1
  74. data/lib/spec/runner/formatter/html_formatter.rb +4 -6
  75. data/lib/spec/runner/formatter/nested_text_formatter.rb +2 -2
  76. data/lib/spec/runner/formatter/no_op_method_missing.rb +21 -0
  77. data/lib/spec/runner/formatter/profile_formatter.rb +0 -4
  78. data/lib/spec/runner/formatter/progress_bar_formatter.rb +4 -5
  79. data/lib/spec/runner/formatter/silent_formatter.rb +10 -0
  80. data/lib/spec/runner/formatter/specdoc_formatter.rb +2 -2
  81. data/lib/spec/runner/option_parser.rb +14 -7
  82. data/lib/spec/runner/options.rb +17 -7
  83. data/lib/spec/runner/reporter.rb +69 -64
  84. data/lib/spec/version.rb +1 -1
  85. data/spec/spec/dsl/main_spec.rb +7 -0
  86. data/spec/spec/example/example_group_methods_spec.rb +16 -32
  87. data/spec/spec/example/example_group_proxy_spec.rb +25 -5
  88. data/spec/spec/example/example_group_spec.rb +2 -2
  89. data/spec/spec/example/example_methods_spec.rb +19 -24
  90. data/spec/spec/example/example_proxy_spec.rb +11 -1
  91. data/spec/spec/example/pending_module_spec.rb +2 -108
  92. data/spec/spec/example/predicate_matcher_spec.rb +23 -3
  93. data/spec/spec/expectations/extensions/object_spec.rb +9 -9
  94. data/spec/spec/expectations/fail_with_spec.rb +13 -13
  95. data/spec/spec/expectations/handler_spec.rb +44 -15
  96. data/spec/spec/matchers/be_close_spec.rb +9 -0
  97. data/spec/spec/matchers/be_instance_of_spec.rb +6 -4
  98. data/spec/spec/matchers/be_kind_of_spec.rb +5 -3
  99. data/spec/spec/matchers/be_spec.rb +1 -5
  100. data/spec/spec/matchers/compatibility_spec.rb +1 -1
  101. data/spec/spec/matchers/dsl_spec.rb +10 -1
  102. data/spec/spec/matchers/eql_spec.rb +8 -4
  103. data/spec/spec/matchers/equal_spec.rb +8 -4
  104. data/spec/spec/matchers/exist_spec.rb +1 -1
  105. data/spec/spec/matchers/include_spec.rb +1 -1
  106. data/spec/spec/matchers/match_array_spec.rb +26 -1
  107. data/spec/spec/matchers/match_spec.rb +2 -2
  108. data/spec/spec/matchers/matcher_methods_spec.rb +0 -3
  109. data/spec/spec/matchers/matcher_spec.rb +53 -0
  110. data/spec/spec/matchers/operator_matcher_spec.rb +1 -1
  111. data/spec/spec/mocks/mock_spec.rb +6 -0
  112. data/spec/spec/runner/configuration_spec.rb +11 -1
  113. data/spec/spec/runner/drb_command_line_spec.rb +13 -1
  114. data/spec/spec/runner/example_group_runner_spec.rb +0 -7
  115. data/spec/spec/runner/formatter/base_formatter_spec.rb +19 -12
  116. data/spec/spec/runner/formatter/base_text_formatter_spec.rb +2 -2
  117. data/spec/spec/runner/formatter/failing_example_groups_formatter_spec.rb +4 -4
  118. data/spec/spec/runner/formatter/failing_examples_formatter_spec.rb +2 -2
  119. data/spec/spec/runner/formatter/html_formatted-1.8.6.html +31 -34
  120. data/spec/spec/runner/formatter/html_formatter_spec.rb +20 -0
  121. data/spec/spec/runner/formatter/nested_text_formatter_spec.rb +20 -20
  122. data/spec/spec/runner/formatter/profile_formatter_spec.rb +22 -3
  123. data/spec/spec/runner/formatter/progress_bar_formatter_spec.rb +24 -3
  124. data/spec/spec/runner/formatter/specdoc_formatter_spec.rb +6 -6
  125. data/spec/spec/runner/formatter/text_mate_formatted-1.8.6.html +25 -28
  126. data/spec/spec/runner/formatter/text_mate_formatter_spec.rb +2 -2
  127. data/spec/spec/runner/option_parser_spec.rb +108 -40
  128. data/spec/spec/runner/reporter_spec.rb +16 -17
  129. data/spec/spec_helper.rb +4 -1
  130. metadata +20 -18
  131. data/features/example_groups/output.feature +0 -20
  132. data/features/step_definitions/running_rspec.rb +0 -69
  133. data/lib/spec/expectations/extensions/object.rb +0 -63
  134. data/resources/spec/example_group_with_should_methods.rb +0 -12
  135. data/resources/spec/simple_spec.rb +0 -8
  136. data/resources/test/spec_and_test_together.rb +0 -56
  137. data/resources/test/spec_including_test_but_not_unit.rb +0 -11
  138. data/resources/test/test_case_with_should_methods.rb +0 -29
@@ -1,7 +1,7 @@
1
1
  module Spec
2
2
  module Example
3
- # Lightweight representation of an example group. This is the object
4
- # that is passed to Spec::Runner::Formatter::BaseFormatter#add_example_group
3
+ # Lightweight proxy for an example group. This is the object that is passed
4
+ # to Spec::Runner::Formatter::BaseFormatter#example_group_started
5
5
  class ExampleGroupProxy
6
6
 
7
7
  def initialize(example_group) # :nodoc:
@@ -9,37 +9,38 @@ module Spec
9
9
  @nested_descriptions = example_group.nested_descriptions
10
10
  @examples = example_group.example_proxies
11
11
  @location = example_group.location
12
- @backtrace = example_group.backtrace
12
+ @backtrace = example_group.location # deprecated - see the backtrace method below
13
13
  end
14
14
 
15
- # This is the docstring passed to the <tt>describe()</tt> method or any
15
+ # This is the description passed to the <tt>describe()</tt> method or any
16
16
  # of its aliases
17
17
  attr_reader :description
18
18
 
19
19
  # Used by Spec::Runner::Formatter::NestedTextFormatter to access the
20
- # docstrings for each example group in a nested group.
20
+ # description of each example group in a nested group separately.
21
21
  attr_reader :nested_descriptions
22
22
 
23
- # A collection of ExampleGroupProxy instances, one for each example
23
+ # A collection of ExampleGroupProxy objects, one for each example
24
24
  # declared in this group.
25
25
  attr_reader :examples
26
26
 
27
- # The file and line number at which the represented example group
27
+ # The file and line number at which the proxied example group
28
28
  # was declared. This is extracted from <tt>caller</tt>, and is therefore
29
29
  # formatted as an individual line in a backtrace.
30
30
  attr_reader :location
31
31
 
32
32
  # Deprecated - use location() instead
33
33
  def backtrace
34
+ Spec::deprecate("ExampleGroupProxy#backtrace","ExampleGroupProxy#location")
34
35
  @backtrace
35
36
  end
36
37
 
37
- # Returns the nested_descriptions collection with any descriptions
38
- # matching the submitted regexp removed.
38
+ # Deprecated - just use gsub on the description instead.
39
39
  def filtered_description(regexp)
40
- build_description_from(
40
+ Spec::deprecate("ExampleGroupProxy#filtered_description","gsub (or similar) to modify ExampleGroupProxy#description")
41
+ ExampleGroupMethods.build_description_from(
41
42
  *nested_descriptions.collect do |description|
42
- description =~ regexp ? $1 : description
43
+ description =~ regexp ? description.gsub($1, "") : description
43
44
  end
44
45
  )
45
46
  end
@@ -47,17 +48,6 @@ module Spec
47
48
  def ==(other) # :nodoc:
48
49
  other.description == description
49
50
  end
50
-
51
- private
52
-
53
- # FIXME - this is duplicated from ExampleGroupMethods
54
- def build_description_from(*args)
55
- text = args.inject("") do |description, arg|
56
- description << " " unless (description == "" || arg.to_s =~ /^(\s|\.|#)/)
57
- description << arg.to_s
58
- end
59
- text == "" ? nil : text
60
- end
61
51
  end
62
52
  end
63
53
  end
@@ -18,7 +18,11 @@ module Spec
18
18
  # description
19
19
  # => "should start with a balance of 0"
20
20
  def description
21
- @_proxy.description || ::Spec::Matchers.generated_description || "NO NAME"
21
+ if description = @_proxy.description || ::Spec::Matchers.generated_description
22
+ description
23
+ else
24
+ raise Spec::Example::NoDescriptionError.new("example", @_proxy.location)
25
+ end
22
26
  end
23
27
 
24
28
  def options # :nodoc:
@@ -81,20 +85,6 @@ module Spec
81
85
  end
82
86
  end
83
87
 
84
- # Provides the backtrace up to where this example was declared.
85
- def backtrace
86
- @_backtrace
87
- end
88
-
89
- # Deprecated - use +backtrace()+
90
- def implementation_backtrace
91
- Kernel.warn <<-WARNING
92
- ExampleMethods#implementation_backtrace is deprecated and will be removed
93
- from a future version. Please use ExampleMethods#backtrace instead.
94
- WARNING
95
- backtrace
96
- end
97
-
98
88
  # Run all the before(:each) blocks for this example
99
89
  def run_before_each
100
90
  example_group_hierarchy.run_before_each(self)
@@ -116,19 +106,19 @@ WARNING
116
106
  include Matchers
117
107
  include Pending
118
108
 
119
- def before_each_example # :nodoc:
109
+ def before_each_example
120
110
  setup_mocks_for_rspec
121
111
  run_before_each
122
112
  end
123
113
 
124
- def after_each_example # :nodoc:
114
+ def after_each_example
125
115
  run_after_each
126
116
  verify_mocks_for_rspec
127
117
  ensure
128
118
  teardown_mocks_for_rspec
129
119
  end
130
120
 
131
- def described_class # :nodoc:
121
+ def described_class
132
122
  self.class.described_class
133
123
  end
134
124
 
@@ -1,8 +1,7 @@
1
1
  module Spec
2
2
  module Example
3
- # Lightweight representation of an example. This is the object
4
- # that is passed to example-related methods in
5
- # Spec::Runner::Formatter::BaseFormatter
3
+ # Lightweight proxy for an example. This is the object that is passed to
4
+ # example-related methods in Spec::Runner::Formatter::BaseFormatter
6
5
  class ExampleProxy
7
6
 
8
7
  def initialize(description=nil, options={}, location=nil) # :nodoc:
@@ -24,6 +23,7 @@ module Spec
24
23
 
25
24
  # Deprecated - use location()
26
25
  def backtrace
26
+ Spec.deprecate("ExampleProxy#backtrace","ExampleProxy#location")
27
27
  location
28
28
  end
29
29
 
@@ -35,7 +35,7 @@ module Spec
35
35
  end
36
36
 
37
37
  def ==(other) # :nodoc:
38
- (other.description == description) & (other.backtrace == backtrace)
38
+ (other.description == description) & (other.location == location)
39
39
  end
40
40
  end
41
41
  end
@@ -2,16 +2,15 @@ module Spec
2
2
  module Example
3
3
  module Pending
4
4
  def pending(message = "TODO")
5
- pending_caller = caller[0]
6
5
  if block_given?
7
6
  begin
8
7
  yield
9
- rescue Exception => e
10
- raise Spec::Example::ExamplePendingError.new(message, pending_caller)
8
+ rescue Exception
9
+ raise Spec::Example::ExamplePendingError.new(message)
11
10
  end
12
11
  raise Spec::Example::PendingExampleFixedError.new("Expected pending '#{message}' to fail. No Error was raised.")
13
12
  else
14
- raise Spec::Example::ExamplePendingError.new(message, pending_caller)
13
+ raise Spec::Example::ExamplePendingError.new(message)
15
14
  end
16
15
  end
17
16
  end
@@ -30,7 +30,7 @@ module Spec
30
30
  # end
31
31
  # end
32
32
  def predicate_matchers
33
- @predicate_matchers ||= {}
33
+ @predicate_matchers ||= Spec::HashWithDeprecationNotice.new("predicate_matchers", "the new Matcher DSL")
34
34
  end
35
35
 
36
36
  def define_methods_from_predicate_matchers # :nodoc:
@@ -19,6 +19,8 @@ module Spec
19
19
  explicit_subject || implicit_subject : @_explicit_subject_block = block
20
20
  end
21
21
 
22
+ private
23
+
22
24
  def explicit_subject
23
25
  if defined?(@_explicit_subject_block)
24
26
  @_explicit_subject_block
@@ -1,4 +1,5 @@
1
1
  require 'spec/matchers'
2
+ require 'spec/expectations/fail_with'
2
3
  require 'spec/expectations/errors'
3
4
  require 'spec/expectations/extensions'
4
5
  require 'spec/expectations/handler'
@@ -30,31 +31,5 @@ module Spec
30
31
  # RSpec ships with a standard set of useful matchers, and writing your own
31
32
  # matchers is quite simple. See Spec::Matchers for details.
32
33
  module Expectations
33
- def self.differ
34
- defined?(@differ) ? @differ : nil
35
- end
36
-
37
- def self.differ=(differ)
38
- @differ = differ
39
- end
40
-
41
- # raises a Spec::Expectations::ExpectationNotMetError with message
42
- #
43
- # When a differ has been assigned and fail_with is passed
44
- # <code>expected</code> and <code>target</code>, passes them
45
- # to the differ to append a diff message to the failure message.
46
- def self.fail_with(message, expected=nil, target=nil) # :nodoc:
47
- if (Array === message) & (message.length == 3)
48
- message, expected, target = message[0], message[1], message[2]
49
- end
50
- unless (differ.nil? || expected.nil? || target.nil?)
51
- if expected.is_a?(String)
52
- message << "\nDiff:" << self.differ.diff_as_string(target.to_s, expected)
53
- elsif !target.is_a?(Proc)
54
- message << "\nDiff:" << self.differ.diff_as_object(target, expected)
55
- end
56
- end
57
- Kernel::raise(Spec::Expectations::ExpectationNotMetError.new(message))
58
- end
59
34
  end
60
35
  end
@@ -1 +1 @@
1
- require 'spec/expectations/extensions/object'
1
+ require 'spec/expectations/extensions/kernel'
@@ -0,0 +1,52 @@
1
+ module Kernel
2
+ # :call-seq:
3
+ # should(matcher)
4
+ # should == expected
5
+ # should === expected
6
+ # should =~ expected
7
+ #
8
+ # receiver.should(matcher)
9
+ # => Passes if matcher.matches?(receiver)
10
+ #
11
+ # receiver.should == expected #any value
12
+ # => Passes if (receiver == expected)
13
+ #
14
+ # receiver.should === expected #any value
15
+ # => Passes if (receiver === expected)
16
+ #
17
+ # receiver.should =~ regexp
18
+ # => Passes if (receiver =~ regexp)
19
+ #
20
+ # See Spec::Matchers for more information about matchers
21
+ #
22
+ # == Warning
23
+ #
24
+ # NOTE that this does NOT support receiver.should != expected.
25
+ # Instead, use receiver.should_not == expected
26
+ def should(matcher=nil, &block)
27
+ Spec::Expectations::PositiveExpectationHandler.handle_matcher(self, matcher, &block)
28
+ end
29
+
30
+ # :call-seq:
31
+ # should_not(matcher)
32
+ # should_not == expected
33
+ # should_not === expected
34
+ # should_not =~ expected
35
+ #
36
+ # receiver.should_not(matcher)
37
+ # => Passes unless matcher.matches?(receiver)
38
+ #
39
+ # receiver.should_not == expected
40
+ # => Passes unless (receiver == expected)
41
+ #
42
+ # receiver.should_not === expected
43
+ # => Passes unless (receiver === expected)
44
+ #
45
+ # receiver.should_not =~ regexp
46
+ # => Passes unless (receiver =~ regexp)
47
+ #
48
+ # See Spec::Matchers for more information about matchers
49
+ def should_not(matcher=nil, &block)
50
+ Spec::Expectations::NegativeExpectationHandler.handle_matcher(self, matcher, &block)
51
+ end
52
+ end
@@ -2,7 +2,7 @@ module Spec
2
2
  module Expectations
3
3
  class InvalidMatcherError < ArgumentError; end
4
4
 
5
- class ExpectationMatcherHandler
5
+ class PositiveExpectationHandler
6
6
  def self.handle_matcher(actual, matcher, &block)
7
7
  ::Spec::Matchers.last_should = :should
8
8
  ::Spec::Matchers.last_matcher = matcher
@@ -10,28 +10,39 @@ module Spec
10
10
 
11
11
  match = matcher.matches?(actual, &block)
12
12
  return match if match
13
-
14
- ::Spec::Expectations.fail_with matcher.respond_to?(:failure_message_for_should) ?
15
- matcher.failure_message_for_should :
16
- matcher.failure_message
13
+
14
+ message = matcher.respond_to?(:failure_message_for_should) ?
15
+ matcher.failure_message_for_should :
16
+ matcher.failure_message
17
+
18
+ if matcher.respond_to?(:diffable?) && matcher.diffable?
19
+ ::Spec::Expectations.fail_with message, matcher.expected.first, matcher.actual
20
+ else
21
+ ::Spec::Expectations.fail_with message
22
+ end
17
23
  end
18
24
  end
19
25
 
20
- class NegativeExpectationMatcherHandler
26
+ class NegativeExpectationHandler
21
27
  def self.handle_matcher(actual, matcher, &block)
22
28
  ::Spec::Matchers.last_should = :should_not
23
29
  ::Spec::Matchers.last_matcher = matcher
24
-
25
30
  return ::Spec::Matchers::NegativeOperatorMatcher.new(actual) if matcher.nil?
26
31
 
27
32
  match = matcher.respond_to?(:does_not_match?) ?
28
33
  !matcher.does_not_match?(actual, &block) :
29
34
  matcher.matches?(actual, &block)
30
35
  return match unless match
36
+
37
+ message = matcher.respond_to?(:failure_message_for_should_not) ?
38
+ matcher.failure_message_for_should_not :
39
+ matcher.negative_failure_message
31
40
 
32
- ::Spec::Expectations.fail_with matcher.respond_to?(:failure_message_for_should_not) ?
33
- matcher.failure_message_for_should_not :
34
- matcher.negative_failure_message
41
+ if matcher.respond_to?(:diffable?) && matcher.diffable?
42
+ ::Spec::Expectations.fail_with message, matcher.expected.first, matcher.actual
43
+ else
44
+ ::Spec::Expectations.fail_with message
45
+ end
35
46
  end
36
47
  end
37
48
  end
@@ -41,12 +41,12 @@ module Spec
41
41
  else
42
42
  message = <<-MESSAGE
43
43
  'should_not be #{@comparison_method} #{expected}' not only FAILED,
44
- it reads really poorly.
44
+ it is a bit confusing.
45
45
  MESSAGE
46
46
 
47
47
  raise message << ([:===,:==].include?(@comparison_method) ?
48
- "Why don't you try expressing it without the \"be\"?" :
49
- "Why don't you try expressing it in the positive?")
48
+ "It might be more clearly expressed without the \"be\"?" :
49
+ "It might be more clearly expressed in the positive?")
50
50
  end
51
51
  end
52
52
 
@@ -1,25 +1,5 @@
1
1
  module Spec
2
2
  module Matchers
3
-
4
- class BeClose
5
- def initialize(expected, delta)
6
- @expected, @delta = expected, delta
7
- end
8
-
9
- def matches?(actual)
10
- @actual = actual
11
- (@actual - @expected).abs < @delta
12
- end
13
-
14
- def failure_message_for_should
15
- "expected #{@expected} +/- (< #{@delta}), got #{@actual}"
16
- end
17
-
18
- def description
19
- "be close to #{@expected} (within +- #{@delta})"
20
- end
21
- end
22
-
23
3
  # :call-seq:
24
4
  # should be_close(expected, delta)
25
5
  # should_not be_close(expected, delta)
@@ -30,7 +10,23 @@ module Spec
30
10
  #
31
11
  # result.should be_close(3.0, 0.5)
32
12
  def be_close(expected, delta)
33
- BeClose.new(expected, delta)
13
+ Matcher.new :be_close, expected, delta do |expected, delta|
14
+ match do |actual|
15
+ (actual - expected).abs < delta
16
+ end
17
+
18
+ failure_message_for_should do |actual|
19
+ "expected #{expected} +/- (< #{delta}), got #{actual}"
20
+ end
21
+
22
+ failure_message_for_should_not do |actual|
23
+ "expected #{expected} +/- (< #{delta}), got #{actual}"
24
+ end
25
+
26
+ description do
27
+ "be close to #{expected} (within +- #{delta})"
28
+ end
29
+ end
34
30
  end
35
31
  end
36
32
  end
@@ -1,28 +1,5 @@
1
1
  module Spec
2
2
  module Matchers
3
- class BeInstanceOf
4
- def initialize(expected)
5
- @expected = expected
6
- end
7
-
8
- def matches?(actual)
9
- @actual = actual
10
- @actual.instance_of?(@expected)
11
- end
12
-
13
- def description
14
- "be an instance of #{@expected}"
15
- end
16
-
17
- def failure_message_for_should
18
- "expected instance of #{@expected}, got #{@actual.inspect}"
19
- end
20
-
21
- def failure_message_for_should_not
22
- "expected #{@actual.inspect} not to be an instance of #{@expected}"
23
- end
24
- end
25
-
26
3
  # :call-seq:
27
4
  # should be_instance_of(expected)
28
5
  # should be_an_instance_of(expected)
@@ -36,10 +13,14 @@ module Spec
36
13
  # 5.should be_instance_of(Fixnum)
37
14
  # 5.should_not be_instance_of(Numeric)
38
15
  # 5.should_not be_instance_of(Float)
39
- def be_instance_of(expected)
40
- BeInstanceOf.new(expected)
16
+ 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
41
22
  end
42
23
 
43
- alias_method :be_an_instance_of, :be_instance_of
24
+ alias_method :be_instance_of, :be_an_instance_of
44
25
  end
45
26
  end