2008-01-05 08:32:06 -05:00
|
|
|
require 'abstract_unit'
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2010-07-07 15:03:01 -04:00
|
|
|
ActionController::Base.helpers_path = File.expand_path('../../fixtures/helpers', __FILE__)
|
2007-02-24 15:31:34 -05:00
|
|
|
|
2005-02-14 20:45:35 -05:00
|
|
|
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
|
|
|
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
|
|
|
|
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
|
|
|
|
|
2010-08-26 22:18:35 -04:00
|
|
|
module ImpressiveLibrary
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
included do
|
|
|
|
helper_method :useful_function
|
|
|
|
end
|
|
|
|
|
|
|
|
def useful_function() end
|
|
|
|
end
|
|
|
|
|
|
|
|
ActionController::Base.send :include, ImpressiveLibrary
|
|
|
|
|
2010-08-26 17:17:50 -04:00
|
|
|
class JustMeController < ActionController::Base
|
|
|
|
clear_helpers
|
2010-08-26 22:18:35 -04:00
|
|
|
|
|
|
|
def flash
|
|
|
|
render :inline => "<h1><%= notice %></h1>"
|
|
|
|
end
|
2010-09-05 21:06:43 -04:00
|
|
|
|
2010-08-26 22:18:35 -04:00
|
|
|
def lib
|
|
|
|
render :inline => '<%= useful_function %>'
|
|
|
|
end
|
2010-08-26 17:17:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class MeTooController < JustMeController
|
|
|
|
end
|
|
|
|
|
2012-05-27 08:01:44 -04:00
|
|
|
class HelpersPathsController < ActionController::Base
|
|
|
|
paths = ["helpers2_pack", "helpers1_pack"].map do |path|
|
|
|
|
File.join(File.expand_path('../../fixtures', __FILE__), path)
|
|
|
|
end
|
|
|
|
$:.unshift(*paths)
|
|
|
|
|
|
|
|
self.helpers_path = paths
|
|
|
|
helper :all
|
|
|
|
|
|
|
|
def index
|
|
|
|
render :inline => "<%= conflicting_helper %>"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-25 07:58:38 -04:00
|
|
|
class HelpersTypoController < ActionController::Base
|
|
|
|
path = File.expand_path('../../fixtures/helpers_typo', __FILE__)
|
|
|
|
$:.unshift(path)
|
|
|
|
self.helpers_path = path
|
|
|
|
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
|
|
|
|
2012-05-27 08:01:44 -04:00
|
|
|
class HelperPathsTest < ActiveSupport::TestCase
|
|
|
|
def setup
|
|
|
|
@request = ActionController::TestRequest.new
|
|
|
|
@response = ActionController::TestResponse.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_helpers_paths_priority
|
|
|
|
request = ActionController::TestRequest.new
|
|
|
|
responses = HelpersPathsController.action(:index).call(request.env)
|
|
|
|
|
|
|
|
# helpers1_pack was given as a second path, so pack1_helper should be
|
|
|
|
# included as the second one
|
|
|
|
assert_equal "pack1", responses.last.body
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-25 07:58:38 -04:00
|
|
|
class HelpersTypoControllerTest < ActiveSupport::TestCase
|
|
|
|
def setup
|
|
|
|
@autoload_paths = ActiveSupport::Dependencies.autoload_paths
|
|
|
|
ActiveSupport::Dependencies.autoload_paths = Array(HelpersTypoController.helpers_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_helper_typo_error_message
|
|
|
|
e = assert_raise(NameError) { HelpersTypoController.helper 'admin/users' }
|
|
|
|
assert_equal "Couldn't find Admin::UsersHelper, expected it to be defined in helpers/admin/users_helper.rb", e.message
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
ActiveSupport::Dependencies.autoload_paths = @autoload_paths
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-25 17:38:47 -05:00
|
|
|
class HelperTest < ActiveSupport::TestCase
|
2009-11-04 18:25:15 -05:00
|
|
|
class TestController < ActionController::Base
|
|
|
|
attr_accessor :delegate_attr
|
|
|
|
def delegate_method() end
|
|
|
|
end
|
|
|
|
|
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
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2011-05-24 17:38:59 -04:00
|
|
|
def test_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_helper_method
|
|
|
|
assert_nothing_raised { @controller_class.helper_method :delegate_method }
|
2012-06-04 15:59:34 -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 }
|
2012-06-04 15:59:34 -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
|
2010-08-14 01:13:00 -04:00
|
|
|
klass.action(action).call(request.env)
|
2009-05-02 05:15:09 -04:00
|
|
|
end
|
2005-11-08 03:23:13 -05:00
|
|
|
|
2009-05-02 05:15:09 -04:00
|
|
|
def test_helper_for_nested_controller
|
2010-08-14 01:13:00 -04:00
|
|
|
assert_equal 'hello: Iz guuut!',
|
2009-05-02 05:15:09 -04:00
|
|
|
call_controller(Fun::GamesController, "render_hello_world").last.body
|
|
|
|
# request = ActionController::TestRequest.new
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2009-05-02 05:15:09 -04:00
|
|
|
# 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
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2009-05-02 05:15:09 -04:00
|
|
|
# request = ActionController::TestRequest.new
|
|
|
|
# response = ActionController::TestResponse.new
|
|
|
|
# request.action = 'test'
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2009-05-02 05:15:09 -04:00
|
|
|
# assert_equal 'test: baz', Fun::PdfController.process(request, response).body
|
2007-02-24 15:31:34 -05:00
|
|
|
end
|
|
|
|
|
2010-08-26 17:17:50 -04:00
|
|
|
def test_default_helpers_only
|
|
|
|
assert_equal [JustMeHelper], JustMeController._helpers.ancestors.reject(&:anonymous?)
|
|
|
|
assert_equal [MeTooHelper, JustMeHelper], MeTooController._helpers.ancestors.reject(&:anonymous?)
|
|
|
|
end
|
|
|
|
|
2010-08-26 22:18:35 -04:00
|
|
|
def test_base_helper_methods_after_clear_helpers
|
|
|
|
assert_nothing_raised do
|
|
|
|
call_controller(JustMeController, "flash")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_lib_helper_methods_after_clear_helpers
|
|
|
|
assert_nothing_raised do
|
|
|
|
call_controller(JustMeController, "lib")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-02-24 15:31:34 -05:00
|
|
|
def test_all_helpers
|
2012-06-04 15:59:34 -04:00
|
|
|
methods = AllHelpersController._helpers.instance_methods
|
2007-12-14 21:29:36 -05:00
|
|
|
|
2007-02-24 15:31:34 -05:00
|
|
|
# abc_helper.rb
|
2012-06-04 15:59:34 -04:00
|
|
|
assert methods.include?(:bare_a)
|
2007-02-24 15:31:34 -05:00
|
|
|
|
|
|
|
# fun/games_helper.rb
|
2012-06-04 15:59:34 -04:00
|
|
|
assert methods.include?(:stratego)
|
2007-02-24 15:31:34 -05:00
|
|
|
|
|
|
|
# fun/pdf_helper.rb
|
2012-06-04 15:59:34 -04: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
|
2010-07-07 15:03:01 -04:00
|
|
|
@controller_class.helpers_path = File.expand_path('../../fixtures/alternate_helpers', __FILE__)
|
2008-11-20 17:05:20 -05:00
|
|
|
|
|
|
|
# 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
|
2012-06-04 15:59:34 -04:00
|
|
|
assert !master_helper_methods.include?(:bare_a)
|
2008-11-20 17:05:20 -05:00
|
|
|
|
|
|
|
# alternate_helpers/foo_helper.rb
|
2012-06-04 15:59:34 -04:00
|
|
|
assert master_helper_methods.include?(:baz)
|
2008-11-20 17:05:20 -05:00
|
|
|
end
|
|
|
|
|
2008-04-06 14:42:34 -04:00
|
|
|
def test_helper_proxy
|
2012-06-04 15:59:34 -04:00
|
|
|
methods = AllHelpersController.helpers.methods
|
2008-04-06 14:42:34 -04:00
|
|
|
|
2010-06-14 17:21:53 -04:00
|
|
|
# Action View
|
2012-06-04 15:59:34 -04:00
|
|
|
assert methods.include?(:pluralize)
|
2008-04-06 14:42:34 -04:00
|
|
|
|
|
|
|
# abc_helper.rb
|
2012-06-04 15:59:34 -04:00
|
|
|
assert methods.include?(:bare_a)
|
2008-04-06 14:42:34 -04:00
|
|
|
|
|
|
|
# fun/games_helper.rb
|
2012-06-04 15:59:34 -04:00
|
|
|
assert methods.include?(:stratego)
|
2008-04-06 14:42:34 -04:00
|
|
|
|
|
|
|
# fun/pdf_helper.rb
|
2012-06-04 15:59:34 -04:00
|
|
|
assert methods.include?(:foobar)
|
2008-04-06 14:42:34 -04:00
|
|
|
end
|
2013-10-24 00:54:20 -04:00
|
|
|
|
|
|
|
def test_helper_proxy_config
|
|
|
|
AllHelpersController.config.my_var = 'smth'
|
|
|
|
|
|
|
|
assert_equal 'smth', AllHelpersController.helpers.config.my_var
|
|
|
|
end
|
2008-04-06 14:42:34 -04:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
private
|
2005-06-15 13:17:58 -04:00
|
|
|
def expected_helper_methods
|
2012-06-04 15:59:34 -04:00
|
|
|
TestHelper.instance_methods
|
2005-06-15 13:17:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def master_helper_methods
|
2012-06-04 15:59:34 -04:00
|
|
|
@controller_class._helpers.instance_methods
|
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
|
|
|
|
|
|
|
|
2010-01-25 17:38:47 -05:00
|
|
|
class IsolatedHelpersTest < ActiveSupport::TestCase
|
2005-06-15 13:17:58 -04:00
|
|
|
class A < ActionController::Base
|
|
|
|
def index
|
|
|
|
render :inline => '<%= shout %>'
|
|
|
|
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
|
2010-08-14 01:13:00 -04:00
|
|
|
klass.action(action).call(request.env)
|
2009-05-02 05:15:09 -04:00
|
|
|
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-12-02 23:01:01 -05:00
|
|
|
assert_raise(ActionView::Template::Error) { 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
|