cells 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,159 @@
1
+ require File.dirname(__FILE__) + '/../../../../test/test_helper'
2
+ require File.dirname(__FILE__) + '/testing_helper'
3
+
4
+ # usually done by rails' autoloading:
5
+ require File.dirname(__FILE__) + '/cells/test_cell'
6
+
7
+ class CellsHelperTest < ActionController::TestCase
8
+ include CellsTestMethods
9
+
10
+
11
+ def test_helper
12
+ cell = HelperUsingCell.new(@controller)
13
+
14
+ content = cell.render_state(:state_with_helper_invocation)
15
+ assert_selekt content, "p#stateWithHelperInvocation", "mysterious"
16
+ end
17
+
18
+ #ActiveSupport::Dependencies.load_paths << RAILS_ROOT + "/vendor/plugins/cells/test/helpers"
19
+ # test if the HelperUsingCellHelper is automatically included:
20
+
21
+ ## FIXME: currently loading should happen in render_view_for_state, since
22
+ # there seems to be no automatic mechanism.
23
+ def dont_test_auto_helper
24
+ #ActionController::Base.helpers_dir = RAILS_ROOT + "/vendor/plugins/cells/test/helpers"
25
+
26
+ Cell::Base.helpers_dir = RAILS_ROOT + "/vendor/plugins/cells/test/helpers"
27
+ setup
28
+
29
+ cell = HelperUsingCell.new(@controller)
30
+
31
+ content = cell.render_state(:state_with_automatic_helper_invocation)
32
+ assert_selekt content, "p#stateWithAutomaticHelperInvocation", "automatic"
33
+ end
34
+
35
+ def test_helper_method
36
+ cell = HelperUsingCell.new(@controller)
37
+
38
+ content = cell.render_state(:state_with_helper_method_invocation)
39
+ assert_selekt content, "p#stateWithHelperMethodInvocation", "helped by a method"
40
+ end
41
+
42
+ def test_helper_with_subclassing
43
+ subclassedcell = HelperUsingSubCell.new(@controller)
44
+ content = subclassedcell.render_state(:state_with_helper_invocation)
45
+ assert_selekt content, "p#stateWithHelperInvocation", "mysterious"
46
+ end
47
+
48
+ def test_helper_including_and_cleanup
49
+ # this cell includes a helper, and uses it:
50
+ cell = HelperUsingCell.new(@controller)
51
+
52
+ content = cell.render_state(:state_with_helper_invocation)
53
+ assert_selekt content, "p#stateWithHelperInvocation", "mysterious"
54
+
55
+ # this cell doesn't include the helper, but uses it anyway, which should
56
+ # produce an error:
57
+
58
+ cell = TestCell.new(@controller)
59
+
60
+ # assert_raises (NameError) do
61
+ assert_raises (ActionView::TemplateError) do
62
+ cell.render_state(:state_with_not_included_helper_method)
63
+ end
64
+ end
65
+
66
+
67
+ def test_helpers_included_on_different_inheritance_levels
68
+ cell = TwoHelpersIncludingCell.new(@controller)
69
+
70
+ c = cell.render_state(:state_with_helper_invocation)
71
+ assert_selekt c, "p#stateWithHelperInvocation", "mysterious"
72
+
73
+ c = cell.render_state(:state_using_another_helper)
74
+ assert_selekt c, "p#stateUsingAnotherHelper", "senseless"
75
+ end
76
+
77
+
78
+ def test_application_helper
79
+ cell = HelperUsingCell.new(@controller)
80
+
81
+ c = cell.render_state(:state_using_application_helper)
82
+ assert_selekt c, "p#stateUsingApplicationHelper", "global"
83
+ end
84
+
85
+ def test_rails_helper_url_for
86
+ cell = HelperUsingCell.new(@controller)
87
+ cell.instance_eval do
88
+ def state_with_url_for
89
+ render :inline => "<%= url_for '/test' %>"
90
+ end
91
+ end
92
+
93
+ c = cell.render_state(:state_with_url_for)
94
+ assert_equal "/test", c
95
+ end
96
+ end
97
+
98
+
99
+ module ApplicationHelper
100
+ def application_helper_method
101
+ "global"
102
+ end
103
+ end
104
+
105
+
106
+ module CellsTestHelper
107
+ def a_mysterious_helper_method
108
+ "mysterious"
109
+ end
110
+ end
111
+
112
+
113
+ module AnotherHelper
114
+ def senseless_helper_method
115
+ "senseless"
116
+ end
117
+ end
118
+
119
+
120
+ class HelperUsingCell < Cell::Base
121
+ helper CellsTestHelper
122
+
123
+ def state_with_helper_invocation
124
+ render
125
+ end
126
+
127
+ def state_with_automatic_helper_invocation
128
+ render
129
+ end
130
+
131
+ def state_with_helper_method_invocation
132
+ render
133
+ end
134
+
135
+ def state_using_application_helper
136
+ render
137
+ end
138
+
139
+ protected
140
+
141
+ def my_helper_method
142
+ "helped by a method"
143
+ end
144
+
145
+ helper_method :my_helper_method
146
+ end
147
+
148
+ class TwoHelpersIncludingCell < HelperUsingCell
149
+
150
+ helper AnotherHelper
151
+
152
+ def state_using_another_helper
153
+ render
154
+ end
155
+ end
156
+
157
+
158
+ class HelperUsingSubCell < HelperUsingCell
159
+ end
@@ -0,0 +1,5 @@
1
+ module HelperUsingCellHelper
2
+ def an_automatically_included_helper_method
3
+ "automatic"
4
+ end
5
+ end
@@ -0,0 +1,25 @@
1
+ require File.dirname(__FILE__) + '/../../../../test/test_helper'
2
+ require File.dirname(__FILE__) + '/testing_helper'
3
+
4
+ class ACell < Cell::Base
5
+ def existing_view
6
+ @a = "a"; render
7
+ end
8
+ end
9
+
10
+ ### DISCUSS: rename file. just found out that rails automagically tries to load a file named
11
+ ### after the test, which fails with RailsExtensions.
12
+
13
+ class RenderCellTest < ActionController::TestCase
14
+ include CellsTestMethods
15
+ include Cell::ActionController
16
+
17
+ def test_render_cell
18
+ assert_equal "A/existing_view/a", render_cell(:a, :existing_view)
19
+ end
20
+
21
+ # #render_cell_to_string is just an alias of #render_cell
22
+ def test_render_cell_to_string
23
+ assert_equal render_cell_to_string(:a, :existing_view), render_cell(:a, :existing_view)
24
+ end
25
+ end
@@ -0,0 +1,229 @@
1
+ require File.dirname(__FILE__) + '/../../../../test/test_helper'
2
+ require File.dirname(__FILE__) + '/testing_helper'
3
+
4
+ class ACell < Cell::Base
5
+ def existing_view
6
+ @a = "a"; render
7
+ end
8
+
9
+ def not_existing_view
10
+ @a = "a"; render
11
+ end
12
+ end
13
+
14
+ class BCell < ACell
15
+ def existing_view
16
+ @b = "b"; render
17
+ end
18
+
19
+ def not_existing_view
20
+ @b = "b"; render
21
+ end
22
+ end
23
+
24
+ class RenderTest < ActionController::TestCase
25
+ include CellsTestMethods
26
+ include Cell::ActionView
27
+
28
+ def test_return_nil_with_locally_existing_view
29
+ assert_equal "A/existing_view/a", render_cell(:a, :existing_view)
30
+ assert_equal "B/existing_view/b", render_cell(:b, :existing_view)
31
+ end
32
+
33
+ def test_return_nil_with_inherited_view
34
+ BCell.class_eval do
35
+ def inherited_view; @a = "b"; render; end
36
+ end
37
+ assert_equal "A/inherited_view/b", render_cell(:b, :inherited_view)
38
+ end
39
+
40
+ def test_render_with_not_existing_view
41
+ assert_raises ActionView::MissingTemplate do render_cell(:a, :not_existing_view) end
42
+ assert_raises ActionView::MissingTemplate do render_cell(:b, :not_existing_view) end
43
+ end
44
+
45
+ def test_render_without_arguments_with_locally_existing_view
46
+ ACell.class_eval do
47
+ def existing_view; @a = "a"; render; end
48
+ end
49
+ BCell.class_eval do
50
+ def existing_view; @b = "b"; render; end
51
+ end
52
+ assert_equal "A/existing_view/a", render_cell(:a, :existing_view)
53
+ assert_equal "B/existing_view/b", render_cell(:b, :existing_view)
54
+ end
55
+
56
+ def test_render_passing_view_with_locally_existing_view
57
+ ACell.class_eval do
58
+ def existing_view; @a = "a"; render :view => "existing_view"; end
59
+ end
60
+ BCell.class_eval do
61
+ def existing_view; @b = "b"; render :view => "existing_view"; end
62
+ end
63
+ assert_equal "A/existing_view/a", render_cell(:a, :existing_view)
64
+ assert_equal "B/existing_view/b", render_cell(:b, :existing_view)
65
+ end
66
+
67
+ def test_render_passing_view_and_layout_with_locally_existing_view
68
+ BCell.class_eval do
69
+ def existing_view; @b = "b";
70
+ render :view => "existing_view", :layout => "metal"; end
71
+ end
72
+ assert_equal "Metal:B/existing_view/b", render_cell(:b, :existing_view)
73
+ end
74
+
75
+ def test_render_passing_view_and_template_format_with_locally_existing_view
76
+ BCell.class_eval do
77
+ def existing_view; @b = "b";
78
+ render :view => "existing_view", :template_format => :js; end
79
+ end
80
+ assert_equal "B/existing_view/b/js", render_cell(:b, :existing_view)
81
+ end
82
+
83
+ def test_render_passing_view_and_template_format_and_layout_with_locally_existing_view
84
+ BCell.class_eval do
85
+ def existing_view; @b = "b";
86
+ render :view => "existing_view", :template_format => :js, :layout => "metal"; end
87
+ end
88
+ assert_equal "Metal:B/existing_view/b/js", render_cell(:b, :existing_view)
89
+ end
90
+
91
+ # test :layout
92
+ def test_render_passing_layout_located_in_cells_layout
93
+ ACell.class_eval do
94
+ def existing_view; @a = "a";
95
+ render :layout => "metal"; end
96
+ end
97
+ assert_equal "Metal:A/existing_view/a", render_cell(:a, :existing_view)
98
+ end
99
+
100
+ ### DISCUSS: currently undocumented feature:
101
+ def test_render_passing_layout_located_in_cells_b_layouts
102
+ BCell.class_eval do
103
+ def existing_view; @b = "b";
104
+ render :layout => "b/layouts/metal"; end
105
+ end
106
+ assert_equal "B-Metal:B/existing_view/b", render_cell(:b, :existing_view)
107
+ end
108
+
109
+ # test with inherited view:
110
+ def test_render_without_arguments_with_inherited_view
111
+ BCell.class_eval do
112
+ def inherited_view; @a = "b"; render; end
113
+ end
114
+ assert_equal "A/inherited_view/b", render_cell(:b, :inherited_view)
115
+ end
116
+
117
+ def test_render_passing_view_with_inherited_view
118
+ BCell.class_eval do
119
+ def existing_view; @a = "b"; render :view => "inherited_view"; end
120
+ end
121
+ assert_equal "A/inherited_view/b", render_cell(:b, :existing_view)
122
+ end
123
+
124
+ def test_render_passing_view_and_layout_with_inherited_view
125
+ BCell.class_eval do
126
+ def inherited_view; @a = "b";
127
+ render :view => "inherited_view", :layout => "metal"; end
128
+ end
129
+ assert_equal "Metal:A/inherited_view/b", render_cell(:b, :inherited_view)
130
+ end
131
+
132
+ def test_render_passing_view_and_template_format_with_inherited_view
133
+ BCell.class_eval do
134
+ def inherited_view; @a = "b";
135
+ render :view => "inherited_view", :template_format => :js; end
136
+ end
137
+ assert_equal "A/inherited_view/b/js", render_cell(:b, :inherited_view)
138
+ end
139
+
140
+ def test_render_passing_view_and_template_format_and_layout_with_inherited_view
141
+ BCell.class_eval do
142
+ def existing_view; @a = "b";
143
+ render :view => "inherited_view", :template_format => :js, :layout => "metal"; end
144
+ end
145
+ assert_equal "Metal:A/inherited_view/b/js", render_cell(:b, :existing_view)
146
+ end
147
+
148
+
149
+ def test_render_passing_locals
150
+ ACell.class_eval do
151
+ def view_with_locals; @a = "a";
152
+ render :locals => {:name => "Nick"}; end
153
+ end
154
+ assert_equal "A/view_with_locals/a/Nick", render_cell(:a, :view_with_locals)
155
+ end
156
+
157
+
158
+ def test_recursive_render_view_with_existing_view
159
+ ACell.class_eval do
160
+ def view_with_render_call; @a = "a";
161
+ render; end
162
+ end
163
+ assert_equal "A/view_with_render_call/a:A/existing_view/a", render_cell(:a, :view_with_render_call)
164
+ end
165
+
166
+ def test_recursive_render_view_with_inherited_view
167
+ BCell.class_eval do
168
+ def view_with_render_call; @a = "b";
169
+ render; end
170
+ end
171
+ assert_equal "B/view_with_render_call/b:A/inherited_view/b", render_cell(:b, :view_with_render_call)
172
+ end
173
+
174
+ def test_render_text
175
+ ACell.class_eval do
176
+ def existing_view;
177
+ render :text => "Cells kick ass!"; end
178
+ end
179
+ assert_equal "Cells kick ass!", render_cell(:a, :existing_view)
180
+ end
181
+
182
+ def test_render_text_with_layout
183
+ ACell.class_eval do
184
+ def existing_view;
185
+ render :text => "Cells kick ass!", :layout => 'metal'; end
186
+ end
187
+ assert_equal "Metal:Cells kick ass!", render_cell(:a, :existing_view)
188
+ end
189
+
190
+ def test_render_nothing
191
+ ACell.class_eval do
192
+ def existing_view;
193
+ render :nothing => true; end
194
+ end
195
+ assert_equal "", render_cell(:a, :existing_view)
196
+ end
197
+
198
+ def test_render_inline
199
+ ACell.class_eval do
200
+ def existing_view; @a = "a";
201
+ render :inline => 'A/existing_view/a:<%= a %>', :type => :erb, :locals => {:a=>'a'}; end
202
+ end
203
+ assert_equal "A/existing_view/a:a", render_cell(:a, :existing_view)
204
+ end
205
+
206
+ def test_render_state
207
+ ACell.class_eval do
208
+ def existing_view; @a = "a";
209
+ render :state => :another_state
210
+ end
211
+ def another_state; @b = "b";
212
+ render
213
+ end
214
+ end
215
+ assert_equal "A/another_state/a,b", render_cell(:a, :existing_view)
216
+ end
217
+
218
+ def test_render_state_with_layout
219
+ ACell.class_eval do
220
+ def existing_view; @a = "a";
221
+ render :state => :another_state, :layout => 'metal'
222
+ end
223
+ def another_state; @b = "b";
224
+ render
225
+ end
226
+ end
227
+ assert_equal "Metal:A/another_state/a,b", render_cell(:a, :existing_view)
228
+ end
229
+ end
@@ -0,0 +1,67 @@
1
+ Cell::Base.add_view_path "vendor/plugins/cells/test/cells"
2
+ Cell::Base.add_view_path "vendor/plugins/cells/test/cells/layouts"
3
+
4
+ module CellsTestMethods
5
+
6
+ def assert_selekt(content, *args)
7
+ assert_select(HTML::Document.new(content).root, *args)
8
+ end
9
+
10
+ def setup
11
+ @controller = CellTestController.new
12
+ @request = ActionController::TestRequest.new
13
+ @response = ActionController::TestResponse.new
14
+ @controller.request = @request
15
+ @controller.response = @response
16
+ @controller.params = {}
17
+ end
18
+
19
+ def self.views_path
20
+ File.dirname(__FILE__) + '/views/'
21
+ end
22
+ end
23
+
24
+
25
+
26
+ class CellTestController < ApplicationController
27
+ def rescue_action(e) raise e end
28
+
29
+ def render_cell_state
30
+ cell = params[:cell]
31
+ state = params[:state]
32
+
33
+ render :text => render_cell_to_string(cell, state)
34
+ end
35
+
36
+ def call_render_cell_with_strings
37
+ static = render_cell_to_string("my_test", "direct_output")
38
+ render :text => static
39
+ end
40
+
41
+ def call_render_cell_with_syms
42
+ static = render_cell_to_string(:my_test, :direct_output)
43
+ render :text => static
44
+ end
45
+
46
+ def call_render_cell_with_state_view
47
+ render :text => render_cell_to_string(:my_test, :view_with_instance_var)
48
+ return
49
+ end
50
+
51
+ def render_view_with_render_cell_invocation
52
+ render :file => "#{RAILS_ROOT}/vendor/plugins/cells/test/views/view_with_render_cell_invocation.html.erb"
53
+ return
54
+ end
55
+
56
+ def render_just_one_view_cell
57
+ static = render_cell_to_string("just_one_view", "some_state")
58
+ render :text => static
59
+ end
60
+
61
+
62
+ def render_state_with_link_to
63
+ static = render_cell_to_string("my_test", "state_with_link_to")
64
+ render :text => static
65
+ end
66
+
67
+ end