public void Tests()
        {
            Assert.AreEqual(5, InvokeParam(ConstParameter.From(5), MagicBagTests.EmptyBag));

            Assert.AreEqual(6, InvokeParam(new DelegateParameter<int>(() => 6), MagicBagTests.EmptyBag));
            Assert.AreEqual(true, InvokeParam(new DelegateParameter<bool>(bag => object.ReferenceEquals(bag, MagicBagTests.EmptyBag)), MagicBagTests.EmptyBag));

            var magicBag = new Mechanical.MagicBag.MagicBag.Basic(Map<int>.To<int>(() => 7).AsTransient());
            Assert.AreEqual(7, InvokeParam(new InjectParameter<int>(), magicBag));
            Assert.AreEqual(7, InvokeParam(new InjectParameter(typeof(int)), magicBag));
        }
Esempio n. 2
0
        public void BasicTests()
        {
            var bag = new Mechanical.MagicBag.MagicBag.Basic(
                Map<int>.To(() => 5).AsTransient(),
                Map<float>.To(() => 3.14f).AsTransient());

            Assert.True(bag.IsRegistered<int>());
            Assert.True(bag.IsRegistered<float>());
            Assert.False(bag.IsRegistered<object>());

            Assert.AreEqual(5, bag.Pull<int>());
            Assert.AreEqual(3.14f, bag.Pull<float>());
            Assert.Throws<KeyNotFoundException>(() => bag.Pull<object>());
        }
        public void ConstructorAllocatorTest()
        {
            var mapping = Map<MagicBagTests.IntWrapper>.ToDefault<MagicBagTests.IntWrapper>().AsTransient().ToMapping();
            Assert.AreEqual(0, MagicBagTests.GetIntFromWrapper(mapping.Get(MagicBagTests.EmptyBag)));

            var ctor = Reveal.Constructor(() => new KeyValuePair<int, int>(0, 0));
            mapping = Map<KeyValuePair<int, int>>.To<KeyValuePair<int, int>>(ctor, p => p.Const(6).Func(() => 7)).AsTransient().ToMapping();
            var pair = (KeyValuePair<int, int>)mapping.Get(MagicBagTests.EmptyBag);
            Assert.AreEqual(6, pair.Key);
            Assert.AreEqual(7, pair.Value);

            var bag = new Mechanical.MagicBag.MagicBag.Basic(Map<int>.To(() => 8).AsTransient());
            mapping = Map<KeyValuePair<int, int>>.ToInject<KeyValuePair<int, int>>(ctor).AsTransient().ToMapping();
            pair = (KeyValuePair<int, int>)mapping.Get(bag);
            Assert.AreEqual(8, pair.Key);
            Assert.AreEqual(8, pair.Value);
        }
Esempio n. 4
0
        public void SupplementTests()
        {
            var parentBag = new Mechanical.MagicBag.MagicBag.Basic(
                Map<int>.To(() => 5).AsTransient(),
                Map<float>.To(() => 3.14f).AsTransient());

            var bag = new Mechanical.MagicBag.MagicBag.Extend(
                parentBag,
                Map<int>.To(() => 6).AsTransient(),
                Map<string>.To(() => "abc").AsTransient());

            Assert.True(bag.IsRegistered<int>());
            Assert.True(bag.IsRegistered<float>());
            Assert.True(bag.IsRegistered<string>());
            Assert.False(bag.IsRegistered<object>());

            Assert.AreEqual(5, bag.Pull<int>());
            Assert.AreEqual(3.14f, bag.Pull<float>());
            Test.OrdinalEquals("abc", bag.Pull<string>());
            Assert.Throws<KeyNotFoundException>(() => bag.Pull<object>());
        }
Esempio n. 5
0
        public void BlacklistTests()
        {
            var parentBag = new Mechanical.MagicBag.MagicBag.Basic(
                Map<int>.To(() => 5).AsTransient(),
                Map<float>.To(() => 3.14f).AsTransient());

            var bag = new Mechanical.MagicBag.MagicBag.Blacklist(
                parentBag,
                typeof(int),
                typeof(string));

            Assert.True(bag.IsRegistered<float>());
            Assert.False(bag.IsRegistered<int>());
            Assert.False(bag.IsRegistered<string>());
            Assert.False(bag.IsRegistered<object>());

            Assert.AreEqual(3.14f, bag.Pull<float>());
            Assert.Throws<KeyNotFoundException>(() => bag.Pull<int>());
            Assert.Throws<KeyNotFoundException>(() => bag.Pull<string>());
            Assert.Throws<KeyNotFoundException>(() => bag.Pull<object>());
        }