2017-07-23 11:36:41 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:50:17 -04:00
|
|
|
require "abstract_unit"
|
2009-03-03 19:42:20 -05:00
|
|
|
|
2017-05-15 10:17:28 -04:00
|
|
|
ActionController::Base.helpers_path = File.expand_path("../../fixtures/helpers", __dir__)
|
2009-10-21 12:42:58 -04:00
|
|
|
|
2009-03-03 19:42:20 -05:00
|
|
|
module AbstractController
|
|
|
|
module Testing
|
|
|
|
class ControllerWithHelpers < AbstractController::Base
|
2012-03-16 21:54:25 -04:00
|
|
|
include AbstractController::Helpers
|
2013-07-05 08:34:39 -04:00
|
|
|
include AbstractController::Rendering
|
|
|
|
include ActionView::Rendering
|
2009-10-18 20:52:36 -04:00
|
|
|
|
2009-10-21 12:42:58 -04:00
|
|
|
def with_module
|
2016-08-06 13:36:34 -04:00
|
|
|
render inline: "Module <%= included_method %>"
|
2009-03-20 19:50:51 -04:00
|
|
|
end
|
2009-03-03 19:42:20 -05:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2009-03-03 19:42:20 -05:00
|
|
|
module HelperyTest
|
|
|
|
def included_method
|
|
|
|
"Included"
|
|
|
|
end
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2009-10-21 12:42:58 -04:00
|
|
|
class AbstractHelpers < ControllerWithHelpers
|
2009-03-03 19:42:20 -05:00
|
|
|
helper(HelperyTest) do
|
|
|
|
def helpery_test
|
|
|
|
"World"
|
|
|
|
end
|
|
|
|
end
|
2009-10-21 12:42:58 -04:00
|
|
|
|
|
|
|
helper :abc
|
|
|
|
|
|
|
|
def with_block
|
2016-08-06 13:36:34 -04:00
|
|
|
render inline: "Hello <%= helpery_test %>"
|
2009-10-21 12:42:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def with_symbol
|
2016-08-06 13:36:34 -04:00
|
|
|
render inline: "I respond to bare_a: <%= respond_to?(:bare_a) %>"
|
2009-10-21 12:42:58 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-08-26 15:03:10 -04:00
|
|
|
class ::HelperyTestController < AbstractHelpers
|
|
|
|
clear_helpers
|
|
|
|
end
|
|
|
|
|
2009-10-21 12:42:58 -04:00
|
|
|
class AbstractHelpersBlock < ControllerWithHelpers
|
|
|
|
helper do
|
2012-03-16 21:54:25 -04:00
|
|
|
include AbstractController::Testing::HelperyTest
|
2009-03-03 19:42:20 -05:00
|
|
|
end
|
|
|
|
end
|
2009-10-21 12:42:58 -04:00
|
|
|
|
2013-05-16 09:37:19 -04:00
|
|
|
class AbstractInvalidHelpers < AbstractHelpers
|
|
|
|
include ActionController::Helpers
|
|
|
|
|
2017-05-15 10:17:28 -04:00
|
|
|
path = File.expand_path("../../fixtures/helpers_missing", __dir__)
|
2013-05-16 09:37:19 -04:00
|
|
|
$:.unshift(path)
|
|
|
|
self.helpers_path = path
|
|
|
|
end
|
|
|
|
|
2009-03-03 19:42:20 -05:00
|
|
|
class TestHelpers < ActiveSupport::TestCase
|
2009-10-21 12:42:58 -04:00
|
|
|
def setup
|
|
|
|
@controller = AbstractHelpers.new
|
2009-03-03 19:42:20 -05:00
|
|
|
end
|
2009-10-21 12:42:58 -04:00
|
|
|
|
|
|
|
def test_helpers_with_block
|
|
|
|
@controller.process(:with_block)
|
|
|
|
assert_equal "Hello World", @controller.response_body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_helpers_with_module
|
|
|
|
@controller.process(:with_module)
|
|
|
|
assert_equal "Module Included", @controller.response_body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_helpers_with_symbol
|
|
|
|
@controller.process(:with_symbol)
|
|
|
|
assert_equal "I respond to bare_a: true", @controller.response_body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_declare_missing_helper
|
2013-12-19 06:03:39 -05:00
|
|
|
e = assert_raise AbstractController::Helpers::MissingHelperError do
|
|
|
|
AbstractHelpers.helper :missing
|
|
|
|
end
|
2013-01-05 00:45:18 -05:00
|
|
|
assert_equal "helpers/missing_helper.rb", e.path
|
2009-10-21 12:42:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_helpers_with_module_through_block
|
|
|
|
@controller = AbstractHelpersBlock.new
|
|
|
|
@controller.process(:with_module)
|
|
|
|
assert_equal "Module Included", @controller.response_body
|
|
|
|
end
|
2009-03-03 19:42:20 -05:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-08-26 15:03:10 -04:00
|
|
|
class ClearHelpersTest < ActiveSupport::TestCase
|
|
|
|
def setup
|
|
|
|
@controller = HelperyTestController.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_clears_up_previous_helpers
|
|
|
|
@controller.process(:with_symbol)
|
|
|
|
assert_equal "I respond to bare_a: false", @controller.response_body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_includes_controller_default_helper
|
|
|
|
@controller.process(:with_block)
|
|
|
|
assert_equal "Hello Default", @controller.response_body
|
|
|
|
end
|
|
|
|
end
|
2013-05-16 09:37:19 -04:00
|
|
|
|
|
|
|
class InvalidHelpersTest < ActiveSupport::TestCase
|
|
|
|
def test_controller_raise_error_about_real_require_problem
|
|
|
|
e = assert_raise(LoadError) { AbstractInvalidHelpers.helper(:invalid_require) }
|
2016-11-24 16:56:05 -05:00
|
|
|
assert_equal "No such file to load -- very_invalid_file_name.rb", e.message
|
2013-05-16 09:37:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_controller_raise_error_about_missing_helper
|
2013-07-12 03:37:15 -04:00
|
|
|
e = assert_raise(AbstractController::Helpers::MissingHelperError) { AbstractInvalidHelpers.helper(:missing) }
|
2013-05-16 09:37:19 -04:00
|
|
|
assert_equal "Missing helper file helpers/missing_helper.rb", e.message
|
|
|
|
end
|
2013-07-12 09:40:47 -04:00
|
|
|
|
|
|
|
def test_missing_helper_error_has_the_right_path
|
|
|
|
e = assert_raise(AbstractController::Helpers::MissingHelperError) { AbstractInvalidHelpers.helper(:missing) }
|
|
|
|
assert_equal "helpers/missing_helper.rb", e.path
|
|
|
|
end
|
2013-05-16 09:37:19 -04:00
|
|
|
end
|
2009-03-03 19:42:20 -05:00
|
|
|
end
|
2009-08-06 17:48:48 -04:00
|
|
|
end
|