2017-07-24 16:38:04 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
require "abstract_unit"
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2017-05-15 10:17:28 -04:00
|
|
|
ActionController::Base.helpers_path = File.expand_path("../fixtures/helpers", __dir__)
|
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
|
2016-08-06 13:35:13 -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
|
2016-08-06 13:35:13 -04:00
|
|
|
render inline: "test: <%= foobar %>"
|
2005-11-08 03:23:13 -05:00
|
|
|
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
|
|
|
|
|
2015-01-31 23:12:37 -05:00
|
|
|
ActionController::Base.include(ImpressiveLibrary)
|
2010-08-26 22:18:35 -04:00
|
|
|
|
2010-08-26 17:17:50 -04:00
|
|
|
class JustMeController < ActionController::Base
|
|
|
|
clear_helpers
|
2010-08-26 22:18:35 -04:00
|
|
|
|
|
|
|
def flash
|
2016-08-06 13:35:13 -04:00
|
|
|
render inline: "<h1><%= notice %></h1>"
|
2010-08-26 22:18:35 -04:00
|
|
|
end
|
2010-09-05 21:06:43 -04:00
|
|
|
|
2010-08-26 22:18:35 -04:00
|
|
|
def lib
|
2016-08-06 13:35:13 -04:00
|
|
|
render inline: "<%= useful_function %>"
|
2010-08-26 22:18:35 -04:00
|
|
|
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|
|
2017-05-15 10:17:28 -04:00
|
|
|
File.join(File.expand_path("../fixtures", __dir__), path)
|
2012-05-27 08:01:44 -04:00
|
|
|
end
|
|
|
|
$:.unshift(*paths)
|
|
|
|
|
|
|
|
self.helpers_path = paths
|
|
|
|
helper :all
|
|
|
|
|
|
|
|
def index
|
2016-08-06 13:35:13 -04:00
|
|
|
render inline: "<%= conflicting_helper %>"
|
2012-05-27 08:01:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-25 07:58:38 -04:00
|
|
|
class HelpersTypoController < ActionController::Base
|
2017-05-15 10:17:28 -04:00
|
|
|
path = File.expand_path("../fixtures/helpers_typo", __dir__)
|
2014-10-25 07:58:38 -04:00
|
|
|
$:.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 test_helpers_paths_priority
|
2015-07-08 17:14:59 -04:00
|
|
|
responses = HelpersPathsController.action(:index).call(ActionController::TestRequest::DEFAULT_ENV.dup)
|
2012-05-27 08:01:44 -04:00
|
|
|
|
|
|
|
# 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
|
2016-08-06 12:54:50 -04:00
|
|
|
e = assert_raise(NameError) { HelpersTypoController.helper "admin/users" }
|
2014-10-25 07:58:38 -04:00
|
|
|
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.
|
2017-07-24 16:38:04 -04:00
|
|
|
@symbol = (@@counter ||= "A0").succ.dup
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
# 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 }
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes master_helper_methods, :delegate_method
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_helper_attr
|
|
|
|
assert_nothing_raised { @controller_class.helper_attr :delegate_attr }
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes master_helper_methods, :delegate_attr
|
|
|
|
assert_includes master_helper_methods, :delegate_attr=
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2009-05-02 05:15:09 -04:00
|
|
|
def call_controller(klass, action)
|
2015-07-08 17:14:59 -04:00
|
|
|
klass.action(action).call(ActionController::TestRequest::DEFAULT_ENV.dup)
|
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
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "hello: Iz guuut!",
|
2009-05-02 05:15:09 -04:00
|
|
|
call_controller(Fun::GamesController, "render_hello_world").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
|
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
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes methods, :bare_a
|
2007-02-24 15:31:34 -05:00
|
|
|
|
|
|
|
# fun/games_helper.rb
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes methods, :stratego
|
2007-02-24 15:31:34 -05:00
|
|
|
|
|
|
|
# fun/pdf_helper.rb
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes methods, :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
|
2017-05-15 10:17:28 -04:00
|
|
|
@controller_class.helpers_path = File.expand_path("../fixtures/alternate_helpers", __dir__)
|
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
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_not_includes master_helper_methods, :bare_a
|
2008-11-20 17:05:20 -05:00
|
|
|
|
|
|
|
# alternate_helpers/foo_helper.rb
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes master_helper_methods, :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
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes methods, :pluralize
|
2008-04-06 14:42:34 -04:00
|
|
|
|
|
|
|
# abc_helper.rb
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes methods, :bare_a
|
2008-04-06 14:42:34 -04:00
|
|
|
|
|
|
|
# fun/games_helper.rb
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes methods, :stratego
|
2008-04-06 14:42:34 -04:00
|
|
|
|
|
|
|
# fun/pdf_helper.rb
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes methods, :foobar
|
2008-04-06 14:42:34 -04:00
|
|
|
end
|
2015-01-31 23:12:37 -05:00
|
|
|
|
2016-05-05 13:02:28 -04:00
|
|
|
def test_helper_proxy_in_instance
|
|
|
|
methods = AllHelpersController.new.helpers.methods
|
|
|
|
|
|
|
|
# Action View
|
|
|
|
assert_includes methods, :pluralize
|
|
|
|
|
|
|
|
# abc_helper.rb
|
|
|
|
assert_includes methods, :bare_a
|
|
|
|
|
|
|
|
# fun/games_helper.rb
|
|
|
|
assert_includes methods, :stratego
|
|
|
|
|
|
|
|
# fun/pdf_helper.rb
|
|
|
|
assert_includes methods, :foobar
|
|
|
|
end
|
|
|
|
|
2013-10-24 00:54:20 -04:00
|
|
|
def test_helper_proxy_config
|
2016-08-06 12:54:50 -04:00
|
|
|
AllHelpersController.config.my_var = "smth"
|
2015-01-31 23:12:37 -05:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "smth", AllHelpersController.helpers.config.my_var
|
2013-10-24 00:54:20 -04:00
|
|
|
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)
|
2016-08-06 12:54:50 -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
|
|
|
|
2015-07-08 17:14:59 -04:00
|
|
|
class IsolatedHelpersTest < ActionController::TestCase
|
2005-06-15 13:17:58 -04:00
|
|
|
class A < ActionController::Base
|
|
|
|
def index
|
2016-08-06 13:35:13 -04:00
|
|
|
render inline: "<%= shout %>"
|
2005-06-15 13:17:58 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class B < A
|
2016-08-06 12:54:50 -04:00
|
|
|
helper { def shout; "B" end }
|
2005-06-15 13:17:58 -04:00
|
|
|
|
|
|
|
def index
|
2016-08-06 13:35:13 -04:00
|
|
|
render inline: "<%= shout %>"
|
2005-06-15 13:17:58 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class C < A
|
2016-08-06 12:54:50 -04:00
|
|
|
helper { def shout; "C" end }
|
2005-06-15 13:17:58 -04:00
|
|
|
|
|
|
|
def index
|
2016-08-06 13:35:13 -04:00
|
|
|
render inline: "<%= shout %>"
|
2005-06-15 13:17:58 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-05-02 05:15:09 -04:00
|
|
|
def call_controller(klass, action)
|
2015-07-08 17:14:59 -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
|
2015-07-08 17:14:59 -04:00
|
|
|
super
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.action = "index"
|
2005-06-15 13:17:58 -04:00
|
|
|
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
|
2016-08-06 12:54:50 -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
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "C", call_controller(C, "index").last.body
|
2005-06-15 13:17:58 -04:00
|
|
|
end
|
|
|
|
end
|