aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/dependency_module_test.rb
blob: be7db0fa7bc05888f7e81d2713c6ffb46f807055 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
require 'abstract_unit'
require 'active_support/dependency_module'

class DependencyModuleTest < Test::Unit::TestCase
  module Baz
    extend ActiveSupport::DependencyModule

    module ClassMethods
      def baz
        "baz"
      end

      def included_ran=(value)
        @@included_ran = value
      end

      def included_ran
        @@included_ran
      end
    end

    included do
      self.included_ran = true
    end

    def baz
      "baz"
    end
  end

  module Bar
    extend ActiveSupport::DependencyModule

    depends_on Baz

    def bar
      "bar"
    end

    def baz
      "bar+" + super
    end
  end

  module Foo
    extend ActiveSupport::DependencyModule

    depends_on Bar, Baz
  end

  def setup
    @klass = Class.new
  end

  def test_module_is_included_normally
    @klass.send(:include, Baz)
    assert_equal "baz", @klass.new.baz
    assert_equal DependencyModuleTest::Baz, @klass.included_modules[0]

    @klass.send(:include, Baz)
    assert_equal "baz", @klass.new.baz
    assert_equal DependencyModuleTest::Baz, @klass.included_modules[0]
  end

  def test_class_methods_are_extended
    @klass.send(:include, Baz)
    assert_equal "baz", @klass.baz
    assert_equal DependencyModuleTest::Baz::ClassMethods, (class << @klass; self.included_modules; end)[0]
  end

  def test_included_block_is_ran
    @klass.send(:include, Baz)
    assert_equal true, @klass.included_ran
  end

  def test_modules_dependencies_are_met
    @klass.send(:include, Bar)
    assert_equal "bar", @klass.new.bar
    assert_equal "bar+baz", @klass.new.baz
    assert_equal "baz", @klass.baz
    assert_equal [DependencyModuleTest::Bar, DependencyModuleTest::Baz], @klass.included_modules[0..1]
  end

  def test_depends_on_with_multiple_modules
    @klass.send(:include, Foo)
    assert_equal [DependencyModuleTest::Foo, DependencyModuleTest::Bar, DependencyModuleTest::Baz], @klass.included_modules[0..2]
  end
end