resource_controller 0.4.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (211) hide show
  1. data/LICENSE +22 -0
  2. data/README +282 -0
  3. data/README.rdoc +282 -0
  4. data/Rakefile +35 -0
  5. data/generators/scaffold_resource/USAGE +29 -0
  6. data/generators/scaffold_resource/scaffold_resource_generator.rb +179 -0
  7. data/generators/scaffold_resource/templates/controller.rb +2 -0
  8. data/generators/scaffold_resource/templates/fixtures.yml +10 -0
  9. data/generators/scaffold_resource/templates/functional_test.rb +57 -0
  10. data/generators/scaffold_resource/templates/helper.rb +2 -0
  11. data/generators/scaffold_resource/templates/migration.rb +15 -0
  12. data/generators/scaffold_resource/templates/model.rb +2 -0
  13. data/generators/scaffold_resource/templates/old_migration.rb +13 -0
  14. data/generators/scaffold_resource/templates/rspec/functional_spec.rb +255 -0
  15. data/generators/scaffold_resource/templates/rspec/helper_spec.rb +11 -0
  16. data/generators/scaffold_resource/templates/rspec/routing_spec.rb +61 -0
  17. data/generators/scaffold_resource/templates/rspec/unit_spec.rb +11 -0
  18. data/generators/scaffold_resource/templates/rspec/views/edit_spec.rb +28 -0
  19. data/generators/scaffold_resource/templates/rspec/views/index_spec.rb +26 -0
  20. data/generators/scaffold_resource/templates/rspec/views/new_spec.rb +30 -0
  21. data/generators/scaffold_resource/templates/rspec/views/show_spec.rb +25 -0
  22. data/generators/scaffold_resource/templates/shoulda_functional_test.rb +19 -0
  23. data/generators/scaffold_resource/templates/unit_test.rb +7 -0
  24. data/generators/scaffold_resource/templates/view__form.erb +6 -0
  25. data/generators/scaffold_resource/templates/view__form.haml +5 -0
  26. data/generators/scaffold_resource/templates/view_edit.erb +16 -0
  27. data/generators/scaffold_resource/templates/view_edit.haml +11 -0
  28. data/generators/scaffold_resource/templates/view_index.erb +22 -0
  29. data/generators/scaffold_resource/templates/view_index.haml +19 -0
  30. data/generators/scaffold_resource/templates/view_new.erb +12 -0
  31. data/generators/scaffold_resource/templates/view_new.haml +9 -0
  32. data/generators/scaffold_resource/templates/view_show.erb +9 -0
  33. data/generators/scaffold_resource/templates/view_show.haml +9 -0
  34. data/init.rb +1 -0
  35. data/lib/resource_controller.rb +15 -0
  36. data/lib/resource_controller/accessors.rb +77 -0
  37. data/lib/resource_controller/action_options.rb +40 -0
  38. data/lib/resource_controller/actions.rb +75 -0
  39. data/lib/resource_controller/base.rb +15 -0
  40. data/lib/resource_controller/class_methods.rb +22 -0
  41. data/lib/resource_controller/controller.rb +63 -0
  42. data/lib/resource_controller/failable_action_options.rb +25 -0
  43. data/lib/resource_controller/helpers.rb +28 -0
  44. data/lib/resource_controller/helpers/current_objects.rb +69 -0
  45. data/lib/resource_controller/helpers/internal.rb +76 -0
  46. data/lib/resource_controller/helpers/nested.rb +45 -0
  47. data/lib/resource_controller/helpers/urls.rb +124 -0
  48. data/lib/resource_controller/response_collector.rb +27 -0
  49. data/lib/resource_controller/version.rb +9 -0
  50. data/lib/tasks/gem.rake +67 -0
  51. data/lib/urligence.rb +50 -0
  52. data/rails/init.rb +6 -0
  53. data/test/Rakefile +10 -0
  54. data/test/app/controllers/application.rb +7 -0
  55. data/test/app/controllers/cms/options_controller.rb +3 -0
  56. data/test/app/controllers/cms/products_controller.rb +3 -0
  57. data/test/app/controllers/comments_controller.rb +3 -0
  58. data/test/app/controllers/people_controller.rb +9 -0
  59. data/test/app/controllers/photos_controller.rb +11 -0
  60. data/test/app/controllers/posts_controller.rb +10 -0
  61. data/test/app/controllers/projects_controller.rb +3 -0
  62. data/test/app/controllers/somethings_controller.rb +3 -0
  63. data/test/app/controllers/tags_controller.rb +13 -0
  64. data/test/app/controllers/users_controller.rb +12 -0
  65. data/test/app/helpers/application_helper.rb +3 -0
  66. data/test/app/helpers/cms/products_helper.rb +2 -0
  67. data/test/app/helpers/comments_helper.rb +2 -0
  68. data/test/app/helpers/options_helper.rb +2 -0
  69. data/test/app/helpers/people_helper.rb +2 -0
  70. data/test/app/helpers/photos_helper.rb +2 -0
  71. data/test/app/helpers/posts_helper.rb +2 -0
  72. data/test/app/helpers/projects_helper.rb +2 -0
  73. data/test/app/helpers/somethings_helper.rb +2 -0
  74. data/test/app/helpers/tags_helper.rb +2 -0
  75. data/test/app/helpers/users_helper.rb +2 -0
  76. data/test/app/models/account.rb +3 -0
  77. data/test/app/models/comment.rb +3 -0
  78. data/test/app/models/option.rb +3 -0
  79. data/test/app/models/photo.rb +4 -0
  80. data/test/app/models/post.rb +3 -0
  81. data/test/app/models/product.rb +3 -0
  82. data/test/app/models/project.rb +2 -0
  83. data/test/app/models/something.rb +2 -0
  84. data/test/app/models/tag.rb +3 -0
  85. data/test/app/views/cms/options/edit.rhtml +17 -0
  86. data/test/app/views/cms/options/index.rhtml +20 -0
  87. data/test/app/views/cms/options/new.rhtml +16 -0
  88. data/test/app/views/cms/options/show.rhtml +8 -0
  89. data/test/app/views/cms/products/edit.rhtml +17 -0
  90. data/test/app/views/cms/products/index.rhtml +20 -0
  91. data/test/app/views/cms/products/new.rhtml +16 -0
  92. data/test/app/views/cms/products/show.rhtml +8 -0
  93. data/test/app/views/comments/edit.rhtml +27 -0
  94. data/test/app/views/comments/index.rhtml +24 -0
  95. data/test/app/views/comments/new.rhtml +26 -0
  96. data/test/app/views/comments/show.rhtml +18 -0
  97. data/test/app/views/layouts/application.rhtml +17 -0
  98. data/test/app/views/layouts/comments.rhtml +17 -0
  99. data/test/app/views/layouts/options.rhtml +17 -0
  100. data/test/app/views/layouts/people.rhtml +17 -0
  101. data/test/app/views/layouts/photos.rhtml +17 -0
  102. data/test/app/views/layouts/projects.rhtml +17 -0
  103. data/test/app/views/layouts/somethings.rhtml +17 -0
  104. data/test/app/views/layouts/tags.rhtml +17 -0
  105. data/test/app/views/people/edit.rhtml +17 -0
  106. data/test/app/views/people/index.rhtml +20 -0
  107. data/test/app/views/people/new.rhtml +16 -0
  108. data/test/app/views/people/show.rhtml +8 -0
  109. data/test/app/views/photos/edit.rhtml +17 -0
  110. data/test/app/views/photos/index.rhtml +20 -0
  111. data/test/app/views/photos/new.rhtml +16 -0
  112. data/test/app/views/photos/show.rhtml +8 -0
  113. data/test/app/views/posts/edit.rhtml +22 -0
  114. data/test/app/views/posts/index.rhtml +22 -0
  115. data/test/app/views/posts/new.rhtml +21 -0
  116. data/test/app/views/posts/show.rhtml +13 -0
  117. data/test/app/views/projects/edit.rhtml +17 -0
  118. data/test/app/views/projects/index.rhtml +20 -0
  119. data/test/app/views/projects/new.rhtml +16 -0
  120. data/test/app/views/projects/show.rhtml +8 -0
  121. data/test/app/views/somethings/edit.rhtml +17 -0
  122. data/test/app/views/somethings/index.rhtml +20 -0
  123. data/test/app/views/somethings/new.rhtml +16 -0
  124. data/test/app/views/somethings/show.rhtml +8 -0
  125. data/test/app/views/tags/edit.rhtml +17 -0
  126. data/test/app/views/tags/index.rhtml +20 -0
  127. data/test/app/views/tags/index.rjs +0 -0
  128. data/test/app/views/tags/new.rhtml +16 -0
  129. data/test/app/views/tags/show.rhtml +8 -0
  130. data/test/app/views/users/edit.rhtml +17 -0
  131. data/test/app/views/users/index.rhtml +20 -0
  132. data/test/app/views/users/new.rhtml +16 -0
  133. data/test/app/views/users/show.rhtml +8 -0
  134. data/test/config/boot.rb +109 -0
  135. data/test/config/database.yml +16 -0
  136. data/test/config/environment.rb +64 -0
  137. data/test/config/environments/development.rb +21 -0
  138. data/test/config/environments/test.rb +19 -0
  139. data/test/config/routes.rb +51 -0
  140. data/test/db/migrate/001_create_posts.rb +12 -0
  141. data/test/db/migrate/002_create_products.rb +11 -0
  142. data/test/db/migrate/003_create_comments.rb +13 -0
  143. data/test/db/migrate/004_create_options.rb +12 -0
  144. data/test/db/migrate/005_create_photos.rb +11 -0
  145. data/test/db/migrate/006_create_tags.rb +17 -0
  146. data/test/db/migrate/007_create_somethings.rb +11 -0
  147. data/test/db/migrate/008_create_accounts.rb +11 -0
  148. data/test/db/migrate/009_add_account_id_to_photos.rb +9 -0
  149. data/test/db/migrate/010_create_projects.rb +11 -0
  150. data/test/db/schema.rb +65 -0
  151. data/test/log/development.log +2918 -0
  152. data/test/log/test.log +135619 -0
  153. data/test/log/thin.log +12 -0
  154. data/test/script/console +3 -0
  155. data/test/script/destroy +3 -0
  156. data/test/script/generate +3 -0
  157. data/test/script/server +3 -0
  158. data/test/test/fixtures/accounts.yml +7 -0
  159. data/test/test/fixtures/comments.yml +11 -0
  160. data/test/test/fixtures/options.yml +9 -0
  161. data/test/test/fixtures/photos.yml +9 -0
  162. data/test/test/fixtures/photos_tags.yml +3 -0
  163. data/test/test/fixtures/posts.yml +9 -0
  164. data/test/test/fixtures/products.yml +7 -0
  165. data/test/test/fixtures/projects.yml +7 -0
  166. data/test/test/fixtures/somethings.yml +7 -0
  167. data/test/test/fixtures/tags.yml +7 -0
  168. data/test/test/functional/cms/options_controller_test.rb +23 -0
  169. data/test/test/functional/cms/products_controller_test.rb +23 -0
  170. data/test/test/functional/comments_controller_test.rb +26 -0
  171. data/test/test/functional/people_controller_test.rb +34 -0
  172. data/test/test/functional/photos_controller_test.rb +130 -0
  173. data/test/test/functional/posts_controller_test.rb +34 -0
  174. data/test/test/functional/projects_controller_test.rb +18 -0
  175. data/test/test/functional/somethings_controller_test.rb +28 -0
  176. data/test/test/functional/tags_controller_test.rb +64 -0
  177. data/test/test/functional/users_controller_test.rb +24 -0
  178. data/test/test/test_helper.rb +12 -0
  179. data/test/test/unit/accessors_test.rb +110 -0
  180. data/test/test/unit/account_test.rb +7 -0
  181. data/test/test/unit/action_options_test.rb +109 -0
  182. data/test/test/unit/base_test.rb +11 -0
  183. data/test/test/unit/comment_test.rb +10 -0
  184. data/test/test/unit/failable_action_options_test.rb +77 -0
  185. data/test/test/unit/helpers/current_objects_test.rb +127 -0
  186. data/test/test/unit/helpers/internal_test.rb +106 -0
  187. data/test/test/unit/helpers/nested_test.rb +82 -0
  188. data/test/test/unit/helpers/urls_test.rb +71 -0
  189. data/test/test/unit/helpers_test.rb +25 -0
  190. data/test/test/unit/option_test.rb +10 -0
  191. data/test/test/unit/photo_test.rb +10 -0
  192. data/test/test/unit/post_test.rb +10 -0
  193. data/test/test/unit/project_test.rb +10 -0
  194. data/test/test/unit/response_collector_test.rb +49 -0
  195. data/test/test/unit/something_test.rb +10 -0
  196. data/test/test/unit/tag_test.rb +10 -0
  197. data/test/test/unit/urligence_test.rb +203 -0
  198. data/test/vendor/plugins/shoulda/Rakefile +32 -0
  199. data/test/vendor/plugins/shoulda/bin/convert_to_should_syntax +40 -0
  200. data/test/vendor/plugins/shoulda/init.rb +3 -0
  201. data/test/vendor/plugins/shoulda/lib/shoulda.rb +43 -0
  202. data/test/vendor/plugins/shoulda/lib/shoulda/active_record_helpers.rb +580 -0
  203. data/test/vendor/plugins/shoulda/lib/shoulda/color.rb +77 -0
  204. data/test/vendor/plugins/shoulda/lib/shoulda/controller_tests/controller_tests.rb +467 -0
  205. data/test/vendor/plugins/shoulda/lib/shoulda/controller_tests/formats/html.rb +201 -0
  206. data/test/vendor/plugins/shoulda/lib/shoulda/controller_tests/formats/xml.rb +170 -0
  207. data/test/vendor/plugins/shoulda/lib/shoulda/gem/proc_extensions.rb +14 -0
  208. data/test/vendor/plugins/shoulda/lib/shoulda/gem/shoulda.rb +239 -0
  209. data/test/vendor/plugins/shoulda/lib/shoulda/general.rb +118 -0
  210. data/test/vendor/plugins/shoulda/lib/shoulda/private_helpers.rb +22 -0
  211. metadata +312 -0
@@ -0,0 +1,201 @@
1
+ module ThoughtBot # :nodoc:
2
+ module Shoulda # :nodoc:
3
+ module Controller # :nodoc:
4
+ module HTML # :nodoc: all
5
+ def self.included(other)
6
+ other.class_eval do
7
+ extend ThoughtBot::Shoulda::Controller::HTML::ClassMethods
8
+ end
9
+ end
10
+
11
+ module ClassMethods
12
+ def controller_name_from_class
13
+ self.name.gsub(/Test/, '')
14
+ end
15
+
16
+ def make_show_html_tests(res)
17
+ context "on GET to #{controller_name_from_class}#show" do
18
+ setup do
19
+ record = get_existing_record(res)
20
+ parent_params = make_parent_params(res, record)
21
+ get :show, parent_params.merge({ res.identifier => record.to_param })
22
+ end
23
+
24
+ if res.denied.actions.include?(:show)
25
+ should_not_assign_to res.object
26
+ should_redirect_to res.denied.redirect
27
+ should_set_the_flash_to res.denied.flash
28
+ else
29
+ should_assign_to res.object
30
+ should_respond_with :success
31
+ should_render_template :show
32
+ should_not_set_the_flash
33
+ end
34
+ end
35
+ end
36
+
37
+ def make_edit_html_tests(res)
38
+ context "on GET to #{controller_name_from_class}#edit" do
39
+ setup do
40
+ @record = get_existing_record(res)
41
+ parent_params = make_parent_params(res, @record)
42
+ get :edit, parent_params.merge({ res.identifier => @record.to_param })
43
+ end
44
+
45
+ if res.denied.actions.include?(:edit)
46
+ should_not_assign_to res.object
47
+ should_redirect_to res.denied.redirect
48
+ should_set_the_flash_to res.denied.flash
49
+ else
50
+ should_assign_to res.object
51
+ should_respond_with :success
52
+ should_render_template :edit
53
+ should_not_set_the_flash
54
+ should_render_a_form
55
+ should "set @#{res.object} to requested instance" do
56
+ assert_equal @record, assigns(res.object)
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ def make_index_html_tests(res)
63
+ context "on GET to #{controller_name_from_class}#index" do
64
+ setup do
65
+ record = get_existing_record(res) rescue nil
66
+ parent_params = make_parent_params(res, record)
67
+ get(:index, parent_params)
68
+ end
69
+
70
+ if res.denied.actions.include?(:index)
71
+ should_not_assign_to res.object.to_s.pluralize
72
+ should_redirect_to res.denied.redirect
73
+ should_set_the_flash_to res.denied.flash
74
+ else
75
+ should_respond_with :success
76
+ should_assign_to res.object.to_s.pluralize
77
+ should_render_template :index
78
+ should_not_set_the_flash
79
+ end
80
+ end
81
+ end
82
+
83
+ def make_new_html_tests(res)
84
+ context "on GET to #{controller_name_from_class}#new" do
85
+ setup do
86
+ record = get_existing_record(res) rescue nil
87
+ parent_params = make_parent_params(res, record)
88
+ get(:new, parent_params)
89
+ end
90
+
91
+ if res.denied.actions.include?(:new)
92
+ should_not_assign_to res.object
93
+ should_redirect_to res.denied.redirect
94
+ should_set_the_flash_to res.denied.flash
95
+ else
96
+ should_respond_with :success
97
+ should_assign_to res.object
98
+ should_not_set_the_flash
99
+ should_render_template :new
100
+ should_render_a_form
101
+ end
102
+ end
103
+ end
104
+
105
+ def make_destroy_html_tests(res)
106
+ context "on DELETE to #{controller_name_from_class}#destroy" do
107
+ setup do
108
+ @record = get_existing_record(res)
109
+ parent_params = make_parent_params(res, @record)
110
+ delete :destroy, parent_params.merge({ res.identifier => @record.to_param })
111
+ end
112
+
113
+ if res.denied.actions.include?(:destroy)
114
+ should_redirect_to res.denied.redirect
115
+ should_set_the_flash_to res.denied.flash
116
+
117
+ should "not destroy record" do
118
+ assert_nothing_raised { assert @record.reload }
119
+ end
120
+ else
121
+ should_set_the_flash_to res.destroy.flash
122
+ if res.destroy.redirect.is_a? Symbol
123
+ should_respond_with res.destroy.redirect
124
+ else
125
+ should_redirect_to res.destroy.redirect
126
+ end
127
+
128
+ should "destroy record" do
129
+ assert_raises(::ActiveRecord::RecordNotFound, "@#{res.object} was not destroyed.") do
130
+ @record.reload
131
+ end
132
+ end
133
+ end
134
+ end
135
+ end
136
+
137
+ def make_create_html_tests(res)
138
+ context "on POST to #{controller_name_from_class}#create with #{res.create.params.inspect}" do
139
+ setup do
140
+ record = get_existing_record(res) rescue nil
141
+ parent_params = make_parent_params(res, record)
142
+ @count = res.klass.count
143
+ post :create, parent_params.merge(res.object => res.create.params)
144
+ end
145
+
146
+ if res.denied.actions.include?(:create)
147
+ should_redirect_to res.denied.redirect
148
+ should_set_the_flash_to res.denied.flash
149
+ should_not_assign_to res.object
150
+
151
+ should "not create new record" do
152
+ assert_equal @count, res.klass.count
153
+ end
154
+ else
155
+ should_assign_to res.object
156
+ should_set_the_flash_to res.create.flash
157
+ if res.create.redirect.is_a? Symbol
158
+ should_respond_with res.create.redirect
159
+ else
160
+ should_redirect_to res.create.redirect
161
+ end
162
+
163
+ should "not have errors on @#{res.object}" do
164
+ assert_equal [], pretty_error_messages(assigns(res.object)), "@#{res.object} has errors:"
165
+ end
166
+ end
167
+ end
168
+ end
169
+
170
+ def make_update_html_tests(res)
171
+ context "on PUT to #{controller_name_from_class}#update with #{res.create.params.inspect}" do
172
+ setup do
173
+ @record = get_existing_record(res)
174
+ parent_params = make_parent_params(res, @record)
175
+ put :update, parent_params.merge(res.identifier => @record.to_param, res.object => res.update.params)
176
+ end
177
+
178
+ if res.denied.actions.include?(:update)
179
+ should_not_assign_to res.object
180
+ should_redirect_to res.denied.redirect
181
+ should_set_the_flash_to res.denied.flash
182
+ else
183
+ should_assign_to res.object
184
+ should_set_the_flash_to(res.update.flash)
185
+ if res.update.redirect.is_a? Symbol
186
+ should_respond_with res.update.redirect
187
+ else
188
+ should_redirect_to res.update.redirect
189
+ end
190
+
191
+ should "not have errors on @#{res.object}" do
192
+ assert_equal [], pretty_error_messages(assigns(res.object)), "@#{res.object} has errors:"
193
+ end
194
+ end
195
+ end
196
+ end
197
+ end
198
+ end
199
+ end
200
+ end
201
+ end
@@ -0,0 +1,170 @@
1
+ module ThoughtBot # :nodoc:
2
+ module Shoulda # :nodoc:
3
+ module Controller # :nodoc:
4
+ module XML
5
+ def self.included(other) #:nodoc:
6
+ other.class_eval do
7
+ extend ThoughtBot::Shoulda::Controller::XML::ClassMethods
8
+ end
9
+ end
10
+
11
+ module ClassMethods
12
+ # Macro that creates a test asserting that the controller responded with an XML content-type
13
+ # and that the XML contains +<name/>+ as the root element.
14
+ def should_respond_with_xml_for(name = nil)
15
+ should "have ContentType set to 'application/xml'" do
16
+ assert_xml_response
17
+ end
18
+
19
+ if name
20
+ should "return <#{name}/> as the root element" do
21
+ body = @response.body.first(100).map {|l| " #{l}"}
22
+ assert_select name.to_s.dasherize, 1, "Body:\n#{body}...\nDoes not have <#{name}/> as the root element."
23
+ end
24
+ end
25
+ end
26
+ alias should_respond_with_xml should_respond_with_xml_for
27
+
28
+ protected
29
+
30
+ def make_show_xml_tests(res) # :nodoc:
31
+ context "on GET to #{controller_name_from_class}#show as xml" do
32
+ setup do
33
+ request_xml
34
+ record = get_existing_record(res)
35
+ parent_params = make_parent_params(res, record)
36
+ get :show, parent_params.merge({ res.identifier => record.to_param })
37
+ end
38
+
39
+ if res.denied.actions.include?(:show)
40
+ should_not_assign_to res.object
41
+ should_respond_with 401
42
+ else
43
+ should_assign_to res.object
44
+ should_respond_with :success
45
+ should_respond_with_xml_for res.object
46
+ end
47
+ end
48
+ end
49
+
50
+ def make_edit_xml_tests(res) # :nodoc:
51
+ # XML doesn't need an :edit action
52
+ end
53
+
54
+ def make_new_xml_tests(res) # :nodoc:
55
+ # XML doesn't need a :new action
56
+ end
57
+
58
+ def make_index_xml_tests(res) # :nodoc:
59
+ context "on GET to #{controller_name_from_class}#index as xml" do
60
+ setup do
61
+ request_xml
62
+ parent_params = make_parent_params(res)
63
+ get(:index, parent_params)
64
+ end
65
+
66
+ if res.denied.actions.include?(:index)
67
+ should_not_assign_to res.object.to_s.pluralize
68
+ should_respond_with 401
69
+ else
70
+ should_respond_with :success
71
+ should_respond_with_xml_for res.object.to_s.pluralize
72
+ should_assign_to res.object.to_s.pluralize
73
+ end
74
+ end
75
+ end
76
+
77
+ def make_destroy_xml_tests(res) # :nodoc:
78
+ context "on DELETE to #{controller_name_from_class}#destroy as xml" do
79
+ setup do
80
+ request_xml
81
+ @record = get_existing_record(res)
82
+ parent_params = make_parent_params(res, @record)
83
+ delete :destroy, parent_params.merge({ res.identifier => @record.to_param })
84
+ end
85
+
86
+ if res.denied.actions.include?(:destroy)
87
+ should_respond_with 401
88
+
89
+ should "not destroy record" do
90
+ assert @record.reload
91
+ end
92
+ else
93
+ should "destroy record" do
94
+ assert_raises(::ActiveRecord::RecordNotFound, "@#{res.object} was not destroyed.") do
95
+ @record.reload
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
101
+
102
+ def make_create_xml_tests(res) # :nodoc:
103
+ context "on POST to #{controller_name_from_class}#create as xml" do
104
+ setup do
105
+ request_xml
106
+ parent_params = make_parent_params(res)
107
+ @count = res.klass.count
108
+ post :create, parent_params.merge(res.object => res.create.params)
109
+ end
110
+
111
+ if res.denied.actions.include?(:create)
112
+ should_respond_with 401
113
+ should_not_assign_to res.object
114
+
115
+ should "not create new record" do
116
+ assert_equal @count, res.klass.count
117
+ end
118
+ else
119
+ should_assign_to res.object
120
+
121
+ should "not have errors on @#{res.object}" do
122
+ assert_equal [], pretty_error_messages(assigns(res.object)), "@#{res.object} has errors:"
123
+ end
124
+ end
125
+ end
126
+ end
127
+
128
+ def make_update_xml_tests(res) # :nodoc:
129
+ context "on PUT to #{controller_name_from_class}#update as xml" do
130
+ setup do
131
+ request_xml
132
+ @record = get_existing_record(res)
133
+ parent_params = make_parent_params(res, @record)
134
+ put :update, parent_params.merge(res.identifier => @record.to_param, res.object => res.update.params)
135
+ end
136
+
137
+ if res.denied.actions.include?(:update)
138
+ should_not_assign_to res.object
139
+ should_respond_with 401
140
+ else
141
+ should_assign_to res.object
142
+
143
+ should "not have errors on @#{res.object}" do
144
+ assert_equal [], assigns(res.object).errors.full_messages, "@#{res.object} has errors:"
145
+ end
146
+ end
147
+ end
148
+ end
149
+ end
150
+
151
+ # Sets the next request's format to 'application/xml'
152
+ def request_xml
153
+ @request.accept = "application/xml"
154
+ end
155
+
156
+ # Asserts that the controller's response was 'application/xml'
157
+ def assert_xml_response
158
+ content_type = (@response.headers["Content-Type"] || @response.headers["type"]).to_s
159
+ regex = %r{\bapplication/xml\b}
160
+
161
+ msg = "Content Type '#{content_type.inspect}' doesn't match '#{regex.inspect}'\n"
162
+ msg += "Body: #{@response.body.first(100).chomp} ..."
163
+
164
+ assert_match regex, content_type, msg
165
+ end
166
+
167
+ end
168
+ end
169
+ end
170
+ end
@@ -0,0 +1,14 @@
1
+ # Stolen straight from ActiveSupport
2
+
3
+ class Proc #:nodoc:
4
+ def bind(object)
5
+ block, time = self, Time.now
6
+ (class << object; self end).class_eval do
7
+ method_name = "__bind_#{time.to_i}_#{time.usec}"
8
+ define_method(method_name, &block)
9
+ method = instance_method(method_name)
10
+ remove_method(method_name)
11
+ method
12
+ end.bind(object)
13
+ end
14
+ end
@@ -0,0 +1,239 @@
1
+ require File.join(File.dirname(__FILE__), 'proc_extensions')
2
+
3
+ module Thoughtbot
4
+ module Shoulda
5
+ class << self
6
+ attr_accessor :current_context
7
+ end
8
+
9
+ VERSION = '1.1.1'
10
+
11
+ # = Should statements
12
+ #
13
+ # Should statements are just syntactic sugar over normal Test::Unit test methods. A should block
14
+ # contains all the normal code and assertions you're used to seeing, with the added benefit that
15
+ # they can be wrapped inside context blocks (see below).
16
+ #
17
+ # == Example:
18
+ #
19
+ # class UserTest << Test::Unit::TestCase
20
+ #
21
+ # def setup
22
+ # @user = User.new("John", "Doe")
23
+ # end
24
+ #
25
+ # should "return its full name"
26
+ # assert_equal 'John Doe', @user.full_name
27
+ # end
28
+ #
29
+ # end
30
+ #
31
+ # ...will produce the following test:
32
+ # * <tt>"test: User should return its full name. "</tt>
33
+ #
34
+ # Note: The part before <tt>should</tt> in the test name is gleamed from the name of the Test::Unit class.
35
+
36
+ def should(name, &blk)
37
+ if Shoulda.current_context
38
+ Shoulda.current_context.should(name, &blk)
39
+ else
40
+ context_name = self.name.gsub(/Test/, "")
41
+ context = Thoughtbot::Shoulda::Context.new(context_name, self) do
42
+ should(name, &blk)
43
+ end
44
+ context.build
45
+ end
46
+ end
47
+
48
+ # Just like should, but never runs, and instead prints an 'X' in the Test::Unit output.
49
+ def should_eventually(name, &blk)
50
+ context_name = self.name.gsub(/Test/, "")
51
+ context = Thoughtbot::Shoulda::Context.new(context_name, self) do
52
+ should_eventually(name, &blk)
53
+ end
54
+ context.build
55
+ end
56
+
57
+ # = Contexts
58
+ #
59
+ # A context block groups should statements under a common set of setup/teardown methods.
60
+ # Context blocks can be arbitrarily nested, and can do wonders for improving the maintainability
61
+ # and readability of your test code.
62
+ #
63
+ # A context block can contain setup, should, should_eventually, and teardown blocks.
64
+ #
65
+ # class UserTest << Test::Unit::TestCase
66
+ # context "A User instance" do
67
+ # setup do
68
+ # @user = User.find(:first)
69
+ # end
70
+ #
71
+ # should "return its full name"
72
+ # assert_equal 'John Doe', @user.full_name
73
+ # end
74
+ # end
75
+ # end
76
+ #
77
+ # This code will produce the method <tt>"test: A User instance should return its full name. "</tt>.
78
+ #
79
+ # Contexts may be nested. Nested contexts run their setup blocks from out to in before each
80
+ # should statement. They then run their teardown blocks from in to out after each should statement.
81
+ #
82
+ # class UserTest << Test::Unit::TestCase
83
+ # context "A User instance" do
84
+ # setup do
85
+ # @user = User.find(:first)
86
+ # end
87
+ #
88
+ # should "return its full name"
89
+ # assert_equal 'John Doe', @user.full_name
90
+ # end
91
+ #
92
+ # context "with a profile" do
93
+ # setup do
94
+ # @user.profile = Profile.find(:first)
95
+ # end
96
+ #
97
+ # should "return true when sent :has_profile?"
98
+ # assert @user.has_profile?
99
+ # end
100
+ # end
101
+ # end
102
+ # end
103
+ #
104
+ # This code will produce the following methods
105
+ # * <tt>"test: A User instance should return its full name. "</tt>
106
+ # * <tt>"test: A User instance with a profile should return true when sent :has_profile?. "</tt>
107
+ #
108
+ # <b>Just like should statements, a context block can exist next to normal <tt>def test_the_old_way; end</tt>
109
+ # tests</b>. This means you do not have to fully commit to the context/should syntax in a test file.
110
+
111
+ def context(name, &blk)
112
+ if Shoulda.current_context
113
+ Shoulda.current_context.context(name, &blk)
114
+ else
115
+ context = Thoughtbot::Shoulda::Context.new(name, self, &blk)
116
+ context.build
117
+ end
118
+ end
119
+
120
+ class Context # :nodoc:
121
+
122
+ attr_accessor :name # my name
123
+ attr_accessor :parent # may be another context, or the original test::unit class.
124
+ attr_accessor :subcontexts # array of contexts nested under myself
125
+ attr_accessor :setup_block # block given via a setup method
126
+ attr_accessor :teardown_block # block given via a teardown method
127
+ attr_accessor :shoulds # array of hashes representing the should statements
128
+ attr_accessor :should_eventuallys # array of hashes representing the should eventually statements
129
+
130
+ def initialize(name, parent, &blk)
131
+ Shoulda.current_context = self
132
+ self.name = name
133
+ self.parent = parent
134
+ self.setup_block = nil
135
+ self.teardown_block = nil
136
+ self.shoulds = []
137
+ self.should_eventuallys = []
138
+ self.subcontexts = []
139
+
140
+ blk.bind(self).call
141
+ Shoulda.current_context = nil
142
+ end
143
+
144
+ def context(name, &blk)
145
+ subcontexts << Context.new(name, self, &blk)
146
+ Shoulda.current_context = self
147
+ end
148
+
149
+ def setup(&blk)
150
+ self.setup_block = blk
151
+ end
152
+
153
+ def teardown(&blk)
154
+ self.teardown_block = blk
155
+ end
156
+
157
+ def should(name, &blk)
158
+ self.shoulds << { :name => name, :block => blk }
159
+ end
160
+
161
+ def should_eventually(name, &blk)
162
+ self.should_eventuallys << { :name => name, :block => blk }
163
+ end
164
+
165
+ def full_name
166
+ parent_name = parent.full_name if am_subcontext?
167
+ return [parent_name, name].join(" ").strip
168
+ end
169
+
170
+ def am_subcontext?
171
+ parent.is_a?(self.class) # my parent is the same class as myself.
172
+ end
173
+
174
+ def test_unit_class
175
+ am_subcontext? ? parent.test_unit_class : parent
176
+ end
177
+
178
+ def create_test_from_should_hash(should)
179
+ test_name = ["test:", full_name, "should", "#{should[:name]}. "].flatten.join(' ').to_sym
180
+
181
+ if test_unit_class.instance_methods.include?(test_name.to_s)
182
+ warn " * WARNING: '#{test_name}' is already defined"
183
+ end
184
+
185
+ context = self
186
+ test_unit_class.send(:define_method, test_name) do |*args|
187
+ begin
188
+ context.run_all_setup_blocks(self)
189
+ should[:block].bind(self).call
190
+ ensure
191
+ context.run_all_teardown_blocks(self)
192
+ end
193
+ end
194
+ end
195
+
196
+ def run_all_setup_blocks(binding)
197
+ self.parent.run_all_setup_blocks(binding) if am_subcontext?
198
+ setup_block.bind(binding).call if setup_block
199
+ end
200
+
201
+ def run_all_teardown_blocks(binding)
202
+ teardown_block.bind(binding).call if teardown_block
203
+ self.parent.run_all_teardown_blocks(binding) if am_subcontext?
204
+ end
205
+
206
+ def print_should_eventuallys
207
+ should_eventuallys.each do |should|
208
+ test_name = [full_name, "should", "#{should[:name]}. "].flatten.join(' ')
209
+ puts " * DEFERRED: " + test_name
210
+ end
211
+ subcontexts.each { |context| context.print_should_eventuallys }
212
+ end
213
+
214
+ def build
215
+ shoulds.each do |should|
216
+ create_test_from_should_hash(should)
217
+ end
218
+
219
+ subcontexts.each { |context| context.build }
220
+
221
+ print_should_eventuallys
222
+ end
223
+
224
+ def method_missing(method, *args, &blk)
225
+ test_unit_class.send(method, *args, &blk)
226
+ end
227
+
228
+ end
229
+ end
230
+ end
231
+
232
+ module Test # :nodoc: all
233
+ module Unit
234
+ class TestCase
235
+ extend Thoughtbot::Shoulda
236
+ end
237
+ end
238
+ end
239
+