2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
require "abstract_unit"
|
|
|
|
require "active_support/concern"
|
2009-05-07 11:03:39 -04:00
|
|
|
|
2012-01-05 20:12:46 -05:00
|
|
|
class ConcernTest < ActiveSupport::TestCase
|
2009-05-07 11:03:39 -04:00
|
|
|
module Baz
|
2009-05-28 12:35:36 -04:00
|
|
|
extend ActiveSupport::Concern
|
2009-05-07 11:29:22 -04:00
|
|
|
|
2014-02-23 14:06:18 -05:00
|
|
|
class_methods do
|
2009-05-07 11:03:39 -04:00
|
|
|
def baz
|
|
|
|
"baz"
|
|
|
|
end
|
|
|
|
|
2009-05-07 11:38:57 -04:00
|
|
|
def included_ran=(value)
|
|
|
|
@@included_ran = value
|
2009-05-07 11:03:39 -04:00
|
|
|
end
|
|
|
|
|
2009-05-07 11:38:57 -04:00
|
|
|
def included_ran
|
|
|
|
@@included_ran
|
2009-05-07 11:03:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-05-07 11:38:57 -04:00
|
|
|
included do
|
|
|
|
self.included_ran = true
|
2009-05-07 11:03:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def baz
|
|
|
|
"baz"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module Bar
|
2009-05-28 12:35:36 -04:00
|
|
|
extend ActiveSupport::Concern
|
2009-05-07 11:29:22 -04:00
|
|
|
|
2009-05-29 18:03:23 -04:00
|
|
|
include Baz
|
2009-05-07 11:03:39 -04:00
|
|
|
|
2014-02-23 14:06:18 -05:00
|
|
|
module ClassMethods
|
|
|
|
def baz
|
|
|
|
"bar's baz + " + super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-05-07 11:03:39 -04:00
|
|
|
def bar
|
|
|
|
"bar"
|
|
|
|
end
|
|
|
|
|
|
|
|
def baz
|
|
|
|
"bar+" + super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-05-20 21:04:17 -04:00
|
|
|
module Foo
|
2009-05-28 12:35:36 -04:00
|
|
|
extend ActiveSupport::Concern
|
2009-05-20 21:04:17 -04:00
|
|
|
|
2009-05-29 18:03:23 -04:00
|
|
|
include Bar, Baz
|
2009-05-20 21:04:17 -04:00
|
|
|
end
|
|
|
|
|
2015-06-09 13:00:24 -04:00
|
|
|
module Qux
|
|
|
|
module ClassMethods
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-05-07 11:03:39 -04:00
|
|
|
def setup
|
|
|
|
@klass = Class.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_module_is_included_normally
|
2015-01-31 23:12:37 -05:00
|
|
|
@klass.include(Baz)
|
2009-05-07 11:03:39 -04:00
|
|
|
assert_equal "baz", @klass.new.baz
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes @klass.included_modules, ConcernTest::Baz
|
2009-05-07 11:03:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_class_methods_are_extended
|
2015-01-31 23:12:37 -05:00
|
|
|
@klass.include(Baz)
|
2009-05-07 11:03:39 -04:00
|
|
|
assert_equal "baz", @klass.baz
|
2016-08-07 19:05:28 -04:00
|
|
|
assert_equal ConcernTest::Baz::ClassMethods, (class << @klass; included_modules; end)[0]
|
2009-05-07 11:03:39 -04:00
|
|
|
end
|
|
|
|
|
2015-06-09 13:00:24 -04:00
|
|
|
def test_class_methods_are_extended_only_on_expected_objects
|
2016-11-27 08:20:38 -05:00
|
|
|
::Object.include(Qux)
|
2015-06-09 13:00:24 -04:00
|
|
|
Object.extend(Qux::ClassMethods)
|
|
|
|
# module needs to be created after Qux is included in Object or bug won't
|
|
|
|
# be triggered
|
|
|
|
test_module = Module.new do
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
class_methods do
|
|
|
|
def test
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@klass.include test_module
|
2018-01-24 21:14:10 -05:00
|
|
|
assert_not_respond_to Object, :test
|
2015-06-09 13:00:24 -04:00
|
|
|
Qux.class_eval do
|
|
|
|
remove_const :ClassMethods
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-05-07 11:38:57 -04:00
|
|
|
def test_included_block_is_ran
|
2015-01-31 23:12:37 -05:00
|
|
|
@klass.include(Baz)
|
2009-05-07 11:38:57 -04:00
|
|
|
assert_equal true, @klass.included_ran
|
2009-05-07 11:03:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_modules_dependencies_are_met
|
2015-01-31 23:12:37 -05:00
|
|
|
@klass.include(Bar)
|
2009-05-07 11:03:39 -04:00
|
|
|
assert_equal "bar", @klass.new.bar
|
|
|
|
assert_equal "bar+baz", @klass.new.baz
|
2014-02-23 14:06:18 -05:00
|
|
|
assert_equal "bar's baz + baz", @klass.baz
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes @klass.included_modules, ConcernTest::Bar
|
2009-05-07 11:03:39 -04:00
|
|
|
end
|
2009-05-20 21:04:17 -04:00
|
|
|
|
2009-05-29 18:03:23 -04:00
|
|
|
def test_dependencies_with_multiple_modules
|
2015-01-31 23:12:37 -05:00
|
|
|
@klass.include(Foo)
|
2011-11-21 17:07:24 -05:00
|
|
|
assert_equal [ConcernTest::Foo, ConcernTest::Bar, ConcernTest::Baz], @klass.included_modules[0..2]
|
2009-05-20 21:04:17 -04:00
|
|
|
end
|
2013-05-16 14:11:27 -04:00
|
|
|
|
|
|
|
def test_raise_on_multiple_included_calls
|
|
|
|
assert_raises(ActiveSupport::Concern::MultipleIncludedBlocks) do
|
|
|
|
Module.new do
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
end
|
|
|
|
|
|
|
|
included do
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-11-29 13:36:20 -05:00
|
|
|
|
|
|
|
def test_no_raise_on_same_included_call
|
|
|
|
assert_nothing_raised do
|
|
|
|
2.times do
|
|
|
|
load File.expand_path("../fixtures/concern/some_concern.rb", __FILE__)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-05-07 11:03:39 -04:00
|
|
|
end
|