rails-arts 1.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2006 Kevin Clark
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1 @@
1
+ require 'rails-arts/railtie'
@@ -0,0 +1,144 @@
1
+ module RailsArts
2
+ include ActionView::Helpers::PrototypeHelper
3
+ include ActionView::Helpers::ScriptaculousHelper
4
+ include ActionView::Helpers::JavaScriptHelper
5
+
6
+ include ActionView::Helpers::UrlHelper
7
+ include ActionView::Helpers::TagHelper
8
+
9
+ def assert_rjs(action, *args, &block)
10
+ respond_to?("assert_rjs_#{action}") ?
11
+ send("assert_rjs_#{action}", *args) :
12
+ assert_response_contains(create_generator.send(action, *args, &block),
13
+ generic_error(action, args))
14
+ end
15
+
16
+ def assert_no_rjs(action, *args, &block)
17
+ assert_raises(Test::Unit::AssertionFailedError) { assert_rjs(action, *args, &block) }
18
+ end
19
+
20
+ def assert_rjs_insert_html(*args)
21
+ position = args.shift
22
+ item_id = args.shift
23
+
24
+ content = extract_matchable_content(args)
25
+
26
+ unless content.blank?
27
+ case content
28
+ when Regexp
29
+ assert_match Regexp.new("Element.insert\\(\"#{item_id}\",\\ \\{\\ #{position.to_s}.*#{content.source}.*"),
30
+ @response.body
31
+ when String
32
+ assert_response_contains("Element.insert(\"#{item_id}\", { #{position.to_s}: #{content} });",
33
+ "No insert_html call found for \n" +
34
+ " position: '#{position}' id: '#{item_id}' \ncontent: \n" +
35
+ "#{content}\n" +
36
+ "in response:\n#{@response.body}")
37
+ else
38
+ raise "Invalid content type"
39
+ end
40
+ else
41
+ assert_match Regexp.new("Element.insert\\(\"#{item_id}\",\\ \\{\\ #{position.to_s}.*\\);"),
42
+ @response.body
43
+ end
44
+ end
45
+
46
+ def assert_rjs_replace_html(*args)
47
+ div = args.shift
48
+ content = extract_matchable_content(args)
49
+
50
+ unless content.blank?
51
+ case content
52
+ when Regexp
53
+ assert_match Regexp.new("Element.update(.*#{div}.*,.*#{content.source}.*);"),
54
+ @response.body
55
+ when String
56
+ assert_response_contains("Element.update(\"#{div}\", #{content});",
57
+ "No replace_html call found on div: '#{div}' and content: \n#{content}\n" +
58
+ "in response:\n#{@response.body}")
59
+ else
60
+ raise "Invalid content type"
61
+ end
62
+ else
63
+ assert_match Regexp.new("Element.update(.*#{div}.*,.*?);"), @response.body
64
+ end
65
+ end
66
+
67
+ def assert_rjs_replace(*args)
68
+ div = args.shift
69
+ content = extract_matchable_content(args)
70
+
71
+ unless content.blank?
72
+ case content
73
+ when Regexp
74
+ assert_match Regexp.new("Element.replace(.*#{div}.*,.*#{content.source}.*);"),
75
+ @response.body
76
+ when String
77
+ assert_response_contains("Element.replace(\"#{div}\", #{content});",
78
+ "No replace call found on div: '#{div}' and content: \n#{content}\n" +
79
+ "in response:\n#{@response.body}")
80
+ else
81
+ raise "Invalid content type"
82
+ end
83
+ else
84
+ assert_match Regexp.new("Element.replace(.*#{div}.*,.*?);"), @response.body
85
+ end
86
+ end
87
+
88
+ # To deal with [] syntax. I hate JavaScriptProxy so.. SO very much
89
+ def assert_rjs_page(*args)
90
+ content = build_method_chain!(args)
91
+ assert_match Regexp.new(Regexp.escape(content)), @response.body,
92
+ "Content did not include:\n #{content.to_s}"
93
+ end
94
+
95
+ protected
96
+
97
+ def assert_response_contains(str, message)
98
+ assert @response.body.to_s.index(str), message
99
+ end
100
+
101
+ def build_method_chain!(args)
102
+ content = create_generator.send(:[], args.shift) # start $('some_id')....
103
+
104
+ while !args.empty?
105
+ if (method = args.shift.to_s) =~ /(.*)=$/
106
+ content = content.__send__(method, args.shift)
107
+ break
108
+ else
109
+ content = content.__send__(method)
110
+ content = content.__send__(:function_chain).first if args.empty?
111
+ end
112
+ end
113
+
114
+ content
115
+ end
116
+
117
+ def create_generator
118
+ block = Proc.new { |*args| yield *args if block_given? }
119
+ JavaScriptGenerator.new self, &block
120
+ end
121
+
122
+ def generic_error(action, args)
123
+ "#{action} with args [#{args.join(" ")}] does not show up in response:\n#{@response.body}"
124
+ end
125
+
126
+ def extract_matchable_content(args)
127
+ if args.size == 1 and args.first.is_a? Regexp
128
+ return args.first
129
+ else
130
+ return create_generator.send(:arguments_for_call, args)
131
+ end
132
+ end
133
+
134
+ public
135
+
136
+ # needed for JavaScriptGenerator to work - rails 2.3?
137
+ attr_accessor :template_format
138
+
139
+ # hack for rails 2.2.2
140
+ def with_output_buffer(lines=[], &block)
141
+ block.call
142
+ end
143
+
144
+ end
@@ -0,0 +1,4 @@
1
+ ActiveSupport.on_load(:after_initialize) do
2
+ require 'rails-arts/assertions'
3
+ Test::Unit::TestCase.send :include, RailsArts
4
+ end
@@ -0,0 +1,3 @@
1
+ module RailsArts
2
+ VERSION = "1.1"
3
+ end
@@ -0,0 +1,402 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
2
+
3
+ require File.dirname(__FILE__) + '/../../../../config/environment'
4
+ require 'test/unit'
5
+ require 'rubygems'
6
+ require 'breakpoint'
7
+
8
+ require 'action_controller/test_process'
9
+
10
+ ActionController::Base.logger = nil
11
+ ActionController::Base.ignore_missing_templates = false
12
+ ActionController::Routing::Routes.reload rescue nil
13
+
14
+ class ArtsController < ActionController::Base
15
+ def alert
16
+ render :update do |page|
17
+ page.alert 'This is an alert'
18
+ end
19
+ end
20
+
21
+ def assign
22
+ render :update do |page|
23
+ page.assign 'a', '2'
24
+ end
25
+ end
26
+
27
+ def call
28
+ render :update do |page|
29
+ page.call 'foo', 'bar', 'baz'
30
+ end
31
+ end
32
+
33
+ def draggable
34
+ render :update do |page|
35
+ page.draggable 'my_image', :revert => true
36
+ end
37
+ end
38
+
39
+ def drop_receiving
40
+ render :update do |page|
41
+ page.drop_receiving "my_cart", :url => { :controller => "cart", :action => "add" }
42
+ end
43
+ end
44
+
45
+ def hide
46
+ render :update do |page|
47
+ page.hide 'some_div'
48
+ end
49
+ end
50
+
51
+ def insert_html
52
+ render :update do |page|
53
+ page.insert_html :bottom, 'content', 'Stuff in the content div'
54
+ end
55
+ end
56
+
57
+ def redirect
58
+ render :update do |page|
59
+ page.redirect_to :controller => 'sample', :action => 'index'
60
+ end
61
+ end
62
+
63
+ def remove
64
+ render :update do |page|
65
+ page.remove 'offending_div'
66
+ end
67
+ end
68
+
69
+ def replace
70
+ render :update do |page|
71
+ page.replace 'person_45', '<div>This replaces person_45</div>'
72
+ end
73
+ end
74
+
75
+ def replace_html
76
+ render :update do |page|
77
+ page.replace_html 'person_45', 'This goes inside person_45'
78
+ end
79
+ end
80
+
81
+ def show
82
+ render :update do |page|
83
+ page.show 'post_1', 'post_2', 'post_3'
84
+ end
85
+ end
86
+
87
+ def sortable
88
+ render :update do |page|
89
+ page.sortable 'sortable_item'
90
+ end
91
+ end
92
+
93
+ def toggle
94
+ render :update do |page|
95
+ page.toggle "post_1", "post_2", "post_3"
96
+ end
97
+ end
98
+
99
+ def visual_effect
100
+ render :update do |page|
101
+ page.visual_effect :highlight, "posts", :duration => '1.0'
102
+ end
103
+ end
104
+
105
+ def page_with_one_chained_method
106
+ render :update do |page|
107
+ page['some_id'].toggle
108
+ end
109
+ end
110
+
111
+ def page_with_assignment
112
+ render :update do |page|
113
+ page['some_id'].style.color = 'red'
114
+ end
115
+ end
116
+
117
+ def rescue_errors(e) raise e end
118
+
119
+ end
120
+
121
+ class ArtsTest < Test::Unit::TestCase
122
+ def setup
123
+ @controller = ArtsController.new
124
+ @request = ActionController::TestRequest.new
125
+ @response = ActionController::TestResponse.new
126
+ end
127
+
128
+ def test_alert
129
+ get :alert
130
+
131
+ assert_nothing_raised { assert_rjs :alert, 'This is an alert' }
132
+ assert_raises(Test::Unit::AssertionFailedError) do
133
+ assert_rjs :alert, 'This is not an alert'
134
+ end
135
+
136
+ assert_nothing_raised { assert_no_rjs :alert, 'This is not an alert' }
137
+ assert_raises(Test::Unit::AssertionFailedError) do
138
+ assert_no_rjs :alert, 'This is an alert'
139
+ end
140
+ end
141
+
142
+ def test_assign
143
+ get :assign
144
+
145
+ assert_nothing_raised { assert_rjs :assign, 'a', '2' }
146
+ assert_raises(Test::Unit::AssertionFailedError) do
147
+ assert_rjs :assign, 'a', '3'
148
+ end
149
+
150
+ assert_nothing_raised { assert_no_rjs :assign, 'a', '3' }
151
+ assert_raises(Test::Unit::AssertionFailedError) do
152
+ assert_no_rjs :assign, 'a', '2'
153
+ end
154
+ end
155
+
156
+ def test_call
157
+ get :call
158
+
159
+ assert_nothing_raised { assert_rjs :call, 'foo', 'bar', 'baz' }
160
+ assert_raises(Test::Unit::AssertionFailedError) do
161
+ assert_rjs :call, 'foo', 'bar'
162
+ end
163
+
164
+ assert_nothing_raised { assert_no_rjs :call, 'foo', 'bar' }
165
+ assert_raises(Test::Unit::AssertionFailedError) do
166
+ assert_no_rjs :call, 'foo', 'bar', 'baz'
167
+ end
168
+ end
169
+
170
+ def test_draggable
171
+ get :draggable
172
+
173
+ assert_nothing_raised { assert_rjs :draggable, 'my_image', :revert => true }
174
+ assert_raises(Test::Unit::AssertionFailedError) do
175
+ assert_rjs :draggable, 'not_my_image'
176
+ end
177
+
178
+ assert_nothing_raised { assert_no_rjs :draggable, 'not_my_image' }
179
+ assert_raises(Test::Unit::AssertionFailedError) do
180
+ assert_no_rjs :draggable, 'my_image', :revert => true
181
+ end
182
+ end
183
+
184
+ def test_drop_receiving
185
+ get :drop_receiving
186
+
187
+ assert_nothing_raised { assert_rjs :drop_receiving, "my_cart", :url => { :controller => "cart", :action => "add" } }
188
+ assert_raises(Test::Unit::AssertionFailedError) do
189
+ assert_rjs :drop_receiving, "my_cart"
190
+ end
191
+
192
+ assert_nothing_raised { assert_no_rjs :drop_receiving, "my_cart" }
193
+ assert_raises(Test::Unit::AssertionFailedError) do
194
+ assert_no_rjs :drop_receiving, "my_cart", :url => { :controller => "cart", :action => "add" }
195
+ end
196
+ end
197
+
198
+ def test_hide
199
+ get :hide
200
+
201
+ assert_nothing_raised { assert_rjs :hide, 'some_div' }
202
+ assert_raises(Test::Unit::AssertionFailedError) do
203
+ assert_rjs :hide, 'some_other_div'
204
+ end
205
+
206
+ assert_nothing_raised { assert_no_rjs :hide, 'not_some_div' }
207
+ assert_raises(Test::Unit::AssertionFailedError) do
208
+ assert_no_rjs :hide, 'some_div'
209
+ end
210
+ end
211
+
212
+ def test_insert_html
213
+ get :insert_html
214
+
215
+
216
+ assert_nothing_raised do
217
+ # No content matching
218
+ assert_rjs :insert_html, :bottom, 'content'
219
+ # Exact content matching
220
+ assert_rjs :insert_html, :bottom, 'content', 'Stuff in the content div'
221
+ # Regex matching
222
+ assert_rjs :insert_html, :bottom, 'content', /in.*content/
223
+
224
+ assert_no_rjs :insert_html, :bottom, 'not_our_div'
225
+
226
+ assert_no_rjs :insert_html, :bottom, 'content', /in.*no content/
227
+ end
228
+
229
+ assert_raises(Test::Unit::AssertionFailedError) do
230
+ assert_no_rjs :insert_html, :bottom, 'content'
231
+ end
232
+
233
+ assert_raises(Test::Unit::AssertionFailedError) do
234
+ assert_rjs :insert_html, :bottom, 'no_content'
235
+ end
236
+
237
+ assert_raises(Test::Unit::AssertionFailedError) do
238
+ assert_no_rjs :insert_html, :bottom, 'content', /in the/
239
+ end
240
+ end
241
+
242
+ def test_redirect_to
243
+ get :redirect
244
+
245
+ assert_nothing_raised do
246
+ assert_rjs :redirect_to, :controller => 'sample', :action => 'index'
247
+ assert_no_rjs :redirect_to, :controller => 'sample', :action => 'show'
248
+ end
249
+
250
+ assert_raises(Test::Unit::AssertionFailedError) do
251
+ assert_rjs :redirect_to, :controller => 'doesnt', :action => 'exist'
252
+ end
253
+
254
+ assert_raises(Test::Unit::AssertionFailedError) do
255
+ assert_no_rjs :redirect_to, :controller => 'sample', :action => 'index'
256
+ end
257
+ end
258
+
259
+ def test_remove
260
+ get :remove
261
+
262
+ assert_nothing_raised do
263
+ assert_rjs :remove, 'offending_div'
264
+ assert_no_rjs :remove, 'dancing_happy_div'
265
+ end
266
+
267
+ assert_raises(Test::Unit::AssertionFailedError) do
268
+ assert_rjs :remove, 'dancing_happy_div'
269
+ end
270
+
271
+ assert_raises(Test::Unit::AssertionFailedError) do
272
+ assert_no_rjs :remove, 'offending_div'
273
+ end
274
+ end
275
+
276
+ def test_replace
277
+ get :replace
278
+
279
+ assert_nothing_raised do
280
+ # No content matching
281
+ assert_rjs :replace, 'person_45'
282
+ # String content matching
283
+ assert_rjs :replace, 'person_45', '<div>This replaces person_45</div>'
284
+ # regexp content matching
285
+ assert_rjs :replace, 'person_45', /<div>.*person_45.*<\/div>/
286
+
287
+ assert_no_rjs :replace, 'person_45', '<div>This replaces person_46</div>'
288
+
289
+ assert_no_rjs :replace, 'person_45', /person_46/
290
+ end
291
+
292
+ assert_raises(Test::Unit::AssertionFailedError) { assert_no_rjs :replace, 'person_45' }
293
+ assert_raises(Test::Unit::AssertionFailedError) { assert_no_rjs :replace, 'person_45', /person_45/ }
294
+ assert_raises(Test::Unit::AssertionFailedError) { assert_rjs :replace, 'person_46' }
295
+ assert_raises(Test::Unit::AssertionFailedError) { assert_rjs :replace, 'person_45', 'bad stuff' }
296
+ assert_raises(Test::Unit::AssertionFailedError) { assert_rjs :replace, 'person_45', /not there/}
297
+ end
298
+
299
+ def test_replace_html
300
+ get :replace_html
301
+
302
+ assert_nothing_raised do
303
+ # No content matching
304
+ assert_rjs :replace_html, 'person_45'
305
+ # String content matching
306
+ assert_rjs :replace_html, 'person_45', 'This goes inside person_45'
307
+ # Regexp content matching
308
+ assert_rjs :replace_html, 'person_45', /goes inside/
309
+
310
+ assert_no_rjs :replace_html, 'person_46'
311
+
312
+ assert_no_rjs :replace_html, 'person_45', /doesn't go inside/
313
+ end
314
+
315
+ assert_raises(Test::Unit::AssertionFailedError) { assert_no_rjs :replace_html, 'person_45' }
316
+ assert_raises(Test::Unit::AssertionFailedError) { assert_no_rjs :replace_html, 'person_45', /goes/ }
317
+ assert_raises(Test::Unit::AssertionFailedError) { assert_rjs :replace_html, 'person_46' }
318
+ assert_raises(Test::Unit::AssertionFailedError) { assert_rjs :replace_html, 'person_45', /gos inside/ }
319
+ end
320
+
321
+ def test_show
322
+ get :show
323
+ assert_nothing_raised do
324
+ assert_rjs :show, "post_1", "post_2", "post_3"
325
+ assert_no_rjs :show, 'post_4'
326
+ end
327
+
328
+ assert_raises(Test::Unit::AssertionFailedError) { assert_rjs :show, 'post_4' }
329
+ assert_raises(Test::Unit::AssertionFailedError) do
330
+ assert_no_rjs :show, "post_1", "post_2", "post_3"
331
+ end
332
+ end
333
+
334
+ def test_sortable
335
+ get :sortable
336
+ assert_nothing_raised do
337
+ assert_rjs :sortable, 'sortable_item'
338
+ assert_no_rjs :sortable, 'non-sortable-item'
339
+ end
340
+
341
+ assert_raises(Test::Unit::AssertionFailedError) { assert_rjs :sortable, 'non-sortable-item' }
342
+ assert_raises(Test::Unit::AssertionFailedError) { assert_no_rjs :sortable, 'sortable_item' }
343
+ end
344
+
345
+ def test_toggle
346
+ get :toggle
347
+ assert_nothing_raised do
348
+ assert_rjs :toggle, "post_1", "post_2", "post_3"
349
+ assert_no_rjs :toggle, 'post_4'
350
+ end
351
+
352
+ assert_raises(Test::Unit::AssertionFailedError) { assert_rjs :toggle, 'post_4' }
353
+ assert_raises(Test::Unit::AssertionFailedError) do
354
+ assert_no_rjs :toggle, "post_1", "post_2", "post_3"
355
+ end
356
+ end
357
+
358
+ def test_visual_effect
359
+ get :visual_effect
360
+ assert_nothing_raised do
361
+ assert_rjs :visual_effect, :highlight, "posts", :duration => '1.0'
362
+ assert_no_rjs :visual_effect, :highlight, "lists"
363
+ end
364
+
365
+ assert_raises(Test::Unit::AssertionFailedError) do
366
+ assert_rjs :visual_effect, :highlight, "lists"
367
+ end
368
+
369
+ assert_raises(Test::Unit::AssertionFailedError) do
370
+ assert_no_rjs :visual_effect, :highlight, "posts", :duration => '1.0'
371
+ end
372
+ end
373
+
374
+ # [] support
375
+
376
+ def test_page_with_one_chained_method
377
+ get :page_with_one_chained_method
378
+ assert_nothing_raised do
379
+ assert_rjs :page, 'some_id', :toggle
380
+ assert_no_rjs :page, 'some_other_id', :toggle
381
+ end
382
+
383
+ assert_raises(Test::Unit::AssertionFailedError) do
384
+ assert_rjs :page, 'some_other_id', :toggle
385
+ assert_no_rjs :page, 'some_id', :toggle
386
+ end
387
+ end
388
+
389
+ def test_page_with_assignment
390
+ get :page_with_assignment
391
+
392
+ assert_nothing_raised do
393
+ assert_rjs :page, 'some_id', :style, :color=, 'red'
394
+ assert_no_rjs :page, 'some_id', :color=, 'red'
395
+ end
396
+
397
+ assert_raises(Test::Unit::AssertionFailedError) do
398
+ assert_no_rjs :page, 'some_id', :style, :color=, 'red'
399
+ assert_rjs :page, 'some_other_id', :style, :color=, 'red'
400
+ end
401
+ end
402
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-arts
3
+ version: !ruby/object:Gem::Version
4
+ hash: 13
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 1
9
+ version: "1.1"
10
+ platform: ruby
11
+ authors:
12
+ - Kevin Clark
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2013-03-22 00:00:00 Z
18
+ dependencies:
19
+ - !ruby/object:Gem::Dependency
20
+ name: rails
21
+ prerelease: false
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ hash: 3
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ description: RJS Assertion Plugin (gemified)
34
+ email:
35
+ - kevin.clark@gmail.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files: []
41
+
42
+ files:
43
+ - lib/rails-arts/assertions.rb
44
+ - lib/rails-arts/railtie.rb
45
+ - lib/rails-arts/version.rb
46
+ - lib/rails-arts.rb
47
+ - test/arts_test.rb
48
+ - LICENSE
49
+ homepage: http://github.com/yyyc514/rails-arts
50
+ licenses: []
51
+
52
+ post_install_message:
53
+ rdoc_options: []
54
+
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 3
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 23
72
+ segments:
73
+ - 1
74
+ - 3
75
+ - 6
76
+ version: 1.3.6
77
+ requirements: []
78
+
79
+ rubyforge_project: arts
80
+ rubygems_version: 1.8.15
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: RJS Assertion Plugin
84
+ test_files: []
85
+
86
+ has_rdoc: