Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 182 lines (159 sloc) 6.757 kb
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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
require 'rubygems'
require 'test/unit'
require 'mixology'

# in 1.9.1 the test/unit library seems to mix in PP:ObjectMixin automatically so i added
# that module into the list of modules to test for when checking the inheritance chains
# i also didn't have dust installed (and have no idea what it is) but created a self.test method
# that i think may perform at least some of the function; anyway this test suite passes 100% on my setup

class MixologyTest < Test::Unit::TestCase

    def self.test(name, &block)
        name.gsub!(/ /, "_")
        define_method("test_#{name}", &block)
    end
    
    test "mixin" do
        mixin = Module.new { def foo; "foo"; end }
        object = Object.new
        object.mixin mixin
        assert_equal "foo", object.foo
    end
    
    test "unmix" do
        mixin = Module.new { def foo; "mixin"; end }
        object = Class.new { def foo; "object"; end }.new
        object.mixin mixin
        assert_equal "mixin", object.foo
        object.unmix mixin
        assert_equal "object", object.foo
    end
    
    test "mixin twice" do
        first_mixin = Module.new { def foo; "first"; end }
        second_mixin = Module.new { def foo; "second"; end }
        object = Object.new
        object.mixin first_mixin
        object.mixin second_mixin
        assert_equal "second", object.foo
    end
    
    test "mixin to class" do
        mix = Module.new { def foo; "foo"; end }
        klass = Class.new { mixin mix }
        assert_equal "foo", klass.foo
    end
    
    test "can mixin again" do
        first_mixin = Module.new { def foo; "first"; end }
        second_mixin = Module.new { def foo; "second"; end }
        object = Object.new
        object.mixin first_mixin
        object.mixin second_mixin
        object.mixin first_mixin
        assert_equal "first", object.foo
    end
    
    test "unmix effects limited to instance" do
        mixin = Module.new { def foo; "mixin"; end }
        object = Class.new {include mixin}.new
        assert_equal "mixin", object.foo
        object.unmix mixin
        assert_equal "mixin", object.foo
    end
    
    test "can add mod to an instance even when already included by class" do
        mixin = Module.new { def foo; "mixin"; end }
        klass = Class.new {include mixin; def foo; "class"; end }
        object = klass.new
        assert_equal "class", object.foo
        object.mixin mixin
        assert_equal "mixin", object.foo
    end
    
    test "included modules after mixin" do
        mixin = Module.new
        object = Object.new
        object.mixin mixin
        assert_equal [mixin, Mixology,PP::ObjectMixin, Kernel], (class << object; self; end).included_modules
    end
    
    test "included modules after unmix" do
        mixin = Module.new
        object = Object.new
        object.mixin mixin
        object.unmix mixin
        assert_equal [Mixology, PP::ObjectMixin, Kernel], (class << object; self; end).included_modules
    end
    
    test "included modules after remix" do
        mixin_one = Module.new
        mixin_two = Module.new
        object = Object.new
        object.mixin mixin_one
        object.mixin mixin_two
        assert_equal [mixin_two, mixin_one, Mixology, PP::ObjectMixin, Kernel], (class << object; self; end).included_modules
        object.mixin mixin_one
        assert_equal [mixin_one, mixin_two, Mixology, PP::ObjectMixin, Kernel], (class << object; self; end).included_modules
    end
    
    test "mixin returns object" do
        object = Object.new
        mixin = Module.new
        assert_equal object, object.mixin(mixin)
    end
    
    test "unmix returns object" do
        object = Object.new
        mixin = Module.new
        object.mixin mixin
        assert_equal object, object.unmix(mixin)
    end
    
    test "nested modules are mixedin" do
        nested_module = Module.new { def foo; "foo"; end }
        mixin = Module.new { include nested_module }
        object = Object.new
        object.mixin mixin
        assert_equal [mixin, nested_module, Mixology, PP::ObjectMixin, Kernel], (class << object; self; end).included_modules
    end

    test "nested modules are mixedin deeply" do
        nested_module_ultimate = Module.new
        nested_module_penultimate = Module.new { include nested_module_ultimate }
        nested_module = Module.new { include nested_module_penultimate }
        mixin = Module.new { include nested_module }
        object = Object.new
        object.mixin mixin
        assert_equal [mixin, nested_module, nested_module_penultimate, nested_module_ultimate, Mixology, PP::ObjectMixin, Kernel], (class << object; self; end).included_modules
    end
    
    test "nested modules are mixedin even if alrady mixed in" do
        nested_module = Module.new { def foo; "foo"; end }
        mixin = Module.new { include nested_module }
        object = Object.new
        object.mixin nested_module
        object.mixin mixin
        assert_equal [mixin, nested_module, nested_module, Mixology, PP::ObjectMixin, Kernel], (class << object; self; end).included_modules
    end
    
    test "module is not unmixed if it is outside nested chain" do
        nested_module = Module.new
        mixin = Module.new { include nested_module }
        object = Object.new
        object.mixin nested_module
        object.mixin mixin
        object.unmix mixin
        assert_equal [nested_module, Mixology, PP::ObjectMixin, Kernel], (class << object; self; end).included_modules
    end
    
    test "nested modules are unmixed" do
        nested_module = Module.new
        mixin = Module.new { include nested_module }
        object = Object.new
        object.mixin mixin
        object.unmix mixin
        assert_equal [Mixology, PP::ObjectMixin, Kernel], (class << object; self; end).included_modules
    end
    
    test "nested modules are unmixed deeply" do
        nested_module_ultimate = Module.new
        nested_module_penultimate = Module.new { include nested_module_ultimate }
        nested_module = Module.new { include nested_module_penultimate }
        mixin = Module.new { include nested_module }
        object = Object.new
        object.mixin mixin
        object.unmix mixin
        assert_equal [Mixology, PP::ObjectMixin, Kernel], (class << object; self; end).included_modules
    end

    test "unrelated modules are not unmixed" do
        unrelated = Module.new
        nested_module = Module.new
        mixin = Module.new { include nested_module }
        object = Object.new
        object.mixin unrelated
        object.mixin mixin
        object.unmix mixin
        assert_equal [unrelated, Mixology, PP::ObjectMixin, Kernel], (class << object; self; end).included_modules
    end

end
Something went wrong with that request. Please try again.