// Author: Simon Gockner // Created: 2019-12-07 // Copyright(c) 2019 SimonG. All Rights Reserved. using JetBrains.Annotations; using LightweightIocContainer; 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 IocContainer _container; [SetUp] public void SetUp() => _container = new IocContainer(); [TearDown] public void TearDown() => _container.Dispose(); [Test] public void TestRegistrationOnCreate2() { _container.Register(r => r.Add().OnCreate(foo => foo.ThrowFoo())); Exception fooException = Assert.Throws(() => _container.Resolve()); Assert.That(fooException?.Message, Is.EqualTo("Foo")); Exception barException = Assert.Throws(() => _container.Resolve()); Assert.That(barException?.Message, Is.EqualTo("Foo")); } [Test] public void TestRegistrationOnCreate3() { _container.Register(r => r.Add().OnCreate(foo => foo.ThrowFoo())); Exception fooException = Assert.Throws(() => _container.Resolve()); Assert.That(fooException?.Message, Is.EqualTo("Foo")); Exception barException = Assert.Throws(() => _container.Resolve()); Assert.That(barException?.Message, Is.EqualTo("Foo")); Exception anotherFooException = Assert.Throws(() => _container.Resolve()); Assert.That(anotherFooException?.Message, Is.EqualTo("Foo")); } [Test] public void TestRegistrationOnCreate4() { _container.Register(r => r.Add().OnCreate(foo => foo.ThrowFoo())); Exception fooException = Assert.Throws(() => _container.Resolve()); Assert.That(fooException?.Message, Is.EqualTo("Foo")); Exception barException = Assert.Throws(() => _container.Resolve()); Assert.That(barException?.Message, Is.EqualTo("Foo")); Exception anotherFooException = Assert.Throws(() => _container.Resolve()); Assert.That(anotherFooException?.Message, Is.EqualTo("Foo")); Exception anotherBarException = Assert.Throws(() => _container.Resolve()); Assert.That(anotherBarException?.Message, Is.EqualTo("Foo")); } [Test] public void TestRegistrationOnCreate5() { _container.Register(r => r.Add().OnCreate(foo => foo.ThrowFoo())); Exception fooException = Assert.Throws(() => _container.Resolve()); Assert.That(fooException?.Message, Is.EqualTo("Foo")); Exception barException = Assert.Throws(() => _container.Resolve()); Assert.That(barException?.Message, Is.EqualTo("Foo")); Exception anotherFooException = Assert.Throws(() => _container.Resolve()); Assert.That(anotherFooException?.Message, Is.EqualTo("Foo")); Exception anotherBarException = Assert.Throws(() => _container.Resolve()); Assert.That(anotherBarException?.Message, Is.EqualTo("Foo")); Exception anotherOneException = Assert.Throws(() => _container.Resolve()); Assert.That(anotherOneException?.Message, Is.EqualTo("Foo")); } [Test] public void TestResolveTransient() { _container.Register(r => r.Add()); IFoo foo = _container.Resolve(); IBar bar = _container.Resolve(); IAnotherFoo anotherFoo = _container.Resolve(); IAnotherBar anotherBar = _container.Resolve(); IAnotherOne anotherOne = _container.Resolve(); Assert.That(foo, Is.InstanceOf()); Assert.That(bar, Is.InstanceOf()); Assert.That(anotherFoo, Is.InstanceOf()); Assert.That(anotherBar, Is.InstanceOf()); Assert.That(anotherOne, Is.InstanceOf()); } [Test] public void TestResolveSingleton() { _container.Register(r => r.Add(Lifestyle.Singleton)); IFoo foo = _container.Resolve(); IBar bar = _container.Resolve(); IAnotherFoo anotherFoo = _container.Resolve(); IAnotherBar anotherBar = _container.Resolve(); IAnotherOne anotherOne = _container.Resolve(); Assert.That(foo, Is.InstanceOf()); Assert.That(bar, Is.InstanceOf()); Assert.That(anotherFoo, Is.InstanceOf()); Assert.That(anotherBar, Is.InstanceOf()); Assert.That(anotherOne, Is.InstanceOf()); Assert.That(bar, Is.EqualTo(foo)); Assert.That(anotherFoo, Is.EqualTo(foo)); Assert.That(anotherBar, Is.EqualTo(foo)); Assert.That(anotherOne, Is.EqualTo(foo)); Assert.That(bar, Is.SameAs(foo)); Assert.That(anotherFoo, Is.SameAs(foo)); Assert.That(anotherBar, Is.SameAs(foo)); Assert.That(anotherOne, Is.SameAs(foo)); } }