Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Tagging 0.9.0 release again after previously removing it.

  • Loading branch information...
commit 6c0513ffac3ca6be0c66fc0f8bbb49b2c6b4064b 2 parents ab6e4a2 + e162cd7
James Mead floehopper authored
Showing with 1,160 additions and 13,982 deletions.
  1. +57 −33 Rakefile
  2. +15 −13 TODO
  3. +44 −36 examples/misc.rb
  4. +1 −1  examples/stubba.rb
  5. +1 −0  lib/mocha.rb
  6. +24 −4 lib/mocha/any_instance_method.rb
  7. +0 −111 lib/mocha/auto_verify.rb
  8. 0  {trunk → }/lib/mocha/backtrace_filter.rb
  9. 0  {trunk → }/lib/mocha/cardinality.rb
  10. +2 −10 lib/mocha/central.rb
  11. 0  {trunk → }/lib/mocha/change_state_side_effect.rb
  12. +30 −6 lib/mocha/class_method.rb
  13. 0  {trunk → }/lib/mocha/configuration.rb
  14. +1 −1  lib/mocha/exception_raiser.rb
  15. +152 −89 lib/mocha/expectation.rb
  16. +10 −1 lib/mocha/expectation_error.rb
  17. +17 −20 lib/mocha/expectation_list.rb
  18. 0  {trunk → }/lib/mocha/in_state_ordering_constraint.rb
  19. +0 −25 lib/mocha/infinite_range.rb
  20. +9 −0 lib/mocha/instance_method.rb
  21. 0  {trunk → }/lib/mocha/logger.rb
  22. 0  {trunk → }/lib/mocha/method_matcher.rb
  23. +0 −27 lib/mocha/missing_expectation.rb
  24. +44 −37 lib/mocha/mock.rb
  25. 0  {trunk → }/lib/mocha/mockery.rb
  26. 0  {trunk → }/lib/mocha/module_method.rb
  27. 0  {trunk → }/lib/mocha/names.rb
  28. +26 −9 lib/mocha/object.rb
  29. +19 −2 lib/mocha/parameter_matchers.rb
  30. +8 −5 lib/mocha/parameter_matchers/all_of.rb
  31. +8 −5 lib/mocha/parameter_matchers/any_of.rb
  32. 0  {trunk → }/lib/mocha/parameter_matchers/any_parameters.rb
  33. +6 −3 lib/mocha/parameter_matchers/anything.rb
  34. 0  {trunk → }/lib/mocha/parameter_matchers/base.rb
  35. 0  {trunk → }/lib/mocha/parameter_matchers/equals.rb
  36. 0  {trunk → }/lib/mocha/parameter_matchers/has_entries.rb
  37. +23 −6 lib/mocha/parameter_matchers/has_entry.rb
  38. +6 −3 lib/mocha/parameter_matchers/has_key.rb
  39. +6 −3 lib/mocha/parameter_matchers/has_value.rb
  40. +5 −2 lib/mocha/parameter_matchers/includes.rb
  41. +5 −2 lib/mocha/parameter_matchers/instance_of.rb
  42. 0  {trunk → }/lib/mocha/parameter_matchers/is_a.rb
  43. +5 −2 lib/mocha/parameter_matchers/kind_of.rb
  44. 0  {trunk → }/lib/mocha/parameter_matchers/not.rb
  45. 0  {trunk → }/lib/mocha/parameter_matchers/object.rb
  46. 0  {trunk → }/lib/mocha/parameter_matchers/optionally.rb
  47. +7 −4 lib/mocha/parameter_matchers/regexp_matches.rb
  48. 0  {trunk → }/lib/mocha/parameter_matchers/responds_with.rb
  49. 0  {trunk → }/lib/mocha/parameter_matchers/yaml_equivalent.rb
  50. 0  {trunk → }/lib/mocha/parameters_matcher.rb
  51. +1 −1  lib/mocha/pretty_parameters.rb
  52. +7 −4 lib/mocha/return_values.rb
  53. 0  {trunk → }/lib/mocha/sequence.rb
  54. +0 −23 lib/mocha/setup_and_teardown.rb
  55. +2 −9 lib/mocha/single_return_value.rb
  56. +151 −17 lib/mocha/standalone.rb
  57. 0  {trunk → }/lib/mocha/state_machine.rb
  58. +0 −18 lib/mocha/stub.rb
  59. 0  {trunk → }/lib/mocha/stubbing_error.rb
  60. +76 −22 lib/mocha/test_case_adapter.rb
  61. +3 −3 lib/mocha/yield_parameters.rb
  62. +2 −1  lib/stubba.rb
  63. 0  {trunk → }/test/acceptance/acceptance_test_helper.rb
  64. 0  {trunk → }/test/acceptance/bug_18914_test.rb
  65. +0 −187 test/acceptance/expected_invocation_count_acceptance_test.rb
  66. 0  {trunk → }/test/acceptance/expected_invocation_count_test.rb
  67. 0  {trunk → }/test/acceptance/failure_messages_test.rb
  68. +0 −98 test/acceptance/mocha_acceptance_test.rb
  69. 0  {trunk → }/test/acceptance/mocha_example_test.rb
  70. 0  {trunk → }/test/acceptance/mocha_test_result_test.rb
  71. 0  {trunk → }/test/acceptance/mock_test.rb
  72. 0  {trunk → }/test/acceptance/mock_with_initializer_block_test.rb
  73. +0 −71 test/acceptance/mocked_methods_dispatch_acceptance_test.rb
  74. 0  {trunk → }/test/acceptance/mocked_methods_dispatch_test.rb
  75. 0  {trunk → }/test/acceptance/optional_parameters_test.rb
  76. +0 −81 test/acceptance/parameter_matcher_acceptance_test.rb
  77. 0  {trunk → }/test/acceptance/parameter_matcher_test.rb
  78. 0  {trunk → }/test/acceptance/partial_mocks_test.rb
  79. 0  {trunk → }/test/acceptance/return_value_test.rb
  80. 0  {trunk → }/test/acceptance/sequence_test.rb
  81. +0 −131 test/acceptance/standalone_acceptance_test.rb
  82. 0  {trunk → }/test/acceptance/standalone_test.rb
  83. 0  {trunk → }/test/acceptance/states_test.rb
  84. 0  {trunk → }/test/acceptance/stub_any_instance_method_test.rb
  85. 0  {trunk → }/test/acceptance/stub_class_method_test.rb
  86. 0  {trunk → }/test/acceptance/stub_everything_test.rb
  87. 0  {trunk → }/test/acceptance/stub_instance_method_test.rb
  88. 0  {trunk → }/test/acceptance/stub_module_method_test.rb
  89. 0  {trunk → }/test/acceptance/stub_test.rb
  90. +0 −102 test/acceptance/stubba_acceptance_test.rb
  91. 0  {trunk → }/test/acceptance/stubba_example_test.rb
  92. 0  {trunk → }/test/acceptance/stubba_test_result_test.rb
  93. 0  {trunk → }/test/acceptance/stubbing_error_backtrace_test.rb
  94. 0  {trunk → }/test/acceptance/stubbing_method_unnecessarily_test.rb
  95. 0  {trunk → }/test/acceptance/stubbing_non_existent_any_instance_method_test.rb
  96. 0  {trunk → }/test/acceptance/stubbing_non_existent_class_method_test.rb
  97. 0  {trunk → }/test/acceptance/stubbing_non_existent_instance_method_test.rb
  98. 0  {trunk → }/test/acceptance/stubbing_non_public_any_instance_method_test.rb
  99. 0  {trunk → }/test/acceptance/stubbing_non_public_class_method_test.rb
  100. 0  {trunk → }/test/acceptance/stubbing_non_public_instance_method_test.rb
  101. 0  {trunk → }/test/acceptance/stubbing_on_non_mock_object_test.rb
  102. +3 −1 test/execution_point.rb
  103. +0 −105 test/integration/mocha_test_result_integration_test.rb
  104. +0 −89 test/integration/stubba_integration_test.rb
  105. +0 −85 test/integration/stubba_test_result_integration_test.rb
  106. 0  {trunk → }/test/simple_counter.rb
  107. +0 −1  test/test_helper.rb
  108. +8 −6 test/test_runner.rb
  109. +3 −1 test/unit/any_instance_method_test.rb
  110. +1 −1  test/unit/array_inspect_test.rb
  111. +0 −122 test/unit/auto_verify_test.rb
  112. 0  {trunk → }/test/unit/backtrace_filter_test.rb
  113. 0  {trunk → }/test/unit/cardinality_test.rb
  114. +4 −63 test/unit/central_test.rb
  115. 0  {trunk → }/test/unit/change_state_side_effect_test.rb
  116. +42 −1 test/unit/class_method_test.rb
  117. +1 −1  test/unit/date_time_inspect_test.rb
  118. 0  {trunk → }/test/unit/exception_raiser_test.rb
  119. +9 −21 test/unit/expectation_list_test.rb
  120. +0 −28 test/unit/expectation_raiser_test.rb
  121. +175 −115 test/unit/expectation_test.rb
  122. 0  {trunk → }/test/unit/in_state_ordering_constraint_test.rb
  123. +0 −53 test/unit/infinite_range_test.rb
  124. 0  {trunk → }/test/unit/method_matcher_test.rb
  125. +0 −45 test/unit/missing_expectation_test.rb
  126. +39 −53 test/unit/mock_test.rb
  127. 0  {trunk → }/test/unit/mockery_test.rb
  128. +0 −18 test/unit/no_yield_test.rb
  129. 0  {trunk → }/test/unit/no_yields_test.rb
  130. +4 −2 test/unit/object_inspect_test.rb
  131. +6 −89 test/unit/object_test.rb
  132. +2 −2 test/unit/parameter_matchers/all_of_test.rb
  133. +2 −2 test/unit/parameter_matchers/any_of_test.rb
  134. +2 −2 test/unit/parameter_matchers/anything_test.rb
  135. 0  {trunk → }/test/unit/parameter_matchers/equals_test.rb
  136. 0  {trunk → }/test/unit/parameter_matchers/has_entries_test.rb
  137. +43 −6 test/unit/parameter_matchers/has_entry_test.rb
  138. +13 −2 test/unit/parameter_matchers/has_key_test.rb
  139. +14 −2 test/unit/parameter_matchers/has_value_test.rb
  140. +2 −2 test/unit/parameter_matchers/includes_test.rb
  141. +2 −2 test/unit/parameter_matchers/instance_of_test.rb
  142. 0  {trunk → }/test/unit/parameter_matchers/is_a_test.rb
  143. +3 −3 test/unit/parameter_matchers/kind_of_test.rb
  144. 0  {trunk → }/test/unit/parameter_matchers/not_test.rb
  145. +3 −3 test/unit/parameter_matchers/regexp_matches_test.rb
  146. 0  {trunk → }/test/unit/parameter_matchers/responds_with_test.rb
  147. +6 −1 test/unit/parameter_matchers/stub_matcher.rb
  148. 0  {trunk → }/test/unit/parameter_matchers/yaml_equivalent_test.rb
  149. 0  {trunk → }/test/unit/parameters_matcher_test.rb
  150. +0 −32 test/unit/pretty_parameters_test.rb
  151. 0  {trunk → }/test/unit/sequence_test.rb
  152. +0 −76 test/unit/setup_and_teardown_test.rb
  153. +0 −19 test/unit/single_return_value_test.rb
  154. 0  {trunk → }/test/unit/state_machine_test.rb
  155. +0 −24 test/unit/stub_test.rb
  156. +0 −3  trunk/COPYING
  157. +0 −7 trunk/MIT-LICENSE
  158. +0 −35 trunk/README
  159. +0 −233 trunk/RELEASE
  160. +0 −171 trunk/Rakefile
  161. +0 −50 trunk/TODO
  162. +0 −44 trunk/examples/misc.rb
  163. +0 −26 trunk/examples/mocha.rb
  164. +0 −65 trunk/examples/stubba.rb
  165. +0 −3  trunk/init.rb
  166. +0 −20 trunk/lib/mocha.rb
  167. +0 −55 trunk/lib/mocha/any_instance_method.rb
  168. +0 −27 trunk/lib/mocha/central.rb
  169. +0 −86 trunk/lib/mocha/class_method.rb
  170. +0 −22 trunk/lib/mocha/deprecation.rb
  171. +0 −17 trunk/lib/mocha/exception_raiser.rb
  172. +0 −445 trunk/lib/mocha/expectation.rb
  173. +0 −15 trunk/lib/mocha/expectation_error.rb
  174. +0 −42 trunk/lib/mocha/expectation_list.rb
  175. +0 −39 trunk/lib/mocha/inspect.rb
  176. +0 −17 trunk/lib/mocha/instance_method.rb
  177. +0 −9 trunk/lib/mocha/is_a.rb
  178. +0 −7 trunk/lib/mocha/metaclass.rb
  179. +0 −207 trunk/lib/mocha/mock.rb
  180. +0 −20 trunk/lib/mocha/multiple_yields.rb
  181. +0 −11 trunk/lib/mocha/no_yields.rb
  182. +0 −127 trunk/lib/mocha/object.rb
  183. +0 −26 trunk/lib/mocha/parameter_matchers.rb
  184. +0 −42 trunk/lib/mocha/parameter_matchers/all_of.rb
  185. +0 −47 trunk/lib/mocha/parameter_matchers/any_of.rb
  186. +0 −33 trunk/lib/mocha/parameter_matchers/anything.rb
  187. +0 −56 trunk/lib/mocha/parameter_matchers/has_entry.rb
  188. +0 −42 trunk/lib/mocha/parameter_matchers/has_key.rb
  189. +0 −42 trunk/lib/mocha/parameter_matchers/has_value.rb
  190. +0 −40 trunk/lib/mocha/parameter_matchers/includes.rb
  191. +0 −42 trunk/lib/mocha/parameter_matchers/instance_of.rb
  192. +0 −42 trunk/lib/mocha/parameter_matchers/kind_of.rb
  193. +0 −43 trunk/lib/mocha/parameter_matchers/regexp_matches.rb
  194. +0 −28 trunk/lib/mocha/pretty_parameters.rb
  195. +0 −34 trunk/lib/mocha/return_values.rb
  196. +0 −17 trunk/lib/mocha/single_return_value.rb
  197. +0 −18 trunk/lib/mocha/single_yield.rb
  198. +0 −166 trunk/lib/mocha/standalone.rb
  199. +0 −103 trunk/lib/mocha/test_case_adapter.rb
  200. +0 −31 trunk/lib/mocha/yield_parameters.rb
  201. +0 −2  trunk/lib/mocha_standalone.rb
  202. +0 −3  trunk/lib/stubba.rb
  203. +0 −746 trunk/templates/html_with_google_analytics.rb
  204. +0 −36 trunk/test/active_record_test_case.rb
  205. +0 −15 trunk/test/deprecation_disabler.rb
  206. +0 −36 trunk/test/execution_point.rb
  207. +0 −18 trunk/test/method_definer.rb
  208. +0 −11 trunk/test/test_helper.rb
  209. +0 −33 trunk/test/test_runner.rb
  210. +0 −126 trunk/test/unit/any_instance_method_test.rb
  211. +0 −16 trunk/test/unit/array_inspect_test.rb
  212. +0 −65 trunk/test/unit/central_test.rb
  213. +0 −237 trunk/test/unit/class_method_test.rb
  214. +0 −21 trunk/test/unit/date_time_inspect_test.rb
  215. +0 −57 trunk/test/unit/expectation_list_test.rb
  216. +0 −459 trunk/test/unit/expectation_test.rb
  217. +0 −16 trunk/test/unit/hash_inspect_test.rb
  218. +0 −22 trunk/test/unit/metaclass_test.rb
  219. +0 −302 trunk/test/unit/mock_test.rb
  220. +0 −18 trunk/test/unit/multiple_yields_test.rb
  221. +0 −37 trunk/test/unit/object_inspect_test.rb
  222. +0 −82 trunk/test/unit/object_test.rb
  223. +0 −26 trunk/test/unit/parameter_matchers/all_of_test.rb
  224. +0 −26 trunk/test/unit/parameter_matchers/any_of_test.rb
  225. +0 −21 trunk/test/unit/parameter_matchers/anything_test.rb
  226. +0 −62 trunk/test/unit/parameter_matchers/has_entry_test.rb
  227. +0 −36 trunk/test/unit/parameter_matchers/has_key_test.rb
  228. +0 −37 trunk/test/unit/parameter_matchers/has_value_test.rb
  229. +0 −25 trunk/test/unit/parameter_matchers/includes_test.rb
  230. +0 −25 trunk/test/unit/parameter_matchers/instance_of_test.rb
  231. +0 −25 trunk/test/unit/parameter_matchers/kind_of_test.rb
  232. +0 −25 trunk/test/unit/parameter_matchers/regexp_matches_test.rb
  233. +0 −27 trunk/test/unit/parameter_matchers/stub_matcher.rb
  234. +0 −63 trunk/test/unit/return_values_test.rb
  235. +0 −14 trunk/test/unit/single_return_value_test.rb
  236. +0 −18 trunk/test/unit/single_yield_test.rb
  237. +0 −11 trunk/test/unit/string_inspect_test.rb
  238. +0 −93 trunk/test/unit/yield_parameters_test.rb
  239. +0 −53 trunk/vendor/coderay-0.7.4.215/FOLDERS
  240. +0 −340 trunk/vendor/coderay-0.7.4.215/LICENSE
  241. +0 −117 trunk/vendor/coderay-0.7.4.215/README
  242. +0 −77 trunk/vendor/coderay-0.7.4.215/bin/coderay
  243. +0 −4 trunk/vendor/coderay-0.7.4.215/bin/coderay_stylesheet
  244. +0 −319 trunk/vendor/coderay-0.7.4.215/lib/coderay.rb
  245. +0 −29 trunk/vendor/coderay-0.7.4.215/lib/coderay/duo.rb
  246. +0 −173 trunk/vendor/coderay-0.7.4.215/lib/coderay/encoder.rb
  247. +0 −8 trunk/vendor/coderay-0.7.4.215/lib/coderay/encoders/_map.rb
  248. +0 −21 trunk/vendor/coderay-0.7.4.215/lib/coderay/encoders/count.rb
  249. +0 −46 trunk/vendor/coderay-0.7.4.215/lib/coderay/encoders/debug.rb
  250. +0 −20 trunk/vendor/coderay-0.7.4.215/lib/coderay/encoders/div.rb
  251. +0 −260 trunk/vendor/coderay-0.7.4.215/lib/coderay/encoders/html.rb
  252. +0 −77 trunk/vendor/coderay-0.7.4.215/lib/coderay/encoders/html/classes.rb
  253. +0 −65 trunk/vendor/coderay-0.7.4.215/lib/coderay/encoders/html/css.rb
  254. +0 −122 trunk/vendor/coderay-0.7.4.215/lib/coderay/encoders/html/numerization.rb
  255. +0 −195 trunk/vendor/coderay-0.7.4.215/lib/coderay/encoders/html/output.rb
  256. +0 −26 trunk/vendor/coderay-0.7.4.215/lib/coderay/encoders/null.rb
  257. +0 −21 trunk/vendor/coderay-0.7.4.215/lib/coderay/encoders/page.rb
  258. +0 −20 trunk/vendor/coderay-0.7.4.215/lib/coderay/encoders/span.rb
  259. +0 −81 trunk/vendor/coderay-0.7.4.215/lib/coderay/encoders/statistic.rb
  260. +0 −33 trunk/vendor/coderay-0.7.4.215/lib/coderay/encoders/text.rb
  261. +0 −44 trunk/vendor/coderay-0.7.4.215/lib/coderay/encoders/tokens.rb
  262. +0 −71 trunk/vendor/coderay-0.7.4.215/lib/coderay/encoders/xml.rb
  263. +0 −22 trunk/vendor/coderay-0.7.4.215/lib/coderay/encoders/yaml.rb
  264. +0 −182 trunk/vendor/coderay-0.7.4.215/lib/coderay/helpers/file_type.rb
  265. +0 −122 trunk/vendor/coderay-0.7.4.215/lib/coderay/helpers/gzip_simple.rb
  266. +0 −326 trunk/vendor/coderay-0.7.4.215/lib/coderay/helpers/plugin.rb
  267. +0 −119 trunk/vendor/coderay-0.7.4.215/lib/coderay/helpers/word_list.rb
  268. +0 −238 trunk/vendor/coderay-0.7.4.215/lib/coderay/scanner.rb
  269. +0 −15 trunk/vendor/coderay-0.7.4.215/lib/coderay/scanners/_map.rb
  270. +0 −163 trunk/vendor/coderay-0.7.4.215/lib/coderay/scanners/c.rb
  271. +0 −60 trunk/vendor/coderay-0.7.4.215/lib/coderay/scanners/debug.rb
  272. +0 −149 trunk/vendor/coderay-0.7.4.215/lib/coderay/scanners/delphi.rb
  273. +0 −177 trunk/vendor/coderay-0.7.4.215/lib/coderay/scanners/html.rb
  274. +0 −133 trunk/vendor/coderay-0.7.4.215/lib/coderay/scanners/nitro_xhtml.rb
  275. +0 −16 trunk/vendor/coderay-0.7.4.215/lib/coderay/scanners/plaintext.rb
  276. +0 −73 trunk/vendor/coderay-0.7.4.215/lib/coderay/scanners/rhtml.rb
  277. +0 −367 trunk/vendor/coderay-0.7.4.215/lib/coderay/scanners/ruby.rb
  278. +0 −230 trunk/vendor/coderay-0.7.4.215/lib/coderay/scanners/ruby/patterns.rb
  279. +0 −18 trunk/vendor/coderay-0.7.4.215/lib/coderay/scanners/xml.rb
  280. +0 −20 trunk/vendor/coderay-0.7.4.215/lib/coderay/style.rb
  281. +0 −7 trunk/vendor/coderay-0.7.4.215/lib/coderay/styles/_map.rb
  282. +0 −125 trunk/vendor/coderay-0.7.4.215/lib/coderay/styles/cycnus.rb
  283. +0 −119 trunk/vendor/coderay-0.7.4.215/lib/coderay/styles/murphy.rb
  284. +0 −368 trunk/vendor/coderay-0.7.4.215/lib/coderay/tokens.rb
  285. +0 −295 trunk/vendor/meta_project-0.4.15/CHANGES
  286. +0 −21 trunk/vendor/meta_project-0.4.15/MIT-LICENSE
  287. +0 −129 trunk/vendor/meta_project-0.4.15/README
  288. +0 −152 trunk/vendor/meta_project-0.4.15/Rakefile
  289. +0 −2  trunk/vendor/meta_project-0.4.15/doc/base_attrs.rdoc
  290. +0 −19 trunk/vendor/meta_project-0.4.15/lib/meta_project.rb
  291. +0 −22 trunk/vendor/meta_project-0.4.15/lib/meta_project/core_ext/open_uri.rb
  292. +0 −36 trunk/vendor/meta_project-0.4.15/lib/meta_project/core_ext/pathname.rb
  293. +0 −5 trunk/vendor/meta_project-0.4.15/lib/meta_project/core_ext/string.rb
  294. +0 −32 trunk/vendor/meta_project-0.4.15/lib/meta_project/http/multipart.rb
  295. +0 −1  trunk/vendor/meta_project-0.4.15/lib/meta_project/patois.rb
  296. +0 −99 trunk/vendor/meta_project-0.4.15/lib/meta_project/patois/parser.rb
  297. +0 −4 trunk/vendor/meta_project-0.4.15/lib/meta_project/project.rb
  298. +0 −9 trunk/vendor/meta_project-0.4.15/lib/meta_project/project/base.rb
  299. +0 −1  trunk/vendor/meta_project-0.4.15/lib/meta_project/project/codehaus.rb
  300. +0 −31 trunk/vendor/meta_project-0.4.15/lib/meta_project/project/codehaus/codehaus_project_svn.rb
Sorry, we could not display the entire diff because too many files (332) changed.
90 Rakefile
View
@@ -5,50 +5,72 @@ require 'rake/testtask'
require 'rake/contrib/sshpublisher'
module Mocha
- VERSION = "0.5.5"
+ VERSION = "0.5.6"
end
desc "Run all tests"
-task :default => :test_all
-
-task :test_all => [:test_unit, :test_integration, :test_acceptance]
-
-desc "Run unit tests"
-Rake::TestTask.new(:test_unit) do |t|
- t.libs << 'test'
- t.test_files = FileList['test/unit/**/*_test.rb']
- t.verbose = true
- t.warning = true
-end
+task 'default' => ['test:units', 'test:acceptance']
+
+namespace 'test' do
+
+ unit_tests = FileList['test/unit/**/*_test.rb']
+ acceptance_tests = FileList['test/acceptance/*_test.rb']
+
+ desc "Run unit tests"
+ Rake::TestTask.new('units') do |t|
+ t.libs << 'test'
+ t.test_files = unit_tests
+ t.verbose = true
+ t.warning = true
+ end
-desc "Run integration tests"
-Rake::TestTask.new(:test_integration) do |t|
- t.libs << 'test'
- t.test_files = FileList['test/integration/*_test.rb']
- t.verbose = true
- t.warning = true
-end
+ desc "Run acceptance tests"
+ Rake::TestTask.new('acceptance') do |t|
+ t.libs << 'test'
+ t.test_files = acceptance_tests
+ t.verbose = true
+ t.warning = true
+ end
+
+ # require 'rcov/rcovtask'
+ # Rcov::RcovTask.new('coverage') do |t|
+ # t.libs << 'test'
+ # t.test_files = unit_tests + acceptance_tests
+ # t.verbose = true
+ # t.warning = true
+ # t.rcov_opts << '--sort coverage'
+ # t.rcov_opts << '--xref'
+ # end
-desc "Run acceptance tests"
-Rake::TestTask.new(:test_acceptance) do |t|
- t.libs << 'test'
- t.test_files = FileList['test/acceptance/*_test.rb']
- t.verbose = true
- t.warning = true
end
desc 'Generate RDoc'
-Rake::RDocTask.new do |task|
+Rake::RDocTask.new('rdoc') do |task|
task.main = 'README'
task.title = "Mocha #{Mocha::VERSION}"
task.rdoc_dir = 'doc'
task.template = File.expand_path(File.join(File.dirname(__FILE__), "templates", "html_with_google_analytics"))
- task.rdoc_files.include('README', 'RELEASE', 'COPYING', 'MIT-LICENSE', 'agiledox.txt', 'lib/mocha/auto_verify.rb', 'lib/mocha/mock.rb', 'lib/mocha/expectation.rb', 'lib/mocha/object.rb', 'lib/mocha/parameter_matchers.rb', 'lib/mocha/parameter_matchers')
+ task.rdoc_files.include(
+ 'README',
+ 'RELEASE',
+ 'COPYING',
+ 'MIT-LICENSE',
+ 'agiledox.txt',
+ 'lib/mocha/standalone.rb',
+ 'lib/mocha/mock.rb',
+ 'lib/mocha/expectation.rb',
+ 'lib/mocha/object.rb',
+ 'lib/mocha/parameter_matchers.rb',
+ 'lib/mocha/parameter_matchers',
+ 'lib/mocha/state_machine.rb',
+ 'lib/mocha/configuration.rb',
+ 'lib/mocha/stubbing_error.rb'
+ )
end
-task :rdoc => :examples
+task 'rdoc' => 'examples'
desc "Upload RDoc to RubyForge"
-task :publish_rdoc => [:rdoc, :examples] do
+task 'publish_rdoc' => ['rdoc', 'examples'] do
Rake::SshDirPublisher.new("jamesmead@rubyforge.org", "/var/www/gforge-projects/mocha", "doc").upload
end
@@ -69,7 +91,7 @@ file 'agiledox.txt' do
end
desc "Convert example ruby files to syntax-highlighted html"
-task :examples do
+task 'examples' do
$:.unshift File.expand_path(File.join(File.dirname(__FILE__), "vendor", "coderay-0.7.4.215", "lib"))
require 'coderay'
mkdir_p 'doc/examples'
@@ -96,6 +118,7 @@ specification = Gem::Specification.new do |s|
s.name = "mocha"
s.summary = "Mocking and stubbing library"
s.version = Mocha::VERSION
+ s.platform = Gem::Platform::RUBY
s.author = 'James Mead'
s.description = <<-EOF
Mocking and stubbing library with JMock/SchMock syntax, which allows mocking and stubbing of methods on real (non-mock) classes.
@@ -109,6 +132,7 @@ specification = Gem::Specification.new do |s|
s.rdoc_options << '--title' << 'Mocha' << '--main' << 'README' << '--line-numbers'
s.autorequire = 'mocha'
+ s.add_dependency('rake')
s.files = FileList['{lib,test,examples}/**/*.rb', '[A-Z]*'].exclude('TODO').to_a
end
@@ -117,16 +141,16 @@ Rake::GemPackageTask.new(specification) do |package|
package.need_tar = true
end
-task :verify_user do
+task 'verify_user' do
raise "RUBYFORGE_USER environment variable not set!" unless ENV['RUBYFORGE_USER']
end
-task :verify_password do
+task 'verify_password' do
raise "RUBYFORGE_PASSWORD environment variable not set!" unless ENV['RUBYFORGE_PASSWORD']
end
desc "Publish package files on RubyForge."
-task :publish_packages => [:verify_user, :verify_password, :package] do
+task 'publish_packages' => ['verify_user', 'verify_password', 'package'] do
$:.unshift File.expand_path(File.join(File.dirname(__FILE__), "vendor", "meta_project-0.4.15", "lib"))
require 'meta_project'
require 'rake/contrib/xforge'
28 TODO
View
@@ -1,17 +1,22 @@
+=> Patch [#15235] Provide a pseudo-call syntax for simplifying set-up of expectations and stubs
+=> Patch [#10412] Merge 'with' and the expected method
+=> Bug [#8687] Block's return value is dropped on stubbed yielding methods
+
+=> interesting links
+- http://www.jmock.org/match-object-or-method.html
+- http://rspec.lighthouseapp.com/projects/5645-rspec/tickets/106-12985-with_block_which_returns-val
+
+- think about making class_method_test & any_instance_method_test more blackbox to avoid ruby warnings
+
+=> update vendor libraries
+- does updating meta_project have any effect on broken tarball issue?
+
=> release management
- why does gem rdoc include all source files? what about other package types?
- why does zip include Rakefile? what about other package types?
- tag releases in repos
- trac?
-=> restrict partial mocks to existing public methods?
-- investigate where/why check for existence of stubbed method has gone.
-- allow stubbing of private/protected methods? - force_stub?
-- allow specification of class to be mocked, so we can check for existence of methods
-
-=> bugs
-- assertion count is not incremented for missing expectation errors
-
=> multiple expectations for same method
- fail fast if expectation count exceeded during test...? c.f. JMock?
@@ -26,13 +31,11 @@
=> improve design
- use builder object c.f. JMock
+- use expectation builder object to allow partial mocks to accept a hash in expects or stubs methods
- possibly simplify by making mock the same as a partial mock of Object
- provide test::unit agnostic api - mockery/context type objects on which the user must call setup/teardown?
- allow stubs/expects methods to accept method names as strings and symbols to retain similarity to define_method() et al
-=> documentation
-- read and improve tutorial emailed by user
-
=> naming
- default mock names to mock1, mock2, etc or something similar
- stubba mocks should named according to parent stubbee
@@ -42,7 +45,6 @@
- test for setting expectations on class methods (and instance methods?) from within TestCase#setup
=> possible extra functionality
-- more jmock style stuff - e.g. labels/required order, more sophisticated param matching?
- maybe allow unstubbing of a specific method from within a test...?
- oomatron type ideas
-- should all instances share expectations for any_instance or should each instance have their own - in which case how do we provide access to the instances
+- should all instances share expectations for any_instance or should each instance have their own - in which case how do we provide access to the instances
80 examples/misc.rb
View
@@ -1,36 +1,44 @@
-# Mocking a class method
-
- product = Product.new
- Product.expects(:find).with(1).returns(product)
- assert_equal product, Product.find(1)
-
-# Mocking an instance method on a real object
-
- product = Product.new
- product.expects(:save).returns(true)
- assert product.save
-
-# Stubbing instance methods on real object
-
- prices = [stub(:pence => 1000), stub(:pence => 2000)]
- product = Product.new
- product.stubs(:prices).returns(prices)
- assert_equal [1000, 2000], product.prices.collect {|p| p.pence}
-
-# Stubbing an instance method on all instances of a class
-
- Product.any_instance.stubs(:name).returns('stubbed_name')
- product = Product.new
- assert_equal 'stubbed_name', product.name
-
-# Traditional mocking
-
- object = mock()
- object.expects(:expected_method).with(:p1, :p2).returns(:result)
- assert_equal :result, object.expected_method(:p1, :p2)
-
-# Shortcuts
-
- object = stub(:method1 => :result1, :method2 => :result2)
- assert_equal :result1, object.method1
- assert_equal :result2, object.method2
+require 'test/unit'
+require 'rubygems'
+require 'mocha'
+
+class MiscExampleTest < Test::Unit::TestCase
+
+ def test_mocking_a_class_method
+ product = Product.new
+ Product.expects(:find).with(1).returns(product)
+ assert_equal product, Product.find(1)
+ end
+
+ def test_mocking_an_instance_method_on_a_real_object
+ product = Product.new
+ product.expects(:save).returns(true)
+ assert product.save
+ end
+
+ def test_stubbing_instance_methods_on_real_objects
+ prices = [stub(:pence => 1000), stub(:pence => 2000)]
+ product = Product.new
+ product.stubs(:prices).returns(prices)
+ assert_equal [1000, 2000], product.prices.collect {|p| p.pence}
+ end
+
+ def test_stubbing_an_instance_method_on_all_instances_of_a_class
+ Product.any_instance.stubs(:name).returns('stubbed_name')
+ product = Product.new
+ assert_equal 'stubbed_name', product.name
+ end
+
+ def test_traditional_mocking
+ object = mock()
+ object.expects(:expected_method).with(:p1, :p2).returns(:result)
+ assert_equal :result, object.expected_method(:p1, :p2)
+ end
+
+ def test_shortcuts
+ object = stub(:method1 => :result1, :method2 => :result2)
+ assert_equal :result1, object.method1
+ assert_equal :result2, object.method2
+ end
+
+end
2  examples/stubba.rb
View
@@ -21,7 +21,7 @@ def find_all
end
def number_shipped_since(date)
- find_all.select { |order| order.shipped_on > date }.size
+ find_all.select { |order| order.shipped_on > date }.length
end
def unshipped_value
1  lib/mocha.rb
View
@@ -1,5 +1,6 @@
require 'mocha_standalone'
require 'mocha/test_case_adapter'
+require 'mocha/configuration'
require 'test/unit/testcase'
28 lib/mocha/any_instance_method.rb
View
@@ -15,21 +15,41 @@ def mock
end
def hide_original_method
- stubbee.class_eval "alias_method :#{hidden_method}, :#{method}" if stubbee.method_defined?(method)
+ if method_exists?(method)
+ begin
+ stubbee.class_eval("alias_method :#{hidden_method}, :#{method}", __FILE__, __LINE__)
+ rescue NameError
+ # deal with nasties like ActiveRecord::Associations::AssociationProxy
+ end
+ end
end
def define_new_method
- stubbee.class_eval "def #{method}(*args, &block); self.class.any_instance.mocha.method_missing(:#{method}, *args, &block); end"
+ stubbee.class_eval("def #{method}(*args, &block); self.class.any_instance.mocha.method_missing(:#{method}, *args, &block); end", __FILE__, __LINE__)
end
def remove_new_method
- stubbee.class_eval "remove_method :#{method}"
+ stubbee.class_eval("remove_method :#{method}", __FILE__, __LINE__)
end
def restore_original_method
- stubbee.class_eval "alias_method :#{method}, :#{hidden_method}; remove_method :#{hidden_method}" if stubbee.method_defined?(hidden_method)
+ if method_exists?(hidden_method)
+ begin
+ stubbee.class_eval("alias_method :#{method}, :#{hidden_method}; remove_method :#{hidden_method}", __FILE__, __LINE__)
+ rescue NameError
+ # deal with nasties like ActiveRecord::Associations::AssociationProxy
+ end
+ end
end
+ def method_exists?(method)
+ existing_methods = []
+ existing_methods += stubbee.public_instance_methods(false)
+ existing_methods += stubbee.protected_instance_methods(false)
+ existing_methods += stubbee.private_instance_methods(false)
+ existing_methods.any? { |m| m.to_s == method.to_s }
+ end
+
end
end
111 lib/mocha/auto_verify.rb
View
@@ -1,111 +0,0 @@
-require 'mocha/mock'
-
-module Mocha # :nodoc:
-
- # Methods added to TestCase allowing creation of traditional mock objects.
- #
- # Mocks created this way will have their expectations automatically verified at the end of the test.
- #
- # See Mock for methods on mock objects.
- module AutoVerify
-
- def mocks # :nodoc:
- @mocks ||= []
- end
-
- def reset_mocks # :nodoc:
- @mocks = nil
- end
-
- # :call-seq: mock(name) -> mock object
- # mock(expected_methods = {}) -> mock object
- # mock(name, expected_methods = {}) -> mock object
- #
- # Creates a mock object.
- #
- # +name+ is a +String+ identifier for the mock object.
- #
- # +expected_methods+ is a +Hash+ with expected method name symbols as keys and corresponding return values as values.
- #
- # Note that (contrary to expectations set up by #stub) these expectations <b>must</b> be fulfilled during the test.
- # def test_product
- # product = mock('ipod_product', :manufacturer => 'ipod', :price => 100)
- # assert_equal 'ipod', product.manufacturer
- # assert_equal 100, product.price
- # # an error will be raised unless both Product#manufacturer and Product#price have been called
- # end
- def mock(*args)
- name, expectations = name_and_expectations_from_args(args)
- mock = Mock.new(false, name)
- mock.expects(expectations)
- mocks << mock
- mock
- end
-
- # :call-seq: stub(name) -> mock object
- # stub(stubbed_methods = {}) -> mock object
- # stub(name, stubbed_methods = {}) -> mock object
- #
- # Creates a mock object.
- #
- # +name+ is a +String+ identifier for the mock object.
- #
- # +stubbed_methods+ is a +Hash+ with stubbed method name symbols as keys and corresponding return values as values.
- #
- # Note that (contrary to expectations set up by #mock) these expectations <b>need not</b> be fulfilled during the test.
- # def test_product
- # product = stub('ipod_product', :manufacturer => 'ipod', :price => 100)
- # assert_equal 'ipod', product.manufacturer
- # assert_equal 100, product.price
- # # an error will not be raised even if Product#manufacturer and Product#price have not been called
- # end
- def stub(*args)
- name, expectations = name_and_expectations_from_args(args)
- mock = Mock.new(false, name)
- mock.stubs(expectations)
- mocks << mock
- mock
- end
-
- # :call-seq: stub_everything(name) -> mock object
- # stub_everything(stubbed_methods = {}) -> mock object
- # stub_everything(name, stubbed_methods = {}) -> mock object
- #
- # Creates a mock object that accepts calls to any method.
- #
- # By default it will return +nil+ for any method call.
- #
- # +name+ and +stubbed_methods+ work in the same way as for #stub.
- # def test_product
- # product = stub_everything('ipod_product', :price => 100)
- # assert_nil product.manufacturer
- # assert_nil product.any_old_method
- # assert_equal 100, product.price
- # end
- def stub_everything(*args)
- name, expectations = name_and_expectations_from_args(args)
- mock = Mock.new(true, name)
- mock.stubs(expectations)
- mocks << mock
- mock
- end
-
- def verify_mocks # :nodoc:
- mocks.each { |mock| mock.verify { yield if block_given? } }
- end
-
- def teardown_mocks # :nodoc:
- reset_mocks
- end
-
- private
-
- def name_and_expectations_from_args(args) # :nodoc:
- name = args.first.is_a?(String) ? args.delete_at(0) : nil
- expectations = args.first || {}
- [name, expectations]
- end
-
- end
-
-end
0  trunk/lib/mocha/backtrace_filter.rb → lib/mocha/backtrace_filter.rb
View
File renamed without changes
0  trunk/lib/mocha/cardinality.rb → lib/mocha/cardinality.rb
View
File renamed without changes
12 lib/mocha/central.rb
View
@@ -11,20 +11,12 @@ def initialize
def stub(method)
unless stubba_methods.include?(method)
method.stub
- stubba_methods.push method
+ stubba_methods.push(method)
end
end
- def verify_all(&block)
- unique_mocks.each { |mock| mock.verify(&block) }
- end
-
- def unique_mocks
- stubba_methods.inject({}) { |mocks, method| mocks[method.mock.__id__] = method.mock; mocks }.values
- end
-
def unstub_all
- while stubba_methods.size > 0
+ while stubba_methods.length > 0
method = stubba_methods.pop
method.unstub
end
0  trunk/lib/mocha/change_state_side_effect.rb → lib/mocha/change_state_side_effect.rb
View
File renamed without changes
36 lib/mocha/class_method.rb
View
@@ -26,29 +26,45 @@ def mock
end
def hide_original_method
- stubbee.__metaclass__.class_eval "alias_method :#{hidden_method}, :#{method}" if stubbee.__metaclass__.method_defined?(method)
+ if method_exists?(method)
+ begin
+ stubbee.__metaclass__.class_eval("alias_method :#{hidden_method}, :#{method}", __FILE__, __LINE__)
+ rescue NameError
+ # deal with nasties like ActiveRecord::Associations::AssociationProxy
+ end
+ end
end
def define_new_method
- stubbee.__metaclass__.class_eval "def #{method}(*args, &block); mocha.method_missing(:#{method}, *args, &block); end"
+ stubbee.__metaclass__.class_eval("def #{method}(*args, &block); mocha.method_missing(:#{method}, *args, &block); end", __FILE__, __LINE__)
end
def remove_new_method
- stubbee.__metaclass__.class_eval "remove_method :#{method}"
+ stubbee.__metaclass__.class_eval("remove_method :#{method}", __FILE__, __LINE__)
end
def restore_original_method
- stubbee.__metaclass__.class_eval "alias_method :#{method}, :#{hidden_method}; remove_method :#{hidden_method}" if stubbee.__metaclass__.method_defined?(hidden_method)
+ if method_exists?(hidden_method)
+ begin
+ stubbee.__metaclass__.class_eval("alias_method :#{method}, :#{hidden_method}; remove_method :#{hidden_method}", __FILE__, __LINE__)
+ rescue NameError
+ # deal with nasties like ActiveRecord::Associations::AssociationProxy
+ end
+ end
end
def hidden_method
- method_name = method.to_s.gsub(/\W/) {|s| "_substituted_character_#{s[0]}_" }
+ if RUBY_VERSION < '1.9'
+ method_name = method.to_s.gsub(/\W/) { |s| "_substituted_character_#{s[0]}_" }
+ else
+ method_name = method.to_s.gsub(/\W/) { |s| "_substituted_character_#{s.ord}_" }
+ end
"__stubba__#{method_name}__stubba__"
end
def eql?(other)
return false unless (other.class == self.class)
- (stubbee == other.stubbee) and (method == other.method)
+ (stubbee.object_id == other.stubbee.object_id) and (method == other.method)
end
alias_method :==, :eql?
@@ -56,6 +72,14 @@ def eql?(other)
def to_s
"#{stubbee}.#{method}"
end
+
+ def method_exists?(method)
+ existing_methods = []
+ existing_methods += stubbee.public_methods(true) - stubbee.superclass.public_methods(true)
+ existing_methods += stubbee.protected_methods(true) - stubbee.superclass.protected_methods(true)
+ existing_methods += stubbee.private_methods(true) - stubbee.superclass.private_methods(true)
+ existing_methods.any? { |m| m.to_s == method.to_s }
+ end
end
0  trunk/lib/mocha/configuration.rb → lib/mocha/configuration.rb
View
File renamed without changes
2  lib/mocha/exception_raiser.rb
View
@@ -7,7 +7,7 @@ def initialize(exception, message)
end
def evaluate
- raise @exception, @exception.to_s if @exception == Interrupt
+ raise @exception, @exception.to_s if @exception.is_a?(Module) && @exception.ancestors.include?(Interrupt)
raise @exception, @message if @message
raise @exception
end
241 lib/mocha/expectation.rb
View
@@ -1,56 +1,19 @@
-require 'mocha/infinite_range'
-require 'mocha/pretty_parameters'
+require 'mocha/method_matcher'
+require 'mocha/parameters_matcher'
require 'mocha/expectation_error'
require 'mocha/return_values'
require 'mocha/exception_raiser'
require 'mocha/yield_parameters'
require 'mocha/is_a'
+require 'mocha/in_state_ordering_constraint'
+require 'mocha/change_state_side_effect'
+require 'mocha/cardinality'
module Mocha # :nodoc:
# Methods on expectations returned from Mock#expects, Mock#stubs, Object#expects and Object#stubs.
class Expectation
- # :stopdoc:
-
- class AlwaysEqual
- def ==(other)
- true
- end
- end
-
- attr_reader :method_name, :backtrace
-
- def initialize(mock, method_name, backtrace = nil)
- @mock, @method_name = mock, method_name
- @expected_count = 1
- @parameters, @parameter_block = AlwaysEqual.new, nil
- @invoked_count, @return_values = 0, ReturnValues.new
- @backtrace = backtrace || caller
- @yield_parameters = YieldParameters.new
- @final_expectation = false
- end
-
- def match?(method_name, *arguments)
- return false unless @method_name == method_name
- if @parameter_block then
- return false unless @parameter_block.call(*arguments)
- else
- return false unless (@parameters == arguments)
- end
- return true
- end
-
- def invocations_allowed?
- if @expected_count.is_a?(Range) then
- @invoked_count < @expected_count.last
- else
- @invoked_count < @expected_count
- end
- end
-
- # :startdoc:
-
# :call-seq: times(range) -> expectation
#
# Modifies expectation so that the number of calls to the expected method must be within a specific +range+.
@@ -76,7 +39,7 @@ def invocations_allowed?
# object.expected_method
# # => verify fails
def times(range)
- @expected_count = range
+ @cardinality = Cardinality.times(range)
self
end
@@ -98,8 +61,8 @@ def times(range)
# object = mock()
# object.expects(:expected_method).once
# # => verify fails
- def once()
- times(1)
+ def once
+ @cardinality = Cardinality.exactly(1)
self
end
@@ -116,7 +79,7 @@ def once()
# object.expected_method
# # => verify succeeds
def never
- times(0)
+ @cardinality = Cardinality.exactly(0)
self
end
@@ -133,7 +96,7 @@ def never
# object.expected_method
# # => verify fails
def at_least(minimum_number_of_times)
- times(Range.at_least(minimum_number_of_times))
+ @cardinality = Cardinality.at_least(minimum_number_of_times)
self
end
@@ -148,7 +111,7 @@ def at_least(minimum_number_of_times)
# object = mock()
# object.expects(:expected_method).at_least_once
# # => verify fails
- def at_least_once()
+ def at_least_once
at_least(1)
self
end
@@ -166,7 +129,7 @@ def at_least_once()
# 3.times { object.expected_method }
# # => verify fails
def at_most(maximum_number_of_times)
- times(Range.at_most(maximum_number_of_times))
+ @cardinality = Cardinality.at_most(maximum_number_of_times)
self
end
@@ -187,9 +150,9 @@ def at_most_once()
self
end
- # :call-seq: with(*arguments, &parameter_block) -> expectation
+ # :call-seq: with(*expected_parameters, &matching_block) -> expectation
#
- # Modifies expectation so that the expected method must be called with specified +arguments+.
+ # Modifies expectation so that the expected method must be called with +expected_parameters+.
# object = mock()
# object.expects(:expected_method).with(:param1, :param2)
# object.expected_method(:param1, :param2)
@@ -201,7 +164,7 @@ def at_most_once()
# # => verify fails
# May be used with parameter matchers in Mocha::ParameterMatchers.
#
- # If a +parameter_block+ is given, the block is called with the parameters passed to the expected method.
+ # If a +matching_block+ is given, the block is called with the parameters passed to the expected method.
# The expectation is matched if the block evaluates to +true+.
# object = mock()
# object.expects(:expected_method).with() { |value| value % 4 == 0 }
@@ -212,9 +175,8 @@ def at_most_once()
# object.expects(:expected_method).with() { |value| value % 4 == 0 }
# object.expected_method(17)
# # => verify fails
- def with(*arguments, &parameter_block)
- @parameters, @parameter_block = arguments, parameter_block
- class << @parameters; def to_s; join(', '); end; end
+ def with(*expected_parameters, &matching_block)
+ @parameters_matcher = ParametersMatcher.new(expected_parameters, &matching_block)
self
end
@@ -263,7 +225,7 @@ def multiple_yields(*parameter_groups)
end
# :call-seq: returns(value) -> expectation
- # :call-seq: returns(*values) -> expectation
+ # returns(*values) -> expectation
#
# Modifies expectation so that when the expected method is called, it returns the specified +value+.
# object = mock()
@@ -287,16 +249,6 @@ def multiple_yields(*parameter_groups)
# object.expected_method # => 1
# object.expected_method # => 2
# object.expected_method # => raises exception of class Exception1
- # If +value+ is a +Proc+, then the expected method will return the result of calling <tt>Proc#call</tt>.
- #
- # This usage is _deprecated_.
- # Use explicit multiple return values and/or multiple expectations instead.
- #
- # A +Proc+ instance will be treated the same as any other value in a future release.
- # object = mock()
- # object.stubs(:stubbed_method).returns(lambda { rand(100) })
- # object.stubbed_method # => 41
- # object.stubbed_method # => 77
def returns(*values)
@return_values += ReturnValues.build(*values)
self
@@ -325,22 +277,132 @@ def raises(exception = RuntimeError, message = nil)
end
# :call-seq: then() -> expectation
+ # then(state_machine.is(state)) -> expectation
#
- # Syntactic sugar to improve readability. Has no effect on state of the expectation.
+ # <tt>then()</tt> is used as syntactic sugar to improve readability. It has no effect on state of the expectation.
# object = mock()
# object.stubs(:expected_method).returns(1, 2).then.raises(Exception).then.returns(4)
# object.expected_method # => 1
# object.expected_method # => 2
# object.expected_method # => raises exception of class Exception
# object.expected_method # => 4
- def then
+ #
+ # <tt>then(state_machine.is(state))</tt> is used to change the +state_machine+ to the specified +state+ when the invocation occurs.
+ #
+ # See also Standalone#states, StateMachine and Expectation#when.
+ # power = states('power').starts_as('off')
+ #
+ # radio = mock('radio')
+ # radio.expects(:switch_on).then(power.is('on'))
+ # radio.expects(:select_channel).with('BBC Radio 4').when(power.is('on'))
+ # radio.expects(:adjust_volume).with(+5).when(power.is('on'))
+ # radio.expects(:select_channel).with('BBC World Service').when(power.is('on'))
+ # radio.expects(:adjust_volume).with(-5).when(power.is('on'))
+ # radio.expects(:switch_off).then(power.is('off'))
+ def then(*parameters)
+ if parameters.length == 1
+ state = parameters.first
+ add_side_effect(ChangeStateSideEffect.new(state))
+ end
+ self
+ end
+
+ # :call-seq: when(state_machine.is(state)) -> exception
+ #
+ # Constrains the expectation to occur only when the +state_machine+ is in the named +state+.
+ #
+ # See also Standalone#states, StateMachine#starts_as and Expectation#then.
+ # power = states('power').starts_as('off')
+ #
+ # radio = mock('radio')
+ # radio.expects(:switch_on).then(power.is('on'))
+ # radio.expects(:select_channel).with('BBC Radio 4').when(power.is('on'))
+ # radio.expects(:adjust_volume).with(+5).when(power.is('on'))
+ # radio.expects(:select_channel).with('BBC World Service').when(power.is('on'))
+ # radio.expects(:adjust_volume).with(-5).when(power.is('on'))
+ # radio.expects(:switch_off).then(power.is('off'))
+ def when(state_predicate)
+ add_ordering_constraint(InStateOrderingConstraint.new(state_predicate))
+ self
+ end
+
+ # :call-seq: in_sequence(*sequences) -> expectation
+ #
+ # Constrains this expectation so that it must be invoked at the current point in the sequence.
+ #
+ # To expect a sequence of invocations, write the expectations in order and add the in_sequence(sequence) clause to each one.
+ #
+ # Expectations in a sequence can have any invocation count.
+ #
+ # If an expectation in a sequence is stubbed, rather than expected, it can be skipped in the sequence.
+ #
+ # See also Standalone#sequence.
+ # breakfast = sequence('breakfast')
+ #
+ # egg = mock('egg')
+ # egg.expects(:crack).in_sequence(breakfast)
+ # egg.expects(:fry).in_sequence(breakfast)
+ # egg.expects(:eat).in_sequence(breakfast)
+ def in_sequence(*sequences)
+ sequences.each { |sequence| add_in_sequence_ordering_constraint(sequence) }
self
end
# :stopdoc:
+ attr_reader :backtrace
+
+ def initialize(mock, expected_method_name, backtrace = nil)
+ @mock = mock
+ @method_matcher = MethodMatcher.new(expected_method_name)
+ @parameters_matcher = ParametersMatcher.new
+ @ordering_constraints = []
+ @side_effects = []
+ @cardinality, @invocation_count = Cardinality.exactly(1), 0
+ @return_values = ReturnValues.new
+ @yield_parameters = YieldParameters.new
+ @backtrace = backtrace || caller
+ end
+
+ def add_ordering_constraint(ordering_constraint)
+ @ordering_constraints << ordering_constraint
+ end
+
+ def add_in_sequence_ordering_constraint(sequence)
+ sequence.constrain_as_next_in_sequence(self)
+ end
+
+ def add_side_effect(side_effect)
+ @side_effects << side_effect
+ end
+
+ def perform_side_effects
+ @side_effects.each { |side_effect| side_effect.perform }
+ end
+
+ def in_correct_order?
+ @ordering_constraints.all? { |ordering_constraint| ordering_constraint.allows_invocation_now? }
+ end
+
+ def matches_method?(method_name)
+ @method_matcher.match?(method_name)
+ end
+
+ def match?(actual_method_name, *actual_parameters)
+ @method_matcher.match?(actual_method_name) && @parameters_matcher.match?(actual_parameters) && in_correct_order?
+ end
+
+ def invocations_allowed?
+ @cardinality.invocations_allowed?(@invocation_count)
+ end
+
+ def satisfied?
+ @cardinality.satisfied?(@invocation_count)
+ end
+
def invoke
- @invoked_count += 1
+ @invocation_count += 1
+ perform_side_effects()
if block_given? then
@yield_parameters.next_invocation.each do |yield_parameters|
yield(*yield_parameters)
@@ -349,34 +411,35 @@ def invoke
@return_values.next
end
- def verify
- yield(self) if block_given?
- unless (@expected_count === @invoked_count) then
- error = ExpectationError.new(error_message(@expected_count, @invoked_count))
- error.set_backtrace(filtered_backtrace)
- raise error
- end
+ def verified?(assertion_counter = nil)
+ assertion_counter.increment if assertion_counter && @cardinality.needs_verifying?
+ @cardinality.verified?(@invocation_count)
end
- def mocha_lib_directory
- File.expand_path(File.join(File.dirname(__FILE__), "..")) + File::SEPARATOR
+ def used?
+ @cardinality.used?(@invocation_count)
end
- def filtered_backtrace
- backtrace.reject { |location| Regexp.new(mocha_lib_directory).match(File.expand_path(location)) }
- end
-
- def method_signature
- return "#{method_name}" if @parameters.__is_a__(AlwaysEqual)
- "#{@method_name}(#{PrettyParameters.new(@parameters).pretty})"
+ def mocha_inspect
+ message = "#{@cardinality.mocha_inspect}, "
+ if @invocation_count > 0
+ message << "already invoked #{@invocation_count} time"
+ message << "s" if @invocation_count > 1
+ else
+ message << "never invoked"
+ end
+ message << ": "
+ message << method_signature
+ message << "; #{@ordering_constraints.map { |oc| oc.mocha_inspect }.join("; ")}" unless @ordering_constraints.empty?
+ message
end
- def error_message(expected_count, actual_count)
- "#{@mock.mocha_inspect}.#{method_signature} - expected calls: #{expected_count.mocha_inspect}, actual calls: #{actual_count}"
+ def method_signature
+ "#{@mock.mocha_inspect}.#{@method_matcher.mocha_inspect}#{@parameters_matcher.mocha_inspect}"
end
-
+
# :startdoc:
end
-end
+end
11 lib/mocha/expectation_error.rb
View
@@ -1,6 +1,15 @@
+require 'mocha/backtrace_filter'
+
module Mocha
class ExpectationError < StandardError
+
+ def initialize(message = nil, backtrace = [])
+ super(message)
+ filter = BacktraceFilter.new
+ set_backtrace(filter.filtered(backtrace))
+ end
+
end
-end
+end
37 lib/mocha/expectation_list.rb
View
@@ -5,41 +5,38 @@ class ExpectationList
def initialize
@expectations = []
end
-
+
def add(expectation)
@expectations << expectation
+ expectation
end
-
- def respond_to?(method_name)
- @expectations.any? { |expectation| expectation.method_name == method_name }
+
+ def matches_method?(method_name)
+ @expectations.any? { |expectation| expectation.matches_method?(method_name) }
end
-
+
def detect(method_name, *arguments)
- expectations = @expectations.reverse.select { |expectation| expectation.match?(method_name, *arguments) }
- expectation = expectations.detect { |expectation| expectation.invocations_allowed? }
+ expectations = @expectations.reverse.select { |e| e.match?(method_name, *arguments) }
+ expectation = expectations.detect { |e| e.invocations_allowed? }
expectation || expectations.first
end
-
- def similar(method_name)
- @expectations.select { |expectation| expectation.method_name == method_name }
- end
-
- def verify(&block)
- @expectations.each { |expectation| expectation.verify(&block) }
+
+ def verified?(assertion_counter = nil)
+ @expectations.all? { |expectation| expectation.verified?(assertion_counter) }
end
-
+
def to_a
@expectations
end
-
+
def to_set
@expectations.to_set
end
-
- def size
- @expectations.size
+
+ def length
+ @expectations.length
end
-
+
end
end
0  trunk/lib/mocha/in_state_ordering_constraint.rb → lib/mocha/in_state_ordering_constraint.rb
View
File renamed without changes
25 lib/mocha/infinite_range.rb
View
@@ -1,25 +0,0 @@
-class Range
-
- def self.at_least(minimum_value)
- Range.new(minimum_value, infinite)
- end
-
- def self.at_most(maximum_value)
- Range.new(-infinite, maximum_value, false)
- end
-
- def self.infinite
- 1/0.0
- end
-
- def mocha_inspect
- if first.respond_to?(:to_f) and first.to_f.infinite? then
- return "at most #{last}"
- elsif last.respond_to?(:to_f) and last.to_f.infinite? then
- return "at least #{first}"
- else
- to_s
- end
- end
-
-end
9 lib/mocha/instance_method.rb
View
@@ -3,6 +3,15 @@
module Mocha
class InstanceMethod < ClassMethod
+
+ def method_exists?(method)
+ existing_methods = []
+ existing_methods += stubbee.public_methods(false)
+ existing_methods += stubbee.protected_methods(false)
+ existing_methods += stubbee.private_methods(false)
+ existing_methods.any? { |m| m.to_s == method.to_s }
+ end
+
end
end
0  trunk/lib/mocha/logger.rb → lib/mocha/logger.rb
View
File renamed without changes
0  trunk/lib/mocha/method_matcher.rb → lib/mocha/method_matcher.rb
View
File renamed without changes
27 lib/mocha/missing_expectation.rb
View
@@ -1,27 +0,0 @@
-require 'mocha/expectation'
-
-module Mocha # :nodoc:
-
- class MissingExpectation < Expectation # :nodoc:
-
- def initialize(mock, method_name)
- super
- @invoked_count = true
- end
-
- def verify
- msg = error_message(0, 1)
- similar_expectations_list = @mock.similar_expectations(@method_name).collect { |expectation| expectation.method_signature }.join("\n")
- msg << "\nSimilar expectations:\n#{similar_expectations_list}" unless similar_expectations_list.empty?
- error = ExpectationError.new(msg)
- error.set_backtrace(filtered_backtrace)
- raise error if @invoked_count
- end
-
- def similar_expectations
- @mock.similar_expectations(method_name)
- end
-
- end
-
-end
81 lib/mocha/mock.rb
View
@@ -1,8 +1,10 @@
require 'mocha/expectation'
require 'mocha/expectation_list'
-require 'mocha/stub'
-require 'mocha/missing_expectation'
require 'mocha/metaclass'
+require 'mocha/names'
+require 'mocha/mockery'
+require 'mocha/method_matcher'
+require 'mocha/parameters_matcher'
module Mocha # :nodoc:
@@ -11,19 +13,6 @@ module Mocha # :nodoc:
# Methods return an Expectation which can be further modified by methods on Expectation.
class Mock
- # :stopdoc:
-
- def initialize(stub_everything = false, name = nil)
- @stub_everything = stub_everything
- @mock_name = name
- @expectations = ExpectationList.new
- @responder = nil
- end
-
- attr_reader :stub_everything, :expectations
-
- # :startdoc:
-
# :call-seq: expects(method_name) -> expectation
# expects(method_names) -> last expectation
#
@@ -51,10 +40,12 @@ def initialize(stub_everything = false, name = nil)
def expects(method_name_or_hash, backtrace = nil)
if method_name_or_hash.is_a?(Hash) then
method_name_or_hash.each do |method_name, return_value|
- add_expectation(Expectation.new(self, method_name, backtrace).returns(return_value))
+ ensure_method_not_already_defined(method_name)
+ @expectations.add(Expectation.new(self, method_name, backtrace).returns(return_value))
end
else
- add_expectation(Expectation.new(self, method_name_or_hash, backtrace))
+ ensure_method_not_already_defined(method_name_or_hash)
+ @expectations.add(Expectation.new(self, method_name_or_hash, backtrace))
end
end
@@ -82,10 +73,12 @@ def expects(method_name_or_hash, backtrace = nil)
def stubs(method_name_or_hash, backtrace = nil)
if method_name_or_hash.is_a?(Hash) then
method_name_or_hash.each do |method_name, return_value|
- add_expectation(Stub.new(self, method_name, backtrace).returns(return_value))
+ ensure_method_not_already_defined(method_name)
+ @expectations.add(Expectation.new(self, method_name, backtrace).at_least(0).returns(return_value))
end
else
- add_expectation(Stub.new(self, method_name_or_hash, backtrace))
+ ensure_method_not_already_defined(method_name_or_hash)
+ @expectations.add(Expectation.new(self, method_name_or_hash, backtrace).at_least(0))
end
end
@@ -135,6 +128,16 @@ def responds_like(object)
end
# :stopdoc:
+
+ def initialize(name = nil, &block)
+ @name = name || DefaultName.new(self)
+ @expectations = ExpectationList.new
+ @everything_stubbed = false
+ @responder = nil
+ instance_eval(&block) if block
+ end
+
+ attr_reader :everything_stubbed, :expectations
alias_method :__expects__, :expects
@@ -144,11 +147,12 @@ def responds_like(object)
def add_expectation(expectation)
@expectations.add(expectation)
- method_name = expectation.method_name
- self.__metaclass__.send(:undef_method, method_name) if self.__metaclass__.method_defined?(method_name)
- expectation
end
-
+
+ def stub_everything
+ @everything_stubbed = true
+ end
+
def method_missing(symbol, *arguments, &block)
if @responder and not @responder.respond_to?(symbol)
raise NoMethodError, "undefined method `#{symbol}' for #{self.mocha_inspect} which responds like #{@responder.mocha_inspect}"
@@ -156,7 +160,7 @@ def method_missing(symbol, *arguments, &block)
matching_expectation = @expectations.detect(symbol, *arguments)
if matching_expectation then
matching_expectation.invoke(&block)
- elsif stub_everything then
+ elsif @everything_stubbed then
return
else
unexpected_method_called(symbol, *arguments)
@@ -167,30 +171,33 @@ def respond_to?(symbol)
if @responder then
@responder.respond_to?(symbol)
else
- @expectations.respond_to?(symbol)
+ @expectations.matches_method?(symbol)
end
end
-
+
def unexpected_method_called(symbol, *arguments)
- MissingExpectation.new(self, symbol).with(*arguments).verify
+ method_matcher = MethodMatcher.new(symbol)
+ parameters_matcher = ParametersMatcher.new(arguments)
+ method_signature = "#{mocha_inspect}.#{method_matcher.mocha_inspect}#{parameters_matcher.mocha_inspect}"
+ message = "unexpected invocation: #{method_signature}\n"
+ message << Mockery.instance.mocha_inspect
+ raise ExpectationError.new(message, caller)
end
-
- def verify(&block)
- @expectations.verify(&block)
+
+ def verified?(assertion_counter = nil)
+ @expectations.verified?(assertion_counter)
end
-
+
def mocha_inspect
- address = self.__id__ * 2
- address += 0x100000000 if address < 0
- @mock_name ? "#<Mock:#{@mock_name}>" : "#<Mock:0x#{'%x' % address}>"
+ @name.mocha_inspect
end
def inspect
mocha_inspect
end
-
- def similar_expectations(method_name)
- @expectations.similar(method_name)
+
+ def ensure_method_not_already_defined(method_name)
+ self.__metaclass__.send(:undef_method, method_name) if self.__metaclass__.method_defined?(method_name)
end
# :startdoc:
0  trunk/lib/mocha/mockery.rb → lib/mocha/mockery.rb
View
File renamed without changes
0  trunk/lib/mocha/module_method.rb → lib/mocha/module_method.rb
View
File renamed without changes
0  trunk/lib/mocha/names.rb → lib/mocha/names.rb
View
File renamed without changes
35 lib/mocha/object.rb
View
@@ -1,6 +1,7 @@
-require 'mocha/mock'
+require 'mocha/mockery'
require 'mocha/instance_method'
require 'mocha/class_method'
+require 'mocha/module_method'
require 'mocha/any_instance_method'
# Methods added all objects to allow mocking and stubbing on real objects.
@@ -9,7 +10,7 @@
class Object
def mocha # :nodoc:
- @mocha ||= Mocha::Mock.new
+ @mocha ||= Mocha::Mockery.instance.mock_impersonating(self)
end
def reset_mocha # :nodoc:
@@ -35,9 +36,11 @@ def stubba_object # :nodoc:
# The original implementation of <tt>Product#save</tt> is replaced temporarily.
#
# The original implementation of <tt>Product#save</tt> is restored at the end of the test.
- def expects(symbol)
+ def expects(symbol)
+ mockery = Mocha::Mockery.instance
+ mockery.on_stubbing(self, symbol)
method = stubba_method.new(stubba_object, symbol)
- $stubba.stub(method)
+ mockery.stubba.stub(method)
mocha.expects(symbol, caller)
end
@@ -52,14 +55,18 @@ def expects(symbol)
# The original implementation of <tt>Product#save</tt> is replaced temporarily.
#
# The original implementation of <tt>Product#save</tt> is restored at the end of the test.
- def stubs(symbol)
+ def stubs(symbol)
+ mockery = Mocha::Mockery.instance
+ mockery.on_stubbing(self, symbol)
method = stubba_method.new(stubba_object, symbol)
- $stubba.stub(method)
+ mockery.stubba.stub(method)
mocha.stubs(symbol, caller)
end
- def verify # :nodoc:
- mocha.verify
+ def method_exists?(symbol, include_public_methods = true)
+ existing_methods = private_methods(include_superclass_methods = true) + protected_methods(include_superclass_methods = true)
+ existing_methods += public_methods(include_superclass_methods = true) if include_public_methods
+ existing_methods.any? { |m| m.to_s == symbol.to_s } || (respond_to?(symbol) && include_public_methods)
end
end
@@ -67,7 +74,7 @@ def verify # :nodoc:
class Module # :nodoc:
def stubba_method
- Mocha::ClassMethod
+ Mocha::ModuleMethod
end
end
@@ -84,6 +91,10 @@ def initialize(klass)
@stubba_object = klass
end
+ def mocha
+ @mocha ||= Mocha::Mockery.instance.mock_impersonating_any_instance_of(@stubba_object)
+ end
+
def stubba_method
Mocha::AnyInstanceMethod
end
@@ -92,6 +103,12 @@ def stubba_object
@stubba_object
end
+ def method_exists?(symbol, include_public_methods = true)
+ existing_methods = @stubba_object.private_instance_methods(include_superclass_methods = true) + @stubba_object.protected_instance_methods(include_superclass_methods = true)
+ existing_methods += @stubba_object.public_instance_methods(include_superclass_methods = true) if include_public_methods
+ existing_methods.any? { |m| m.to_s == symbol.to_s }
+ end
+
end
# :call-seq: any_instance -> mock object
21 lib/mocha/parameter_matchers.rb
View
@@ -1,9 +1,26 @@
module Mocha
- # Used as parameters for Expectation#with to restrict the parameter values which will match the expectation.
+ # Used as parameters for Expectation#with to restrict the parameter values which will match the expectation. Can be nested.
module ParameterMatchers; end
end
+require 'mocha/parameter_matchers/object'
-Dir[File.expand_path(File.join(File.dirname(__FILE__), 'parameter_matchers', "*.rb"))].each { |lib| require lib }
+require 'mocha/parameter_matchers/all_of'
+require 'mocha/parameter_matchers/any_of'
+require 'mocha/parameter_matchers/any_parameters'
+require 'mocha/parameter_matchers/anything'
+require 'mocha/parameter_matchers/equals'
+require 'mocha/parameter_matchers/has_entry'
+require 'mocha/parameter_matchers/has_entries'
+require 'mocha/parameter_matchers/has_key'
+require 'mocha/parameter_matchers/has_value'
+require 'mocha/parameter_matchers/includes'
+require 'mocha/parameter_matchers/instance_of'
+require 'mocha/parameter_matchers/is_a'
+require 'mocha/parameter_matchers/kind_of'
+require 'mocha/parameter_matchers/not'
+require 'mocha/parameter_matchers/optionally'
+require 'mocha/parameter_matchers/regexp_matches'
+require 'mocha/parameter_matchers/yaml_equivalent'
13 lib/mocha/parameter_matchers/all_of.rb
View
@@ -1,10 +1,12 @@
+require 'mocha/parameter_matchers/base'
+
module Mocha
module ParameterMatchers
- # :call-seq: all_of -> parameter_matcher
+ # :call-seq: all_of(*parameter_matchers) -> parameter_matcher
#
- # Matches if all +matchers+ match.
+ # Matches if all +parameter_matchers+ match.
# object = mock()
# object.expects(:method_1).with(all_of(includes(1), includes(3)))
# object.method_1([1, 3])
@@ -18,14 +20,15 @@ def all_of(*matchers)
AllOf.new(*matchers)
end
- class AllOf # :nodoc:
+ class AllOf < Base # :nodoc:
def initialize(*matchers)
@matchers = matchers
end
- def ==(parameter)
- @matchers.all? { |matcher| matcher == parameter }
+ def matches?(available_parameters)
+ parameter = available_parameters.shift
+ @matchers.all? { |matcher| matcher.to_matcher.matches?([parameter]) }
end
def mocha_inspect
13 lib/mocha/parameter_matchers/any_of.rb
View
@@ -1,10 +1,12 @@
+require 'mocha/parameter_matchers/base'
+
module Mocha
module ParameterMatchers
- # :call-seq: any_of -> parameter_matcher
+ # :call-seq: any_of(*parameter_matchers) -> parameter_matcher
#
- # Matches if any +matchers+ match.
+ # Matches if any +parameter_matchers+ match.
# object = mock()
# object.expects(:method_1).with(any_of(1, 3))
# object.method_1(1)
@@ -23,14 +25,15 @@ def any_of(*matchers)
AnyOf.new(*matchers)
end
- class AnyOf # :nodoc:
+ class AnyOf < Base # :nodoc:
def initialize(*matchers)
@matchers = matchers
end
- def ==(parameter)
- @matchers.any? { |matcher| matcher == parameter }
+ def matches?(available_parameters)
+ parameter = available_parameters.shift
+ @matchers.any? { |matcher| matcher.to_matcher.matches?([parameter]) }
end
def mocha_inspect
0  trunk/lib/mocha/parameter_matchers/any_parameters.rb → lib/mocha/parameter_matchers/any_parameters.rb
View
File renamed without changes
9 lib/mocha/parameter_matchers/anything.rb
View
@@ -1,8 +1,10 @@
+require 'mocha/parameter_matchers/base'
+
module Mocha
module ParameterMatchers
- # :call-seq: anything -> parameter_matcher
+ # :call-seq: anything() -> parameter_matcher
#
# Matches any object.
# object = mock()
@@ -13,9 +15,10 @@ def anything
Anything.new
end
- class Anything # :nodoc:
+ class Anything < Base # :nodoc:
- def ==(parameter)
+ def matches?(available_parameters)
+ available_parameters.shift
return true
end
0  trunk/lib/mocha/parameter_matchers/base.rb → lib/mocha/parameter_matchers/base.rb
View
File renamed without changes
0  trunk/lib/mocha/parameter_matchers/equals.rb → lib/mocha/parameter_matchers/equals.rb
View
File renamed without changes
0  trunk/lib/mocha/parameter_matchers/has_entries.rb → lib/mocha/parameter_matchers/has_entries.rb
View
File renamed without changes
29 lib/mocha/parameter_matchers/has_entry.rb
View
@@ -1,8 +1,11 @@
+require 'mocha/parameter_matchers/base'
+
module Mocha
module ParameterMatchers
# :call-seq: has_entry(key, value) -> parameter_matcher
+ # has_entry(key => value) -> parameter_matcher
#
# Matches +Hash+ containing entry with +key+ and +value+.
# object = mock()
@@ -11,25 +14,39 @@ module ParameterMatchers
# # no error raised
#
# object = mock()
+ # object.expects(:method_1).with(has_entry('key_1' => 1))
+ # object.method_1('key_1' => 1, 'key_2' => 2)
+ # # no error raised
+ #
+ # object = mock()
# object.expects(:method_1).with(has_entry('key_1', 1))
# object.method_1('key_1' => 2, 'key_2' => 1)
# # error raised, because method_1 was not called with Hash containing entry: 'key_1' => 1
- def has_entry(key, value)
+ #
+ # object = mock()
+ # object.expects(:method_1).with(has_entry('key_1' => 1))
+ # object.method_1('key_1' => 2, 'key_2' => 1)
+ # # error raised, because method_1 was not called with Hash containing entry: 'key_1' => 1
+ def has_entry(*options)
+ key, value = options.shift, options.shift
+ key, value = key.to_a[0][0..1] if key.is_a?(Hash)
HasEntry.new(key, value)
end
-
- class HasEntry # :nodoc:
+
+ class HasEntry < Base # :nodoc:
def initialize(key, value)
@key, @value = key, value
end
- def ==(parameter)
- parameter[@key] == @value
+ def matches?(available_parameters)
+ parameter = available_parameters.shift
+ matching_keys = parameter.keys.select { |key| @key.to_matcher.matches?([key]) }
+ matching_keys.any? { |key| @value.to_matcher.matches?([parameter[key]]) }
end
def mocha_inspect
- "has_entry(#{@key.mocha_inspect}, #{@value.mocha_inspect})"
+ "has_entry(#{@key.mocha_inspect} => #{@value.mocha_inspect})"
end
end
9 lib/mocha/parameter_matchers/has_key.rb
View
@@ -1,3 +1,5 @@
+require 'mocha/parameter_matchers/base'
+
module Mocha
module ParameterMatchers
@@ -18,14 +20,15 @@ def has_key(key)
HasKey.new(key)
end
- class HasKey # :nodoc:
+ class HasKey < Base # :nodoc:
def initialize(key)
@key = key
end
- def ==(parameter)
- parameter.keys.include?(@key)
+ def matches?(available_parameters)
+ parameter = available_parameters.shift
+ parameter.keys.any? { |key| @key.to_matcher.matches?([key]) }
end
def mocha_inspect
9 lib/mocha/parameter_matchers/has_value.rb
View
@@ -1,3 +1,5 @@
+require 'mocha/parameter_matchers/base'
+
module Mocha
module ParameterMatchers
@@ -18,14 +20,15 @@ def has_value(value)
HasValue.new(value)
end
- class HasValue # :nodoc:
+ class HasValue < Base # :nodoc:
def initialize(value)
@value = value
end
- def ==(parameter)
- parameter.values.include?(@value)
+ def matches?(available_parameters)
+ parameter = available_parameters.shift
+ parameter.values.any? { |value| @value.to_matcher.matches?([value]) }
end
def mocha_inspect
7 lib/mocha/parameter_matchers/includes.rb
View
@@ -1,3 +1,5 @@
+require 'mocha/parameter_matchers/base'
+
module Mocha
module ParameterMatchers
@@ -16,13 +18,14 @@ def includes(item)
Includes.new(item)
end
- class Includes # :nodoc:
+ class Includes < Base # :nodoc:
def initialize(item)
@item = item
end
- def ==(parameter)
+ def matches?(available_parameters)
+ parameter = available_parameters.shift
return parameter.include?(@item)
end
7 lib/mocha/parameter_matchers/instance_of.rb
View
@@ -1,3 +1,5 @@
+require 'mocha/parameter_matchers/base'
+
module Mocha
module ParameterMatchers
@@ -18,13 +20,14 @@ def instance_of(klass)
InstanceOf.new(klass)
end
- class InstanceOf # :nodoc:
+ class InstanceOf < Base # :nodoc:
def initialize(klass)
@klass = klass
end
- def ==(parameter)
+ def matches?(available_parameters)
+ parameter = available_parameters.shift
parameter.instance_of?(@klass)
end
0  trunk/lib/mocha/parameter_matchers/is_a.rb → lib/mocha/parameter_matchers/is_a.rb
View
File renamed without changes
7 lib/mocha/parameter_matchers/kind_of.rb
View
@@ -1,3 +1,5 @@
+require 'mocha/parameter_matchers/base'
+
module Mocha
module ParameterMatchers
@@ -18,13 +20,14 @@ def kind_of(klass)
KindOf.new(klass)
end
- class KindOf # :nodoc:
+ class KindOf < Base # :nodoc:
def initialize(klass)
@klass = klass
end
- def ==(parameter)
+ def matches?(available_parameters)
+ parameter = available_parameters.shift
parameter.kind_of?(@klass)
end
0  trunk/lib/mocha/parameter_matchers/not.rb → lib/mocha/parameter_matchers/not.rb
View
File renamed without changes
0  trunk/lib/mocha/parameter_matchers/object.rb → lib/mocha/parameter_matchers/object.rb
View
File renamed without changes
0  trunk/lib/mocha/parameter_matchers/optionally.rb → lib/mocha/parameter_matchers/optionally.rb
View
File renamed without changes
11 lib/mocha/parameter_matchers/regexp_matches.rb
View
@@ -1,10 +1,12 @@
+require 'mocha/parameter_matchers/base'
+
module Mocha
module ParameterMatchers
- # :call-seq: regexp_matches(regexp) -> parameter_matcher
+ # :call-seq: regexp_matches(regular_expression) -> parameter_matcher
#
- # Matches any object that matches the regular expression, +regexp+.
+ # Matches any object that matches +regular_expression+.
# object = mock()
# object.expects(:method_1).with(regexp_matches(/e/))
# object.method_1('hello')
@@ -19,13 +21,14 @@ def regexp_matches(regexp)
RegexpMatches.new(regexp)
end
- class RegexpMatches # :nodoc:
+ class RegexpMatches < Base # :nodoc:
def initialize(regexp)
@regexp = regexp
end
- def ==(parameter)
+ def matches?(available_parameters)
+ parameter = available_parameters.shift
parameter =~ @regexp
end
0  trunk/lib/mocha/parameter_matchers/responds_with.rb → lib/mocha/parameter_matchers/responds_with.rb
View
File renamed without changes
0  ...k/lib/mocha/parameter_matchers/yaml_equivalent.rb → lib/mocha/parameter_matchers/yaml_equivalent.rb
View
File renamed without changes
0  trunk/lib/mocha/parameters_matcher.rb → lib/mocha/parameters_matcher.rb
View
File renamed without changes
2  lib/mocha/pretty_parameters.rb