shoulda 2.11.3 → 3.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (262) hide show
  1. data/Gemfile +9 -0
  2. data/Gemfile.lock +108 -0
  3. data/README.md +95 -0
  4. data/Rakefile +6 -50
  5. data/lib/shoulda/version.rb +1 -1
  6. metadata +50 -269
  7. data/README.rdoc +0 -154
  8. data/bin/convert_to_should_syntax +0 -42
  9. data/lib/shoulda.rb +0 -9
  10. data/lib/shoulda/action_controller.rb +0 -34
  11. data/lib/shoulda/action_controller/macros.rb +0 -221
  12. data/lib/shoulda/action_controller/matchers.rb +0 -39
  13. data/lib/shoulda/action_controller/matchers/assign_to_matcher.rb +0 -112
  14. data/lib/shoulda/action_controller/matchers/filter_param_matcher.rb +0 -74
  15. data/lib/shoulda/action_controller/matchers/redirect_to_matcher.rb +0 -62
  16. data/lib/shoulda/action_controller/matchers/render_template_matcher.rb +0 -54
  17. data/lib/shoulda/action_controller/matchers/render_with_layout_matcher.rb +0 -99
  18. data/lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb +0 -74
  19. data/lib/shoulda/action_controller/matchers/respond_with_matcher.rb +0 -85
  20. data/lib/shoulda/action_controller/matchers/route_matcher.rb +0 -93
  21. data/lib/shoulda/action_controller/matchers/set_session_matcher.rb +0 -98
  22. data/lib/shoulda/action_controller/matchers/set_the_flash_matcher.rb +0 -100
  23. data/lib/shoulda/action_mailer.rb +0 -13
  24. data/lib/shoulda/action_mailer/assertions.rb +0 -42
  25. data/lib/shoulda/action_mailer/matchers.rb +0 -22
  26. data/lib/shoulda/action_mailer/matchers/have_sent_email.rb +0 -110
  27. data/lib/shoulda/active_record.rb +0 -16
  28. data/lib/shoulda/active_record/assertions.rb +0 -69
  29. data/lib/shoulda/active_record/helpers.rb +0 -32
  30. data/lib/shoulda/active_record/macros.rb +0 -457
  31. data/lib/shoulda/active_record/matchers.rb +0 -42
  32. data/lib/shoulda/active_record/matchers/allow_mass_assignment_of_matcher.rb +0 -83
  33. data/lib/shoulda/active_record/matchers/allow_value_matcher.rb +0 -110
  34. data/lib/shoulda/active_record/matchers/association_matcher.rb +0 -226
  35. data/lib/shoulda/active_record/matchers/ensure_inclusion_of_matcher.rb +0 -87
  36. data/lib/shoulda/active_record/matchers/ensure_length_of_matcher.rb +0 -141
  37. data/lib/shoulda/active_record/matchers/have_db_column_matcher.rb +0 -169
  38. data/lib/shoulda/active_record/matchers/have_db_index_matcher.rb +0 -112
  39. data/lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb +0 -59
  40. data/lib/shoulda/active_record/matchers/validate_acceptance_of_matcher.rb +0 -41
  41. data/lib/shoulda/active_record/matchers/validate_format_of_matcher.rb +0 -65
  42. data/lib/shoulda/active_record/matchers/validate_numericality_of_matcher.rb +0 -39
  43. data/lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb +0 -60
  44. data/lib/shoulda/active_record/matchers/validate_uniqueness_of_matcher.rb +0 -148
  45. data/lib/shoulda/active_record/matchers/validation_matcher.rb +0 -56
  46. data/lib/shoulda/assertions.rb +0 -79
  47. data/lib/shoulda/autoload_macros.rb +0 -46
  48. data/lib/shoulda/context.rb +0 -433
  49. data/lib/shoulda/helpers.rb +0 -8
  50. data/lib/shoulda/integrations/rspec.rb +0 -13
  51. data/lib/shoulda/integrations/rspec2.rb +0 -22
  52. data/lib/shoulda/integrations/test_unit.rb +0 -22
  53. data/lib/shoulda/macros.rb +0 -161
  54. data/lib/shoulda/private_helpers.rb +0 -13
  55. data/lib/shoulda/proc_extensions.rb +0 -14
  56. data/lib/shoulda/rails.rb +0 -8
  57. data/lib/shoulda/tasks.rb +0 -3
  58. data/lib/shoulda/tasks/list_tests.rake +0 -29
  59. data/lib/shoulda/tasks/yaml_to_shoulda.rake +0 -28
  60. data/rails/init.rb +0 -8
  61. data/test/README +0 -36
  62. data/test/fail_macros.rb +0 -55
  63. data/test/fixtures/addresses.yml +0 -3
  64. data/test/fixtures/friendships.yml +0 -0
  65. data/test/fixtures/posts.yml +0 -5
  66. data/test/fixtures/products.yml +0 -0
  67. data/test/fixtures/taggings.yml +0 -0
  68. data/test/fixtures/tags.yml +0 -9
  69. data/test/fixtures/users.yml +0 -6
  70. data/test/functional/posts_controller_test.rb +0 -121
  71. data/test/functional/users_controller_test.rb +0 -19
  72. data/test/matchers/action_mailer/have_sent_email_test.rb +0 -70
  73. data/test/matchers/active_record/allow_mass_assignment_of_matcher_test.rb +0 -74
  74. data/test/matchers/active_record/allow_value_matcher_test.rb +0 -64
  75. data/test/matchers/active_record/association_matcher_test.rb +0 -263
  76. data/test/matchers/active_record/ensure_inclusion_of_matcher_test.rb +0 -81
  77. data/test/matchers/active_record/ensure_length_of_matcher_test.rb +0 -158
  78. data/test/matchers/active_record/have_db_column_matcher_test.rb +0 -169
  79. data/test/matchers/active_record/have_db_index_matcher_test.rb +0 -91
  80. data/test/matchers/active_record/have_readonly_attributes_matcher_test.rb +0 -29
  81. data/test/matchers/active_record/validate_acceptance_of_matcher_test.rb +0 -44
  82. data/test/matchers/active_record/validate_format_of_matcher_test.rb +0 -39
  83. data/test/matchers/active_record/validate_numericality_of_matcher_test.rb +0 -52
  84. data/test/matchers/active_record/validate_presence_of_matcher_test.rb +0 -86
  85. data/test/matchers/active_record/validate_uniqueness_of_matcher_test.rb +0 -147
  86. data/test/matchers/controller/assign_to_matcher_test.rb +0 -45
  87. data/test/matchers/controller/filter_param_matcher_test.rb +0 -40
  88. data/test/matchers/controller/redirect_to_matcher_test.rb +0 -37
  89. data/test/matchers/controller/render_template_matcher_test.rb +0 -37
  90. data/test/matchers/controller/render_with_layout_matcher_test.rb +0 -47
  91. data/test/matchers/controller/respond_with_content_type_matcher_test.rb +0 -32
  92. data/test/matchers/controller/respond_with_matcher_test.rb +0 -96
  93. data/test/matchers/controller/route_matcher_test.rb +0 -75
  94. data/test/matchers/controller/set_session_matcher_test.rb +0 -48
  95. data/test/matchers/controller/set_the_flash_matcher.rb +0 -95
  96. data/test/other/autoload_macro_test.rb +0 -18
  97. data/test/other/context_test.rb +0 -372
  98. data/test/other/convert_to_should_syntax_test.rb +0 -63
  99. data/test/other/helpers_test.rb +0 -317
  100. data/test/other/private_helpers_test.rb +0 -32
  101. data/test/other/should_test.rb +0 -271
  102. data/test/rails2_model_builder.rb +0 -130
  103. data/test/rails2_root/app/controllers/application_controller.rb +0 -22
  104. data/test/rails2_root/app/controllers/posts_controller.rb +0 -87
  105. data/test/rails2_root/app/controllers/users_controller.rb +0 -84
  106. data/test/rails2_root/app/helpers/application_helper.rb +0 -3
  107. data/test/rails2_root/app/helpers/posts_helper.rb +0 -2
  108. data/test/rails2_root/app/helpers/users_helper.rb +0 -2
  109. data/test/rails2_root/app/models/address.rb +0 -7
  110. data/test/rails2_root/app/models/flea.rb +0 -11
  111. data/test/rails2_root/app/models/friendship.rb +0 -4
  112. data/test/rails2_root/app/models/notifier.rb +0 -8
  113. data/test/rails2_root/app/models/pets/cat.rb +0 -7
  114. data/test/rails2_root/app/models/pets/dog.rb +0 -10
  115. data/test/rails2_root/app/models/post.rb +0 -12
  116. data/test/rails2_root/app/models/product.rb +0 -12
  117. data/test/rails2_root/app/models/profile.rb +0 -2
  118. data/test/rails2_root/app/models/registration.rb +0 -2
  119. data/test/rails2_root/app/models/tag.rb +0 -8
  120. data/test/rails2_root/app/models/tagging.rb +0 -4
  121. data/test/rails2_root/app/models/treat.rb +0 -3
  122. data/test/rails2_root/app/models/user.rb +0 -32
  123. data/test/rails2_root/app/views/layouts/posts.rhtml +0 -19
  124. data/test/rails2_root/app/views/layouts/users.rhtml +0 -17
  125. data/test/rails2_root/app/views/layouts/wide.html.erb +0 -1
  126. data/test/rails2_root/app/views/notifier/the_email.html.erb +0 -1
  127. data/test/rails2_root/app/views/posts/edit.rhtml +0 -27
  128. data/test/rails2_root/app/views/posts/index.rhtml +0 -25
  129. data/test/rails2_root/app/views/posts/new.rhtml +0 -26
  130. data/test/rails2_root/app/views/posts/show.rhtml +0 -18
  131. data/test/rails2_root/app/views/users/edit.rhtml +0 -22
  132. data/test/rails2_root/app/views/users/index.rhtml +0 -22
  133. data/test/rails2_root/app/views/users/new.rhtml +0 -21
  134. data/test/rails2_root/app/views/users/show.rhtml +0 -13
  135. data/test/rails2_root/config/boot.rb +0 -110
  136. data/test/rails2_root/config/database.yml +0 -4
  137. data/test/rails2_root/config/environment.rb +0 -17
  138. data/test/rails2_root/config/environments/test.rb +0 -23
  139. data/test/rails2_root/config/initializers/new_rails_defaults.rb +0 -15
  140. data/test/rails2_root/config/initializers/shoulda.rb +0 -8
  141. data/test/rails2_root/config/routes.rb +0 -6
  142. data/test/rails2_root/db/migrate/001_create_users.rb +0 -19
  143. data/test/rails2_root/db/migrate/002_create_posts.rb +0 -13
  144. data/test/rails2_root/db/migrate/003_create_taggings.rb +0 -12
  145. data/test/rails2_root/db/migrate/004_create_tags.rb +0 -11
  146. data/test/rails2_root/db/migrate/005_create_dogs.rb +0 -12
  147. data/test/rails2_root/db/migrate/006_create_addresses.rb +0 -14
  148. data/test/rails2_root/db/migrate/007_create_fleas.rb +0 -11
  149. data/test/rails2_root/db/migrate/008_create_dogs_fleas.rb +0 -12
  150. data/test/rails2_root/db/migrate/009_create_products.rb +0 -17
  151. data/test/rails2_root/db/migrate/010_create_friendships.rb +0 -14
  152. data/test/rails2_root/db/migrate/011_create_treats.rb +0 -12
  153. data/test/rails2_root/db/migrate/20090506203502_create_profiles.rb +0 -12
  154. data/test/rails2_root/db/migrate/20090506203536_create_registrations.rb +0 -14
  155. data/test/rails2_root/db/migrate/20090513104502_create_cats.rb +0 -12
  156. data/test/rails2_root/db/schema.rb +0 -0
  157. data/test/rails2_root/log/test.log +0 -50381
  158. data/test/rails2_root/public/404.html +0 -30
  159. data/test/rails2_root/public/422.html +0 -30
  160. data/test/rails2_root/public/500.html +0 -30
  161. data/test/rails2_root/script/console +0 -3
  162. data/test/rails2_root/script/generate +0 -3
  163. data/test/rails2_root/test/shoulda_macros/custom_macro.rb +0 -6
  164. data/test/rails2_root/vendor/gems/gem_with_macro-0.0.1/shoulda_macros/gem_macro.rb +0 -6
  165. data/test/rails2_root/vendor/plugins/plugin_with_macro/shoulda_macros/plugin_macro.rb +0 -6
  166. data/test/rails2_test_helper.rb +0 -6
  167. data/test/rails3_model_builder.rb +0 -118
  168. data/test/rails3_root/Gemfile +0 -28
  169. data/test/rails3_root/README +0 -244
  170. data/test/rails3_root/Rakefile +0 -10
  171. data/test/rails3_root/app/controllers/application_controller.rb +0 -22
  172. data/test/rails3_root/app/controllers/posts_controller.rb +0 -87
  173. data/test/rails3_root/app/controllers/users_controller.rb +0 -82
  174. data/test/rails3_root/app/helpers/application_helper.rb +0 -2
  175. data/test/rails3_root/app/models/address.rb +0 -7
  176. data/test/rails3_root/app/models/flea.rb +0 -11
  177. data/test/rails3_root/app/models/friendship.rb +0 -4
  178. data/test/rails3_root/app/models/notifier.rb +0 -8
  179. data/test/rails3_root/app/models/pets/cat.rb +0 -7
  180. data/test/rails3_root/app/models/pets/dog.rb +0 -10
  181. data/test/rails3_root/app/models/post.rb +0 -12
  182. data/test/rails3_root/app/models/product.rb +0 -12
  183. data/test/rails3_root/app/models/profile.rb +0 -2
  184. data/test/rails3_root/app/models/registration.rb +0 -2
  185. data/test/rails3_root/app/models/tag.rb +0 -8
  186. data/test/rails3_root/app/models/tagging.rb +0 -4
  187. data/test/rails3_root/app/models/treat.rb +0 -3
  188. data/test/rails3_root/app/models/user.rb +0 -32
  189. data/test/rails3_root/app/views/layouts/application.html.erb +0 -14
  190. data/test/rails3_root/app/views/layouts/posts.rhtml +0 -19
  191. data/test/rails3_root/app/views/layouts/users.rhtml +0 -17
  192. data/test/rails3_root/app/views/layouts/wide.html.erb +0 -1
  193. data/test/rails3_root/app/views/notifier/the_email.html.erb +0 -1
  194. data/test/rails3_root/app/views/posts/edit.rhtml +0 -27
  195. data/test/rails3_root/app/views/posts/index.rhtml +0 -25
  196. data/test/rails3_root/app/views/posts/new.rhtml +0 -24
  197. data/test/rails3_root/app/views/posts/show.rhtml +0 -18
  198. data/test/rails3_root/app/views/users/edit.rhtml +0 -22
  199. data/test/rails3_root/app/views/users/index.rhtml +0 -22
  200. data/test/rails3_root/app/views/users/new.rhtml +0 -21
  201. data/test/rails3_root/app/views/users/show.rhtml +0 -13
  202. data/test/rails3_root/config.ru +0 -4
  203. data/test/rails3_root/config/application.rb +0 -46
  204. data/test/rails3_root/config/boot.rb +0 -6
  205. data/test/rails3_root/config/database.yml +0 -22
  206. data/test/rails3_root/config/environment.rb +0 -5
  207. data/test/rails3_root/config/environments/development.rb +0 -19
  208. data/test/rails3_root/config/environments/production.rb +0 -42
  209. data/test/rails3_root/config/environments/test.rb +0 -32
  210. data/test/rails3_root/config/initializers/backtrace_silencers.rb +0 -7
  211. data/test/rails3_root/config/initializers/inflections.rb +0 -10
  212. data/test/rails3_root/config/initializers/mime_types.rb +0 -5
  213. data/test/rails3_root/config/initializers/secret_token.rb +0 -7
  214. data/test/rails3_root/config/initializers/session_store.rb +0 -8
  215. data/test/rails3_root/config/locales/en.yml +0 -5
  216. data/test/rails3_root/config/routes.rb +0 -4
  217. data/test/rails3_root/db/migrate/001_create_users.rb +0 -19
  218. data/test/rails3_root/db/migrate/002_create_posts.rb +0 -13
  219. data/test/rails3_root/db/migrate/003_create_taggings.rb +0 -12
  220. data/test/rails3_root/db/migrate/004_create_tags.rb +0 -11
  221. data/test/rails3_root/db/migrate/005_create_dogs.rb +0 -12
  222. data/test/rails3_root/db/migrate/006_create_addresses.rb +0 -14
  223. data/test/rails3_root/db/migrate/007_create_fleas.rb +0 -11
  224. data/test/rails3_root/db/migrate/008_create_dogs_fleas.rb +0 -12
  225. data/test/rails3_root/db/migrate/009_create_products.rb +0 -17
  226. data/test/rails3_root/db/migrate/010_create_friendships.rb +0 -14
  227. data/test/rails3_root/db/migrate/011_create_treats.rb +0 -12
  228. data/test/rails3_root/db/migrate/20090506203502_create_profiles.rb +0 -12
  229. data/test/rails3_root/db/migrate/20090506203536_create_registrations.rb +0 -14
  230. data/test/rails3_root/db/migrate/20090513104502_create_cats.rb +0 -12
  231. data/test/rails3_root/db/seeds.rb +0 -7
  232. data/test/rails3_root/db/test.sqlite3 +0 -0
  233. data/test/rails3_root/log/test.log +0 -10834
  234. data/test/rails3_root/public/404.html +0 -26
  235. data/test/rails3_root/public/422.html +0 -26
  236. data/test/rails3_root/public/500.html +0 -26
  237. data/test/rails3_root/public/favicon.ico +0 -0
  238. data/test/rails3_root/public/images/rails.png +0 -0
  239. data/test/rails3_root/public/index.html +0 -279
  240. data/test/rails3_root/public/javascripts/application.js +0 -2
  241. data/test/rails3_root/public/javascripts/controls.js +0 -965
  242. data/test/rails3_root/public/javascripts/dragdrop.js +0 -974
  243. data/test/rails3_root/public/javascripts/effects.js +0 -1123
  244. data/test/rails3_root/public/javascripts/prototype.js +0 -4874
  245. data/test/rails3_root/public/javascripts/rails.js +0 -118
  246. data/test/rails3_root/public/robots.txt +0 -5
  247. data/test/rails3_root/script/rails +0 -9
  248. data/test/rails3_root/test/performance/browsing_test.rb +0 -9
  249. data/test/rails3_root/test/test_helper.rb +0 -13
  250. data/test/rails3_test_helper.rb +0 -6
  251. data/test/rspec_test.rb +0 -207
  252. data/test/test_helper.rb +0 -36
  253. data/test/unit/address_test.rb +0 -10
  254. data/test/unit/cat_test.rb +0 -7
  255. data/test/unit/dog_test.rb +0 -9
  256. data/test/unit/flea_test.rb +0 -14
  257. data/test/unit/friendship_test.rb +0 -6
  258. data/test/unit/post_test.rb +0 -15
  259. data/test/unit/product_test.rb +0 -23
  260. data/test/unit/tag_test.rb +0 -11
  261. data/test/unit/tagging_test.rb +0 -6
  262. data/test/unit/user_test.rb +0 -46
@@ -1,79 +0,0 @@
1
- module Shoulda # :nodoc:
2
- module Assertions
3
- # Asserts that two arrays contain the same elements, the same number of times. Essentially ==, but unordered.
4
- #
5
- # assert_same_elements([:a, :b, :c], [:c, :a, :b]) => passes
6
- def assert_same_elements(a1, a2, msg = nil)
7
- [:select, :inject, :size].each do |m|
8
- [a1, a2].each {|a| assert_respond_to(a, m, "Are you sure that #{a.inspect} is an array? It doesn't respond to #{m}.") }
9
- end
10
-
11
- assert a1h = a1.inject({}) { |h,e| h[e] = a1.select { |i| i == e }.size; h }
12
- assert a2h = a2.inject({}) { |h,e| h[e] = a2.select { |i| i == e }.size; h }
13
-
14
- assert_equal(a1h, a2h, msg)
15
- end
16
-
17
- # Asserts that the given collection contains item x. If x is a regular expression, ensure that
18
- # at least one element from the collection matches x. +extra_msg+ is appended to the error message if the assertion fails.
19
- #
20
- # assert_contains(['a', '1'], /\d/) => passes
21
- # assert_contains(['a', '1'], 'a') => passes
22
- # assert_contains(['a', '1'], /not there/) => fails
23
- def assert_contains(collection, x, extra_msg = "")
24
- collection = [collection] unless collection.is_a?(Array)
25
- msg = "#{x.inspect} not found in #{collection.to_a.inspect} #{extra_msg}"
26
- case x
27
- when Regexp
28
- assert(collection.detect { |e| e =~ x }, msg)
29
- else
30
- assert(collection.include?(x), msg)
31
- end
32
- end
33
-
34
- # Asserts that the given collection does not contain item x. If x is a regular expression, ensure that
35
- # none of the elements from the collection match x.
36
- def assert_does_not_contain(collection, x, extra_msg = "")
37
- collection = [collection] unless collection.is_a?(Array)
38
- msg = "#{x.inspect} found in #{collection.to_a.inspect} " + extra_msg
39
- case x
40
- when Regexp
41
- assert(!collection.detect { |e| e =~ x }, msg)
42
- else
43
- assert(!collection.include?(x), msg)
44
- end
45
- end
46
-
47
- # Asserts that the given matcher returns true when +target+ is passed to #matches?
48
- def assert_accepts(matcher, target, options = {})
49
- if matcher.respond_to?(:in_context)
50
- matcher.in_context(self)
51
- end
52
-
53
- if matcher.matches?(target)
54
- assert_block { true }
55
- if options[:message]
56
- assert_match options[:message], matcher.negative_failure_message
57
- end
58
- else
59
- assert_block(matcher.failure_message) { false }
60
- end
61
- end
62
-
63
- # Asserts that the given matcher returns false when +target+ is passed to #matches?
64
- def assert_rejects(matcher, target, options = {})
65
- if matcher.respond_to?(:in_context)
66
- matcher.in_context(self)
67
- end
68
-
69
- unless matcher.matches?(target)
70
- assert_block { true }
71
- if options[:message]
72
- assert_match options[:message], matcher.failure_message
73
- end
74
- else
75
- assert_block(matcher.negative_failure_message) { false }
76
- end
77
- end
78
- end
79
- end
@@ -1,46 +0,0 @@
1
- module Shoulda # :nodoc:
2
- # Call autoload_macros when you want to load test macros automatically in a non-Rails
3
- # project (it's done automatically for Rails projects).
4
- # You don't need to specify ROOT/test/shoulda_macros explicitly. Your custom macros
5
- # are loaded automatically when you call autoload_macros.
6
- #
7
- # The first argument is the path to you application's root directory.
8
- # All following arguments are directories relative to your root, which contain
9
- # shoulda_macros subdirectories. These directories support the same kinds of globs as the
10
- # Dir class.
11
- #
12
- # Basic usage (from a test_helper):
13
- # Shoulda.autoload_macros(File.dirname(__FILE__) + '/..')
14
- # will load everything in
15
- # - your_app/test/shoulda_macros
16
- #
17
- # To load vendored macros as well:
18
- # Shoulda.autoload_macros(APP_ROOT, 'vendor/*')
19
- # will load everything in
20
- # - APP_ROOT/vendor/*/shoulda_macros
21
- # - APP_ROOT/test/shoulda_macros
22
- #
23
- # To load macros in an app with a vendor directory laid out like Rails':
24
- # Shoulda.autoload_macros(APP_ROOT, 'vendor/{plugins,gems}/*')
25
- # or
26
- # Shoulda.autoload_macros(APP_ROOT, 'vendor/plugins/*', 'vendor/gems/*')
27
- # will load everything in
28
- # - APP_ROOT/vendor/plugins/*/shoulda_macros
29
- # - APP_ROOT/vendor/gems/*/shoulda_macros
30
- # - APP_ROOT/test/shoulda_macros
31
- #
32
- # If you prefer to stick testing dependencies away from your production dependencies:
33
- # Shoulda.autoload_macros(APP_ROOT, 'vendor/*', 'test/vendor/*')
34
- # will load everything in
35
- # - APP_ROOT/vendor/*/shoulda_macros
36
- # - APP_ROOT/test/vendor/*/shoulda_macros
37
- # - APP_ROOT/test/shoulda_macros
38
- def self.autoload_macros(root, *dirs)
39
- dirs << File.join('test')
40
- complete_dirs = dirs.map{|d| File.join(root, d, 'shoulda_macros')}
41
- all_files = complete_dirs.inject([]){ |files, dir| files + Dir[File.join(dir, '*.rb')] }
42
- all_files.each do |file|
43
- require file
44
- end
45
- end
46
- end
@@ -1,433 +0,0 @@
1
- module Shoulda
2
- class << self
3
- attr_accessor :contexts
4
- def contexts # :nodoc:
5
- @contexts ||= []
6
- end
7
-
8
- def current_context # :nodoc:
9
- self.contexts.last
10
- end
11
-
12
- def add_context(context) # :nodoc:
13
- self.contexts.push(context)
14
- end
15
-
16
- def remove_context # :nodoc:
17
- self.contexts.pop
18
- end
19
- end
20
-
21
- module ClassMethods
22
- # == Should statements
23
- #
24
- # Should statements are just syntactic sugar over normal Test::Unit test
25
- # methods. A should block contains all the normal code and assertions
26
- # you're used to seeing, with the added benefit that they can be wrapped
27
- # inside context blocks (see below).
28
- #
29
- # === Example:
30
- #
31
- # class UserTest < Test::Unit::TestCase
32
- #
33
- # def setup
34
- # @user = User.new("John", "Doe")
35
- # end
36
- #
37
- # should "return its full name"
38
- # assert_equal 'John Doe', @user.full_name
39
- # end
40
- #
41
- # end
42
- #
43
- # ...will produce the following test:
44
- # * <tt>"test: User should return its full name. "</tt>
45
- #
46
- # Note: The part before <tt>should</tt> in the test name is gleamed from the name of the Test::Unit class.
47
- #
48
- # Should statements can also take a Proc as a <tt>:before </tt>option. This proc runs after any
49
- # parent context's setups but before the current context's setup.
50
- #
51
- # === Example:
52
- #
53
- # context "Some context" do
54
- # setup { puts("I run after the :before proc") }
55
- #
56
- # should "run a :before proc", :before => lambda { puts("I run before the setup") } do
57
- # assert true
58
- # end
59
- # end
60
- #
61
- # Should statements can also wrap matchers, making virtually any matcher
62
- # usable in a macro style. The matcher's description is used to generate a
63
- # test name and failure message, and the test will pass if the matcher
64
- # matches the subject.
65
- #
66
- # === Example:
67
- #
68
- # should validate_presence_of(:first_name).with_message(/gotta be there/)
69
- #
70
-
71
- def should(name_or_matcher, options = {}, &blk)
72
- if Shoulda.current_context
73
- Shoulda.current_context.should(name_or_matcher, options, &blk)
74
- else
75
- context_name = self.name.gsub(/Test/, "")
76
- context = Shoulda::Context.new(context_name, self) do
77
- should(name_or_matcher, options, &blk)
78
- end
79
- context.build
80
- end
81
- end
82
-
83
- # Allows negative tests using matchers. The matcher's description is used
84
- # to generate a test name and negative failure message, and the test will
85
- # pass unless the matcher matches the subject.
86
- #
87
- # === Example:
88
- #
89
- # should_not set_the_flash
90
- def should_not(matcher)
91
- if Shoulda.current_context
92
- Shoulda.current_context.should_not(matcher)
93
- else
94
- context_name = self.name.gsub(/Test/, "")
95
- context = Shoulda::Context.new(context_name, self) do
96
- should_not(matcher)
97
- end
98
- context.build
99
- end
100
- end
101
-
102
- # == Before statements
103
- #
104
- # Before statements are should statements that run before the current
105
- # context's setup. These are especially useful when setting expectations.
106
- #
107
- # === Example:
108
- #
109
- # class UserControllerTest < Test::Unit::TestCase
110
- # context "the index action" do
111
- # setup do
112
- # @users = [Factory(:user)]
113
- # User.stubs(:find).returns(@users)
114
- # end
115
- #
116
- # context "on GET" do
117
- # setup { get :index }
118
- #
119
- # should respond_with(:success)
120
- #
121
- # # runs before "get :index"
122
- # before_should "find all users" do
123
- # User.expects(:find).with(:all).returns(@users)
124
- # end
125
- # end
126
- # end
127
- # end
128
- def before_should(name, &blk)
129
- should(name, :before => blk) { assert true }
130
- end
131
-
132
- # Just like should, but never runs, and instead prints an 'X' in the Test::Unit output.
133
- def should_eventually(name, options = {}, &blk)
134
- context_name = self.name.gsub(/Test/, "")
135
- context = Shoulda::Context.new(context_name, self) do
136
- should_eventually(name, &blk)
137
- end
138
- context.build
139
- end
140
-
141
- # == Contexts
142
- #
143
- # A context block groups should statements under a common set of setup/teardown methods.
144
- # Context blocks can be arbitrarily nested, and can do wonders for improving the maintainability
145
- # and readability of your test code.
146
- #
147
- # A context block can contain setup, should, should_eventually, and teardown blocks.
148
- #
149
- # class UserTest < Test::Unit::TestCase
150
- # context "A User instance" do
151
- # setup do
152
- # @user = User.find(:first)
153
- # end
154
- #
155
- # should "return its full name"
156
- # assert_equal 'John Doe', @user.full_name
157
- # end
158
- # end
159
- # end
160
- #
161
- # This code will produce the method <tt>"test: A User instance should return its full name. "</tt>.
162
- #
163
- # Contexts may be nested. Nested contexts run their setup blocks from out to in before each
164
- # should statement. They then run their teardown blocks from in to out after each should statement.
165
- #
166
- # class UserTest < Test::Unit::TestCase
167
- # context "A User instance" do
168
- # setup do
169
- # @user = User.find(:first)
170
- # end
171
- #
172
- # should "return its full name"
173
- # assert_equal 'John Doe', @user.full_name
174
- # end
175
- #
176
- # context "with a profile" do
177
- # setup do
178
- # @user.profile = Profile.find(:first)
179
- # end
180
- #
181
- # should "return true when sent :has_profile?"
182
- # assert @user.has_profile?
183
- # end
184
- # end
185
- # end
186
- # end
187
- #
188
- # This code will produce the following methods
189
- # * <tt>"test: A User instance should return its full name. "</tt>
190
- # * <tt>"test: A User instance with a profile should return true when sent :has_profile?. "</tt>
191
- #
192
- # <b>Just like should statements, a context block can exist next to normal <tt>def test_the_old_way; end</tt>
193
- # tests</b>. This means you do not have to fully commit to the context/should syntax in a test file.
194
-
195
- def context(name, &blk)
196
- if Shoulda.current_context
197
- Shoulda.current_context.context(name, &blk)
198
- else
199
- context = Shoulda::Context.new(name, self, &blk)
200
- context.build
201
- end
202
- end
203
-
204
- # Returns the class being tested, as determined by the test class name.
205
- #
206
- # class UserTest; described_type; end
207
- # # => User
208
- def described_type
209
- self.name.gsub(/Test$/, '').constantize
210
- end
211
-
212
- # Sets the return value of the subject instance method:
213
- #
214
- # class UserTest < Test::Unit::TestCase
215
- # subject { User.first }
216
- #
217
- # # uses the existing user
218
- # should validate_uniqueness_of(:email)
219
- # end
220
- def subject(&block)
221
- @subject_block = block
222
- end
223
-
224
- def subject_block # :nodoc:
225
- @subject_block
226
- end
227
- end
228
-
229
- module InstanceMethods
230
- # Returns an instance of the class under test.
231
- #
232
- # class UserTest
233
- # should "be a user" do
234
- # assert_kind_of User, subject # passes
235
- # end
236
- # end
237
- #
238
- # The subject can be explicitly set using the subject class method:
239
- #
240
- # class UserTest
241
- # subject { User.first }
242
- # should "be an existing user" do
243
- # assert !subject.new_record? # uses the first user
244
- # end
245
- # end
246
- #
247
- # The subject is used by all macros that require an instance of the class
248
- # being tested.
249
- def subject
250
- @shoulda_subject ||= construct_subject
251
- end
252
-
253
- def subject_block # :nodoc:
254
- (@shoulda_context && @shoulda_context.subject_block) || self.class.subject_block
255
- end
256
-
257
- def get_instance_of(object_or_klass) # :nodoc:
258
- if object_or_klass.is_a?(Class)
259
- object_or_klass.new
260
- else
261
- object_or_klass
262
- end
263
- end
264
-
265
- def instance_variable_name_for(klass) # :nodoc:
266
- klass.to_s.split('::').last.underscore
267
- end
268
-
269
- private
270
-
271
- def construct_subject
272
- if subject_block
273
- instance_eval(&subject_block)
274
- else
275
- get_instance_of(self.class.described_type)
276
- end
277
- end
278
- end
279
-
280
- class Context # :nodoc:
281
-
282
- attr_accessor :name # my name
283
- attr_accessor :parent # may be another context, or the original test::unit class.
284
- attr_accessor :subcontexts # array of contexts nested under myself
285
- attr_accessor :setup_blocks # blocks given via setup methods
286
- attr_accessor :teardown_blocks # blocks given via teardown methods
287
- attr_accessor :shoulds # array of hashes representing the should statements
288
- attr_accessor :should_eventuallys # array of hashes representing the should eventually statements
289
- attr_accessor :subject_block
290
-
291
- def initialize(name, parent, &blk)
292
- Shoulda.add_context(self)
293
- self.name = name
294
- self.parent = parent
295
- self.setup_blocks = []
296
- self.teardown_blocks = []
297
- self.shoulds = []
298
- self.should_eventuallys = []
299
- self.subcontexts = []
300
-
301
- merge_block(&blk)
302
- Shoulda.remove_context
303
- end
304
-
305
- def merge_block(&blk)
306
- blk.bind(self).call
307
- end
308
-
309
- def context(name, &blk)
310
- self.subcontexts << Context.new(name, self, &blk)
311
- end
312
-
313
- def setup(&blk)
314
- self.setup_blocks << blk
315
- end
316
-
317
- def teardown(&blk)
318
- self.teardown_blocks << blk
319
- end
320
-
321
- def should(name_or_matcher, options = {}, &blk)
322
- if name_or_matcher.respond_to?(:description) && name_or_matcher.respond_to?(:matches?)
323
- name = name_or_matcher.description
324
- blk = lambda { assert_accepts name_or_matcher, subject }
325
- else
326
- name = name_or_matcher
327
- end
328
-
329
- if blk
330
- self.shoulds << { :name => name, :before => options[:before], :block => blk }
331
- else
332
- self.should_eventuallys << { :name => name }
333
- end
334
- end
335
-
336
- def should_not(matcher)
337
- name = matcher.description
338
- blk = lambda { assert_rejects matcher, subject }
339
- self.shoulds << { :name => "not #{name}", :block => blk }
340
- end
341
-
342
- def should_eventually(name, &blk)
343
- self.should_eventuallys << { :name => name, :block => blk }
344
- end
345
-
346
- def subject(&block)
347
- self.subject_block = block
348
- end
349
-
350
- def subject_block
351
- return @subject_block if @subject_block
352
- parent.subject_block
353
- end
354
-
355
- def full_name
356
- parent_name = parent.full_name if am_subcontext?
357
- return [parent_name, name].join(" ").strip
358
- end
359
-
360
- def am_subcontext?
361
- parent.is_a?(self.class) # my parent is the same class as myself.
362
- end
363
-
364
- def test_unit_class
365
- am_subcontext? ? parent.test_unit_class : parent
366
- end
367
-
368
- def create_test_from_should_hash(should)
369
- test_name = ["test:", full_name, "should", "#{should[:name]}. "].flatten.join(' ').to_sym
370
-
371
- if test_unit_class.instance_methods.include?(test_name.to_s)
372
- warn " * WARNING: '#{test_name}' is already defined"
373
- end
374
-
375
- context = self
376
- test_unit_class.send(:define_method, test_name) do
377
- @shoulda_context = context
378
- begin
379
- context.run_parent_setup_blocks(self)
380
- should[:before].bind(self).call if should[:before]
381
- context.run_current_setup_blocks(self)
382
- should[:block].bind(self).call
383
- ensure
384
- context.run_all_teardown_blocks(self)
385
- end
386
- end
387
- end
388
-
389
- def run_all_setup_blocks(binding)
390
- run_parent_setup_blocks(binding)
391
- run_current_setup_blocks(binding)
392
- end
393
-
394
- def run_parent_setup_blocks(binding)
395
- self.parent.run_all_setup_blocks(binding) if am_subcontext?
396
- end
397
-
398
- def run_current_setup_blocks(binding)
399
- setup_blocks.each do |setup_block|
400
- setup_block.bind(binding).call
401
- end
402
- end
403
-
404
- def run_all_teardown_blocks(binding)
405
- teardown_blocks.reverse.each do |teardown_block|
406
- teardown_block.bind(binding).call
407
- end
408
- self.parent.run_all_teardown_blocks(binding) if am_subcontext?
409
- end
410
-
411
- def print_should_eventuallys
412
- should_eventuallys.each do |should|
413
- test_name = [full_name, "should", "#{should[:name]}. "].flatten.join(' ')
414
- puts " * DEFERRED: " + test_name
415
- end
416
- end
417
-
418
- def build
419
- shoulds.each do |should|
420
- create_test_from_should_hash(should)
421
- end
422
-
423
- subcontexts.each { |context| context.build }
424
-
425
- print_should_eventuallys
426
- end
427
-
428
- def method_missing(method, *args, &blk)
429
- test_unit_class.send(method, *args, &blk)
430
- end
431
-
432
- end
433
- end