faster_shoulda 2.11.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (259) hide show
  1. data/CONTRIBUTION_GUIDELINES.rdoc +10 -0
  2. data/MIT-LICENSE +22 -0
  3. data/README.rdoc +154 -0
  4. data/Rakefile +74 -0
  5. data/bin/convert_to_should_syntax +42 -0
  6. data/lib/shoulda/action_controller/macros.rb +221 -0
  7. data/lib/shoulda/action_controller/matchers/assign_to_matcher.rb +114 -0
  8. data/lib/shoulda/action_controller/matchers/filter_param_matcher.rb +74 -0
  9. data/lib/shoulda/action_controller/matchers/redirect_to_matcher.rb +62 -0
  10. data/lib/shoulda/action_controller/matchers/render_template_matcher.rb +54 -0
  11. data/lib/shoulda/action_controller/matchers/render_with_layout_matcher.rb +99 -0
  12. data/lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb +74 -0
  13. data/lib/shoulda/action_controller/matchers/respond_with_matcher.rb +85 -0
  14. data/lib/shoulda/action_controller/matchers/route_matcher.rb +93 -0
  15. data/lib/shoulda/action_controller/matchers/set_session_matcher.rb +98 -0
  16. data/lib/shoulda/action_controller/matchers/set_the_flash_matcher.rb +100 -0
  17. data/lib/shoulda/action_controller/matchers.rb +39 -0
  18. data/lib/shoulda/action_controller.rb +34 -0
  19. data/lib/shoulda/action_mailer/assertions.rb +42 -0
  20. data/lib/shoulda/action_mailer/matchers/have_sent_email.rb +110 -0
  21. data/lib/shoulda/action_mailer/matchers.rb +22 -0
  22. data/lib/shoulda/action_mailer.rb +13 -0
  23. data/lib/shoulda/active_record/assertions.rb +69 -0
  24. data/lib/shoulda/active_record/helpers.rb +32 -0
  25. data/lib/shoulda/active_record/macros.rb +457 -0
  26. data/lib/shoulda/active_record/matchers/allow_mass_assignment_of_matcher.rb +83 -0
  27. data/lib/shoulda/active_record/matchers/allow_value_matcher.rb +110 -0
  28. data/lib/shoulda/active_record/matchers/association_matcher.rb +226 -0
  29. data/lib/shoulda/active_record/matchers/ensure_inclusion_of_matcher.rb +87 -0
  30. data/lib/shoulda/active_record/matchers/ensure_length_of_matcher.rb +141 -0
  31. data/lib/shoulda/active_record/matchers/have_db_column_matcher.rb +169 -0
  32. data/lib/shoulda/active_record/matchers/have_db_index_matcher.rb +112 -0
  33. data/lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb +59 -0
  34. data/lib/shoulda/active_record/matchers/validate_acceptance_of_matcher.rb +41 -0
  35. data/lib/shoulda/active_record/matchers/validate_format_of_matcher.rb +65 -0
  36. data/lib/shoulda/active_record/matchers/validate_numericality_of_matcher.rb +39 -0
  37. data/lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb +60 -0
  38. data/lib/shoulda/active_record/matchers/validate_uniqueness_of_matcher.rb +148 -0
  39. data/lib/shoulda/active_record/matchers/validation_matcher.rb +56 -0
  40. data/lib/shoulda/active_record/matchers.rb +42 -0
  41. data/lib/shoulda/active_record.rb +16 -0
  42. data/lib/shoulda/assertions.rb +79 -0
  43. data/lib/shoulda/autoload_macros.rb +46 -0
  44. data/lib/shoulda/context.rb +537 -0
  45. data/lib/shoulda/helpers.rb +8 -0
  46. data/lib/shoulda/integrations/rspec.rb +13 -0
  47. data/lib/shoulda/integrations/rspec2.rb +22 -0
  48. data/lib/shoulda/integrations/test_unit.rb +22 -0
  49. data/lib/shoulda/macros.rb +161 -0
  50. data/lib/shoulda/private_helpers.rb +13 -0
  51. data/lib/shoulda/proc_extensions.rb +14 -0
  52. data/lib/shoulda/rails.rb +8 -0
  53. data/lib/shoulda/tasks/list_tests.rake +29 -0
  54. data/lib/shoulda/tasks/yaml_to_shoulda.rake +28 -0
  55. data/lib/shoulda/tasks.rb +3 -0
  56. data/lib/shoulda/version.rb +4 -0
  57. data/lib/shoulda.rb +9 -0
  58. data/rails/init.rb +8 -0
  59. data/test/README +36 -0
  60. data/test/fail_macros.rb +55 -0
  61. data/test/fixtures/addresses.yml +3 -0
  62. data/test/fixtures/friendships.yml +0 -0
  63. data/test/fixtures/posts.yml +5 -0
  64. data/test/fixtures/products.yml +0 -0
  65. data/test/fixtures/taggings.yml +0 -0
  66. data/test/fixtures/tags.yml +9 -0
  67. data/test/fixtures/users.yml +6 -0
  68. data/test/functional/posts_controller_test.rb +121 -0
  69. data/test/functional/users_controller_test.rb +19 -0
  70. data/test/hello_should.rb +37 -0
  71. data/test/matchers/action_mailer/have_sent_email_test.rb +76 -0
  72. data/test/matchers/active_record/allow_mass_assignment_of_matcher_test.rb +74 -0
  73. data/test/matchers/active_record/allow_value_matcher_test.rb +64 -0
  74. data/test/matchers/active_record/association_matcher_test.rb +263 -0
  75. data/test/matchers/active_record/ensure_inclusion_of_matcher_test.rb +81 -0
  76. data/test/matchers/active_record/ensure_length_of_matcher_test.rb +158 -0
  77. data/test/matchers/active_record/have_db_column_matcher_test.rb +169 -0
  78. data/test/matchers/active_record/have_db_index_matcher_test.rb +91 -0
  79. data/test/matchers/active_record/have_readonly_attributes_matcher_test.rb +29 -0
  80. data/test/matchers/active_record/validate_acceptance_of_matcher_test.rb +44 -0
  81. data/test/matchers/active_record/validate_format_of_matcher_test.rb +39 -0
  82. data/test/matchers/active_record/validate_numericality_of_matcher_test.rb +52 -0
  83. data/test/matchers/active_record/validate_presence_of_matcher_test.rb +86 -0
  84. data/test/matchers/active_record/validate_uniqueness_of_matcher_test.rb +147 -0
  85. data/test/matchers/controller/assign_to_matcher_test.rb +55 -0
  86. data/test/matchers/controller/filter_param_matcher_test.rb +40 -0
  87. data/test/matchers/controller/redirect_to_matcher_test.rb +37 -0
  88. data/test/matchers/controller/render_template_matcher_test.rb +37 -0
  89. data/test/matchers/controller/render_with_layout_matcher_test.rb +47 -0
  90. data/test/matchers/controller/respond_with_content_type_matcher_test.rb +32 -0
  91. data/test/matchers/controller/respond_with_matcher_test.rb +96 -0
  92. data/test/matchers/controller/route_matcher_test.rb +75 -0
  93. data/test/matchers/controller/set_session_matcher_test.rb +48 -0
  94. data/test/matchers/controller/set_the_flash_matcher.rb +95 -0
  95. data/test/other/autoload_macro_test.rb +18 -0
  96. data/test/other/context_test.rb +372 -0
  97. data/test/other/convert_to_should_syntax_test.rb +63 -0
  98. data/test/other/helpers_test.rb +317 -0
  99. data/test/other/private_helpers_test.rb +32 -0
  100. data/test/other/should_test.rb +271 -0
  101. data/test/rails2_model_builder.rb +130 -0
  102. data/test/rails2_root/app/controllers/application_controller.rb +22 -0
  103. data/test/rails2_root/app/controllers/posts_controller.rb +87 -0
  104. data/test/rails2_root/app/controllers/users_controller.rb +84 -0
  105. data/test/rails2_root/app/helpers/application_helper.rb +3 -0
  106. data/test/rails2_root/app/helpers/posts_helper.rb +2 -0
  107. data/test/rails2_root/app/helpers/users_helper.rb +2 -0
  108. data/test/rails2_root/app/models/address.rb +7 -0
  109. data/test/rails2_root/app/models/flea.rb +11 -0
  110. data/test/rails2_root/app/models/friendship.rb +4 -0
  111. data/test/rails2_root/app/models/notifier.rb +8 -0
  112. data/test/rails2_root/app/models/pets/cat.rb +7 -0
  113. data/test/rails2_root/app/models/pets/dog.rb +10 -0
  114. data/test/rails2_root/app/models/post.rb +12 -0
  115. data/test/rails2_root/app/models/product.rb +12 -0
  116. data/test/rails2_root/app/models/profile.rb +2 -0
  117. data/test/rails2_root/app/models/registration.rb +2 -0
  118. data/test/rails2_root/app/models/tag.rb +8 -0
  119. data/test/rails2_root/app/models/tagging.rb +4 -0
  120. data/test/rails2_root/app/models/treat.rb +3 -0
  121. data/test/rails2_root/app/models/user.rb +32 -0
  122. data/test/rails2_root/app/views/layouts/posts.rhtml +19 -0
  123. data/test/rails2_root/app/views/layouts/users.rhtml +17 -0
  124. data/test/rails2_root/app/views/layouts/wide.html.erb +1 -0
  125. data/test/rails2_root/app/views/notifier/the_email.html.erb +1 -0
  126. data/test/rails2_root/app/views/posts/edit.rhtml +27 -0
  127. data/test/rails2_root/app/views/posts/index.rhtml +25 -0
  128. data/test/rails2_root/app/views/posts/new.rhtml +26 -0
  129. data/test/rails2_root/app/views/posts/show.rhtml +18 -0
  130. data/test/rails2_root/app/views/users/edit.rhtml +22 -0
  131. data/test/rails2_root/app/views/users/index.rhtml +22 -0
  132. data/test/rails2_root/app/views/users/new.rhtml +21 -0
  133. data/test/rails2_root/app/views/users/show.rhtml +13 -0
  134. data/test/rails2_root/config/boot.rb +110 -0
  135. data/test/rails2_root/config/database.yml +4 -0
  136. data/test/rails2_root/config/environment.rb +17 -0
  137. data/test/rails2_root/config/environments/test.rb +23 -0
  138. data/test/rails2_root/config/initializers/new_rails_defaults.rb +15 -0
  139. data/test/rails2_root/config/initializers/shoulda.rb +8 -0
  140. data/test/rails2_root/config/routes.rb +6 -0
  141. data/test/rails2_root/db/migrate/001_create_users.rb +19 -0
  142. data/test/rails2_root/db/migrate/002_create_posts.rb +13 -0
  143. data/test/rails2_root/db/migrate/003_create_taggings.rb +12 -0
  144. data/test/rails2_root/db/migrate/004_create_tags.rb +11 -0
  145. data/test/rails2_root/db/migrate/005_create_dogs.rb +12 -0
  146. data/test/rails2_root/db/migrate/006_create_addresses.rb +14 -0
  147. data/test/rails2_root/db/migrate/007_create_fleas.rb +11 -0
  148. data/test/rails2_root/db/migrate/008_create_dogs_fleas.rb +12 -0
  149. data/test/rails2_root/db/migrate/009_create_products.rb +17 -0
  150. data/test/rails2_root/db/migrate/010_create_friendships.rb +14 -0
  151. data/test/rails2_root/db/migrate/011_create_treats.rb +12 -0
  152. data/test/rails2_root/db/migrate/20090506203502_create_profiles.rb +12 -0
  153. data/test/rails2_root/db/migrate/20090506203536_create_registrations.rb +14 -0
  154. data/test/rails2_root/db/migrate/20090513104502_create_cats.rb +12 -0
  155. data/test/rails2_root/db/schema.rb +0 -0
  156. data/test/rails2_root/public/404.html +30 -0
  157. data/test/rails2_root/public/422.html +30 -0
  158. data/test/rails2_root/public/500.html +30 -0
  159. data/test/rails2_root/script/console +3 -0
  160. data/test/rails2_root/script/generate +3 -0
  161. data/test/rails2_root/test/shoulda_macros/custom_macro.rb +6 -0
  162. data/test/rails2_root/vendor/gems/gem_with_macro-0.0.1/shoulda_macros/gem_macro.rb +6 -0
  163. data/test/rails2_root/vendor/plugins/plugin_with_macro/shoulda_macros/plugin_macro.rb +6 -0
  164. data/test/rails2_test_helper.rb +6 -0
  165. data/test/rails3_model_builder.rb +118 -0
  166. data/test/rails3_root/Gemfile +28 -0
  167. data/test/rails3_root/README +244 -0
  168. data/test/rails3_root/Rakefile +10 -0
  169. data/test/rails3_root/app/controllers/application_controller.rb +22 -0
  170. data/test/rails3_root/app/controllers/posts_controller.rb +87 -0
  171. data/test/rails3_root/app/controllers/users_controller.rb +82 -0
  172. data/test/rails3_root/app/helpers/application_helper.rb +2 -0
  173. data/test/rails3_root/app/models/address.rb +7 -0
  174. data/test/rails3_root/app/models/flea.rb +11 -0
  175. data/test/rails3_root/app/models/friendship.rb +4 -0
  176. data/test/rails3_root/app/models/notifier.rb +8 -0
  177. data/test/rails3_root/app/models/pets/cat.rb +7 -0
  178. data/test/rails3_root/app/models/pets/dog.rb +10 -0
  179. data/test/rails3_root/app/models/post.rb +12 -0
  180. data/test/rails3_root/app/models/product.rb +12 -0
  181. data/test/rails3_root/app/models/profile.rb +2 -0
  182. data/test/rails3_root/app/models/registration.rb +2 -0
  183. data/test/rails3_root/app/models/tag.rb +8 -0
  184. data/test/rails3_root/app/models/tagging.rb +4 -0
  185. data/test/rails3_root/app/models/treat.rb +3 -0
  186. data/test/rails3_root/app/models/user.rb +32 -0
  187. data/test/rails3_root/app/views/layouts/application.html.erb +14 -0
  188. data/test/rails3_root/app/views/layouts/posts.rhtml +19 -0
  189. data/test/rails3_root/app/views/layouts/users.rhtml +17 -0
  190. data/test/rails3_root/app/views/layouts/wide.html.erb +1 -0
  191. data/test/rails3_root/app/views/notifier/the_email.html.erb +1 -0
  192. data/test/rails3_root/app/views/posts/edit.rhtml +27 -0
  193. data/test/rails3_root/app/views/posts/index.rhtml +25 -0
  194. data/test/rails3_root/app/views/posts/new.rhtml +24 -0
  195. data/test/rails3_root/app/views/posts/show.rhtml +18 -0
  196. data/test/rails3_root/app/views/users/edit.rhtml +22 -0
  197. data/test/rails3_root/app/views/users/index.rhtml +22 -0
  198. data/test/rails3_root/app/views/users/new.rhtml +21 -0
  199. data/test/rails3_root/app/views/users/show.rhtml +13 -0
  200. data/test/rails3_root/config/application.rb +46 -0
  201. data/test/rails3_root/config/boot.rb +6 -0
  202. data/test/rails3_root/config/database.yml +22 -0
  203. data/test/rails3_root/config/environment.rb +5 -0
  204. data/test/rails3_root/config/environments/development.rb +19 -0
  205. data/test/rails3_root/config/environments/production.rb +42 -0
  206. data/test/rails3_root/config/environments/test.rb +32 -0
  207. data/test/rails3_root/config/initializers/backtrace_silencers.rb +7 -0
  208. data/test/rails3_root/config/initializers/inflections.rb +10 -0
  209. data/test/rails3_root/config/initializers/mime_types.rb +5 -0
  210. data/test/rails3_root/config/initializers/secret_token.rb +7 -0
  211. data/test/rails3_root/config/initializers/session_store.rb +8 -0
  212. data/test/rails3_root/config/locales/en.yml +5 -0
  213. data/test/rails3_root/config/routes.rb +4 -0
  214. data/test/rails3_root/config.ru +4 -0
  215. data/test/rails3_root/db/migrate/001_create_users.rb +19 -0
  216. data/test/rails3_root/db/migrate/002_create_posts.rb +13 -0
  217. data/test/rails3_root/db/migrate/003_create_taggings.rb +12 -0
  218. data/test/rails3_root/db/migrate/004_create_tags.rb +11 -0
  219. data/test/rails3_root/db/migrate/005_create_dogs.rb +12 -0
  220. data/test/rails3_root/db/migrate/006_create_addresses.rb +14 -0
  221. data/test/rails3_root/db/migrate/007_create_fleas.rb +11 -0
  222. data/test/rails3_root/db/migrate/008_create_dogs_fleas.rb +12 -0
  223. data/test/rails3_root/db/migrate/009_create_products.rb +17 -0
  224. data/test/rails3_root/db/migrate/010_create_friendships.rb +14 -0
  225. data/test/rails3_root/db/migrate/011_create_treats.rb +12 -0
  226. data/test/rails3_root/db/migrate/20090506203502_create_profiles.rb +12 -0
  227. data/test/rails3_root/db/migrate/20090506203536_create_registrations.rb +14 -0
  228. data/test/rails3_root/db/migrate/20090513104502_create_cats.rb +12 -0
  229. data/test/rails3_root/db/seeds.rb +7 -0
  230. data/test/rails3_root/public/404.html +26 -0
  231. data/test/rails3_root/public/422.html +26 -0
  232. data/test/rails3_root/public/500.html +26 -0
  233. data/test/rails3_root/public/favicon.ico +0 -0
  234. data/test/rails3_root/public/images/rails.png +0 -0
  235. data/test/rails3_root/public/index.html +279 -0
  236. data/test/rails3_root/public/javascripts/application.js +2 -0
  237. data/test/rails3_root/public/javascripts/controls.js +965 -0
  238. data/test/rails3_root/public/javascripts/dragdrop.js +974 -0
  239. data/test/rails3_root/public/javascripts/effects.js +1123 -0
  240. data/test/rails3_root/public/javascripts/prototype.js +4874 -0
  241. data/test/rails3_root/public/javascripts/rails.js +118 -0
  242. data/test/rails3_root/public/robots.txt +5 -0
  243. data/test/rails3_root/script/rails +9 -0
  244. data/test/rails3_root/test/performance/browsing_test.rb +9 -0
  245. data/test/rails3_root/test/test_helper.rb +13 -0
  246. data/test/rails3_test_helper.rb +6 -0
  247. data/test/rspec_test.rb +207 -0
  248. data/test/test_helper.rb +36 -0
  249. data/test/unit/address_test.rb +10 -0
  250. data/test/unit/cat_test.rb +7 -0
  251. data/test/unit/dog_test.rb +9 -0
  252. data/test/unit/flea_test.rb +14 -0
  253. data/test/unit/friendship_test.rb +6 -0
  254. data/test/unit/post_test.rb +15 -0
  255. data/test/unit/product_test.rb +23 -0
  256. data/test/unit/tag_test.rb +11 -0
  257. data/test/unit/tagging_test.rb +6 -0
  258. data/test/unit/user_test.rb +46 -0
  259. metadata +325 -0
@@ -0,0 +1,317 @@
1
+ require 'test_helper'
2
+ require 'action_mailer'
3
+ require 'mocha'
4
+
5
+ class HelpersTest < ActiveSupport::TestCase # :nodoc:
6
+
7
+ context "given delivered emails" do
8
+ setup do
9
+ email1 = stub(:subject => "one", :to => ["none1@email.com"])
10
+ email2 = stub(:subject => "two", :to => ["none2@email.com"])
11
+ ActionMailer::Base.stubs(:deliveries).returns([email1, email2])
12
+ end
13
+
14
+ should "have sent an email" do
15
+ assert_sent_email
16
+
17
+ assert_raises(Test::Unit::AssertionFailedError) do
18
+ assert_did_not_send_email
19
+ end
20
+ end
21
+
22
+ should "find email one" do
23
+ assert_sent_email do |e|
24
+ e.subject =~ /one/
25
+ end
26
+ end
27
+
28
+ should "not find an email that doesn't exist" do
29
+ assert_raises(Test::Unit::AssertionFailedError) do
30
+ assert_sent_email do |e|
31
+ e.subject =~ /whatever/
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ context "when there are no emails" do
38
+ setup do
39
+ ActionMailer::Base.stubs(:deliveries).returns([])
40
+ end
41
+
42
+ should "not have sent an email" do
43
+ assert_did_not_send_email
44
+
45
+ assert_raises(Test::Unit::AssertionFailedError) do
46
+ assert_sent_email
47
+ end
48
+ end
49
+ end
50
+
51
+ context "an array of values" do
52
+ setup do
53
+ @a = ['abc', 'def', 3]
54
+ end
55
+
56
+ [/b/, 'abc', 3].each do |x|
57
+ should "contain #{x.inspect}" do
58
+ assert_raises(Test::Unit::AssertionFailedError) do
59
+ assert_does_not_contain @a, x
60
+ end
61
+ assert_contains @a, x
62
+ end
63
+ end
64
+
65
+ should "not contain 'wtf'" do
66
+ assert_raises(Test::Unit::AssertionFailedError) {assert_contains @a, 'wtf'}
67
+ assert_does_not_contain @a, 'wtf'
68
+ end
69
+
70
+ should "be the same as another array, ordered differently" do
71
+ assert_same_elements(@a, [3, "def", "abc"])
72
+ assert_raises(Test::Unit::AssertionFailedError) do
73
+ assert_same_elements(@a, [3, 3, "def", "abc"])
74
+ end
75
+ assert_raises(Test::Unit::AssertionFailedError) do
76
+ assert_same_elements([@a, "abc"].flatten, [3, 3, "def", "abc"])
77
+ end
78
+ end
79
+ end
80
+
81
+ context "an array of values" do
82
+ setup do
83
+ @a = [1, 2, "(3)"]
84
+ end
85
+
86
+ context "after adding another value" do
87
+ setup do
88
+ @a.push(4)
89
+ end
90
+
91
+ should_change("the number of elements", :by => 1) { @a.length }
92
+ should_change("the number of elements", :from => 3) { @a.length }
93
+ should_change("the number of elements", :to => 4) { @a.length }
94
+ should_change("the first element", :by => 0) { @a[0] }
95
+ should_not_change("the first element") { @a[0] }
96
+ end
97
+
98
+ context "after replacing it with an array of strings" do
99
+ setup do
100
+ @a = %w(a b c d e f)
101
+ end
102
+
103
+ should_change("the number of elements", :by => 3) { @a.length }
104
+ should_change("the number of elements", :from => 3, :to => 6, :by => 3) { @a.length }
105
+ should_change("the first element") { @a[0] }
106
+ should_change("the second element", :from => 2, :to => "b") { @a[1] }
107
+ should_change("the third element", :from => /\d/, :to => /\w/) { @a[2] }
108
+ should_change("the last element", :to => String) { @a[3] }
109
+ end
110
+ end
111
+
112
+ context "assert_good_value" do
113
+ should "validate a good email address" do
114
+ assert_good_value User.new, :email, "good@example.com"
115
+ end
116
+
117
+ should "validate a good SSN with a custom message" do
118
+ assert_good_value User.new, :ssn, "xxxxxxxxx", /length/
119
+ end
120
+
121
+ should "fail to validate a bad email address" do
122
+ assert_raises Test::Unit::AssertionFailedError do
123
+ assert_good_value User.new, :email, "bad"
124
+ end
125
+ end
126
+
127
+ should "fail to validate a bad SSN that is too short" do
128
+ assert_raises Test::Unit::AssertionFailedError do
129
+ assert_good_value User.new, :ssn, "x", /length/
130
+ end
131
+ end
132
+
133
+ should "accept a class as the first argument" do
134
+ assert_good_value User, :email, "good@example.com"
135
+ end
136
+ end
137
+
138
+ context "assert_bad_value" do
139
+ should "invalidate a bad email address" do
140
+ assert_bad_value User.new, :email, "bad"
141
+ end
142
+
143
+ should "invalidate a bad SSN with a custom message" do
144
+ assert_bad_value User.new, :ssn, "x", /length/
145
+ end
146
+
147
+ should "fail to invalidate a good email address" do
148
+ assert_raises Test::Unit::AssertionFailedError do
149
+ assert_bad_value User.new, :email, "good@example.com"
150
+ end
151
+ end
152
+
153
+ should "fail to invalidate a good SSN" do
154
+ assert_raises Test::Unit::AssertionFailedError do
155
+ assert_bad_value User.new, :ssn, "xxxxxxxxx", /length/
156
+ end
157
+ end
158
+
159
+ should "accept a class as the first argument" do
160
+ assert_bad_value User, :email, "bad"
161
+ end
162
+ end
163
+
164
+ context "a matching matcher" do
165
+ setup do
166
+ @matcher = stub('matcher', :matches? => true,
167
+ :failure_message => 'bad failure message',
168
+ :negative_failure_message => 'big time failure')
169
+ end
170
+
171
+ should "pass when given to assert_accepts with no message expectation" do
172
+ assert_accepts @matcher, 'target'
173
+ end
174
+
175
+ should "pass when given to assert_accepts with a matching message" do
176
+ assert_accepts @matcher, 'target', :message => /big time/
177
+ end
178
+
179
+ should "fail when given to assert_accepts with non-matching message" do
180
+ assert_raise Test::Unit::AssertionFailedError do
181
+ assert_accepts @matcher, 'target', :message => /small time/
182
+ end
183
+ end
184
+
185
+ context "when given to assert_rejects" do
186
+ setup do
187
+ begin
188
+ assert_rejects @matcher, 'target'
189
+ rescue Test::Unit::AssertionFailedError => @error
190
+ end
191
+ end
192
+
193
+ should "fail" do
194
+ assert_not_nil @error
195
+ end
196
+
197
+ should "use the error message from the matcher" do
198
+ assert_equal 'big time failure', @error.message
199
+ end
200
+ end
201
+ end
202
+
203
+ context "a non-matching matcher" do
204
+ setup do
205
+ @matcher = stub('matcher', :matches? => false,
206
+ :failure_message => 'big time failure',
207
+ :negative_failure_message => 'bad failure message')
208
+ end
209
+
210
+ should "pass when given to assert_rejects with no message expectation" do
211
+ assert_rejects @matcher, 'target'
212
+ end
213
+
214
+ should "pass when given to assert_rejects with a matching message" do
215
+ assert_rejects @matcher, 'target', :message => /big time/
216
+ end
217
+
218
+ should "fail when given to assert_rejects with a non-matching message" do
219
+ assert_raise Test::Unit::AssertionFailedError do
220
+ assert_rejects @matcher, 'target', :message => /small time/
221
+ end
222
+ end
223
+
224
+ context "when given to assert_accepts" do
225
+ setup do
226
+ begin
227
+ assert_accepts @matcher, 'target'
228
+ rescue Test::Unit::AssertionFailedError => @error
229
+ end
230
+ end
231
+
232
+ should "fail" do
233
+ assert_not_nil @error
234
+ end
235
+
236
+ should "use the error message from the matcher" do
237
+ assert_equal 'big time failure', @error.message
238
+ end
239
+ end
240
+ end
241
+
242
+ should "assign context to a support matching on assert_accepts" do
243
+ matcher = stub('matcher', :matches? => true)
244
+ matcher.expects(:in_context).with(self)
245
+ assert_accepts matcher, nil
246
+ end
247
+
248
+ should "assign context to a support matching on assert_rejects" do
249
+ matcher = stub('matcher', :matches? => false)
250
+ matcher.expects(:in_context).with(self)
251
+ assert_rejects matcher, nil
252
+ end
253
+
254
+ context "given one treat exists and one post exists" do
255
+ setup do
256
+ Treat.create!
257
+ Post.create!(:title => 'title', :body => 'body', :user_id => 1)
258
+ end
259
+
260
+ teardown do
261
+ Treat.delete_all
262
+ Post.delete_all
263
+ end
264
+
265
+ context "creating a treat" do
266
+ setup do
267
+ Treat.create!
268
+ end
269
+
270
+ should_create :treat
271
+ should_fail do
272
+ should_create :post
273
+ end
274
+ end
275
+
276
+ context "creating a treat and a post" do
277
+ setup do
278
+ Treat.create!
279
+ Post.create!(:title => 'title 2', :body => 'body', :user_id => 1)
280
+ end
281
+
282
+ should_create :treat
283
+ should_create :post
284
+ end
285
+
286
+ context "destroying a treat" do
287
+ setup do
288
+ Treat.first.destroy
289
+ end
290
+
291
+ should_destroy :treat
292
+ should_fail do
293
+ should_destroy :post
294
+ end
295
+ end
296
+
297
+ context "destroying a treat and a post" do
298
+ setup do
299
+ Treat.first.destroy
300
+ Post.first.destroy
301
+ end
302
+
303
+ should_destroy :treat
304
+ should_destroy :post
305
+ end
306
+
307
+ context "doing nothing" do
308
+ should_fail do
309
+ should_create :treat
310
+ end
311
+
312
+ should_fail do
313
+ should_destroy :treat
314
+ end
315
+ end
316
+ end
317
+ end
@@ -0,0 +1,32 @@
1
+ require 'test_helper'
2
+
3
+ class PrivateHelpersTest < ActiveSupport::TestCase # :nodoc:
4
+ include Shoulda::Private
5
+ context "get_options!" do
6
+ should "remove opts from args" do
7
+ args = [:a, :b, {}]
8
+ get_options!(args)
9
+ assert_equal [:a, :b], args
10
+ end
11
+
12
+ should "return wanted opts in order" do
13
+ args = [{:one => 1, :two => 2}]
14
+ one, two = get_options!(args, :one, :two)
15
+ assert_equal 1, one
16
+ assert_equal 2, two
17
+ end
18
+
19
+ should "raise ArgumentError if given unwanted option" do
20
+ args = [{:one => 1, :two => 2}]
21
+ assert_raises ArgumentError do
22
+ get_options!(args, :one)
23
+ end
24
+ end
25
+
26
+ should "return single wanted option" do
27
+ args = [:a, :b, {:class => Object}]
28
+ klass = get_options!(args,:class)
29
+ assert_equal Object, klass
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,271 @@
1
+ require 'test_helper'
2
+
3
+ class ShouldTest < ActiveSupport::TestCase # :nodoc:
4
+ should "be able to define a should statement outside of a context" do
5
+ assert true
6
+ end
7
+
8
+ should "see the name of my class as ShouldTest" do
9
+ assert_equal "ShouldTest", self.class.name
10
+ end
11
+
12
+ def self.should_see_class_methods
13
+ should "be able to see class methods" do
14
+ assert true
15
+ end
16
+ end
17
+
18
+ def self.should_be_able_to_setup_a_should_eventually_in_a_class_method
19
+ should "be able to setup a should eventually in a class method"
20
+ end
21
+
22
+ def self.should_see_a_context_block_like_a_Test_Unit_class
23
+ should "see a context block as a Test::Unit class" do
24
+ assert_equal "ShouldTest", self.class.name
25
+ end
26
+ end
27
+
28
+ def self.should_see_blah
29
+ should "see @blah through a macro" do
30
+ assert @blah
31
+ end
32
+ end
33
+
34
+ def self.should_not_see_blah
35
+ should "not see @blah through a macro" do
36
+ assert_nil @blah
37
+ end
38
+ end
39
+
40
+ def self.should_be_able_to_make_context_macros(prefix = nil)
41
+ context "a macro" do
42
+ should "have the tests named correctly" do
43
+ assert_match(/^test: #{prefix}a macro should have the tests named correctly/, self.to_s)
44
+ end
45
+ end
46
+ end
47
+
48
+ context "Context" do
49
+
50
+ should_see_class_methods
51
+ should_see_a_context_block_like_a_Test_Unit_class
52
+ should_be_able_to_make_context_macros("Context ")
53
+ should_be_able_to_setup_a_should_eventually_in_a_class_method
54
+
55
+ should "not define @blah" do
56
+ assert ! self.instance_variables.include?("@blah")
57
+ end
58
+
59
+ should_not_see_blah
60
+
61
+ should "be able to define a should statement" do
62
+ assert true
63
+ end
64
+
65
+ should "see the name of my class as ShouldTest" do
66
+ assert_equal "ShouldTest", self.class.name
67
+ end
68
+
69
+ context "with a subcontext" do
70
+ should_be_able_to_make_context_macros("Context with a subcontext ")
71
+ end
72
+ end
73
+
74
+ context "Context with setup block" do
75
+ setup do
76
+ @blah = "blah"
77
+ end
78
+
79
+ should "have @blah == 'blah'" do
80
+ assert_equal "blah", @blah
81
+ end
82
+ should_see_blah
83
+
84
+ should "have name set right" do
85
+ assert_match(/^test: Context with setup block/, self.to_s)
86
+ end
87
+
88
+ context "and a subcontext" do
89
+ setup do
90
+ @blah = "#{@blah} twice"
91
+ end
92
+
93
+ should "be named correctly" do
94
+ assert_match(/^test: Context with setup block and a subcontext should be named correctly/, self.to_s)
95
+ end
96
+
97
+ should "run the setup methods in order" do
98
+ assert_equal @blah, "blah twice"
99
+ end
100
+ should_see_blah
101
+ end
102
+ end
103
+
104
+ context "Another context with setup block" do
105
+ setup do
106
+ @blah = "foo"
107
+ end
108
+
109
+ should "have @blah == 'foo'" do
110
+ assert_equal "foo", @blah
111
+ end
112
+
113
+ should "have name set right" do
114
+ assert_match(/^test: Another context with setup block/, self.to_s)
115
+ end
116
+ should_see_blah
117
+ end
118
+
119
+ should_eventually "pass, since it's a should_eventually" do
120
+ flunk "what?"
121
+ end
122
+
123
+ # Context creation and naming
124
+
125
+ def test_should_create_a_new_context
126
+ assert_nothing_raised do
127
+ Shoulda::Context.new("context name", self) do; end
128
+ end
129
+ end
130
+
131
+ def test_should_create_a_nested_context
132
+ assert_nothing_raised do
133
+ parent = Shoulda::Context.new("Parent", self) do; end
134
+ child = Shoulda::Context.new("Child", parent) do; end
135
+ end
136
+ end
137
+
138
+ def test_should_name_a_contexts_correctly
139
+ parent = Shoulda::Context.new("Parent", self) do; end
140
+ child = Shoulda::Context.new("Child", parent) do; end
141
+ grandchild = Shoulda::Context.new("GrandChild", child) do; end
142
+
143
+ assert_equal "Parent", parent.full_name
144
+ assert_equal "Parent Child", child.full_name
145
+ assert_equal "Parent Child GrandChild", grandchild.full_name
146
+ end
147
+
148
+ # Should statements
149
+
150
+ def test_should_have_should_hashes_when_given_should_statements
151
+ context = Shoulda::Context.new("name", self) do
152
+ should "be good" do; end
153
+ should "another" do; end
154
+ end
155
+
156
+ names = context.shoulds.map {|s| s[:name]}
157
+ assert_equal ["another", "be good"], names.sort
158
+ end
159
+
160
+ # setup and teardown
161
+
162
+ def test_should_capture_setup_and_teardown_blocks
163
+ context = Shoulda::Context.new("name", self) do
164
+ setup do; "setup"; end
165
+ teardown do; "teardown"; end
166
+ end
167
+
168
+ assert_equal "setup", context.setup_blocks.first.call
169
+ assert_equal "teardown", context.teardown_blocks.first.call
170
+ end
171
+
172
+ # building
173
+
174
+ def test_should_create_shoulda_test_for_each_should_on_build
175
+ context = Shoulda::Context.new("name", self) do
176
+ should "one" do; end
177
+ should "two" do; end
178
+ end
179
+ context.expects(:create_test_from_should_hash).with(has_entry(:name => "one"))
180
+ context.expects(:create_test_from_should_hash).with(has_entry(:name => "two"))
181
+ context.build
182
+ end
183
+
184
+ def test_should_create_test_methods_on_build
185
+ tu_class = ActiveSupport::TestCase
186
+ context = Shoulda::Context.new("A Context", tu_class) do
187
+ should "define the test" do; end
188
+ end
189
+
190
+ tu_class.expects(:define_method).with(:"test: A Context should define the test. ")
191
+ context.build
192
+ end
193
+
194
+ def test_should_create_test_methods_on_build_when_subcontext
195
+ tu_class = ActiveSupport::TestCase
196
+ context = Shoulda::Context.new("A Context", tu_class) do
197
+ context "with a child" do
198
+ should "define the test" do; end
199
+ end
200
+ end
201
+
202
+ tu_class.expects(:define_method).with(:"test: A Context with a child should define the test. ")
203
+ context.build
204
+ end
205
+
206
+ # Test::Unit integration
207
+
208
+ def test_should_create_a_new_context_and_build_it_on_Test_Unit_context
209
+ c = mock("context")
210
+ c.expects(:build)
211
+ Shoulda::Context.expects(:new).with("foo", kind_of(Class)).returns(c)
212
+ self.class.context "foo" do; end
213
+ end
214
+
215
+ def test_should_create_a_one_off_context_and_build_it_on_Test_Unit_should
216
+ s = mock("test")
217
+ Shoulda::Context.any_instance.expects(:should).with("rock", {}).returns(s)
218
+ Shoulda::Context.any_instance.expects(:build)
219
+ self.class.should "rock" do; end
220
+ end
221
+
222
+ def test_should_create_a_one_off_context_and_build_it_on_Test_Unit_should_eventually
223
+ s = mock("test")
224
+ Shoulda::Context.any_instance.expects(:should_eventually).with("rock").returns(s)
225
+ Shoulda::Context.any_instance.expects(:build)
226
+ self.class.should_eventually "rock" do; end
227
+ end
228
+
229
+ should "run a :before proc", :before => lambda { @value = "before" } do
230
+ assert_equal "before", @value
231
+ end
232
+
233
+ context "A :before proc" do
234
+ setup do
235
+ assert_equal "before", @value
236
+ @value = "setup"
237
+ end
238
+
239
+ should "run before the current setup", :before => lambda { @value = "before" } do
240
+ assert_equal "setup", @value
241
+ end
242
+ end
243
+
244
+ context "a before statement" do
245
+ setup do
246
+ assert_equal "before", @value
247
+ @value = "setup"
248
+ end
249
+
250
+ before_should "run before the current setup" do
251
+ @value = "before"
252
+ end
253
+ end
254
+
255
+ context "A context" do
256
+ setup do
257
+ @value = "outer"
258
+ end
259
+
260
+ context "with a subcontext and a :before proc" do
261
+ before = lambda do
262
+ assert "outer", @value
263
+ @value = "before"
264
+ end
265
+ should "run after the parent setup", :before => before do
266
+ assert_equal "before", @value
267
+ end
268
+ end
269
+ end
270
+
271
+ end