From 3eb25d84fc28a5810e10edb22ce6022bf008575c Mon Sep 17 00:00:00 2001 From: Simon Gockner Date: Sat, 14 Dec 2019 21:25:04 +0100 Subject: [PATCH] #33: add interfaceSegregationTest --- .../IocContainerInterfaceSegregationTest.cs | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 Test.LightweightIocContainer/IocContainerInterfaceSegregationTest.cs diff --git a/Test.LightweightIocContainer/IocContainerInterfaceSegregationTest.cs b/Test.LightweightIocContainer/IocContainerInterfaceSegregationTest.cs new file mode 100644 index 0000000..864aad0 --- /dev/null +++ b/Test.LightweightIocContainer/IocContainerInterfaceSegregationTest.cs @@ -0,0 +1,92 @@ +// Author: Simon Gockner +// Created: 2019-12-07 +// Copyright(c) 2019 SimonG. All Rights Reserved. + +using System; +using LightweightIocContainer; +using LightweightIocContainer.Interfaces; +using NUnit.Framework; + +namespace Test.LightweightIocContainer +{ + [TestFixture] + public class IocContainerInterfaceSegregationTest + { + #region TestClasses + + private interface IBar + { + void ThrowBar(); + } + + private interface IFoo + { + void ThrowFoo(); + } + + private class Foo : IFoo, IBar + { + public void ThrowFoo() + { + throw new Exception("Foo"); + } + + public void ThrowBar() + { + throw new Exception("Bar"); + } + } + + #endregion TestClasses + + private IIocContainer _container; + + [SetUp] + public void SetUp() + { + _container = new IocContainer(); + } + + [TearDown] + public void TearDown() + { + _container.Dispose(); + } + + [Test] + public void TestRegistrationOnCreate() + { + _container.Register().OnCreate(bar => bar.ThrowBar(), foo => foo.ThrowFoo()); + + Exception fooException = Assert.Throws(() => _container.Resolve()); + Assert.AreEqual("Foo", fooException.Message); + + Exception barException = Assert.Throws(() => _container.Resolve()); + Assert.AreEqual("Bar", barException.Message); + } + + [Test] + public void TestResolveTransient() + { + _container.Register(); + IFoo foo = _container.Resolve(); + IBar bar = _container.Resolve(); + + Assert.IsInstanceOf(foo); + Assert.IsInstanceOf(bar); + } + + [Test] + public void TestResolveSingleton() + { + _container.Register(Lifestyle.Singleton); + IFoo foo = _container.Resolve(); + IBar bar = _container.Resolve(); + + Assert.IsInstanceOf(foo); + Assert.IsInstanceOf(bar); + Assert.AreEqual(foo, bar); + Assert.AreSame(foo, bar); + } + } +} \ No newline at end of file