technicalpickles-shoulda 2.0.6 → 2.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (108) hide show
  1. data/README.rdoc +61 -11
  2. data/Rakefile +7 -5
  3. data/lib/shoulda.rb +7 -15
  4. data/lib/shoulda/action_controller.rb +28 -0
  5. data/lib/shoulda/action_controller/helpers.rb +47 -0
  6. data/lib/shoulda/action_controller/macros.rb +277 -0
  7. data/lib/shoulda/action_controller/matchers.rb +37 -0
  8. data/lib/shoulda/action_controller/matchers/assign_to_matcher.rb +109 -0
  9. data/lib/shoulda/action_controller/matchers/filter_param_matcher.rb +57 -0
  10. data/lib/shoulda/action_controller/matchers/render_with_layout_matcher.rb +81 -0
  11. data/lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb +70 -0
  12. data/lib/shoulda/action_controller/matchers/respond_with_matcher.rb +81 -0
  13. data/lib/shoulda/action_controller/matchers/route_matcher.rb +93 -0
  14. data/lib/shoulda/action_controller/matchers/set_session_matcher.rb +83 -0
  15. data/lib/shoulda/action_controller/matchers/set_the_flash_matcher.rb +85 -0
  16. data/lib/shoulda/action_mailer.rb +1 -1
  17. data/lib/shoulda/action_mailer/assertions.rb +32 -33
  18. data/lib/shoulda/action_view.rb +10 -0
  19. data/lib/shoulda/action_view/macros.rb +56 -0
  20. data/lib/shoulda/active_record.rb +6 -2
  21. data/lib/shoulda/active_record/assertions.rb +62 -81
  22. data/lib/shoulda/active_record/helpers.rb +40 -0
  23. data/lib/shoulda/active_record/macros.rb +514 -640
  24. data/lib/shoulda/active_record/matchers.rb +42 -0
  25. data/lib/shoulda/active_record/matchers/allow_mass_assignment_of_matcher.rb +83 -0
  26. data/lib/shoulda/active_record/matchers/allow_value_matcher.rb +102 -0
  27. data/lib/shoulda/active_record/matchers/association_matcher.rb +226 -0
  28. data/lib/shoulda/active_record/matchers/ensure_inclusion_of_matcher.rb +87 -0
  29. data/lib/shoulda/active_record/matchers/ensure_length_of_matcher.rb +141 -0
  30. data/lib/shoulda/active_record/matchers/have_db_column_matcher.rb +169 -0
  31. data/lib/shoulda/active_record/matchers/have_index_matcher.rb +105 -0
  32. data/lib/shoulda/active_record/matchers/have_named_scope_matcher.rb +125 -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_numericality_of_matcher.rb +39 -0
  36. data/lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb +60 -0
  37. data/lib/shoulda/active_record/matchers/validate_uniqueness_of_matcher.rb +148 -0
  38. data/lib/shoulda/active_record/matchers/validation_matcher.rb +56 -0
  39. data/lib/shoulda/assertions.rb +50 -40
  40. data/lib/shoulda/autoload_macros.rb +46 -0
  41. data/lib/shoulda/context.rb +124 -126
  42. data/lib/shoulda/helpers.rb +5 -7
  43. data/lib/shoulda/macros.rb +63 -64
  44. data/lib/shoulda/private_helpers.rb +16 -18
  45. data/lib/shoulda/rails.rb +5 -11
  46. data/lib/shoulda/rspec.rb +11 -0
  47. data/lib/shoulda/tasks/list_tests.rake +6 -1
  48. data/lib/shoulda/test_unit.rb +19 -0
  49. data/rails/init.rb +7 -1
  50. data/test/README +2 -2
  51. data/test/fail_macros.rb +16 -16
  52. data/test/functional/posts_controller_test.rb +33 -24
  53. data/test/functional/users_controller_test.rb +0 -19
  54. data/test/matchers/active_record/allow_mass_assignment_of_matcher_test.rb +68 -0
  55. data/test/matchers/active_record/allow_value_matcher_test.rb +41 -0
  56. data/test/matchers/active_record/association_matcher_test.rb +258 -0
  57. data/test/matchers/active_record/ensure_inclusion_of_matcher_test.rb +80 -0
  58. data/test/matchers/active_record/ensure_length_of_matcher_test.rb +158 -0
  59. data/test/matchers/active_record/have_db_column_matcher_test.rb +169 -0
  60. data/test/matchers/active_record/have_index_matcher_test.rb +74 -0
  61. data/test/matchers/active_record/have_named_scope_matcher_test.rb +65 -0
  62. data/test/matchers/active_record/have_readonly_attributes_matcher_test.rb +29 -0
  63. data/test/matchers/active_record/validate_acceptance_of_matcher_test.rb +44 -0
  64. data/test/matchers/active_record/validate_numericality_of_matcher_test.rb +52 -0
  65. data/test/matchers/active_record/validate_presence_of_matcher_test.rb +86 -0
  66. data/test/matchers/active_record/validate_uniqueness_of_matcher_test.rb +147 -0
  67. data/test/matchers/controller/assign_to_matcher_test.rb +35 -0
  68. data/test/matchers/controller/filter_param_matcher_test.rb +32 -0
  69. data/test/matchers/controller/render_with_layout_matcher_test.rb +33 -0
  70. data/test/matchers/controller/respond_with_content_type_matcher_test.rb +27 -0
  71. data/test/matchers/controller/respond_with_matcher_test.rb +106 -0
  72. data/test/matchers/controller/route_matcher_test.rb +58 -0
  73. data/test/matchers/controller/set_session_matcher_test.rb +27 -0
  74. data/test/matchers/controller/set_the_flash_matcher.rb +41 -0
  75. data/test/model_builder.rb +106 -0
  76. data/test/other/autoload_macro_test.rb +18 -0
  77. data/test/other/helpers_test.rb +58 -0
  78. data/test/other/private_helpers_test.rb +1 -1
  79. data/test/other/should_test.rb +16 -16
  80. data/test/rails_root/app/controllers/posts_controller.rb +6 -5
  81. data/test/rails_root/app/models/pets/dog.rb +10 -0
  82. data/test/rails_root/app/models/treat.rb +3 -0
  83. data/test/rails_root/app/models/user.rb +4 -3
  84. data/test/rails_root/app/views/layouts/posts.rhtml +2 -0
  85. data/test/rails_root/config/database.yml +1 -1
  86. data/test/rails_root/config/environment.rb +1 -1
  87. data/test/rails_root/config/environments/{sqlite3.rb → test.rb} +0 -0
  88. data/test/rails_root/db/migrate/001_create_users.rb +3 -2
  89. data/test/rails_root/db/migrate/011_create_treats.rb +12 -0
  90. data/test/rails_root/test/shoulda_macros/custom_macro.rb +6 -0
  91. data/test/rails_root/vendor/gems/gem_with_macro-0.0.1/shoulda_macros/gem_macro.rb +6 -0
  92. data/test/rails_root/vendor/plugins/plugin_with_macro/shoulda_macros/plugin_macro.rb +6 -0
  93. data/test/rspec_test.rb +207 -0
  94. data/test/test_helper.rb +3 -1
  95. data/test/unit/address_test.rb +1 -1
  96. data/test/unit/dog_test.rb +5 -2
  97. data/test/unit/post_test.rb +7 -3
  98. data/test/unit/product_test.rb +2 -2
  99. data/test/unit/tag_test.rb +2 -1
  100. data/test/unit/user_test.rb +27 -9
  101. metadata +82 -13
  102. data/lib/shoulda/controller.rb +0 -30
  103. data/lib/shoulda/controller/formats/html.rb +0 -201
  104. data/lib/shoulda/controller/formats/xml.rb +0 -170
  105. data/lib/shoulda/controller/helpers.rb +0 -64
  106. data/lib/shoulda/controller/macros.rb +0 -316
  107. data/lib/shoulda/controller/resource_options.rb +0 -236
  108. data/test/rails_root/app/models/dog.rb +0 -5
@@ -1,236 +0,0 @@
1
- module ThoughtBot # :nodoc:
2
- module Shoulda # :nodoc:
3
- module Controller
4
- # Formats tested by #should_be_restful. Defaults to [:html, :xml]
5
- VALID_FORMATS = Dir.glob(File.join(File.dirname(__FILE__), 'formats', '*.rb')).map { |f| File.basename(f, '.rb') }.map(&:to_sym) # :doc:
6
- VALID_FORMATS.each {|f| require "shoulda/controller/formats/#{f}"}
7
-
8
- # Actions tested by #should_be_restful
9
- VALID_ACTIONS = [:index, :show, :new, :edit, :create, :update, :destroy] # :doc:
10
-
11
- # A ResourceOptions object is passed into should_be_restful in order to configure the tests for your controller.
12
- #
13
- # Example:
14
- # class UsersControllerTest < Test::Unit::TestCase
15
- # fixtures :all
16
- #
17
- # def setup
18
- # ...normal setup code...
19
- # @user = User.find(:first)
20
- # end
21
- #
22
- # should_be_restful do |resource|
23
- # resource.identifier = :id
24
- # resource.klass = User
25
- # resource.object = :user
26
- # resource.parent = []
27
- # resource.actions = [:index, :show, :new, :edit, :update, :create, :destroy]
28
- # resource.formats = [:html, :xml]
29
- #
30
- # resource.create.params = { :name => "bob", :email => 'bob@bob.com', :age => 13}
31
- # resource.update.params = { :name => "sue" }
32
- #
33
- # resource.create.redirect = "user_url(@user)"
34
- # resource.update.redirect = "user_url(@user)"
35
- # resource.destroy.redirect = "users_url"
36
- #
37
- # resource.create.flash = /created/i
38
- # resource.update.flash = /updated/i
39
- # resource.destroy.flash = /removed/i
40
- # end
41
- # end
42
- #
43
- # Whenever possible, the resource attributes will be set to sensible defaults.
44
- #
45
- class ResourceOptions
46
- # Configuration options for the create, update, destroy actions under should_be_restful
47
- class ActionOptions
48
- # String evaled to get the target of the redirection.
49
- # All of the instance variables set by the controller will be available to the
50
- # evaled code.
51
- #
52
- # Example:
53
- # resource.create.redirect = "user_url(@user.company, @user)"
54
- #
55
- # Defaults to a generated url based on the name of the controller, the action, and the resource.parents list.
56
- attr_accessor :redirect
57
-
58
- # String or Regexp describing a value expected in the flash. Will match against any flash key.
59
- #
60
- # Defaults:
61
- # destroy:: /removed/
62
- # create:: /created/
63
- # update:: /updated/
64
- attr_accessor :flash
65
-
66
- # Hash describing the params that should be sent in with this action.
67
- attr_accessor :params
68
- end
69
-
70
- # Configuration options for the denied actions under should_be_restful
71
- #
72
- # Example:
73
- # context "The public" do
74
- # setup do
75
- # @request.session[:logged_in] = false
76
- # end
77
- #
78
- # should_be_restful do |resource|
79
- # resource.parent = :user
80
- #
81
- # resource.denied.actions = [:index, :show, :edit, :new, :create, :update, :destroy]
82
- # resource.denied.flash = /get outta here/i
83
- # resource.denied.redirect = 'new_session_url'
84
- # end
85
- # end
86
- #
87
- class DeniedOptions
88
- # String evaled to get the target of the redirection.
89
- # All of the instance variables set by the controller will be available to the
90
- # evaled code.
91
- #
92
- # Example:
93
- # resource.create.redirect = "user_url(@user.company, @user)"
94
- attr_accessor :redirect
95
-
96
- # String or Regexp describing a value expected in the flash. Will match against any flash key.
97
- #
98
- # Example:
99
- # resource.create.flash = /created/
100
- attr_accessor :flash
101
-
102
- # Actions that should be denied (only used by resource.denied). <i>Note that these actions will
103
- # only be tested if they are also listed in +resource.actions+</i>
104
- # The special value of :all will deny all of the REST actions.
105
- attr_accessor :actions
106
- end
107
-
108
- # Name of key in params that references the primary key.
109
- # Will almost always be :id (default), unless you are using a plugin or have patched rails.
110
- attr_accessor :identifier
111
-
112
- # Name of the ActiveRecord class this resource is responsible for. Automatically determined from
113
- # test class if not explicitly set. UserTest => "User"
114
- attr_accessor :klass
115
-
116
- # Name of the instantiated ActiveRecord object that should be used by some of the tests.
117
- # Defaults to the underscored name of the AR class. CompanyManager => :company_manager
118
- attr_accessor :object
119
-
120
- # Name of the parent AR objects. Can be set as parent= or parents=, and can take either
121
- # the name of the parent resource (if there's only one), or an array of names (if there's
122
- # more than one).
123
- #
124
- # Example:
125
- # # in the routes...
126
- # map.resources :companies do
127
- # map.resources :people do
128
- # map.resources :limbs
129
- # end
130
- # end
131
- #
132
- # # in the tests...
133
- # class PeopleControllerTest < Test::Unit::TestCase
134
- # should_be_restful do |resource|
135
- # resource.parent = :companies
136
- # end
137
- # end
138
- #
139
- # class LimbsControllerTest < Test::Unit::TestCase
140
- # should_be_restful do |resource|
141
- # resource.parents = [:companies, :people]
142
- # end
143
- # end
144
- attr_accessor :parent
145
- alias parents parent
146
- alias parents= parent=
147
-
148
- # Actions that should be tested. Must be a subset of VALID_ACTIONS (default).
149
- # Tests for each actionw will only be generated if the action is listed here.
150
- # The special value of :all will test all of the REST actions.
151
- #
152
- # Example (for a read-only controller):
153
- # resource.actions = [:show, :index]
154
- attr_accessor :actions
155
-
156
- # Formats that should be tested. Must be a subset of VALID_FORMATS (default).
157
- # Each action will be tested against the formats listed here. The special value
158
- # of :all will test all of the supported formats.
159
- #
160
- # Example:
161
- # resource.actions = [:html, :xml]
162
- attr_accessor :formats
163
-
164
- # ActionOptions object specifying options for the create action.
165
- attr_accessor :create
166
-
167
- # ActionOptions object specifying options for the update action.
168
- attr_accessor :update
169
-
170
- # ActionOptions object specifying options for the desrtoy action.
171
- attr_accessor :destroy
172
-
173
- # DeniedOptions object specifying which actions should return deny a request, and what should happen in that case.
174
- attr_accessor :denied
175
-
176
- def initialize # :nodoc:
177
- @create = ActionOptions.new
178
- @update = ActionOptions.new
179
- @destroy = ActionOptions.new
180
- @denied = DeniedOptions.new
181
-
182
- @create.flash ||= /created/i
183
- @update.flash ||= /updated/i
184
- @destroy.flash ||= /removed/i
185
- @denied.flash ||= /denied/i
186
-
187
- @create.params ||= {}
188
- @update.params ||= {}
189
-
190
- @actions = VALID_ACTIONS
191
- @formats = VALID_FORMATS
192
- @denied.actions = []
193
- end
194
-
195
- def normalize!(target) # :nodoc:
196
- @denied.actions = VALID_ACTIONS if @denied.actions == :all
197
- @actions = VALID_ACTIONS if @actions == :all
198
- @formats = VALID_FORMATS if @formats == :all
199
-
200
- @denied.actions = @denied.actions.map(&:to_sym)
201
- @actions = @actions.map(&:to_sym)
202
- @formats = @formats.map(&:to_sym)
203
-
204
- ensure_valid_members(@actions, VALID_ACTIONS, 'actions')
205
- ensure_valid_members(@denied.actions, VALID_ACTIONS, 'denied.actions')
206
- ensure_valid_members(@formats, VALID_FORMATS, 'formats')
207
-
208
- @identifier ||= :id
209
- @klass ||= target.name.gsub(/ControllerTest$/, '').singularize.constantize
210
- @object ||= @klass.name.tableize.singularize
211
- @parent ||= []
212
- @parent = [@parent] unless @parent.is_a? Array
213
-
214
- collection_helper = [@parent, @object.to_s.pluralize, 'url'].flatten.join('_')
215
- collection_args = @parent.map {|n| "@#{object}.#{n}"}.join(', ')
216
- @destroy.redirect ||= "#{collection_helper}(#{collection_args})"
217
-
218
- member_helper = [@parent, @object, 'url'].flatten.join('_')
219
- member_args = [@parent.map {|n| "@#{object}.#{n}"}, "@#{object}"].flatten.join(', ')
220
- @create.redirect ||= "#{member_helper}(#{member_args})"
221
- @update.redirect ||= "#{member_helper}(#{member_args})"
222
- @denied.redirect ||= "new_session_url"
223
- end
224
-
225
- private
226
-
227
- def ensure_valid_members(ary, valid_members, name) # :nodoc:
228
- invalid = ary - valid_members
229
- raise ArgumentError, "Unsupported #{name}: #{invalid.inspect}" unless invalid.empty?
230
- end
231
- end
232
- end
233
- end
234
- end
235
-
236
-
@@ -1,5 +0,0 @@
1
- class Dog < ActiveRecord::Base
2
- belongs_to :user, :foreign_key => :owner_id
3
- belongs_to :address
4
- has_and_belongs_to_many :fleas, :join_table => :fleas
5
- end