Upgraded Rails
[monkeycharger.git] / vendor / rails / actionpack / test / controller / helper_test.rb
blob54e2582ca2b0cf5946ce8a3520541471a7b9b6b3
1 require File.dirname(__FILE__) + '/../abstract_unit'
3 silence_warnings { ActionController::Helpers::HELPERS_DIR = File.dirname(__FILE__) + '/../fixtures/helpers' }
5 class TestController < ActionController::Base
6   attr_accessor :delegate_attr
7   def delegate_method() end
8   def rescue_action(e) raise end
9 end
11 module Fun
12   class GamesController < ActionController::Base
13     def render_hello_world
14       render :inline => "hello: <%= stratego %>"
15     end
17     def rescue_action(e) raise end
18   end
20   class PdfController < ActionController::Base
21     def test
22       render :inline => "test: <%= foobar %>"
23     end
25     def rescue_action(e) raise end
26   end
27 end
29 class ApplicationController < ActionController::Base
30   helper :all
31 end
33 module LocalAbcHelper
34   def a() end
35   def b() end
36   def c() end
37 end
39 class HelperTest < Test::Unit::TestCase
40   def setup
41     # Increment symbol counter.
42     @symbol = (@@counter ||= 'A0').succ!.dup
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)
49     # Generate new template class and assign to controller.
50     template_class_name = "Test#{@symbol}View"
51     eval("class #{template_class_name} < ActionView::Base; end")
52     @template_class = self.class.const_get(template_class_name)
53     @controller_class.template_class = @template_class
55     # Set default test helper.
56     self.test_helper = LocalAbcHelper
57   end
59   def teardown
60     # Reset template class.
61     #ActionController::Base.template_class = ActionView::Base
62   end
65   def test_deprecated_helper
66     assert_equal expected_helper_methods, missing_methods
67     assert_nothing_raised { @controller_class.helper TestHelper }
68     assert_equal [], missing_methods
69   end
71   def test_declare_helper
72     require 'abc_helper'
73     self.test_helper = AbcHelper
74     assert_equal expected_helper_methods, missing_methods
75     assert_nothing_raised { @controller_class.helper :abc }
76     assert_equal [], missing_methods
77   end
79   def test_declare_missing_helper
80     assert_equal expected_helper_methods, missing_methods
81     assert_raise(MissingSourceFile) { @controller_class.helper :missing }
82   end
84   def test_declare_missing_file_from_helper
85     require 'broken_helper'
86   rescue LoadError => e
87     assert_nil(/\bbroken_helper\b/.match(e.to_s)[1])
88   end
90   def test_helper_block
91     assert_nothing_raised {
92       @controller_class.helper { def block_helper_method; end }
93     }
94     assert master_helper_methods.include?('block_helper_method')
95   end
97   def test_helper_block_include
98     assert_equal expected_helper_methods, missing_methods
99     assert_nothing_raised {
100       @controller_class.helper { include TestHelper }
101     }
102     assert [], missing_methods
103   end
105   def test_helper_method
106     assert_nothing_raised { @controller_class.helper_method :delegate_method }
107     assert master_helper_methods.include?('delegate_method')
108   end
110   def test_helper_attr
111     assert_nothing_raised { @controller_class.helper_attr :delegate_attr }
112     assert master_helper_methods.include?('delegate_attr')
113     assert master_helper_methods.include?('delegate_attr=')
114   end
116   def test_helper_for_nested_controller
117     request  = ActionController::TestRequest.new
118     response = ActionController::TestResponse.new
119     request.action = 'render_hello_world'
121     assert_equal 'hello: Iz guuut!', Fun::GamesController.process(request, response).body
122   end
124   def test_helper_for_acronym_controller
125     request  = ActionController::TestRequest.new
126     response = ActionController::TestResponse.new
127     request.action = 'test'
129     assert_equal 'test: baz', Fun::PdfController.process(request, response).body
130   end
132   def test_all_helpers
133     # abc_helper.rb
134     assert ApplicationController.master_helper_module.instance_methods.include?("bare_a")
136     # fun/games_helper.rb
137     assert ApplicationController.master_helper_module.instance_methods.include?("stratego")
139     # fun/pdf_helper.rb
140     assert ApplicationController.master_helper_module.instance_methods.include?("foobar")
141   end
143   private
144     def expected_helper_methods
145       TestHelper.instance_methods
146     end
148     def master_helper_methods
149       @controller_class.master_helper_module.instance_methods
150     end
152     def missing_methods
153       expected_helper_methods - master_helper_methods
154     end
156     def test_helper=(helper_module)
157       silence_warnings { self.class.const_set('TestHelper', helper_module) }
158     end
162 class IsolatedHelpersTest < Test::Unit::TestCase
163   class A < ActionController::Base
164     def index
165       render :inline => '<%= shout %>'
166     end
168     def rescue_action(e) raise end
169   end
171   class B < A
172     helper { def shout; 'B' end }
174     def index
175       render :inline => '<%= shout %>'
176     end
177   end
179   class C < A
180     helper { def shout; 'C' end }
182     def index
183       render :inline => '<%= shout %>'
184     end
185   end
187   def setup
188     @request    = ActionController::TestRequest.new
189     @response   = ActionController::TestResponse.new
190     @request.action = 'index'
191   end
193   def test_helper_in_a
194     assert_raise(NameError) { A.process(@request, @response) }
195   end
197   def test_helper_in_b
198     assert_equal 'B', B.process(@request, @response).body
199   end
201   def test_helper_in_c
202     assert_equal 'C', C.process(@request, @response).body
203   end