Froze rails gems
[depot.git] / vendor / rails / actionpack / test / controller / helper_test.rb
1 require 'abstract_unit'
2
3 ActionController::Helpers::HELPERS_DIR.replace File.dirname(__FILE__) + '/../fixtures/helpers'
4
5 class TestController < ActionController::Base
6 attr_accessor :delegate_attr
7 def delegate_method() end
8 def rescue_action(e) raise end
9 end
10
11 module Fun
12 class GamesController < ActionController::Base
13 def render_hello_world
14 render :inline => "hello: <%= stratego %>"
15 end
16
17 def rescue_action(e) raise end
18 end
19
20 class PdfController < ActionController::Base
21 def test
22 render :inline => "test: <%= foobar %>"
23 end
24
25 def rescue_action(e) raise end
26 end
27 end
28
29 class ApplicationController < ActionController::Base
30 helper :all
31 end
32
33 module LocalAbcHelper
34 def a() end
35 def b() end
36 def c() end
37 end
38
39 class HelperTest < Test::Unit::TestCase
40 def setup
41 # Increment symbol counter.
42 @symbol = (@@counter ||= 'A0').succ!.dup
43
44 # Generate new controller class.
45 controller_class_name = "Helper#{@symbol}Controller"
46 eval("class #{controller_class_name} < TestController; end")
47 @controller_class = self.class.const_get(controller_class_name)
48
49 # Set default test helper.
50 self.test_helper = LocalAbcHelper
51 end
52
53 def test_deprecated_helper
54 assert_equal expected_helper_methods, missing_methods
55 assert_nothing_raised { @controller_class.helper TestHelper }
56 assert_equal [], missing_methods
57 end
58
59 def test_declare_helper
60 require 'abc_helper'
61 self.test_helper = AbcHelper
62 assert_equal expected_helper_methods, missing_methods
63 assert_nothing_raised { @controller_class.helper :abc }
64 assert_equal [], missing_methods
65 end
66
67 def test_declare_missing_helper
68 assert_equal expected_helper_methods, missing_methods
69 assert_raise(MissingSourceFile) { @controller_class.helper :missing }
70 end
71
72 def test_declare_missing_file_from_helper
73 require 'broken_helper'
74 rescue LoadError => e
75 assert_nil(/\bbroken_helper\b/.match(e.to_s)[1])
76 end
77
78 def test_helper_block
79 assert_nothing_raised {
80 @controller_class.helper { def block_helper_method; end }
81 }
82 assert master_helper_methods.include?('block_helper_method')
83 end
84
85 def test_helper_block_include
86 assert_equal expected_helper_methods, missing_methods
87 assert_nothing_raised {
88 @controller_class.helper { include HelperTest::TestHelper }
89 }
90 assert [], missing_methods
91 end
92
93 def test_helper_method
94 assert_nothing_raised { @controller_class.helper_method :delegate_method }
95 assert master_helper_methods.include?('delegate_method')
96 end
97
98 def test_helper_attr
99 assert_nothing_raised { @controller_class.helper_attr :delegate_attr }
100 assert master_helper_methods.include?('delegate_attr')
101 assert master_helper_methods.include?('delegate_attr=')
102 end
103
104 def test_helper_for_nested_controller
105 request = ActionController::TestRequest.new
106 response = ActionController::TestResponse.new
107 request.action = 'render_hello_world'
108
109 assert_equal 'hello: Iz guuut!', Fun::GamesController.process(request, response).body
110 end
111
112 def test_helper_for_acronym_controller
113 request = ActionController::TestRequest.new
114 response = ActionController::TestResponse.new
115 request.action = 'test'
116
117 assert_equal 'test: baz', Fun::PdfController.process(request, response).body
118 end
119
120 def test_all_helpers
121 methods = ApplicationController.master_helper_module.instance_methods.map(&:to_s)
122
123 # abc_helper.rb
124 assert methods.include?('bare_a')
125
126 # fun/games_helper.rb
127 assert methods.include?('stratego')
128
129 # fun/pdf_helper.rb
130 assert methods.include?('foobar')
131 end
132
133 def test_helper_proxy
134 methods = ApplicationController.helpers.methods.map(&:to_s)
135
136 # ActionView
137 assert methods.include?('pluralize')
138
139 # abc_helper.rb
140 assert methods.include?('bare_a')
141
142 # fun/games_helper.rb
143 assert methods.include?('stratego')
144
145 # fun/pdf_helper.rb
146 assert methods.include?('foobar')
147 end
148
149 private
150 def expected_helper_methods
151 TestHelper.instance_methods.map(&:to_s)
152 end
153
154 def master_helper_methods
155 @controller_class.master_helper_module.instance_methods.map(&:to_s)
156 end
157
158 def missing_methods
159 expected_helper_methods - master_helper_methods
160 end
161
162 def test_helper=(helper_module)
163 silence_warnings { self.class.const_set('TestHelper', helper_module) }
164 end
165 end
166
167
168 class IsolatedHelpersTest < Test::Unit::TestCase
169 class A < ActionController::Base
170 def index
171 render :inline => '<%= shout %>'
172 end
173
174 def rescue_action(e) raise end
175 end
176
177 class B < A
178 helper { def shout; 'B' end }
179
180 def index
181 render :inline => '<%= shout %>'
182 end
183 end
184
185 class C < A
186 helper { def shout; 'C' end }
187
188 def index
189 render :inline => '<%= shout %>'
190 end
191 end
192
193 def setup
194 @request = ActionController::TestRequest.new
195 @response = ActionController::TestResponse.new
196 @request.action = 'index'
197 end
198
199 def test_helper_in_a
200 assert_raise(NameError) { A.process(@request, @response) }
201 end
202
203 def test_helper_in_b
204 assert_equal 'B', B.process(@request, @response).body
205 end
206
207 def test_helper_in_c
208 assert_equal 'C', C.process(@request, @response).body
209 end
210 end