jakewendt-simply_testable 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Jake
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,42 @@
1
+ = Simply Testable (RubyGem)
2
+
3
+ Contains a collection of complex assertions.
4
+
5
+ == Usage
6
+
7
+ config.gem 'jakewendt-simply_testable',
8
+ :source => 'http://rubygems.org'
9
+
10
+ script/generate simply_testable
11
+
12
+ == ToDo
13
+
14
+ * add assert_should_protect_attributes(*attributes)
15
+ * add assert_should_allow_attributes(*attributes)
16
+ * include assert_only_difference
17
+ * add legitimate text to Rakefile
18
+ * options to polymorphic associations
19
+
20
+ == Gemified with Jeweler
21
+
22
+ vi Rakefile
23
+ rake version:write
24
+
25
+ rake version:bump:patch
26
+ rake version:bump:minor
27
+ rake version:bump:major
28
+
29
+ rake gemspec
30
+
31
+ rake install
32
+ rake release
33
+
34
+
35
+ == Thanks
36
+
37
+ * http://github.com/rails/rails
38
+ * http://github.com/technicalpickles/jeweler
39
+
40
+ == Copyright
41
+
42
+ Copyright (c) 2010 Jake. See LICENSE for details.
File without changes
@@ -0,0 +1,72 @@
1
+ class SimplyTestableGenerator < Rails::Generator::Base
2
+
3
+ def manifest
4
+ # See Rails::Generator::Commands::Create
5
+ # rails-2.3.10/lib/rails_generator/commands.rb
6
+ # for code methods for record (Manifest)
7
+ record do |m|
8
+ File.open('.autotest','a'){|f|
9
+ f.puts <<-EOF
10
+ # From `script/generate simply_testable` ...
11
+ Dir["\#{File.dirname(__FILE__)}/config/autotest/**/*rb"].sort.each { |ext| load ext }
12
+ EOF
13
+ }
14
+
15
+ # %w( create_pages ).each do |migration|
16
+ # m.migration_template "migrations/#{migration}.rb",
17
+ # 'db/migrate', :migration_file_name => migration
18
+ # end
19
+ #
20
+ # m.directory('public/javascripts')
21
+ # Dir["#{File.dirname(__FILE__)}/templates/javascripts/*js"].each{|file|
22
+ # f = file.split('/').slice(-2,2).join('/')
23
+ # m.file(f, "public/javascripts/#{File.basename(file)}")
24
+ # }
25
+ # m.directory('public/stylesheets')
26
+ # Dir["#{File.dirname(__FILE__)}/templates/stylesheets/*css"].each{|file|
27
+ # f = file.split('/').slice(-2,2).join('/')
28
+ # m.file(f, "public/stylesheets/#{File.basename(file)}")
29
+ # }
30
+ # m.directory('test/functional/pages')
31
+ # Dir["#{File.dirname(__FILE__)}/templates/functional/*rb"].each{|file|
32
+ # f = file.split('/').slice(-2,2).join('/')
33
+ # m.file(f, "test/functional/pages/#{File.basename(file)}")
34
+ # }
35
+ # m.directory('test/unit/pages')
36
+ # Dir["#{File.dirname(__FILE__)}/templates/unit/*rb"].each{|file|
37
+ # f = file.split('/').slice(-2,2).join('/')
38
+ # m.file(f, "test/unit/pages/#{File.basename(file)}")
39
+ # }
40
+ end
41
+ end
42
+
43
+ end
44
+ module Rails::Generator::Commands
45
+ class Create
46
+ def migration_template(relative_source,
47
+ relative_destination, template_options = {})
48
+ migration_directory relative_destination
49
+ migration_file_name = template_options[
50
+ :migration_file_name] || file_name
51
+ if migration_exists?(migration_file_name)
52
+ puts "Another migration is already named #{migration_file_name}: #{existing_migrations(migration_file_name).first}: Skipping"
53
+ else
54
+ template(relative_source, "#{relative_destination}/#{next_migration_string}_#{migration_file_name}.rb", template_options)
55
+ end
56
+ end
57
+ end # Create
58
+ class Base
59
+ protected
60
+ # the loop through migrations happens so fast
61
+ # that they all have the same timestamp which
62
+ # won't work when you actually try to migrate.
63
+ # All the timestamps MUST be unique.
64
+ def next_migration_string(padding = 3)
65
+ @s = (!@s.nil?)? @s.to_i + 1 : if ActiveRecord::Base.timestamped_migrations
66
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
67
+ else
68
+ "%.#{padding}d" % next_migration_number
69
+ end
70
+ end
71
+ end # Base
72
+ end
@@ -0,0 +1 @@
1
+ require 'simply_testable'
@@ -0,0 +1,33 @@
1
+ def brand
2
+ "@@ "
3
+ end
4
+ module SimplyTestable
5
+ module ActionControllerExtension
6
+ # predefine namespaces
7
+ end
8
+ end
9
+ #
10
+ # This may require the addition of other gem requirements
11
+ #
12
+ require 'active_support/test_case'
13
+ require 'simply_testable/test_case'
14
+ require 'simply_testable/declarative'
15
+ require 'simply_testable/assertions'
16
+ require 'simply_testable/acts_as_list'
17
+ require 'simply_testable/associations'
18
+ require 'simply_testable/attributes'
19
+ require 'simply_testable/action_controller_extension'
20
+ require 'simply_testable/errors'
21
+ require 'simply_testable/pending'
22
+
23
+ module ActiveSupport
24
+ module Testing
25
+ module AtExit
26
+ at_exit {
27
+ puts Dir.pwd()
28
+ puts Time.now
29
+ }
30
+ end
31
+ end
32
+ end
33
+ ActiveSupport::TestCase.send(:include, ActiveSupport::Testing::AtExit)
@@ -0,0 +1,6 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ require 'action_controller_extension/test_case'
3
+ require 'action_controller_extension/accessible_via_protocol'
4
+ require 'action_controller_extension/accessible_via_format'
5
+ require 'action_controller_extension/accessible_via_user'
6
+ require 'action_controller_extension/routing'
@@ -0,0 +1,4 @@
1
+ module SimplyTestable::ActionControllerExtension
2
+ module AccessibleViaFormat
3
+ end
4
+ end
@@ -0,0 +1,403 @@
1
+ module SimplyTestable::ActionControllerExtension
2
+ module AccessibleViaProtocol
3
+
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+
10
+ def awihttp_title(options={})
11
+ "with #{options[:login]} login#{options[:suffix]}"
12
+ end
13
+
14
+ def assert_access_with_http(*actions)
15
+ user_options = actions.extract_options!
16
+
17
+ options = {
18
+ :login => :admin
19
+ }
20
+ if ( self.constants.include?('ASSERT_ACCESS_OPTIONS') )
21
+ options.merge!(self::ASSERT_ACCESS_OPTIONS)
22
+ end
23
+ options.merge!(user_options)
24
+ actions += options[:actions]||[]
25
+
26
+ m_key = options[:model].try(:underscore).try(:to_sym)
27
+
28
+ test "#{brand}AWiHTTP should get new #{awihttp_title(options)}" do
29
+ turn_https_off
30
+ login_as send(options[:login])
31
+ args = options[:new] || {}
32
+ send(:get,:new,args)
33
+ assert_response :success
34
+ assert_template 'new'
35
+ assert assigns(m_key)
36
+ assert_nil flash[:error]
37
+ end if actions.include?(:new) || options.keys.include?(:new)
38
+
39
+ test "#{brand}AWiHTTP should post create #{awihttp_title(options)}" do
40
+ turn_https_off
41
+ login_as send(options[:login])
42
+ args = if options[:create]
43
+ options[:create]
44
+ elsif options[:attributes_for_create]
45
+ {m_key => send(options[:attributes_for_create])}
46
+ else
47
+ {}
48
+ end
49
+ assert_difference("#{options[:model]}.count",1) do
50
+ send(:post,:create,args)
51
+ end
52
+ assert_response :redirect
53
+ assert_nil flash[:error]
54
+ end if actions.include?(:create) || options.keys.include?(:create)
55
+
56
+ test "#{brand}AWiHTTP should get edit #{awihttp_title(options)}" do
57
+ turn_https_off
58
+ login_as send(options[:login])
59
+ args={}
60
+ if options[:method_for_create]
61
+ obj = send(options[:method_for_create])
62
+ args[:id] = obj.id
63
+ end
64
+ send(:get,:edit, args)
65
+ assert_response :success
66
+ assert_template 'edit'
67
+ assert assigns(m_key)
68
+ assert_nil flash[:error]
69
+ end if actions.include?(:edit) || options.keys.include?(:edit)
70
+
71
+ test "#{brand}AWiHTTP should put update #{awihttp_title(options)}" do
72
+ turn_https_off
73
+ login_as send(options[:login])
74
+ args={}
75
+ if options[:method_for_create] && options[:attributes_for_create]
76
+ obj = send(options[:method_for_create])
77
+ args[:id] = obj.id
78
+ args[m_key] = send(options[:attributes_for_create])
79
+ end
80
+ before = obj.updated_at if obj
81
+ sleep 1 if obj # if updated too quickly, updated_at won't change
82
+ send(:put,:update, args)
83
+ after = obj.reload.updated_at if obj
84
+ assert_not_equal before.to_i,after.to_i if obj
85
+ assert_response :redirect
86
+ assert_nil flash[:error]
87
+ end if actions.include?(:update) || options.keys.include?(:update)
88
+
89
+ test "#{brand}AWiHTTP should get show #{awihttp_title(options)}" do
90
+ turn_https_off
91
+ login_as send(options[:login])
92
+ args={}
93
+ if options[:method_for_create]
94
+ obj = send(options[:method_for_create])
95
+ args[:id] = obj.id
96
+ end
97
+ send(:get,:show, args)
98
+ assert_response :success
99
+ assert_template 'show'
100
+ assert assigns(m_key)
101
+ assert_nil flash[:error]
102
+ end if actions.include?(:show) || options.keys.include?(:show)
103
+
104
+ test "#{brand}AWiHTTP should delete destroy #{awihttp_title(options)}" do
105
+ turn_https_off
106
+ login_as send(options[:login])
107
+ args={}
108
+ if options[:method_for_create]
109
+ obj = send(options[:method_for_create])
110
+ args[:id] = obj.id
111
+ end
112
+ assert_difference("#{options[:model]}.count",-1) do
113
+ send(:delete,:destroy,args)
114
+ end
115
+ assert_response :redirect
116
+ assert assigns(m_key)
117
+ assert_nil flash[:error]
118
+ end if actions.include?(:destroy) || options.keys.include?(:destroy)
119
+
120
+ test "#{brand}AWiHTTP should get index #{awihttp_title(options)}" do
121
+ turn_https_off
122
+ login_as send(options[:login])
123
+ get :index
124
+ assert_response :success
125
+ assert_template 'index'
126
+ assert assigns(m_key.try(:to_s).try(:pluralize).try(:to_sym))
127
+ assert_nil flash[:error]
128
+ end if actions.include?(:index) || options.keys.include?(:index)
129
+
130
+ test "#{brand}AWiHTTP should get index #{awihttp_title(options)} and items" do
131
+ turn_https_off
132
+ send(options[:before]) if !options[:before].blank?
133
+ login_as send(options[:login])
134
+ 3.times{ send(options[:method_for_create]) } if !options[:method_for_create].blank?
135
+ get :index
136
+ assert_response :success
137
+ assert_template 'index'
138
+ assert assigns(m_key.try(:to_s).try(:pluralize).try(:to_sym))
139
+ assert_nil flash[:error]
140
+ end if actions.include?(:index) || options.keys.include?(:index)
141
+
142
+ end
143
+
144
+ def awihttps_title(options={})
145
+ "with #{options[:login]} login#{options[:suffix]}"
146
+ end
147
+
148
+ def assert_access_with_https(*actions)
149
+ user_options = actions.extract_options!
150
+
151
+ options = {
152
+ :login => :admin
153
+ }
154
+ if ( self.constants.include?('ASSERT_ACCESS_OPTIONS') )
155
+ options.merge!(self::ASSERT_ACCESS_OPTIONS)
156
+ end
157
+ options.merge!(user_options)
158
+ actions += options[:actions]||[]
159
+
160
+ m_key = options[:model].try(:underscore).try(:to_sym)
161
+
162
+ test "#{brand}AWiHTTPS should get new #{awihttps_title(options)}" do
163
+ login_as send(options[:login])
164
+ args = options[:new] || {}
165
+ turn_https_on
166
+ send(:get,:new,args)
167
+ assert_response :success
168
+ assert_template 'new'
169
+ assert assigns(m_key)
170
+ assert_nil flash[:error]
171
+ end if actions.include?(:new) || options.keys.include?(:new)
172
+
173
+ test "#{brand}AWiHTTPS should post create #{awihttps_title(options)}" do
174
+ login_as send(options[:login])
175
+ args = if options[:create]
176
+ options[:create]
177
+ elsif options[:attributes_for_create]
178
+ {m_key => send(options[:attributes_for_create])}
179
+ else
180
+ {}
181
+ end
182
+ turn_https_on
183
+ assert_difference("#{options[:model]}.count",1) do
184
+ send(:post,:create,args)
185
+ end
186
+ assert_response :redirect
187
+ assert_nil flash[:error]
188
+ end if actions.include?(:create) || options.keys.include?(:create)
189
+
190
+ test "#{brand}AWiHTTPS should get edit #{awihttps_title(options)}" do
191
+ login_as send(options[:login])
192
+ args={}
193
+ if options[:method_for_create]
194
+ obj = send(options[:method_for_create])
195
+ args[:id] = obj.id
196
+ end
197
+ turn_https_on
198
+ send(:get,:edit, args)
199
+ assert_response :success
200
+ assert_template 'edit'
201
+ assert assigns(m_key)
202
+ assert_nil flash[:error]
203
+ end if actions.include?(:edit) || options.keys.include?(:edit)
204
+
205
+ test "#{brand}AWiHTTPS should put update #{awihttps_title(options)}" do
206
+ login_as send(options[:login])
207
+ args={}
208
+ if options[:method_for_create] && options[:attributes_for_create]
209
+ obj = send(options[:method_for_create])
210
+ args[:id] = obj.id
211
+ args[m_key] = send(options[:attributes_for_create])
212
+ end
213
+ before = obj.updated_at if obj
214
+ sleep 1 if obj # if updated too quickly, updated_at won't change
215
+ turn_https_on
216
+ send(:put,:update, args)
217
+ after = obj.reload.updated_at if obj
218
+ assert_not_equal before.to_i,after.to_i if obj
219
+ assert_response :redirect
220
+ assert_nil flash[:error]
221
+ end if actions.include?(:update) || options.keys.include?(:update)
222
+
223
+ test "#{brand}AWiHTTPS should get show #{awihttps_title(options)}" do
224
+ login_as send(options[:login])
225
+ args={}
226
+ if options[:method_for_create]
227
+ obj = send(options[:method_for_create])
228
+ args[:id] = obj.id
229
+ end
230
+ turn_https_on
231
+ send(:get,:show, args)
232
+ assert_response :success
233
+ assert_template 'show'
234
+ assert assigns(m_key)
235
+ assert_nil flash[:error]
236
+ end if actions.include?(:show) || options.keys.include?(:show)
237
+
238
+ test "#{brand}AWiHTTPS should delete destroy #{awihttps_title(options)}" do
239
+ login_as send(options[:login])
240
+ args={}
241
+ if options[:method_for_create]
242
+ obj = send(options[:method_for_create])
243
+ args[:id] = obj.id
244
+ end
245
+ turn_https_on
246
+ assert_difference("#{options[:model]}.count",-1) do
247
+ send(:delete,:destroy,args)
248
+ end
249
+ assert_response :redirect
250
+ assert assigns(m_key)
251
+ assert_nil flash[:error]
252
+ end if actions.include?(:destroy) || options.keys.include?(:destroy)
253
+
254
+ test "#{brand}AWiHTTPS should get index #{awihttps_title(options)}" do
255
+ login_as send(options[:login])
256
+ turn_https_on
257
+ get :index
258
+ assert_response :success
259
+ assert_template 'index'
260
+ assert assigns(m_key.try(:to_s).try(:pluralize).try(:to_sym))
261
+ assert_nil flash[:error]
262
+ end if actions.include?(:index) || options.keys.include?(:index)
263
+
264
+ test "#{brand}AWiHTTPS should get index #{awihttps_title(options)} and items" do
265
+ send(options[:before]) if !options[:before].blank?
266
+ login_as send(options[:login])
267
+ 3.times{ send(options[:method_for_create]) } if !options[:method_for_create].blank?
268
+ turn_https_on
269
+ get :index
270
+ assert_response :success
271
+ assert_template 'index'
272
+ assert assigns(m_key.try(:to_s).try(:pluralize).try(:to_sym))
273
+ assert_nil flash[:error]
274
+ end if actions.include?(:index) || options.keys.include?(:index)
275
+
276
+ end
277
+
278
+ def nawihttp_title(options={})
279
+ "with #{options[:login]} login#{options[:suffix]}"
280
+ end
281
+
282
+ def assert_no_access_with_http(*actions)
283
+ user_options = actions.extract_options!
284
+
285
+ options = {
286
+ :login => :admin
287
+ }
288
+ if ( self.constants.include?('ASSERT_ACCESS_OPTIONS') )
289
+ options.merge!(self::ASSERT_ACCESS_OPTIONS)
290
+ end
291
+ options.merge!(user_options)
292
+ actions += options[:actions]||[]
293
+
294
+ m_key = options[:model].try(:underscore).try(:to_sym)
295
+
296
+ test "#{brand}NAWiHTTP should NOT get new #{nawihttp_title(options)}" do
297
+ turn_https_off
298
+ login_as send(options[:login])
299
+ args = options[:new]||{}
300
+ send(:get,:new,args)
301
+ assert_redirected_to @controller.url_for(
302
+ :controller => @controller.controller_name,
303
+ :action => 'new', :protocol => "https://")
304
+ end if actions.include?(:new) || options.keys.include?(:new)
305
+
306
+ test "#{brand}NAWiHTTP should NOT post create #{nawihttp_title(options)}" do
307
+ turn_https_off
308
+ login_as send(options[:login])
309
+ args = if options[:create]
310
+ options[:create]
311
+ elsif options[:attributes_for_create]
312
+ {m_key => send(options[:attributes_for_create])}
313
+ else
314
+ {}
315
+ end
316
+ assert_no_difference("#{options[:model]}.count") do
317
+ send(:post,:create,args)
318
+ end
319
+ assert_match @controller.url_for(
320
+ :controller => @controller.controller_name,
321
+ :action => 'create', :protocol => "https://"),@response.redirected_to
322
+ end if actions.include?(:create) || options.keys.include?(:create)
323
+
324
+ test "#{brand}NAWiHTTP should NOT get edit #{nawihttp_title(options)}" do
325
+ turn_https_off
326
+ login_as send(options[:login])
327
+ args=options[:edit]||{}
328
+ if options[:method_for_create]
329
+ obj = send(options[:method_for_create])
330
+ args[:id] = obj.id
331
+ end
332
+ send(:get,:edit, args)
333
+ assert_redirected_to @controller.url_for(
334
+ :controller => @controller.controller_name,
335
+ :action => 'edit', :id => args[:id], :protocol => "https://")
336
+ end if actions.include?(:edit) || options.keys.include?(:edit)
337
+
338
+ test "#{brand}NAWiHTTP should NOT put update #{nawihttp_title(options)}" do
339
+ turn_https_off
340
+ login_as send(options[:login])
341
+ args={}
342
+ if options[:method_for_create] && options[:attributes_for_create]
343
+ obj = send(options[:method_for_create])
344
+ args[:id] = obj.id
345
+ args[m_key] = send(options[:attributes_for_create])
346
+ end
347
+ before = obj.updated_at if obj
348
+ send(:put,:update, args)
349
+ after = obj.reload.updated_at if obj
350
+ assert_equal before.to_s(:db), after.to_s(:db) if obj
351
+ assert_match @controller.url_for(
352
+ :controller => @controller.controller_name,
353
+ :action => 'update', :id => args[:id], :protocol => "https://"), @response.redirected_to
354
+ end if actions.include?(:update) || options.keys.include?(:update)
355
+
356
+ test "#{brand}NAWiHTTP should NOT get show #{nawihttp_title(options)}" do
357
+ turn_https_off
358
+ login_as send(options[:login])
359
+ args=options[:show]||{}
360
+ if options[:method_for_create]
361
+ obj = send(options[:method_for_create])
362
+ args[:id] = obj.id
363
+ end
364
+ send(:get,:show, args)
365
+ assert_redirected_to @controller.url_for(
366
+ :controller => @controller.controller_name,
367
+ :action => 'show', :id => args[:id], :protocol => "https://")
368
+ end if actions.include?(:show) || options.keys.include?(:show)
369
+
370
+ test "#{brand}NAWiHTTP should NOT delete destroy #{nawihttp_title(options)}" do
371
+ turn_https_off
372
+ login_as send(options[:login])
373
+ args=options[:destroy]||{}
374
+ if options[:method_for_create]
375
+ obj = send(options[:method_for_create])
376
+ args[:id] = obj.id
377
+ end
378
+ assert_no_difference("#{options[:model]}.count") do
379
+ send(:delete,:destroy,args)
380
+ end
381
+ assert_redirected_to @controller.url_for(
382
+ :controller => @controller.controller_name,
383
+ :action => 'destroy', :id => args[:id], :protocol => "https://")
384
+ end if actions.include?(:destroy) || options.keys.include?(:destroy)
385
+
386
+ test "#{brand}NAWiHTTP should NOT get index #{nawihttp_title(options)}" do
387
+ turn_https_off
388
+ login_as send(options[:login])
389
+ get :index
390
+ assert_redirected_to @controller.url_for(
391
+ :controller => @controller.controller_name,
392
+ :action => 'index', :protocol => "https://")
393
+ end if actions.include?(:index) || options.keys.include?(:index)
394
+
395
+ end
396
+
397
+ end # module ClassMethods
398
+ end # module AccessibleViaProtocol
399
+ end # module SimplyTestable::ActionControllerExtension
400
+ require 'action_controller'
401
+ require 'action_controller/test_case'
402
+ ActionController::TestCase.send(:include,
403
+ SimplyTestable::ActionControllerExtension::AccessibleViaProtocol)