newbamboo-rspec 1.1.12

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 (324) hide show
  1. data/.autotest +4 -0
  2. data/History.txt +1263 -0
  3. data/License.txt +22 -0
  4. data/Manifest.txt +327 -0
  5. data/README.txt +57 -0
  6. data/Rakefile +104 -0
  7. data/TODO.txt +18 -0
  8. data/bin/autospec +4 -0
  9. data/bin/spec +4 -0
  10. data/examples/failing/README.txt +7 -0
  11. data/examples/failing/diffing_spec.rb +36 -0
  12. data/examples/failing/failing_autogenerated_docstrings_example.rb +19 -0
  13. data/examples/failing/failure_in_after.rb +10 -0
  14. data/examples/failing/failure_in_before.rb +10 -0
  15. data/examples/failing/mocking_example.rb +40 -0
  16. data/examples/failing/mocking_with_flexmock.rb +26 -0
  17. data/examples/failing/mocking_with_mocha.rb +25 -0
  18. data/examples/failing/mocking_with_rr.rb +27 -0
  19. data/examples/failing/partial_mock_example.rb +20 -0
  20. data/examples/failing/predicate_example.rb +34 -0
  21. data/examples/failing/raising_example.rb +47 -0
  22. data/examples/failing/spec_helper.rb +3 -0
  23. data/examples/failing/syntax_error_example.rb +7 -0
  24. data/examples/failing/team_spec.rb +44 -0
  25. data/examples/failing/timeout_behaviour.rb +7 -0
  26. data/examples/passing/autogenerated_docstrings_example.rb +25 -0
  27. data/examples/passing/before_and_after_example.rb +40 -0
  28. data/examples/passing/behave_as_example.rb +45 -0
  29. data/examples/passing/custom_expectation_matchers.rb +54 -0
  30. data/examples/passing/custom_formatter.rb +12 -0
  31. data/examples/passing/dynamic_spec.rb +9 -0
  32. data/examples/passing/file_accessor.rb +19 -0
  33. data/examples/passing/file_accessor_spec.rb +38 -0
  34. data/examples/passing/greeter_spec.rb +31 -0
  35. data/examples/passing/helper_method_example.rb +14 -0
  36. data/examples/passing/io_processor.rb +8 -0
  37. data/examples/passing/io_processor_spec.rb +21 -0
  38. data/examples/passing/legacy_spec.rb +11 -0
  39. data/examples/passing/mocking_example.rb +27 -0
  40. data/examples/passing/multi_threaded_example_group_runner.rb +26 -0
  41. data/examples/passing/nested_classes_example.rb +36 -0
  42. data/examples/passing/partial_mock_example.rb +29 -0
  43. data/examples/passing/pending_example.rb +20 -0
  44. data/examples/passing/predicate_example.rb +27 -0
  45. data/examples/passing/priority.txt +1 -0
  46. data/examples/passing/shared_example_group_example.rb +81 -0
  47. data/examples/passing/shared_stack_examples.rb +36 -0
  48. data/examples/passing/simple_matcher_example.rb +31 -0
  49. data/examples/passing/spec_helper.rb +3 -0
  50. data/examples/passing/stack.rb +36 -0
  51. data/examples/passing/stack_spec.rb +64 -0
  52. data/examples/passing/stack_spec_with_nested_example_groups.rb +67 -0
  53. data/examples/passing/stubbing_example.rb +69 -0
  54. data/examples/passing/yielding_example.rb +33 -0
  55. data/examples/ruby1.9.compatibility/access_to_constants_spec.rb +86 -0
  56. data/features/before_and_after_blocks/before_and_after_blocks.feature +168 -0
  57. data/features/example_groups/autogenerated_docstrings.feature +45 -0
  58. data/features/example_groups/example_group_with_should_methods.feature +17 -0
  59. data/features/example_groups/nested_groups.feature +17 -0
  60. data/features/example_groups/output.feature +20 -0
  61. data/features/interop/examples_and_tests_together.feature +31 -0
  62. data/features/interop/test_but_not_test_unit.feature +14 -0
  63. data/features/interop/test_case_with_should_methods.feature +17 -0
  64. data/features/mock_framework_integration/use_flexmock.feature +27 -0
  65. data/features/step_definitions/running_rspec.rb +54 -0
  66. data/features/support/env.rb +26 -0
  67. data/features/support/helpers/cmdline.rb +9 -0
  68. data/features/support/matchers/smart_match.rb +37 -0
  69. data/init.rb +9 -0
  70. data/lib/adapters/mock_frameworks/flexmock.rb +24 -0
  71. data/lib/adapters/mock_frameworks/mocha.rb +20 -0
  72. data/lib/adapters/mock_frameworks/rr.rb +22 -0
  73. data/lib/adapters/mock_frameworks/rspec.rb +20 -0
  74. data/lib/autotest/discover.rb +3 -0
  75. data/lib/autotest/rspec.rb +47 -0
  76. data/lib/spec/dsl/main.rb +87 -0
  77. data/lib/spec/dsl.rb +1 -0
  78. data/lib/spec/example/before_and_after_hooks.rb +99 -0
  79. data/lib/spec/example/errors.rb +30 -0
  80. data/lib/spec/example/example_group.rb +26 -0
  81. data/lib/spec/example/example_group_factory.rb +98 -0
  82. data/lib/spec/example/example_group_methods.rb +376 -0
  83. data/lib/spec/example/example_matcher.rb +44 -0
  84. data/lib/spec/example/example_methods.rb +137 -0
  85. data/lib/spec/example/module_reopening_fix.rb +21 -0
  86. data/lib/spec/example/pending.rb +18 -0
  87. data/lib/spec/example/shared_example_group.rb +59 -0
  88. data/lib/spec/example.rb +187 -0
  89. data/lib/spec/expectations/differs/default.rb +66 -0
  90. data/lib/spec/expectations/errors.rb +12 -0
  91. data/lib/spec/expectations/extensions/object.rb +63 -0
  92. data/lib/spec/expectations/extensions/string_and_symbol.rb +17 -0
  93. data/lib/spec/expectations/extensions.rb +2 -0
  94. data/lib/spec/expectations/handler.rb +32 -0
  95. data/lib/spec/expectations.rb +60 -0
  96. data/lib/spec/interop/test/unit/autorunner.rb +6 -0
  97. data/lib/spec/interop/test/unit/testcase.rb +73 -0
  98. data/lib/spec/interop/test/unit/testresult.rb +6 -0
  99. data/lib/spec/interop/test/unit/testsuite_adapter.rb +34 -0
  100. data/lib/spec/interop/test/unit/ui/console/testrunner.rb +61 -0
  101. data/lib/spec/interop/test.rb +12 -0
  102. data/lib/spec/matchers/be.rb +208 -0
  103. data/lib/spec/matchers/be_close.rb +21 -0
  104. data/lib/spec/matchers/change.rb +148 -0
  105. data/lib/spec/matchers/eql.rb +25 -0
  106. data/lib/spec/matchers/equal.rb +25 -0
  107. data/lib/spec/matchers/errors.rb +5 -0
  108. data/lib/spec/matchers/exist.rb +16 -0
  109. data/lib/spec/matchers/generated_descriptions.rb +48 -0
  110. data/lib/spec/matchers/has.rb +15 -0
  111. data/lib/spec/matchers/have.rb +150 -0
  112. data/lib/spec/matchers/include.rb +80 -0
  113. data/lib/spec/matchers/match.rb +22 -0
  114. data/lib/spec/matchers/match_array.rb +79 -0
  115. data/lib/spec/matchers/method_missing.rb +9 -0
  116. data/lib/spec/matchers/operator_matcher.rb +70 -0
  117. data/lib/spec/matchers/raise_error.rb +128 -0
  118. data/lib/spec/matchers/respond_to.rb +71 -0
  119. data/lib/spec/matchers/satisfy.rb +47 -0
  120. data/lib/spec/matchers/simple_matcher.rb +132 -0
  121. data/lib/spec/matchers/throw_symbol.rb +106 -0
  122. data/lib/spec/matchers/wrap_expectation.rb +55 -0
  123. data/lib/spec/matchers.rb +141 -0
  124. data/lib/spec/mocks/argument_expectation.rb +49 -0
  125. data/lib/spec/mocks/argument_matchers.rb +237 -0
  126. data/lib/spec/mocks/error_generator.rb +82 -0
  127. data/lib/spec/mocks/errors.rb +10 -0
  128. data/lib/spec/mocks/extensions/object.rb +3 -0
  129. data/lib/spec/mocks/extensions.rb +1 -0
  130. data/lib/spec/mocks/framework.rb +15 -0
  131. data/lib/spec/mocks/message_expectation.rb +331 -0
  132. data/lib/spec/mocks/methods.rb +51 -0
  133. data/lib/spec/mocks/mock.rb +55 -0
  134. data/lib/spec/mocks/order_group.rb +29 -0
  135. data/lib/spec/mocks/proxy.rb +230 -0
  136. data/lib/spec/mocks/space.rb +28 -0
  137. data/lib/spec/mocks/spec_methods.rb +55 -0
  138. data/lib/spec/mocks.rb +200 -0
  139. data/lib/spec/rake/spectask.rb +225 -0
  140. data/lib/spec/rake/verify_rcov.rb +52 -0
  141. data/lib/spec/ruby.rb +9 -0
  142. data/lib/spec/runner/backtrace_tweaker.rb +58 -0
  143. data/lib/spec/runner/class_and_arguments_parser.rb +14 -0
  144. data/lib/spec/runner/command_line.rb +35 -0
  145. data/lib/spec/runner/configuration.rb +166 -0
  146. data/lib/spec/runner/drb_command_line.rb +20 -0
  147. data/lib/spec/runner/example_group_runner.rb +70 -0
  148. data/lib/spec/runner/formatter/base_formatter.rb +79 -0
  149. data/lib/spec/runner/formatter/base_text_formatter.rb +127 -0
  150. data/lib/spec/runner/formatter/failing_example_groups_formatter.rb +27 -0
  151. data/lib/spec/runner/formatter/failing_examples_formatter.rb +20 -0
  152. data/lib/spec/runner/formatter/html_formatter.rb +340 -0
  153. data/lib/spec/runner/formatter/nested_text_formatter.rb +65 -0
  154. data/lib/spec/runner/formatter/profile_formatter.rb +51 -0
  155. data/lib/spec/runner/formatter/progress_bar_formatter.rb +34 -0
  156. data/lib/spec/runner/formatter/snippet_extractor.rb +52 -0
  157. data/lib/spec/runner/formatter/specdoc_formatter.rb +39 -0
  158. data/lib/spec/runner/formatter/text_mate_formatter.rb +16 -0
  159. data/lib/spec/runner/heckle_runner.rb +74 -0
  160. data/lib/spec/runner/heckle_runner_unsupported.rb +10 -0
  161. data/lib/spec/runner/option_parser.rb +204 -0
  162. data/lib/spec/runner/options.rb +320 -0
  163. data/lib/spec/runner/reporter.rb +167 -0
  164. data/lib/spec/runner/spec_parser.rb +73 -0
  165. data/lib/spec/runner.rb +88 -0
  166. data/lib/spec/version.rb +15 -0
  167. data/lib/spec.rb +7 -0
  168. data/resources/rake/examples.rake +7 -0
  169. data/resources/rake/examples_with_rcov.rake +9 -0
  170. data/resources/rake/failing_examples_with_html.rake +9 -0
  171. data/resources/rake/verify_rcov.rake +7 -0
  172. data/resources/spec/example_group_with_should_methods.rb +12 -0
  173. data/resources/spec/simple_spec.rb +8 -0
  174. data/resources/test/spec_and_test_together.rb +57 -0
  175. data/resources/test/spec_including_test_but_not_unit.rb +11 -0
  176. data/resources/test/test_case_with_should_methods.rb +30 -0
  177. data/rspec.gemspec +32 -0
  178. data/spec/README.jruby +15 -0
  179. data/spec/autotest/autotest_helper.rb +6 -0
  180. data/spec/autotest/autotest_matchers.rb +47 -0
  181. data/spec/autotest/discover_spec.rb +8 -0
  182. data/spec/autotest/failed_results_re_spec.rb +24 -0
  183. data/spec/autotest/rspec_spec.rb +114 -0
  184. data/spec/rspec_suite.rb +6 -0
  185. data/spec/ruby_forker.rb +13 -0
  186. data/spec/spec/dsl/main_spec.rb +88 -0
  187. data/spec/spec/example/example_group_class_definition_spec.rb +49 -0
  188. data/spec/spec/example/example_group_factory_spec.rb +224 -0
  189. data/spec/spec/example/example_group_methods_spec.rb +653 -0
  190. data/spec/spec/example/example_group_spec.rb +661 -0
  191. data/spec/spec/example/example_matcher_spec.rb +79 -0
  192. data/spec/spec/example/example_methods_spec.rb +204 -0
  193. data/spec/spec/example/helper_method_spec.rb +24 -0
  194. data/spec/spec/example/nested_example_group_spec.rb +71 -0
  195. data/spec/spec/example/pending_module_spec.rb +139 -0
  196. data/spec/spec/example/predicate_matcher_spec.rb +21 -0
  197. data/spec/spec/example/shared_example_group_spec.rb +257 -0
  198. data/spec/spec/example/subclassing_example_group_spec.rb +25 -0
  199. data/spec/spec/expectations/differs/default_spec.rb +127 -0
  200. data/spec/spec/expectations/extensions/object_spec.rb +45 -0
  201. data/spec/spec/expectations/fail_with_spec.rb +71 -0
  202. data/spec/spec/expectations/wrap_expectation_spec.rb +30 -0
  203. data/spec/spec/interop/test/unit/resources/spec_that_fails.rb +10 -0
  204. data/spec/spec/interop/test/unit/resources/spec_that_passes.rb +10 -0
  205. data/spec/spec/interop/test/unit/resources/spec_with_errors.rb +10 -0
  206. data/spec/spec/interop/test/unit/resources/spec_with_options_hash.rb +13 -0
  207. data/spec/spec/interop/test/unit/resources/test_case_that_fails.rb +10 -0
  208. data/spec/spec/interop/test/unit/resources/test_case_that_passes.rb +10 -0
  209. data/spec/spec/interop/test/unit/resources/test_case_with_errors.rb +10 -0
  210. data/spec/spec/interop/test/unit/resources/testsuite_adapter_spec_with_test_unit.rb +38 -0
  211. data/spec/spec/interop/test/unit/spec_spec.rb +48 -0
  212. data/spec/spec/interop/test/unit/test_unit_spec_helper.rb +18 -0
  213. data/spec/spec/interop/test/unit/testcase_spec.rb +49 -0
  214. data/spec/spec/interop/test/unit/testsuite_adapter_spec.rb +9 -0
  215. data/spec/spec/matchers/be_close_spec.rb +41 -0
  216. data/spec/spec/matchers/be_spec.rb +303 -0
  217. data/spec/spec/matchers/change_spec.rb +329 -0
  218. data/spec/spec/matchers/description_generation_spec.rb +172 -0
  219. data/spec/spec/matchers/eql_spec.rb +29 -0
  220. data/spec/spec/matchers/equal_spec.rb +29 -0
  221. data/spec/spec/matchers/exist_spec.rb +57 -0
  222. data/spec/spec/matchers/handler_spec.rb +111 -0
  223. data/spec/spec/matchers/has_spec.rb +63 -0
  224. data/spec/spec/matchers/have_spec.rb +399 -0
  225. data/spec/spec/matchers/include_spec.rb +88 -0
  226. data/spec/spec/matchers/match_array_spec.rb +83 -0
  227. data/spec/spec/matchers/match_spec.rb +37 -0
  228. data/spec/spec/matchers/matcher_methods_spec.rb +66 -0
  229. data/spec/spec/matchers/operator_matcher_spec.rb +191 -0
  230. data/spec/spec/matchers/raise_error_spec.rb +333 -0
  231. data/spec/spec/matchers/respond_to_spec.rb +116 -0
  232. data/spec/spec/matchers/satisfy_spec.rb +36 -0
  233. data/spec/spec/matchers/simple_matcher_spec.rb +93 -0
  234. data/spec/spec/matchers/throw_symbol_spec.rb +96 -0
  235. data/spec/spec/mocks/any_number_of_times_spec.rb +36 -0
  236. data/spec/spec/mocks/argument_expectation_spec.rb +23 -0
  237. data/spec/spec/mocks/at_least_spec.rb +97 -0
  238. data/spec/spec/mocks/at_most_spec.rb +93 -0
  239. data/spec/spec/mocks/bug_report_10260_spec.rb +8 -0
  240. data/spec/spec/mocks/bug_report_10263_spec.rb +24 -0
  241. data/spec/spec/mocks/bug_report_11545_spec.rb +32 -0
  242. data/spec/spec/mocks/bug_report_15719_spec.rb +30 -0
  243. data/spec/spec/mocks/bug_report_496.rb +19 -0
  244. data/spec/spec/mocks/bug_report_600_spec.rb +22 -0
  245. data/spec/spec/mocks/bug_report_7611_spec.rb +19 -0
  246. data/spec/spec/mocks/bug_report_7805_spec.rb +22 -0
  247. data/spec/spec/mocks/bug_report_8165_spec.rb +31 -0
  248. data/spec/spec/mocks/bug_report_8302_spec.rb +26 -0
  249. data/spec/spec/mocks/failing_argument_matchers_spec.rb +95 -0
  250. data/spec/spec/mocks/hash_including_matcher_spec.rb +90 -0
  251. data/spec/spec/mocks/hash_not_including_matcher_spec.rb +67 -0
  252. data/spec/spec/mocks/mock_ordering_spec.rb +84 -0
  253. data/spec/spec/mocks/mock_space_spec.rb +54 -0
  254. data/spec/spec/mocks/mock_spec.rb +579 -0
  255. data/spec/spec/mocks/multiple_return_value_spec.rb +113 -0
  256. data/spec/spec/mocks/nil_expectation_warning_spec.rb +53 -0
  257. data/spec/spec/mocks/null_object_mock_spec.rb +54 -0
  258. data/spec/spec/mocks/once_counts_spec.rb +53 -0
  259. data/spec/spec/mocks/options_hash_spec.rb +35 -0
  260. data/spec/spec/mocks/partial_mock_spec.rb +149 -0
  261. data/spec/spec/mocks/partial_mock_using_mocks_directly_spec.rb +66 -0
  262. data/spec/spec/mocks/passing_argument_matchers_spec.rb +145 -0
  263. data/spec/spec/mocks/precise_counts_spec.rb +52 -0
  264. data/spec/spec/mocks/record_messages_spec.rb +26 -0
  265. data/spec/spec/mocks/stub_spec.rb +194 -0
  266. data/spec/spec/mocks/stubbed_message_expectations_spec.rb +14 -0
  267. data/spec/spec/mocks/twice_counts_spec.rb +67 -0
  268. data/spec/spec/package/bin_spec_spec.rb +22 -0
  269. data/spec/spec/runner/class_and_argument_parser_spec.rb +23 -0
  270. data/spec/spec/runner/command_line_spec.rb +141 -0
  271. data/spec/spec/runner/configuration_spec.rb +301 -0
  272. data/spec/spec/runner/drb_command_line_spec.rb +97 -0
  273. data/spec/spec/runner/empty_file.txt +0 -0
  274. data/spec/spec/runner/example_group_runner_spec.rb +33 -0
  275. data/spec/spec/runner/examples.txt +2 -0
  276. data/spec/spec/runner/failed.txt +3 -0
  277. data/spec/spec/runner/formatter/base_formatter_spec.rb +23 -0
  278. data/spec/spec/runner/formatter/base_text_formatter_spec.rb +23 -0
  279. data/spec/spec/runner/formatter/failing_example_groups_formatter_spec.rb +45 -0
  280. data/spec/spec/runner/formatter/failing_examples_formatter_spec.rb +33 -0
  281. data/spec/spec/runner/formatter/html_formatted-1.8.4.html +365 -0
  282. data/spec/spec/runner/formatter/html_formatted-1.8.5-jruby.html +387 -0
  283. data/spec/spec/runner/formatter/html_formatted-1.8.5.html +371 -0
  284. data/spec/spec/runner/formatter/html_formatted-1.8.6-jruby.html +381 -0
  285. data/spec/spec/runner/formatter/html_formatted-1.8.6.html +379 -0
  286. data/spec/spec/runner/formatter/html_formatted-1.8.7.html +379 -0
  287. data/spec/spec/runner/formatter/html_formatted-1.9.1.html +379 -0
  288. data/spec/spec/runner/formatter/html_formatter_spec.rb +62 -0
  289. data/spec/spec/runner/formatter/nested_text_formatter_spec.rb +318 -0
  290. data/spec/spec/runner/formatter/profile_formatter_spec.rb +65 -0
  291. data/spec/spec/runner/formatter/progress_bar_formatter_spec.rb +145 -0
  292. data/spec/spec/runner/formatter/snippet_extractor_spec.rb +18 -0
  293. data/spec/spec/runner/formatter/spec_mate_formatter_spec.rb +104 -0
  294. data/spec/spec/runner/formatter/specdoc_formatter_spec.rb +159 -0
  295. data/spec/spec/runner/formatter/text_mate_formatted-1.8.4.html +365 -0
  296. data/spec/spec/runner/formatter/text_mate_formatted-1.8.6.html +373 -0
  297. data/spec/spec/runner/formatter/text_mate_formatted-1.8.7.html +373 -0
  298. data/spec/spec/runner/formatter/text_mate_formatted-1.9.1.html +373 -0
  299. data/spec/spec/runner/heckle_runner_spec.rb +78 -0
  300. data/spec/spec/runner/heckler_spec.rb +20 -0
  301. data/spec/spec/runner/noisy_backtrace_tweaker_spec.rb +45 -0
  302. data/spec/spec/runner/option_parser_spec.rb +396 -0
  303. data/spec/spec/runner/options_spec.rb +469 -0
  304. data/spec/spec/runner/output_one_time_fixture.rb +7 -0
  305. data/spec/spec/runner/output_one_time_fixture_runner.rb +8 -0
  306. data/spec/spec/runner/output_one_time_spec.rb +16 -0
  307. data/spec/spec/runner/quiet_backtrace_tweaker_spec.rb +62 -0
  308. data/spec/spec/runner/reporter_spec.rb +238 -0
  309. data/spec/spec/runner/resources/a_bar.rb +0 -0
  310. data/spec/spec/runner/resources/a_foo.rb +0 -0
  311. data/spec/spec/runner/resources/a_spec.rb +1 -0
  312. data/spec/spec/runner/resources/custom_example_group_runner.rb +14 -0
  313. data/spec/spec/runner/resources/utf8_encoded.rb +7 -0
  314. data/spec/spec/runner/spec.opts +2 -0
  315. data/spec/spec/runner/spec_drb.opts +1 -0
  316. data/spec/spec/runner/spec_parser/spec_parser_fixture.rb +70 -0
  317. data/spec/spec/runner/spec_parser_spec.rb +91 -0
  318. data/spec/spec/runner/spec_spaced.opts +2 -0
  319. data/spec/spec/runner_spec.rb +11 -0
  320. data/spec/spec/spec_classes.rb +133 -0
  321. data/spec/spec/spec_spec.rb +21 -0
  322. data/spec/spec.opts +6 -0
  323. data/spec/spec_helper.rb +107 -0
  324. metadata +388 -0
@@ -0,0 +1,208 @@
1
+ module Spec
2
+ module Matchers
3
+
4
+ class Be #:nodoc:
5
+ def initialize(*args)
6
+ @expected = args.empty? ? true : set_expected(args.shift)
7
+ @args = args
8
+ end
9
+
10
+ def matches?(actual)
11
+ @actual = actual
12
+ handling_predicate? ? run_predicate_on(actual) : match_or_compare(actual)
13
+ end
14
+
15
+ def run_predicate_on(actual)
16
+ begin
17
+ return @result = actual.__send__(predicate, *@args)
18
+ rescue NameError => predicate_missing_error
19
+ "this needs to be here or rcov will not count this branch even though it's executed in a code example"
20
+ end
21
+
22
+ begin
23
+ return @result = actual.__send__(present_tense_predicate, *@args)
24
+ rescue NameError
25
+ raise predicate_missing_error
26
+ end
27
+ end
28
+
29
+ def failure_message
30
+ handling_predicate? ?
31
+ "expected #{predicate}#{args_to_s} to return true, got #{@result.inspect}" :
32
+ "expected #{@comparison_method} #{expected}, got #{@actual.inspect}".gsub(' ',' ')
33
+ end
34
+
35
+ def negative_failure_message
36
+ if handling_predicate?
37
+ "expected #{predicate}#{args_to_s} to return false, got #{@result.inspect}"
38
+ else
39
+ message = <<-MESSAGE
40
+ 'should_not be #{@comparison_method} #{expected}' not only FAILED,
41
+ it reads really poorly.
42
+ MESSAGE
43
+
44
+ raise message << ([:===,:==].include?(@comparison_method) ?
45
+ "Why don't you try expressing it without the \"be\"?" :
46
+ "Why don't you try expressing it in the positive?")
47
+ end
48
+ end
49
+
50
+ def description
51
+ "#{prefix_to_sentence}#{comparison} #{expected_to_sentence}#{args_to_sentence}".gsub(/\s+/,' ')
52
+ end
53
+
54
+ [:==, :<, :<=, :>=, :>, :===].each do |method|
55
+ define_method method do |expected|
56
+ compare_to(expected, :using => method)
57
+ self
58
+ end
59
+ end
60
+
61
+ private
62
+ def match_or_compare(actual)
63
+ case @expected
64
+ when TrueClass
65
+ @actual
66
+ else
67
+ @actual.__send__(comparison_method, @expected)
68
+ end
69
+ end
70
+
71
+ def comparison_method
72
+ @comparison_method || :equal?
73
+ end
74
+
75
+ def expected
76
+ @expected
77
+ end
78
+
79
+ def compare_to(expected, opts)
80
+ @expected, @comparison_method = expected, opts[:using]
81
+ end
82
+
83
+ def set_expected(expected)
84
+ Symbol === expected ? parse_expected(expected) : expected
85
+ end
86
+
87
+ def parse_expected(expected)
88
+ ["be_an_","be_a_","be_"].each do |prefix|
89
+ handling_predicate!
90
+ if expected.starts_with?(prefix)
91
+ set_prefix(prefix)
92
+ expected = expected.to_s.sub(prefix,"")
93
+ [true, false, nil].each do |val|
94
+ return val if val.to_s == expected
95
+ end
96
+ return expected.to_sym
97
+ end
98
+ end
99
+ end
100
+
101
+ def set_prefix(prefix)
102
+ @prefix = prefix
103
+ end
104
+
105
+ def prefix
106
+ @prefix
107
+ end
108
+
109
+ def handling_predicate!
110
+ @handling_predicate = true
111
+ end
112
+
113
+ def handling_predicate?
114
+ return false if [true, false, nil].include?(expected)
115
+ return @handling_predicate
116
+ end
117
+
118
+ def predicate
119
+ "#{@expected.to_s}?".to_sym
120
+ end
121
+
122
+ def present_tense_predicate
123
+ "#{@expected.to_s}s?".to_sym
124
+ end
125
+
126
+ def args_to_s
127
+ @args.empty? ? "" : parenthesize(inspected_args.join(', '))
128
+ end
129
+
130
+ def parenthesize(string)
131
+ return "(#{string})"
132
+ end
133
+
134
+ def inspected_args
135
+ @args.collect{|a| a.inspect}
136
+ end
137
+
138
+ def comparison
139
+ @comparison_method.nil? ? " " : "be #{@comparison_method.to_s} "
140
+ end
141
+
142
+ def expected_to_sentence
143
+ split_words(expected)
144
+ end
145
+
146
+ def prefix_to_sentence
147
+ split_words(prefix)
148
+ end
149
+
150
+ def split_words(sym)
151
+ sym.to_s.gsub(/_/,' ')
152
+ end
153
+
154
+ def args_to_sentence
155
+ case @args.length
156
+ when 0
157
+ ""
158
+ when 1
159
+ " #{@args[0]}"
160
+ else
161
+ " #{@args[0...-1].join(', ')} and #{@args[-1]}"
162
+ end
163
+ end
164
+
165
+ end
166
+
167
+ # :call-seq:
168
+ # should be_true
169
+ # should be_false
170
+ # should be_nil
171
+ # should be_arbitrary_predicate(*args)
172
+ # should_not be_nil
173
+ # should_not be_arbitrary_predicate(*args)
174
+ #
175
+ # Given true, false, or nil, will pass if actual value is
176
+ # true, false or nil (respectively). Given no args means
177
+ # the caller should satisfy an if condition (to be or not to be).
178
+ #
179
+ # Predicates are any Ruby method that ends in a "?" and returns true or false.
180
+ # Given be_ followed by arbitrary_predicate (without the "?"), RSpec will match
181
+ # convert that into a query against the target object.
182
+ #
183
+ # The arbitrary_predicate feature will handle any predicate
184
+ # prefixed with "be_an_" (e.g. be_an_instance_of), "be_a_" (e.g. be_a_kind_of)
185
+ # or "be_" (e.g. be_empty), letting you choose the prefix that best suits the predicate.
186
+ #
187
+ # == Examples
188
+ #
189
+ # target.should be_true
190
+ # target.should be_false
191
+ # target.should be_nil
192
+ # target.should_not be_nil
193
+ #
194
+ # collection.should be_empty #passes if target.empty?
195
+ # target.should_not be_empty #passes unless target.empty?
196
+ # target.should_not be_old_enough(16) #passes unless target.old_enough?(16)
197
+ def be(*args)
198
+ Matchers::Be.new(*args)
199
+ end
200
+
201
+ # passes if target.kind_of?(klass)
202
+ def be_a(klass)
203
+ be_a_kind_of(klass)
204
+ end
205
+
206
+ alias_method :be_an, :be_a
207
+ end
208
+ end
@@ -0,0 +1,21 @@
1
+ module Spec
2
+ module Matchers
3
+
4
+ # :call-seq:
5
+ # should be_close(expected, delta)
6
+ # should_not be_close(expected, delta)
7
+ #
8
+ # Passes if actual == expected +/- delta
9
+ #
10
+ # == Example
11
+ #
12
+ # result.should be_close(3.0, 0.5)
13
+ def be_close(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
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,148 @@
1
+ module Spec
2
+ module Matchers
3
+
4
+ #Based on patch from Wilson Bilkovich
5
+ class Change #:nodoc:
6
+ def initialize(receiver=nil, message=nil, &block)
7
+ @message = message || "result"
8
+ @value_proc = block || lambda {
9
+ receiver.__send__(message)
10
+ }
11
+ end
12
+
13
+ def matches?(event_proc)
14
+ raise_block_syntax_error if block_given?
15
+
16
+ @before = evaluate_value_proc
17
+ event_proc.call
18
+ @after = evaluate_value_proc
19
+
20
+ return false if @from unless @from == @before
21
+ return false if @to unless @to == @after
22
+ return (@before + @amount == @after) if @amount
23
+ return ((@after - @before) >= @minimum) if @minimum
24
+ return ((@after - @before) <= @maximum) if @maximum
25
+ return @before != @after
26
+ end
27
+
28
+ def raise_block_syntax_error
29
+ raise MatcherError.new(<<-MESSAGE
30
+ block passed to should or should_not change must use {} instead of do/end
31
+ MESSAGE
32
+ )
33
+ end
34
+
35
+ def evaluate_value_proc
36
+ @value_proc.call
37
+ end
38
+
39
+ def failure_message
40
+ if @to
41
+ "#{@message} should have been changed to #{@to.inspect}, but is now #{@after.inspect}"
42
+ elsif @from
43
+ "#{@message} should have initially been #{@from.inspect}, but was #{@before.inspect}"
44
+ elsif @amount
45
+ "#{@message} should have been changed by #{@amount.inspect}, but was changed by #{actual_delta.inspect}"
46
+ elsif @minimum
47
+ "#{@message} should have been changed by at least #{@minimum.inspect}, but was changed by #{actual_delta.inspect}"
48
+ elsif @maximum
49
+ "#{@message} should have been changed by at most #{@maximum.inspect}, but was changed by #{actual_delta.inspect}"
50
+ else
51
+ "#{@message} should have changed, but is still #{@before.inspect}"
52
+ end
53
+ end
54
+
55
+ def actual_delta
56
+ @after - @before
57
+ end
58
+
59
+ def negative_failure_message
60
+ "#{@message} should not have changed, but did change from #{@before.inspect} to #{@after.inspect}"
61
+ end
62
+
63
+ def by(amount)
64
+ @amount = amount
65
+ self
66
+ end
67
+
68
+ def by_at_least(minimum)
69
+ @minimum = minimum
70
+ self
71
+ end
72
+
73
+ def by_at_most(maximum)
74
+ @maximum = maximum
75
+ self
76
+ end
77
+
78
+ def to(to)
79
+ @to = to
80
+ self
81
+ end
82
+
83
+ def from (from)
84
+ @from = from
85
+ self
86
+ end
87
+ end
88
+
89
+ # :call-seq:
90
+ # should change(receiver, message, &block)
91
+ # should change(receiver, message, &block).by(value)
92
+ # should change(receiver, message, &block).from(old).to(new)
93
+ # should_not change(receiver, message, &block)
94
+ #
95
+ # Allows you to specify that a Proc will cause some value to change.
96
+ #
97
+ # == Examples
98
+ #
99
+ # lambda {
100
+ # team.add_player(player)
101
+ # }.should change(roster, :count)
102
+ #
103
+ # lambda {
104
+ # team.add_player(player)
105
+ # }.should change(roster, :count).by(1)
106
+ #
107
+ # lambda {
108
+ # team.add_player(player)
109
+ # }.should change(roster, :count).by_at_least(1)
110
+ #
111
+ # lambda {
112
+ # team.add_player(player)
113
+ # }.should change(roster, :count).by_at_most(1)
114
+ #
115
+ # string = "string"
116
+ # lambda {
117
+ # string.reverse!
118
+ # }.should change { string }.from("string").to("gnirts")
119
+ #
120
+ # lambda {
121
+ # person.happy_birthday
122
+ # }.should change(person, :birthday).from(32).to(33)
123
+ #
124
+ # lambda {
125
+ # employee.develop_great_new_social_networking_app
126
+ # }.should change(employee, :title).from("Mail Clerk").to("CEO")
127
+ #
128
+ # Evaluates <tt>receiver.message</tt> or <tt>block</tt> before and after
129
+ # it evaluates the c object (generated by the lambdas in the examples
130
+ # above).
131
+ #
132
+ # Then compares the values before and after the <tt>receiver.message</tt>
133
+ # and evaluates the difference compared to the expected difference.
134
+ #
135
+ # == WARNING
136
+ # <tt>should_not change</tt> only supports the form with no
137
+ # subsequent calls to <tt>by</tt>, <tt>by_at_least</tt>,
138
+ # <tt>by_at_most</tt>, <tt>to</tt> or <tt>from</tt>.
139
+ #
140
+ # blocks passed to <tt>should</tt> <tt>change</tt> and <tt>should_not</tt>
141
+ # <tt>change</tt> must use the <tt>{}</tt> form (<tt>do/end</tt> is not
142
+ # supported).
143
+ #
144
+ def change(receiver=nil, message=nil, &block)
145
+ Matchers::Change.new(receiver, message, &block)
146
+ end
147
+ end
148
+ end
@@ -0,0 +1,25 @@
1
+ module Spec
2
+ module Matchers
3
+
4
+ # :call-seq:
5
+ # should eql(expected)
6
+ # should_not eql(expected)
7
+ #
8
+ # Passes if actual and expected are of equal value, but not necessarily the same object.
9
+ #
10
+ # See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more information about equality in Ruby.
11
+ #
12
+ # == Examples
13
+ #
14
+ # 5.should eql(5)
15
+ # 5.should_not eql(3)
16
+ def eql(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
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ module Spec
2
+ module Matchers
3
+
4
+ # :call-seq:
5
+ # should equal(expected)
6
+ # should_not equal(expected)
7
+ #
8
+ # Passes if actual and expected are the same object (object identity).
9
+ #
10
+ # See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more information about equality in Ruby.
11
+ #
12
+ # == Examples
13
+ #
14
+ # 5.should equal(5) #Fixnums are equal
15
+ # "5".should_not equal("5") #Strings that look the same are not the same object
16
+ def equal(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
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,5 @@
1
+ module Spec
2
+ module Matchers
3
+ class MatcherError < StandardError; end
4
+ end
5
+ end
@@ -0,0 +1,16 @@
1
+ module Spec
2
+ module Matchers
3
+ # :call-seq:
4
+ # should exist
5
+ # should_not exist
6
+ #
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
15
+ end
16
+ end
@@ -0,0 +1,48 @@
1
+ module Spec
2
+ module Matchers
3
+ def self.last_matcher
4
+ @last_matcher
5
+ end
6
+
7
+ def self.last_matcher=(last_matcher)
8
+ @last_matcher = last_matcher
9
+ end
10
+
11
+ def self.last_should
12
+ @last_should
13
+ end
14
+
15
+ def self.last_should=(last_should)
16
+ @last_should = last_should
17
+ end
18
+
19
+ def self.clear_generated_description
20
+ self.last_matcher = nil
21
+ self.last_should = nil
22
+ end
23
+
24
+ def self.generated_description
25
+ return nil if last_should.nil?
26
+ "#{last_should.to_s.gsub('_',' ')} #{last_description}"
27
+ end
28
+
29
+ private
30
+
31
+ def self.last_description
32
+ last_matcher.respond_to?(:description) ? last_matcher.description : <<-MESSAGE
33
+ When you call a matcher in an example without a String, like this:
34
+
35
+ specify { object.should matcher }
36
+
37
+ or this:
38
+
39
+ it { should matcher }
40
+
41
+ the runner expects the matcher to have a #describe method. You should either
42
+ add a String to the example this matcher is being used in, or give it a
43
+ description method. Then you won't have to suffer this lengthy warning again.
44
+ MESSAGE
45
+ end
46
+ end
47
+ end
48
+
@@ -0,0 +1,15 @@
1
+ module Spec
2
+ module Matchers
3
+ def has(sym, *args) # :nodoc:
4
+ simple_matcher do |actual, matcher|
5
+ def matcher.predicate(sym)
6
+ "#{sym.to_s.sub("have_","has_")}?".to_sym
7
+ end
8
+ matcher.failure_message = "expected ##{matcher.predicate(sym)}(#{args[0].inspect}) to return true, got false"
9
+ matcher.negative_failure_message = "expected ##{matcher.predicate(sym)}(#{args[0].inspect}) to return false, got true"
10
+ matcher.description = "have key #{args[0].inspect}"
11
+ actual.__send__(matcher.predicate(sym), *args)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,150 @@
1
+ module Spec
2
+ module Matchers
3
+ class Have #:nodoc:
4
+ def initialize(expected, relativity=:exactly)
5
+ @expected = (expected == :no ? 0 : expected)
6
+ @relativity = relativity
7
+ end
8
+
9
+ def relativities
10
+ @relativities ||= {
11
+ :exactly => "",
12
+ :at_least => "at least ",
13
+ :at_most => "at most "
14
+ }
15
+ end
16
+
17
+ def matches?(collection_owner)
18
+ if collection_owner.respond_to?(@collection_name)
19
+ collection = collection_owner.__send__(@collection_name, *@args, &@block)
20
+ elsif (@plural_collection_name && collection_owner.respond_to?(@plural_collection_name))
21
+ collection = collection_owner.__send__(@plural_collection_name, *@args, &@block)
22
+ elsif (collection_owner.respond_to?(:length) || collection_owner.respond_to?(:size))
23
+ collection = collection_owner
24
+ else
25
+ collection_owner.__send__(@collection_name, *@args, &@block)
26
+ end
27
+ @given = collection.size if collection.respond_to?(:size)
28
+ @given = collection.length if collection.respond_to?(:length)
29
+ raise not_a_collection if @given.nil?
30
+ return @given >= @expected if @relativity == :at_least
31
+ return @given <= @expected if @relativity == :at_most
32
+ return @given == @expected
33
+ end
34
+
35
+ def not_a_collection
36
+ "expected #{@collection_name} to be a collection but it does not respond to #length or #size"
37
+ end
38
+
39
+ def failure_message
40
+ "expected #{relative_expectation} #{@collection_name}, got #{@given}"
41
+ end
42
+
43
+ def negative_failure_message
44
+ if @relativity == :exactly
45
+ return "expected target not to have #{@expected} #{@collection_name}, got #{@given}"
46
+ elsif @relativity == :at_most
47
+ return <<-EOF
48
+ Isn't life confusing enough?
49
+ Instead of having to figure out the meaning of this:
50
+ should_not have_at_most(#{@expected}).#{@collection_name}
51
+ We recommend that you use this instead:
52
+ should have_at_least(#{@expected + 1}).#{@collection_name}
53
+ EOF
54
+ elsif @relativity == :at_least
55
+ return <<-EOF
56
+ Isn't life confusing enough?
57
+ Instead of having to figure out the meaning of this:
58
+ should_not have_at_least(#{@expected}).#{@collection_name}
59
+ We recommend that you use this instead:
60
+ should have_at_most(#{@expected - 1}).#{@collection_name}
61
+ EOF
62
+ end
63
+ end
64
+
65
+ def description
66
+ "have #{relative_expectation} #{@collection_name}"
67
+ end
68
+
69
+ def respond_to?(sym)
70
+ @expected.respond_to?(sym) || super
71
+ end
72
+
73
+ private
74
+
75
+ def method_missing(sym, *args, &block)
76
+ @collection_name = sym
77
+ if inflector = (defined?(ActiveSupport::Inflector) ? ActiveSupport::Inflector : (defined?(Inflector) ? Inflector : nil))
78
+ @plural_collection_name = inflector.pluralize(sym.to_s)
79
+ end
80
+ @args = args
81
+ @block = block
82
+ self
83
+ end
84
+
85
+ def relative_expectation
86
+ "#{relativities[@relativity]}#{@expected}"
87
+ end
88
+ end
89
+
90
+ # :call-seq:
91
+ # should have(number).named_collection__or__sugar
92
+ # should_not have(number).named_collection__or__sugar
93
+ #
94
+ # Passes if receiver is a collection with the submitted
95
+ # number of items OR if the receiver OWNS a collection
96
+ # with the submitted number of items.
97
+ #
98
+ # If the receiver OWNS the collection, you must use the name
99
+ # of the collection. So if a <tt>Team</tt> instance has a
100
+ # collection named <tt>#players</tt>, you must use that name
101
+ # to set the expectation.
102
+ #
103
+ # If the receiver IS the collection, you can use any name
104
+ # you like for <tt>named_collection</tt>. We'd recommend using
105
+ # either "elements", "members", or "items" as these are all
106
+ # standard ways of describing the things IN a collection.
107
+ #
108
+ # This also works for Strings, letting you set an expectation
109
+ # about its length
110
+ #
111
+ # == Examples
112
+ #
113
+ # # Passes if team.players.size == 11
114
+ # team.should have(11).players
115
+ #
116
+ # # Passes if [1,2,3].length == 3
117
+ # [1,2,3].should have(3).items #"items" is pure sugar
118
+ #
119
+ # # Passes if "this string".length == 11
120
+ # "this string".should have(11).characters #"characters" is pure sugar
121
+ def have(n)
122
+ Matchers::Have.new(n)
123
+ end
124
+ alias :have_exactly :have
125
+
126
+ # :call-seq:
127
+ # should have_at_least(number).items
128
+ #
129
+ # Exactly like have() with >=.
130
+ #
131
+ # == Warning
132
+ #
133
+ # +should_not+ +have_at_least+ is not supported
134
+ def have_at_least(n)
135
+ Matchers::Have.new(n, :at_least)
136
+ end
137
+
138
+ # :call-seq:
139
+ # should have_at_most(number).items
140
+ #
141
+ # Exactly like have() with <=.
142
+ #
143
+ # == Warning
144
+ #
145
+ # +should_not+ +have_at_most+ is not supported
146
+ def have_at_most(n)
147
+ Matchers::Have.new(n, :at_most)
148
+ end
149
+ end
150
+ end