// Author: Simon Gockner // Created: 2019-12-07 // Copyright(c) 2019 SimonG. All Rights Reserved. using System; using JetBrains.Annotations; using LightweightIocContainer; using LightweightIocContainer.Interfaces; using NUnit.Framework; namespace Test.LightweightIocContainer { [TestFixture] public class IocContainerInterfaceSegregationTest { private interface IBar { } private interface IFoo { void ThrowFoo(); } private interface IAnotherBar { } private interface IAnotherFoo { } private interface IAnotherOne { } [UsedImplicitly] private class Foo : IFoo, IBar, IAnotherFoo, IAnotherBar, IAnotherOne { public void ThrowFoo() => throw new Exception("Foo"); } private IIocContainer _container; [SetUp] public void SetUp() => _container = new IocContainer(); [TearDown] public void TearDown() => _container.Dispose(); [Test] public void TestRegistrationOnCreate2() { _container.Register().OnCreate(foo => foo.ThrowFoo()); Exception fooException = Assert.Throws(() => _container.Resolve()); Assert.AreEqual("Foo", fooException?.Message); Exception barException = Assert.Throws(() => _container.Resolve()); Assert.AreEqual("Foo", barException?.Message); } [Test] public void TestRegistrationOnCreate3() { _container.Register().OnCreate(foo => foo.ThrowFoo()); Exception fooException = Assert.Throws(() => _container.Resolve()); Assert.AreEqual("Foo", fooException?.Message); Exception barException = Assert.Throws(() => _container.Resolve()); Assert.AreEqual("Foo", barException?.Message); Exception anotherFooException = Assert.Throws(() => _container.Resolve()); Assert.AreEqual("Foo", anotherFooException?.Message); } [Test] public void TestRegistrationOnCreate4() { _container.Register().OnCreate(foo => foo.ThrowFoo()); Exception fooException = Assert.Throws(() => _container.Resolve()); Assert.AreEqual("Foo", fooException?.Message); Exception barException = Assert.Throws(() => _container.Resolve()); Assert.AreEqual("Foo", barException?.Message); Exception anotherFooException = Assert.Throws(() => _container.Resolve()); Assert.AreEqual("Foo", anotherFooException?.Message); Exception anotherBarException = Assert.Throws(() => _container.Resolve()); Assert.AreEqual("Foo", anotherBarException?.Message); } [Test] public void TestRegistrationOnCreate5() { _container.Register().OnCreate(foo => foo.ThrowFoo()); Exception fooException = Assert.Throws(() => _container.Resolve()); Assert.AreEqual("Foo", fooException?.Message); Exception barException = Assert.Throws(() => _container.Resolve()); Assert.AreEqual("Foo", barException?.Message); Exception anotherFooException = Assert.Throws(() => _container.Resolve()); Assert.AreEqual("Foo", anotherFooException?.Message); Exception anotherBarException = Assert.Throws(() => _container.Resolve()); Assert.AreEqual("Foo", anotherBarException?.Message); Exception anotherOneException = Assert.Throws(() => _container.Resolve()); Assert.AreEqual("Foo", anotherOneException?.Message); } [Test] public void TestResolveTransient() { _container.Register(); IFoo foo = _container.Resolve(); IBar bar = _container.Resolve(); IAnotherFoo anotherFoo = _container.Resolve(); IAnotherBar anotherBar = _container.Resolve(); IAnotherOne anotherOne = _container.Resolve(); Assert.IsInstanceOf(foo); Assert.IsInstanceOf(bar); Assert.IsInstanceOf(anotherFoo); Assert.IsInstanceOf(anotherBar); Assert.IsInstanceOf(anotherOne); } [Test] public void TestResolveSingleton() { _container.Register(Lifestyle.Singleton); IFoo foo = _container.Resolve(); IBar bar = _container.Resolve(); IAnotherFoo anotherFoo = _container.Resolve(); IAnotherBar anotherBar = _container.Resolve(); IAnotherOne anotherOne = _container.Resolve(); Assert.IsInstanceOf(foo); Assert.IsInstanceOf(bar); Assert.IsInstanceOf(anotherFoo); Assert.IsInstanceOf(anotherBar); Assert.IsInstanceOf(anotherOne); Assert.AreEqual(foo, bar); Assert.AreEqual(foo, anotherFoo); Assert.AreEqual(foo, anotherBar); Assert.AreEqual(foo, anotherOne); Assert.AreSame(foo, bar); Assert.AreSame(foo, anotherFoo); Assert.AreSame(foo, anotherBar); Assert.AreSame(foo, anotherOne); } } }