Click here to Skip to main content
15,885,216 members
Articles / Programming Languages / C#

Introducing the LinFu Framework, Part II: LinFu.DynamicObject – Adding Dynamic Language Features to Statically Typed Languages

Rate me:
Please Sign up or sign in to vote.
4.97/5 (50 votes)
12 Nov 2007LGPL316 min read 144.7K   882   67  
Using LinFu.DynamicObject to add mixins, duck typing and multiple dispatch to your favorite .NET languages
using System;
using System.Collections.Generic;
using System.Text;
using NMock2;
using NUnit.Framework;

namespace Simple.IoC.Tests
{
    [TestFixture]
    public class ContainerTests
    {
        private Mockery mock;
        [SetUp]
        public void Init()
        {
            mock = new Mockery();
        }
        [TearDown]
        public void Term()
        {
            mock.VerifyAllExpectationsHaveBeenMet();
        }
        [Test]
        public void ContainerShouldStoreGenericFactoryInstance()
        {
            IFactory<ITestObject> factory = mock.NewMock<IFactory<ITestObject>>();
            ITestObject testObject = mock.NewMock<ITestObject>();

            // Add the factory instance
            IContainer container = new SimpleContainer();
            container.AddFactory(factory);

            // Once a query for that test instance is activated,
            // the container should call CreateInstance
            Expect.Once.On(factory).Method("CreateInstance").Will(Return.Value(testObject));
            ITestObject testResult = container.GetService<ITestObject>();
            Assert.AreSame(testObject, testResult);
        }
        [Test]
        public void ContainerShouldStoreFactoryInstance()
        {
            IFactory factory = mock.NewMock<IFactory>();
            ITestObject testObject = mock.NewMock<ITestObject>();

            // Add the factory instance
            IContainer container = new SimpleContainer();
            container.AddFactory(typeof (ITestObject), factory);

            // Once a query for that test instance is activated,
            // the container should call CreateInstance
            Expect.Once.On(factory).Method("CreateInstance").Will(Return.Value(testObject));
            ITestObject testResult = container.GetService<ITestObject>();
            Assert.AreSame(testObject, testResult);
        }
        [Test]
        public void ContainerShouldAddServiceInstance()
        {
            ITestObject testObject = mock.NewMock<ITestObject>();
            IContainer container = new SimpleContainer();

            container.AddService(testObject);
            Assert.AreSame(container.GetService<ITestObject>(), testObject);
        }
        [Test]
        public void ContainerShouldInitializeObject()
        {
            IFactory<ITestObject2> factory = mock.NewMock<IFactory<ITestObject2>>();
            ITestObject2 testObject = mock.NewMock<ITestObject2>();

            // Add the factory instance
            IContainer container = new SimpleContainer();
            container.AddFactory(factory);

            // Once the container instantiates the object, it should 
            // initialize it
            Expect.Once.On(testObject).Method("Initialize").With(container);

            // Once a query for that test instance is activated,
            // the container should call CreateInstance
            Expect.Once.On(factory).Method("CreateInstance").Will(Return.Value(testObject));
            ITestObject2 testResult = container.GetService<ITestObject2>();
            Assert.AreSame(testObject, testResult);
        }

        [Test]
        public void ContainerShouldCallInjectorInterface()
        {
            IFactory<ITestObject2> factory = mock.NewMock<IFactory<ITestObject2>>();
            ITestObject2 testObject = mock.NewMock<ITestObject2>();
            ITypeInjector injector = mock.NewMock<ITypeInjector>();
            
            IContainer container = new SimpleContainer();
            container.TypeInjectors.Add(injector);
            container.AddFactory(factory);

            Expect.Once.On(injector).Method("CanInject").WithAnyArguments().Will(Return.Value(true));
            Expect.Once.On(injector).Method("Inject").WithAnyArguments().Will(Return.Value(testObject));
            Expect.Once.On(testObject).Method("Initialize").With(container);

            container.AddService(injector);
            Expect.Once.On(factory).Method("CreateInstance").Will(Return.Value(testObject));
            ITestObject2 testResult = container.GetService<ITestObject2>();          
        }
        [Test]
        public void ContainerShouldCallPropertyInjectorInterface()
        {
            IContainer container = new SimpleContainer();
            IPropertyInjector injector = mock.NewMock<IPropertyInjector>();
            container.PropertyInjectors.Add(injector);

            ITestObject testObject = mock.NewMock<ITestObject>();
            container.AddService(testObject);

            Expect.Once.On(injector).Method("CanInject").With(testObject, container).Will(Return.Value(true));
            Expect.Once.On(injector).Method("InjectProperties").With(testObject, container);

            ITestObject other = container.GetService<ITestObject>();

            Assert.AreSame(other, testObject);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer (Senior) Readify
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions