rmm5t-shoulda 2.0.6 → 2.9.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. data/README.rdoc +38 -10
  2. data/Rakefile +5 -3
  3. data/lib/shoulda.rb +7 -15
  4. data/lib/shoulda/action_mailer.rb +1 -1
  5. data/lib/shoulda/action_mailer/assertions.rb +32 -33
  6. data/lib/shoulda/active_record.rb +6 -2
  7. data/lib/shoulda/active_record/assertions.rb +62 -81
  8. data/lib/shoulda/active_record/helpers.rb +40 -0
  9. data/lib/shoulda/active_record/macros.rb +511 -640
  10. data/lib/shoulda/active_record/matchers.rb +42 -0
  11. data/lib/shoulda/active_record/matchers/allow_mass_assignment_of_matcher.rb +83 -0
  12. data/lib/shoulda/active_record/matchers/allow_value_matcher.rb +102 -0
  13. data/lib/shoulda/active_record/matchers/association_matcher.rb +226 -0
  14. data/lib/shoulda/active_record/matchers/ensure_inclusion_of_matcher.rb +87 -0
  15. data/lib/shoulda/active_record/matchers/ensure_length_of_matcher.rb +141 -0
  16. data/lib/shoulda/active_record/matchers/have_db_column_matcher.rb +169 -0
  17. data/lib/shoulda/active_record/matchers/have_index_matcher.rb +105 -0
  18. data/lib/shoulda/active_record/matchers/have_named_scope_matcher.rb +125 -0
  19. data/lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb +59 -0
  20. data/lib/shoulda/active_record/matchers/validate_acceptance_of_matcher.rb +41 -0
  21. data/lib/shoulda/active_record/matchers/validate_numericality_of_matcher.rb +39 -0
  22. data/lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb +60 -0
  23. data/lib/shoulda/active_record/matchers/validate_uniqueness_of_matcher.rb +148 -0
  24. data/lib/shoulda/active_record/matchers/validation_matcher.rb +56 -0
  25. data/lib/shoulda/assertions.rb +50 -40
  26. data/lib/shoulda/autoload_macros.rb +46 -0
  27. data/lib/shoulda/context.rb +124 -126
  28. data/lib/shoulda/helpers.rb +5 -7
  29. data/lib/shoulda/macros.rb +63 -64
  30. data/lib/shoulda/private_helpers.rb +16 -18
  31. data/lib/shoulda/rails.rb +5 -11
  32. data/lib/shoulda/rspec.rb +9 -0
  33. data/lib/shoulda/tasks/list_tests.rake +6 -1
  34. data/lib/shoulda/test_unit.rb +19 -0
  35. data/rails/init.rb +7 -1
  36. data/test/README +2 -2
  37. data/test/fail_macros.rb +16 -16
  38. data/test/functional/posts_controller_test.rb +33 -24
  39. data/test/functional/users_controller_test.rb +0 -19
  40. data/test/model_builder.rb +106 -0
  41. data/test/other/autoload_macro_test.rb +18 -0
  42. data/test/other/helpers_test.rb +58 -0
  43. data/test/other/private_helpers_test.rb +1 -1
  44. data/test/other/should_test.rb +16 -16
  45. data/test/rails_root/app/controllers/posts_controller.rb +6 -5
  46. data/test/rails_root/app/models/pets/dog.rb +10 -0
  47. data/test/rails_root/app/models/treat.rb +3 -0
  48. data/test/rails_root/app/models/user.rb +2 -2
  49. data/test/rails_root/app/views/layouts/posts.rhtml +2 -0
  50. data/test/rails_root/config/database.yml +1 -1
  51. data/test/rails_root/config/environments/{sqlite3.rb → test.rb} +0 -0
  52. data/test/rails_root/db/migrate/001_create_users.rb +3 -2
  53. data/test/rails_root/db/migrate/011_create_treats.rb +12 -0
  54. data/test/rails_root/test/shoulda_macros/custom_macro.rb +6 -0
  55. data/test/rails_root/vendor/gems/gem_with_macro-0.0.1/shoulda_macros/gem_macro.rb +6 -0
  56. data/test/rails_root/vendor/plugins/plugin_with_macro/shoulda_macros/plugin_macro.rb +6 -0
  57. data/test/test_helper.rb +3 -1
  58. data/test/unit/address_test.rb +1 -1
  59. data/test/unit/dog_test.rb +5 -2
  60. data/test/unit/post_test.rb +7 -3
  61. data/test/unit/product_test.rb +2 -2
  62. data/test/unit/tag_test.rb +2 -1
  63. data/test/unit/user_test.rb +17 -8
  64. metadata +54 -4
  65. data/lib/shoulda/controller.rb +0 -30
  66. data/lib/shoulda/controller/formats/html.rb +0 -201
  67. data/lib/shoulda/controller/formats/xml.rb +0 -170
  68. data/lib/shoulda/controller/helpers.rb +0 -64
  69. data/lib/shoulda/controller/macros.rb +0 -316
  70. data/lib/shoulda/controller/resource_options.rb +0 -236
  71. 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