2008-01-05 08:32:06 -05:00
|
|
|
require 'abstract_unit'
|
2009-05-13 04:10:37 -04:00
|
|
|
require 'active_support/core_ext/kernel/reporting'
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2008-11-20 17:05:20 -05:00
|
|
|
ActionController::Base.helpers_dir = File.dirname(__FILE__) + '/../fixtures/helpers'
|
2007-02-24 15:31:34 -05:00
|
|
|
|
2005-02-14 20:45:35 -05:00
|
|
|
class TestController < ActionController::Base
|
|
|
|
attr_accessor :delegate_attr
|
|
|
|
def delegate_method() end
|
|
|
|
def rescue_action(e) raise end
|
|
|
|
end
|
|
|
|
|
|
|
|
module Fun
|
|
|
|
class GamesController < ActionController::Base
|
|
|
|
def render_hello_world
|
2005-06-15 13:17:58 -04:00
|
|
|
render :inline => "hello: <%= stratego %>"
|
2005-02-14 20:45:35 -05:00
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
def rescue_action(e) raise end
|
|
|
|
end
|
2005-11-08 03:23:13 -05:00
|
|
|
|
2007-02-24 15:31:34 -05:00
|
|
|
class PdfController < ActionController::Base
|
2005-11-08 03:23:13 -05:00
|
|
|
def test
|
|
|
|
render :inline => "test: <%= foobar %>"
|
|
|
|
end
|
|
|
|
|
|
|
|
def rescue_action(e) raise end
|
|
|
|
end
|
2005-02-14 20:45:35 -05:00
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2009-05-22 18:17:05 -04:00
|
|
|
class AllHelpersController < ActionController::Base
|
2007-02-24 15:31:34 -05:00
|
|
|
helper :all
|
|
|
|
end
|
|
|
|
|
2005-02-14 20:45:35 -05:00
|
|
|
module LocalAbcHelper
|
|
|
|
def a() end
|
|
|
|
def b() end
|
|
|
|
def c() end
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2005-02-14 20:45:35 -05:00
|
|
|
class HelperTest < Test::Unit::TestCase
|
2004-11-23 20:04:44 -05:00
|
|
|
def setup
|
|
|
|
# Increment symbol counter.
|
|
|
|
@symbol = (@@counter ||= 'A0').succ!.dup
|
|
|
|
|
|
|
|
# Generate new controller class.
|
|
|
|
controller_class_name = "Helper#{@symbol}Controller"
|
|
|
|
eval("class #{controller_class_name} < TestController; end")
|
|
|
|
@controller_class = self.class.const_get(controller_class_name)
|
|
|
|
|
|
|
|
# Set default test helper.
|
|
|
|
self.test_helper = LocalAbcHelper
|
|
|
|
end
|
2008-03-05 07:12:17 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_deprecated_helper
|
2005-06-15 13:17:58 -04:00
|
|
|
assert_equal expected_helper_methods, missing_methods
|
2004-11-23 20:04:44 -05:00
|
|
|
assert_nothing_raised { @controller_class.helper TestHelper }
|
|
|
|
assert_equal [], missing_methods
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_declare_helper
|
|
|
|
require 'abc_helper'
|
|
|
|
self.test_helper = AbcHelper
|
2005-06-15 13:17:58 -04:00
|
|
|
assert_equal expected_helper_methods, missing_methods
|
2004-11-23 20:04:44 -05:00
|
|
|
assert_nothing_raised { @controller_class.helper :abc }
|
|
|
|
assert_equal [], missing_methods
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_declare_missing_helper
|
2005-06-15 13:17:58 -04:00
|
|
|
assert_equal expected_helper_methods, missing_methods
|
2005-03-26 08:31:01 -05:00
|
|
|
assert_raise(MissingSourceFile) { @controller_class.helper :missing }
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2004-12-22 08:44:16 -05:00
|
|
|
def test_declare_missing_file_from_helper
|
|
|
|
require 'broken_helper'
|
2006-07-08 14:14:49 -04:00
|
|
|
rescue LoadError => e
|
|
|
|
assert_nil(/\bbroken_helper\b/.match(e.to_s)[1])
|
2004-12-22 08:44:16 -05:00
|
|
|
end
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_helper_block
|
|
|
|
assert_nothing_raised {
|
|
|
|
@controller_class.helper { def block_helper_method; end }
|
|
|
|
}
|
2005-06-15 13:17:58 -04:00
|
|
|
assert master_helper_methods.include?('block_helper_method')
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_helper_block_include
|
2005-06-15 13:17:58 -04:00
|
|
|
assert_equal expected_helper_methods, missing_methods
|
2004-11-23 20:04:44 -05:00
|
|
|
assert_nothing_raised {
|
2008-05-19 15:57:08 -04:00
|
|
|
@controller_class.helper { include HelperTest::TestHelper }
|
2004-11-23 20:04:44 -05:00
|
|
|
}
|
|
|
|
assert [], missing_methods
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_helper_method
|
|
|
|
assert_nothing_raised { @controller_class.helper_method :delegate_method }
|
2005-06-15 13:17:58 -04:00
|
|
|
assert master_helper_methods.include?('delegate_method')
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_helper_attr
|
|
|
|
assert_nothing_raised { @controller_class.helper_attr :delegate_attr }
|
2005-06-15 13:17:58 -04:00
|
|
|
assert master_helper_methods.include?('delegate_attr')
|
|
|
|
assert master_helper_methods.include?('delegate_attr=')
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2009-05-02 05:15:09 -04:00
|
|
|
def call_controller(klass, action)
|
2005-11-08 03:23:13 -05:00
|
|
|
request = ActionController::TestRequest.new
|
2009-05-02 05:15:09 -04:00
|
|
|
klass.action(action).call(request.env)
|
|
|
|
end
|
2005-11-08 03:23:13 -05:00
|
|
|
|
2009-05-02 05:15:09 -04:00
|
|
|
def test_helper_for_nested_controller
|
|
|
|
assert_equal 'hello: Iz guuut!',
|
|
|
|
call_controller(Fun::GamesController, "render_hello_world").last.body
|
|
|
|
# request = ActionController::TestRequest.new
|
|
|
|
#
|
|
|
|
# resp = Fun::GamesController.action(:render_hello_world).call(request.env)
|
|
|
|
# assert_equal 'hello: Iz guuut!', resp.last.body
|
2005-11-08 03:23:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_helper_for_acronym_controller
|
2009-05-02 05:15:09 -04:00
|
|
|
assert_equal "test: baz", call_controller(Fun::PdfController, "test").last.body
|
|
|
|
#
|
|
|
|
# request = ActionController::TestRequest.new
|
|
|
|
# response = ActionController::TestResponse.new
|
|
|
|
# request.action = 'test'
|
|
|
|
#
|
|
|
|
# assert_equal 'test: baz', Fun::PdfController.process(request, response).body
|
2007-02-24 15:31:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_all_helpers
|
2009-06-17 19:51:51 -04:00
|
|
|
methods = AllHelpersController._helpers.instance_methods.map {|m| m.to_s}
|
2007-12-14 21:29:36 -05:00
|
|
|
|
2007-02-24 15:31:34 -05:00
|
|
|
# abc_helper.rb
|
2007-12-14 21:29:36 -05:00
|
|
|
assert methods.include?('bare_a')
|
2007-02-24 15:31:34 -05:00
|
|
|
|
|
|
|
# fun/games_helper.rb
|
2007-12-14 21:29:36 -05:00
|
|
|
assert methods.include?('stratego')
|
2007-02-24 15:31:34 -05:00
|
|
|
|
|
|
|
# fun/pdf_helper.rb
|
2007-12-14 21:29:36 -05:00
|
|
|
assert methods.include?('foobar')
|
2005-02-14 20:45:35 -05:00
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2008-11-20 17:05:20 -05:00
|
|
|
def test_all_helpers_with_alternate_helper_dir
|
|
|
|
@controller_class.helpers_dir = File.dirname(__FILE__) + '/../fixtures/alternate_helpers'
|
|
|
|
|
|
|
|
# Reload helpers
|
2009-06-17 19:51:51 -04:00
|
|
|
@controller_class._helpers = Module.new
|
2008-11-20 17:05:20 -05:00
|
|
|
@controller_class.helper :all
|
|
|
|
|
|
|
|
# helpers/abc_helper.rb should not be included
|
|
|
|
assert !master_helper_methods.include?('bare_a')
|
|
|
|
|
|
|
|
# alternate_helpers/foo_helper.rb
|
|
|
|
assert master_helper_methods.include?('baz')
|
|
|
|
end
|
|
|
|
|
2008-04-06 14:42:34 -04:00
|
|
|
def test_helper_proxy
|
2009-05-22 18:17:05 -04:00
|
|
|
methods = AllHelpersController.helpers.methods.map(&:to_s)
|
2008-04-06 14:42:34 -04:00
|
|
|
|
|
|
|
# ActionView
|
|
|
|
assert methods.include?('pluralize')
|
|
|
|
|
|
|
|
# abc_helper.rb
|
|
|
|
assert methods.include?('bare_a')
|
|
|
|
|
|
|
|
# fun/games_helper.rb
|
|
|
|
assert methods.include?('stratego')
|
|
|
|
|
|
|
|
# fun/pdf_helper.rb
|
|
|
|
assert methods.include?('foobar')
|
|
|
|
end
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
private
|
2005-06-15 13:17:58 -04:00
|
|
|
def expected_helper_methods
|
2009-06-08 16:33:18 -04:00
|
|
|
TestHelper.instance_methods.map {|m| m.to_s }
|
2005-06-15 13:17:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def master_helper_methods
|
2009-06-17 19:51:51 -04:00
|
|
|
@controller_class._helpers.instance_methods.map {|m| m.to_s }
|
2005-06-15 13:17:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def missing_methods
|
|
|
|
expected_helper_methods - master_helper_methods
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
def test_helper=(helper_module)
|
2005-09-11 00:58:27 -04:00
|
|
|
silence_warnings { self.class.const_set('TestHelper', helper_module) }
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2004-12-22 08:44:16 -05:00
|
|
|
end
|
2005-06-15 13:17:58 -04:00
|
|
|
|
|
|
|
|
|
|
|
class IsolatedHelpersTest < Test::Unit::TestCase
|
|
|
|
class A < ActionController::Base
|
|
|
|
def index
|
|
|
|
render :inline => '<%= shout %>'
|
|
|
|
end
|
|
|
|
|
|
|
|
def rescue_action(e) raise end
|
|
|
|
end
|
|
|
|
|
|
|
|
class B < A
|
|
|
|
helper { def shout; 'B' end }
|
|
|
|
|
|
|
|
def index
|
|
|
|
render :inline => '<%= shout %>'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class C < A
|
|
|
|
helper { def shout; 'C' end }
|
|
|
|
|
|
|
|
def index
|
|
|
|
render :inline => '<%= shout %>'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-05-02 05:15:09 -04:00
|
|
|
def call_controller(klass, action)
|
|
|
|
request = ActionController::TestRequest.new
|
|
|
|
klass.action(action).call(request.env)
|
|
|
|
end
|
|
|
|
|
2005-06-15 13:17:58 -04:00
|
|
|
def setup
|
|
|
|
@request = ActionController::TestRequest.new
|
|
|
|
@response = ActionController::TestResponse.new
|
|
|
|
@request.action = 'index'
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_helper_in_a
|
2009-05-02 05:15:09 -04:00
|
|
|
assert_raise(ActionView::TemplateError) { call_controller(A, "index") }
|
2005-06-15 13:17:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_helper_in_b
|
2009-05-02 05:15:09 -04:00
|
|
|
assert_equal 'B', call_controller(B, "index").last.body
|
2005-06-15 13:17:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_helper_in_c
|
2009-05-02 05:15:09 -04:00
|
|
|
assert_equal 'C', call_controller(C, "index").last.body
|
2005-06-15 13:17:58 -04:00
|
|
|
end
|
|
|
|
end
|