|
1 | 1 | require 'abstract_unit' |
2 | 2 | require 'active_support/core_ext/module/concerning' |
3 | 3 |
|
4 | | -class ConcerningTest < ActiveSupport::TestCase |
5 | | - def test_concern_shortcut_creates_a_module_but_doesnt_include_it |
6 | | - mod = Module.new { concern(:Foo) { } } |
7 | | - assert_kind_of Module, mod::Foo |
8 | | - assert mod::Foo.respond_to?(:included) |
9 | | - assert !mod.ancestors.include?(mod::Foo), mod.ancestors.inspect |
| 4 | +class ModuleConcerningTest < ActiveSupport::TestCase |
| 5 | + def test_concerning_declares_a_concern_and_includes_it_immediately |
| 6 | + klass = Class.new { concerning(:Foo) { } } |
| 7 | + assert klass.ancestors.include?(klass::Foo), klass.ancestors.inspect |
10 | 8 | end |
| 9 | +end |
11 | 10 |
|
| 11 | +class ModuleConcernTest < ActiveSupport::TestCase |
12 | 12 | def test_concern_creates_a_module_extended_with_active_support_concern |
13 | 13 | klass = Class.new do |
14 | | - concern :Foo do |
| 14 | + concern :Baz do |
15 | 15 | included { @foo = 1 } |
16 | 16 | def should_be_public; end |
17 | 17 | end |
18 | 18 | end |
19 | 19 |
|
20 | 20 | # Declares a concern but doesn't include it |
21 | | - assert_kind_of Module, klass::Foo |
22 | | - assert !klass.ancestors.include?(klass::Foo), klass.ancestors.inspect |
| 21 | + assert klass.const_defined?(:Baz, false) |
| 22 | + assert !ModuleConcernTest.const_defined?(:Baz) |
| 23 | + assert_kind_of ActiveSupport::Concern, klass::Baz |
| 24 | + assert !klass.ancestors.include?(klass::Baz), klass.ancestors.inspect |
23 | 25 |
|
24 | 26 | # Public method visibility by default |
25 | | - assert klass::Foo.public_instance_methods.map(&:to_s).include?('should_be_public') |
| 27 | + assert klass::Baz.public_instance_methods.map(&:to_s).include?('should_be_public') |
26 | 28 |
|
27 | 29 | # Calls included hook |
28 | | - assert_equal 1, Class.new { include klass::Foo }.instance_variable_get('@foo') |
| 30 | + assert_equal 1, Class.new { include klass::Baz }.instance_variable_get('@foo') |
29 | 31 | end |
30 | 32 |
|
31 | | - def test_concerning_declares_a_concern_and_includes_it_immediately |
32 | | - klass = Class.new { concerning(:Foo) { } } |
33 | | - assert klass.ancestors.include?(klass::Foo), klass.ancestors.inspect |
| 33 | + class Foo |
| 34 | + concerning :Bar do |
| 35 | + module ClassMethods |
| 36 | + def will_be_orphaned; end |
| 37 | + end |
| 38 | + |
| 39 | + const_set :ClassMethods, Module.new { |
| 40 | + def hacked_on; end |
| 41 | + } |
| 42 | + |
| 43 | + # Doesn't overwrite existing ClassMethods module. |
| 44 | + class_methods do |
| 45 | + def nicer_dsl; end |
| 46 | + end |
| 47 | + |
| 48 | + # Doesn't overwrite previous class_methods definitions. |
| 49 | + class_methods do |
| 50 | + def doesnt_clobber; end |
| 51 | + end |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + def test_using_class_methods_blocks_instead_of_ClassMethods_module |
| 56 | + assert !Foo.respond_to?(:will_be_orphaned) |
| 57 | + assert Foo.respond_to?(:hacked_on) |
| 58 | + assert Foo.respond_to?(:nicer_dsl) |
| 59 | + assert Foo.respond_to?(:doesnt_clobber) |
| 60 | + |
| 61 | + # Orphan in Foo::ClassMethods, not Bar::ClassMethods. |
| 62 | + assert Foo.const_defined?(:ClassMethods) |
| 63 | + assert Foo::ClassMethods.method_defined?(:will_be_orphaned) |
34 | 64 | end |
35 | 65 | end |
0 commit comments