rspec 0.7.5.1 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (194) hide show
  1. data/CHANGES +60 -1
  2. data/EXAMPLES.rd +38 -19
  3. data/MIT-LICENSE +1 -1
  4. data/README +24 -17
  5. data/RELEASE-PLAN +117 -0
  6. data/Rakefile +24 -18
  7. data/TODO.0.8.0 +5 -0
  8. data/examples/auto_spec_name_generation_example.rb +18 -0
  9. data/examples/custom_expectation_matchers.rb +53 -0
  10. data/examples/dynamic_spec.rb +9 -0
  11. data/examples/io_processor_spec.rb +2 -2
  12. data/examples/mocking_example.rb +4 -4
  13. data/examples/partial_mock_example.rb +2 -2
  14. data/examples/predicate_example.rb +2 -2
  15. data/examples/stack_spec.rb +32 -36
  16. data/examples/stubbing_example.rb +19 -19
  17. data/examples/test_case_spec.rb +6 -6
  18. data/lib/spec.rb +3 -0
  19. data/lib/spec/callback.rb +8 -0
  20. data/lib/spec/callback/extensions/object.rb +4 -0
  21. data/lib/spec/deprecated.rb +3 -0
  22. data/lib/spec/expectations.rb +44 -17
  23. data/lib/spec/expectations/extensions.rb +1 -2
  24. data/lib/spec/expectations/extensions/object.rb +78 -130
  25. data/lib/spec/expectations/extensions/string_and_symbol.rb +17 -0
  26. data/lib/spec/expectations/handler.rb +47 -0
  27. data/lib/spec/expectations/should/base.rb +32 -29
  28. data/lib/spec/expectations/should/change.rb +1 -1
  29. data/lib/spec/expectations/should/have.rb +9 -17
  30. data/lib/spec/expectations/should/not.rb +54 -56
  31. data/lib/spec/expectations/should/should.rb +59 -65
  32. data/lib/spec/expectations/sugar.rb +27 -4
  33. data/lib/spec/matchers.rb +160 -0
  34. data/lib/spec/matchers/be.rb +161 -0
  35. data/lib/spec/matchers/be_close.rb +37 -0
  36. data/lib/spec/matchers/change.rb +120 -0
  37. data/lib/spec/matchers/eql.rb +43 -0
  38. data/lib/spec/matchers/equal.rb +43 -0
  39. data/lib/spec/matchers/has.rb +44 -0
  40. data/lib/spec/matchers/have.rb +140 -0
  41. data/lib/spec/matchers/include.rb +50 -0
  42. data/lib/spec/matchers/match.rb +41 -0
  43. data/lib/spec/matchers/raise_error.rb +100 -0
  44. data/lib/spec/matchers/respond_to.rb +35 -0
  45. data/lib/spec/matchers/satisfy.rb +47 -0
  46. data/lib/spec/matchers/throw_symbol.rb +75 -0
  47. data/lib/spec/mocks.rb +224 -1
  48. data/lib/spec/mocks/argument_expectation.rb +16 -2
  49. data/lib/spec/mocks/error_generator.rb +5 -3
  50. data/lib/spec/mocks/errors.rb +2 -2
  51. data/lib/spec/mocks/extensions/object.rb +1 -1
  52. data/lib/spec/mocks/message_expectation.rb +29 -19
  53. data/lib/spec/mocks/{mock_methods.rb → methods.rb} +5 -5
  54. data/lib/spec/mocks/mock.rb +2 -2
  55. data/lib/spec/mocks/mock_handler.rb +81 -68
  56. data/lib/spec/rake/spectask.rb +7 -12
  57. data/lib/spec/rake/verify_rcov.rb +1 -1
  58. data/lib/spec/runner.rb +117 -0
  59. data/lib/spec/runner/command_line.rb +8 -5
  60. data/lib/spec/runner/context.rb +13 -37
  61. data/lib/spec/runner/context_eval.rb +4 -3
  62. data/lib/spec/runner/context_runner.rb +7 -4
  63. data/lib/spec/runner/drb_command_line.rb +1 -1
  64. data/lib/spec/runner/execution_context.rb +3 -11
  65. data/lib/spec/runner/extensions/kernel.rb +7 -5
  66. data/lib/spec/runner/extensions/object.rb +4 -1
  67. data/lib/spec/runner/formatter/base_text_formatter.rb +11 -3
  68. data/lib/spec/runner/formatter/html_formatter.rb +21 -10
  69. data/lib/spec/runner/heckle_runner.rb +24 -8
  70. data/lib/spec/runner/heckle_runner_win.rb +10 -0
  71. data/lib/spec/runner/option_parser.rb +58 -13
  72. data/lib/spec/runner/spec_matcher.rb +22 -29
  73. data/lib/spec/runner/spec_parser.rb +1 -0
  74. data/lib/spec/runner/specification.rb +36 -22
  75. data/lib/spec/translator.rb +87 -0
  76. data/lib/spec/version.rb +16 -7
  77. data/spec/spec/callback/callback_container_spec.rb +27 -0
  78. data/spec/spec/callback/module_spec.rb +37 -0
  79. data/spec/spec/callback/object_spec.rb +90 -0
  80. data/spec/spec/callback/object_with_class_callback_spec.rb +19 -0
  81. data/spec/spec/expectations/differs/default_spec.rb +107 -0
  82. data/spec/spec/expectations/extensions/object_spec.rb +46 -0
  83. data/spec/spec/expectations/fail_with_spec.rb +71 -0
  84. data/spec/spec/expectations/should/should_==_spec.rb +19 -0
  85. data/spec/spec/expectations/should/should_=~_spec.rb +13 -0
  86. data/spec/spec/expectations/should/should_be_a_kind_of_spec.rb +21 -0
  87. data/spec/spec/expectations/should/should_be_an_instance_of_spec.rb +30 -0
  88. data/spec/spec/expectations/should/should_be_arbitrary_predicate_spec.rb +81 -0
  89. data/spec/spec/expectations/should/should_be_close_spec.rb +18 -0
  90. data/spec/spec/expectations/should/should_be_comparison_operator_spec.rb +44 -0
  91. data/spec/spec/expectations/should/should_be_false_spec.rb +39 -0
  92. data/spec/spec/expectations/should/should_be_spec.rb +11 -0
  93. data/spec/spec/expectations/should/should_be_true_spec.rb +27 -0
  94. data/spec/spec/expectations/should/should_change_spec.rb +184 -0
  95. data/spec/spec/expectations/should/should_eql_spec.rb +11 -0
  96. data/spec/spec/expectations/should/should_equal_spec.rb +11 -0
  97. data/spec/spec/expectations/should/should_have_at_least_spec.rb +53 -0
  98. data/spec/spec/expectations/should/should_have_at_most_spec.rb +45 -0
  99. data/spec/spec/expectations/should/should_have_key_spec.rb +21 -0
  100. data/spec/spec/expectations/should/should_have_spec.rb +64 -0
  101. data/spec/spec/expectations/should/should_include_spec.rb +59 -0
  102. data/spec/spec/expectations/should/should_match_spec.rb +25 -0
  103. data/spec/spec/expectations/should/should_not_==_spec.rb +15 -0
  104. data/spec/spec/expectations/should/should_not_be_a_kind_of_spec.rb +21 -0
  105. data/spec/spec/expectations/should/should_not_be_an_instance_of_spec.rb +11 -0
  106. data/spec/spec/expectations/should/should_not_be_arbitrary_predicate_spec.rb +68 -0
  107. data/spec/spec/expectations/should/should_not_be_spec.rb +11 -0
  108. data/spec/spec/expectations/should/should_not_change_spec.rb +24 -0
  109. data/spec/spec/expectations/should/should_not_eql_spec.rb +11 -0
  110. data/spec/spec/expectations/should/should_not_equal_spec.rb +11 -0
  111. data/spec/spec/expectations/should/should_not_have_key_spec.rb +15 -0
  112. data/spec/spec/expectations/should/should_not_include_spec.rb +58 -0
  113. data/spec/spec/expectations/should/should_not_match_spec.rb +11 -0
  114. data/spec/spec/expectations/should/should_not_raise_spec.rb +75 -0
  115. data/spec/spec/expectations/should/should_not_respond_to_spec.rb +15 -0
  116. data/spec/spec/expectations/should/should_not_throw_spec.rb +35 -0
  117. data/spec/spec/expectations/should/should_raise_spec.rb +66 -0
  118. data/spec/spec/expectations/should/should_respond_to_spec.rb +15 -0
  119. data/spec/spec/expectations/should/should_satisfy_spec.rb +35 -0
  120. data/spec/spec/expectations/should/should_throw_spec.rb +27 -0
  121. data/spec/spec/matchers/be_close_spec.rb +33 -0
  122. data/spec/spec/matchers/be_spec.rb +182 -0
  123. data/spec/spec/matchers/change_spec.rb +232 -0
  124. data/spec/spec/matchers/description_generation_spec.rb +147 -0
  125. data/spec/spec/matchers/eql_spec.rb +41 -0
  126. data/spec/spec/matchers/equal_spec.rb +41 -0
  127. data/spec/spec/matchers/handler_spec.rb +75 -0
  128. data/spec/spec/matchers/has_spec.rb +37 -0
  129. data/spec/spec/matchers/have_spec.rb +259 -0
  130. data/spec/spec/matchers/include_spec.rb +33 -0
  131. data/spec/spec/matchers/match_spec.rb +37 -0
  132. data/spec/spec/matchers/matcher_methods_spec.rb +85 -0
  133. data/spec/spec/matchers/raise_error_spec.rb +147 -0
  134. data/spec/spec/matchers/respond_to_spec.rb +30 -0
  135. data/spec/spec/matchers/satisfy_spec.rb +36 -0
  136. data/spec/spec/matchers/throw_symbol_spec.rb +59 -0
  137. data/spec/spec/mocks/any_number_of_times_spec.rb +34 -0
  138. data/spec/spec/mocks/at_least_spec.rb +97 -0
  139. data/spec/spec/mocks/at_most_spec.rb +97 -0
  140. data/spec/spec/mocks/bug_report_7611_spec.rb +19 -0
  141. data/spec/spec/mocks/bug_report_7805_spec.rb +22 -0
  142. data/spec/spec/mocks/bug_report_8165_spec.rb +31 -0
  143. data/spec/spec/mocks/bug_report_8302_spec.rb +26 -0
  144. data/spec/spec/mocks/failing_mock_argument_constraints_spec.rb +74 -0
  145. data/spec/spec/mocks/mock_ordering_spec.rb +80 -0
  146. data/spec/spec/mocks/mock_spec.rb +407 -0
  147. data/spec/spec/mocks/multiple_return_value_spec.rb +113 -0
  148. data/spec/spec/mocks/null_object_mock_spec.rb +40 -0
  149. data/spec/spec/mocks/once_counts_spec.rb +56 -0
  150. data/spec/spec/mocks/options_hash_spec.rb +31 -0
  151. data/spec/spec/mocks/partial_mock_spec.rb +52 -0
  152. data/spec/spec/mocks/partial_mock_using_mocks_directly_spec.rb +64 -0
  153. data/spec/spec/mocks/passing_mock_argument_constraints_spec.rb +92 -0
  154. data/spec/spec/mocks/precise_counts_spec.rb +56 -0
  155. data/spec/spec/mocks/record_messages_spec.rb +26 -0
  156. data/spec/spec/mocks/stub_spec.rb +159 -0
  157. data/spec/spec/mocks/twice_counts_spec.rb +67 -0
  158. data/spec/spec/runner/command_line_spec.rb +32 -0
  159. data/spec/spec/runner/context_matching_spec.rb +28 -0
  160. data/spec/spec/runner/context_runner_spec.rb +100 -0
  161. data/spec/spec/runner/context_spec.rb +405 -0
  162. data/spec/spec/runner/drb_command_line_spec.rb +74 -0
  163. data/spec/spec/runner/execution_context_spec.rb +52 -0
  164. data/spec/spec/runner/formatter/html_formatter_spec.rb +40 -0
  165. data/spec/spec/runner/formatter/progress_bar_formatter_dry_run_spec.rb +21 -0
  166. data/spec/spec/runner/formatter/progress_bar_formatter_failure_dump_spec.rb +36 -0
  167. data/spec/spec/runner/formatter/progress_bar_formatter_spec.rb +78 -0
  168. data/spec/spec/runner/formatter/rdoc_formatter_dry_run_spec.rb +18 -0
  169. data/spec/spec/runner/formatter/rdoc_formatter_spec.rb +41 -0
  170. data/spec/spec/runner/formatter/specdoc_formatter_dry_run_spec.rb +21 -0
  171. data/spec/spec/runner/formatter/specdoc_formatter_spec.rb +46 -0
  172. data/spec/spec/runner/heckle_runner_spec.rb +63 -0
  173. data/spec/spec/runner/heckler_spec.rb +14 -0
  174. data/spec/spec/runner/kernel_ext_spec.rb +16 -0
  175. data/spec/spec/runner/noisy_backtrace_tweaker_spec.rb +45 -0
  176. data/spec/spec/runner/object_ext_spec.rb +11 -0
  177. data/spec/spec/runner/option_parser_spec.rb +269 -0
  178. data/spec/spec/runner/quiet_backtrace_tweaker_spec.rb +47 -0
  179. data/spec/spec/runner/reporter_spec.rb +126 -0
  180. data/spec/spec/runner/spec_matcher_spec.rb +107 -0
  181. data/spec/spec/runner/spec_name_generation_spec.rb +102 -0
  182. data/spec/spec/runner/spec_parser_spec.rb +37 -0
  183. data/spec/spec/runner/specification_class_spec.rb +72 -0
  184. data/spec/spec/runner/specification_instance_spec.rb +160 -0
  185. data/spec/spec/runner/specification_should_raise_spec.rb +136 -0
  186. data/spec/spec/spec_classes.rb +102 -0
  187. data/spec/spec/translator_spec.rb +79 -0
  188. data/spec/spec_helper.rb +35 -0
  189. metadata +141 -9
  190. data/bin/drbspec +0 -3
  191. data/lib/spec/expectations/diff.rb +0 -28
  192. data/lib/spec/expectations/extensions/numeric.rb +0 -19
  193. data/lib/spec/expectations/extensions/string.rb +0 -22
  194. data/lib/spec/expectations/message_builder.rb +0 -13
@@ -1,76 +1,74 @@
1
1
  module Spec
2
2
  module Expectations
3
3
  module Should
4
- class Not < Base
5
-
4
+
5
+ class Not < Base #:nodoc:
6
6
  def initialize(target)
7
7
  @target = target
8
8
  @be_seen = false
9
9
  end
10
10
 
11
- def have(expected_number=nil)
12
- NotHave.new(@target, :exactly, expected_number)
13
- end
14
-
15
- def change(receiver, message)
16
- NotChange.new(@target, receiver, message)
17
- end
18
-
19
- def satisfy
20
- fail_with_message "Supplied expectation was satisfied, but should not have been" if (yield @target)
21
- end
22
-
23
- def be(expected = :no_arg)
24
- @be_seen = true
25
- return self if (expected == :no_arg)
26
- fail_with_message(default_message("should not be", expected)) if (@target.equal?(expected))
27
- end
11
+ deprecated do
12
+ #Gone for 0.9
13
+ def be(expected = :___no_arg)
14
+ @be_seen = true
15
+ return self if (expected == :___no_arg)
16
+ fail_with_message(default_message("should not be", expected)) if (@target.equal?(expected))
17
+ end
28
18
 
29
- def an_instance_of expected_class
30
- fail_with_message(default_message("should not be an instance of", expected_class)) if @target.instance_of? expected_class
31
- end
32
-
33
- def a_kind_of expected_class
34
- fail_with_message(default_message("should not be a kind of", expected_class)) if @target.kind_of? expected_class
35
- end
19
+ #Gone for 0.9
20
+ def have(expected_number=nil)
21
+ NotHave.new(@target, :exactly, expected_number)
22
+ end
36
23
 
37
- def respond_to message
38
- fail_with_message(default_message("should not respond to", message)) if @target.respond_to? message
39
- end
40
-
41
- def match(expected)
42
- fail_with_message(default_message("should not match", expected)) if (@target =~ expected)
43
- end
44
-
45
- def raise(exception=Exception, message=nil)
46
- begin
47
- @target.call
48
- rescue exception => e
49
- return unless message.nil? || e.message == message || (message.is_a?(Regexp) && e.message =~ message)
50
- fail_with_message("#{default_message("should not raise", exception)}") if e.instance_of? exception
51
- fail_with_message("#{default_message("should not raise", exception)} but raised #{e.inspect}") unless e.instance_of? exception
52
- rescue
53
- true
24
+ #Gone for 0.9
25
+ def change(receiver, message)
26
+ NotChange.new(@target, receiver, message)
54
27
  end
55
- end
56
-
57
- def throw(symbol=:___this_is_a_symbol_that_will_likely_never_occur___)
58
- begin
59
- catch symbol do
28
+
29
+ #Gone for 0.9
30
+ def raise(exception=Exception, message=nil)
31
+ begin
60
32
  @target.call
61
- return true
33
+ rescue exception => e
34
+ return unless message.nil? || e.message == message || (message.is_a?(Regexp) && e.message =~ message)
35
+ if e.kind_of?(exception)
36
+ failure_message = "expected no "
37
+ failure_message << exception.to_s
38
+ unless message.nil?
39
+ failure_message << " with "
40
+ failure_message << "message matching " if message.is_a?(Regexp)
41
+ failure_message << message.inspect
42
+ end
43
+ failure_message << ", got " << e.inspect
44
+ fail_with_message(failure_message)
45
+ end
46
+ rescue
47
+ true
48
+ end
49
+ end
50
+
51
+ #Gone for 0.9
52
+ def throw(symbol=:___this_is_a_symbol_that_will_likely_never_occur___)
53
+ begin
54
+ catch symbol do
55
+ @target.call
56
+ return true
57
+ end
58
+ fail_with_message("expected #{symbol.inspect} not to be thrown, but it was")
59
+ rescue NameError
60
+ true
62
61
  end
63
- fail_with_message(default_message("should not throw", symbol.inspect))
64
- rescue NameError
65
- true
66
62
  end
67
- end
68
63
 
69
- def __delegate_method_missing_to_target original_sym, actual_sym, *args
70
- return unless @target.__send__(actual_sym, *args)
71
- fail_with_message(default_message("should not#{@be_seen ? ' be' : ''} #{original_sym}" + (args.empty? ? '' : ' ' + args[0].inspect)))
64
+ def __delegate_method_missing_to_target original_sym, actual_sym, *args
65
+ ::Spec::Matchers.generated_description = "should not #{original_sym} #{args[0].inspect}"
66
+ return unless @target.__send__(actual_sym, *args)
67
+ fail_with_message(default_message("not #{original_sym}", args[0]))
68
+ end
72
69
  end
73
70
  end
71
+
74
72
  end
75
73
  end
76
74
  end
@@ -1,87 +1,81 @@
1
1
  module Spec
2
2
  module Expectations
3
3
  module Should # :nodoc:
4
+
4
5
  class Should < Base
5
6
 
6
- def initialize(target)
7
+ def initialize(target, expectation=nil)
7
8
  @target = target
8
9
  @be_seen = false
9
10
  end
10
11
 
11
- def have(expected_number=nil)
12
- Have.new(@target, :exactly, expected_number)
13
- end
14
-
15
- def change(receiver=nil, message=nil, &block)
16
- Change.new(@target, receiver, message, &block)
17
- end
18
-
19
- def not
20
- Not.new(@target)
21
- end
22
-
23
- def satisfy
24
- return if yield(@target)
25
- fail_with_message "Supplied expectation was not satisfied"
26
- end
27
-
28
- def be(expected = :___no_arg)
29
- @be_seen = true
30
- return self if (expected == :___no_arg)
31
- fail_with_message(default_message("should be", expected)) unless (@target.equal?(expected))
32
- end
33
-
34
- def an_instance_of(expected_class)
35
- fail_with_message(default_message("should be an instance of", expected_class)) unless @target.instance_of? expected_class
36
- end
37
-
38
- def a_kind_of(expected_class)
39
- fail_with_message(default_message("should be a kind of", expected_class)) unless @target.kind_of? expected_class
40
- end
41
-
42
- def respond_to(message)
43
- fail_with_message(default_message("should respond to", message)) unless @target.respond_to? message
44
- end
45
-
46
- def __delegate_method_missing_to_target(original_sym, actual_sym, *args)
47
- return if @target.send(actual_sym, *args)
48
- message = default_message("should#{@be_seen ? ' be' : ''} #{original_sym}", args[0])
49
- fail_with_message(message)
50
- end
12
+ deprecated do
13
+ #Gone for 0.9
14
+ def not
15
+ Not.new(@target)
16
+ end
17
+
18
+ #Gone for 0.9
19
+ def be(expected = :___no_arg)
20
+ @be_seen = true
21
+ return self if (expected == :___no_arg)
22
+ if Symbol === expected
23
+ fail_with_message(default_message("should be", expected)) unless (@target.equal?(expected))
24
+ else
25
+ fail_with_message("expected #{expected}, got #{@target} (using .equal?)") unless (@target.equal?(expected))
26
+ end
27
+ end
28
+
29
+ #Gone for 0.9
30
+ def have(expected_number=nil)
31
+ Have.new(@target, :exactly, expected_number)
32
+ end
51
33
 
52
- def match(expected)
53
- fail_with_message(default_message("should match", expected)) unless (@target =~ expected)
54
- end
34
+ #Gone for 0.9
35
+ def change(receiver=nil, message=nil, &block)
36
+ Change.new(@target, receiver, message, &block)
37
+ end
55
38
 
56
- def raise(exception=Exception, message=nil)
57
- begin
58
- @target.call
59
- rescue exception => e
60
- unless message.nil?
61
- if message.is_a?(Regexp)
62
- e.message.should =~ message
63
- else
64
- e.message.should == message
39
+ #Gone for 0.9
40
+ def raise(exception=Exception, message=nil)
41
+ begin
42
+ @target.call
43
+ rescue exception => e
44
+ unless message.nil?
45
+ if message.is_a?(Regexp)
46
+ e.message.should =~ message
47
+ else
48
+ e.message.should == message
49
+ end
65
50
  end
51
+ return
52
+ rescue => e
53
+ fail_with_message("expected #{exception}#{message.nil? ? "" : " with #{message.inspect}"}, got #{e.inspect}")
66
54
  end
67
- return
68
- rescue => e
69
- fail_with_message("#{default_message("should raise", exception)} but raised #{e.inspect}")
55
+ fail_with_message("expected #{exception}#{message.nil? ? "" : " with #{message.inspect}"} but nothing was raised")
70
56
  end
71
- fail_with_message("#{default_message("should raise", exception)} but raised nothing")
72
- end
73
57
 
74
- def throw(symbol)
75
- begin
76
- catch symbol do
77
- @target.call
78
- fail_with_message(default_message("should throw", symbol.inspect))
58
+ #Gone for 0.9
59
+ def throw(symbol)
60
+ begin
61
+ catch symbol do
62
+ @target.call
63
+ fail_with_message("expected #{symbol.inspect} to be thrown, but nothing was thrown")
64
+ end
65
+ rescue NameError => e
66
+ fail_with_message("expected #{symbol.inspect} to be thrown, got #{e.inspect}")
79
67
  end
80
- rescue NameError
81
- fail_with_message(default_message("should throw", symbol.inspect))
82
68
  end
83
69
  end
70
+
71
+ private
72
+ def __delegate_method_missing_to_target(original_sym, actual_sym, *args)
73
+ ::Spec::Matchers.generated_description = "should #{original_sym} #{args[0].inspect}"
74
+ return if @target.send(actual_sym, *args)
75
+ fail_with_message(default_message(original_sym, args[0]), args[0], @target)
76
+ end
84
77
  end
78
+
85
79
  end
86
80
  end
87
81
  end
@@ -1,8 +1,9 @@
1
+ deprecated do
1
2
  module Spec
2
3
  module Expectations
3
4
  # This module adds syntactic sugar that allows usage of should_* instead of should.*
4
- module UnderscoreSugar # :nodoc:
5
- def handle_underscores_for_rspec!
5
+ module UnderscoreSugar
6
+ def handle_underscores_for_rspec! # :nodoc:
6
7
  original_method_missing = instance_method(:method_missing)
7
8
  class_eval do
8
9
  def method_missing(sym, *args, &block)
@@ -11,14 +12,36 @@ module Spec
11
12
 
12
13
  define_method :_method_missing do |sym, args, block|
13
14
  return original_method_missing.bind(self).call(sym, *args, &block) unless sym.to_s =~ /^should_/
14
- return Spec::Expectations::Should::Should.new(self).__send__(__strip_should(sym), *args, &block)
15
+ if sym.to_s =~ /^should_not_/
16
+ if __matcher.respond_to?(__strip_should_not(sym))
17
+ return should_not(__matcher.__send__(__strip_should_not(sym), *args, &block))
18
+ else
19
+ return Spec::Expectations::Should::Not.new(self).__send__(__strip_should_not(sym), *args, &block) if sym.to_s =~ /^should_not_/
20
+ end
21
+ else
22
+ if __matcher.respond_to?(__strip_should(sym))
23
+ return should(__matcher.__send__(__strip_should(sym), *args, &block))
24
+ else
25
+ return Spec::Expectations::Should::Should.new(self).__send__(__strip_should(sym), *args, &block)
26
+ end
27
+ end
15
28
  end
16
29
 
17
- def __strip_should(sym) # :nodoc
30
+ def __strip_should(sym) # :nodoc
18
31
  sym.to_s[7..-1]
19
32
  end
33
+
34
+ def __strip_should_not(sym) # :nodoc
35
+ sym.to_s[11..-1]
36
+ end
37
+
38
+ def __matcher
39
+ @matcher ||= Spec::Matchers::Matcher.new
40
+ end
20
41
  end
21
42
  end
22
43
  end
23
44
  end
45
+ end
46
+
24
47
  end
@@ -0,0 +1,160 @@
1
+ require 'spec/deprecated'
2
+ require 'spec/callback'
3
+ require 'spec/matchers/be'
4
+ require 'spec/matchers/be_close'
5
+ require 'spec/matchers/change'
6
+ require 'spec/matchers/eql'
7
+ require 'spec/matchers/equal'
8
+ require 'spec/matchers/has'
9
+ require 'spec/matchers/have'
10
+ require 'spec/matchers/include'
11
+ require 'spec/matchers/match'
12
+ require 'spec/matchers/raise_error'
13
+ require 'spec/matchers/respond_to'
14
+ require 'spec/matchers/satisfy'
15
+ require 'spec/matchers/throw_symbol'
16
+
17
+ module Spec
18
+
19
+ # RSpec ships with a number of useful Expression Matchers. An Expression Matcher
20
+ # is any object that responds to the following methods:
21
+ #
22
+ # matches?(actual)
23
+ # failure_message
24
+ # negative_failure_message #optional
25
+ # description #optional
26
+ #
27
+ # See Spec::Expectations to learn how to use these as Expectation Matchers.
28
+ # See Spec::Mocks to learn how to use them as Mock Argument Constraints.
29
+ #
30
+ # == Predicates
31
+ #
32
+ # In addition to those Expression Matchers that are defined explicitly, RSpec will
33
+ # create custom Matchers on the fly for any arbitrary predicate, giving your specs
34
+ # a much more natural language feel.
35
+ #
36
+ # A Ruby predicate is a method that ends with a "?" and returns true or false.
37
+ # Common examples are +empty?+, +nil?+, and +instance_of?+.
38
+ #
39
+ # All you need to do is write +should be_+ followed by the predicate without
40
+ # the question mark, and RSpec will figure it out from there. For example:
41
+ #
42
+ # [].should be_empty => [].empty? #passes
43
+ # [].should_not be_empty => [].empty? #fails
44
+ #
45
+ # In addtion to prefixing the predicate matchers with "be_", you can also use "be_a_"
46
+ # and "be_an_", making your specs read much more naturally:
47
+ #
48
+ # "a string".should be_an_instance_of(String) =>"a string".instance_of?(String) #passes
49
+ #
50
+ # 3.should be_a_kind_of(Fixnum) => 3.kind_of?(Numeric) #passes
51
+ # 3.should be_a_kind_of(Numeric) => 3.kind_of?(Numeric) #passes
52
+ # 3.should be_an_instance_of(Fixnum) => 3.instance_of?(Fixnum) #passes
53
+ # 3.should_not be_instance_of(Numeric) => 3.instance_of?(Numeric) #fails
54
+ #
55
+ # RSpec will also create custom matchers for predicates like +has_key?+. To
56
+ # use this feature, just state that the object should have_key(:key) and RSpec will
57
+ # call has_key?(:key) on the target. For example:
58
+ #
59
+ # {:a => "A"}.should have_key(:a) => {:a => "A"}.has_key?(:a) #passes
60
+ # {:a => "A"}.should have_key(:b) => {:a => "A"}.has_key?(:b) #fails
61
+ #
62
+ # You can use this feature to invoke any predicate that begins with "has_", whether it is
63
+ # part of the Ruby libraries (like +Hash#has_key?+) or a method you wrote on your own class.
64
+ #
65
+ # == Custom Expression Matchers
66
+ #
67
+ # When you find that none of the stock Expression Matchers provide a natural
68
+ # feeling expectation, you can very easily write your own.
69
+ #
70
+ # For example, imagine that you are writing a game in which players can
71
+ # be in various zones on a virtual board. To specify that bob should
72
+ # be in zone 4, you could say:
73
+ #
74
+ # bob.current_zone.should eql(Zone.new("4"))
75
+ #
76
+ # But you might find it more expressive to say:
77
+ #
78
+ # bob.should be_in_zone("4")
79
+ #
80
+ # and/or
81
+ #
82
+ # bob.should_not be_in_zone("3")
83
+ #
84
+ # To do this, you would need to write a class like this:
85
+ #
86
+ # class BeInZone
87
+ # def initialize(expected)
88
+ # @expected = expected
89
+ # end
90
+ # def matches?(actual)
91
+ # @actual = actual
92
+ # bob.current_zone.eql?(Zone.new(@expected))
93
+ # end
94
+ # def failure_message
95
+ # "expected #{@actual.inspect} to be in Zone #{@expected}"
96
+ # end
97
+ # def negative_failure_message
98
+ # "expected #{@actual.inspect} not to be in Zone #{@expected}"
99
+ # end
100
+ # end
101
+ #
102
+ # ... and a method like this:
103
+ #
104
+ # def be_in_zone(expected)
105
+ # BeInZone.new(expected)
106
+ # end
107
+ #
108
+ # And then expose the method to your specs. This is normally done
109
+ # by including the method and the class in a module, which is then
110
+ # included in your spec:
111
+ #
112
+ # module CustomGameMatchers
113
+ # class BeInZone
114
+ # ...
115
+ # end
116
+ #
117
+ # def be_in_zone(expected)
118
+ # ...
119
+ # end
120
+ # end
121
+ #
122
+ # context "Player behaviour" do
123
+ # include CustomGameMatchers
124
+ # ...
125
+ # end
126
+ module Matchers
127
+
128
+ class << self
129
+ callback_events :description_generated
130
+ def generated_description=(name)
131
+ notify_callbacks(:description_generated, name)
132
+ end
133
+ end
134
+
135
+ def method_missing(sym, *args, &block) # :nodoc:
136
+ return Matchers::Be.new(sym, *args) if sym.starts_with?("be_")
137
+ return Matchers::Has.new(sym, *args) if sym.starts_with?("have_")
138
+ super
139
+ end
140
+
141
+ deprecated do
142
+ # This supports sugar delegating to Matchers
143
+ class Matcher #:nodoc:
144
+ include Matchers
145
+
146
+ def respond_to?(sym)
147
+ if sym.to_s[0..2] == "be_"
148
+ return true
149
+ else
150
+ super
151
+ end
152
+ end
153
+ end
154
+ end
155
+
156
+ class MatcherError < StandardError
157
+ end
158
+
159
+ end
160
+ end