using System; using System.Collections; using System.Collections.Generic; using System.Linq; using NUnit.Framework; using Umbraco.Core; using Umbraco.Core.ObjectResolution; namespace Umbraco.Tests.Resolvers { [TestFixture] public class LazyManyObjectResolverTests { [SetUp] public void Initialize() { } [TearDown] public void TearDown() { Resolution.Unfreeze(); } [Test] public void Ensure_Lazy_Type_Resolution() { var resolver = new LazyResolver(new[] {new Lazy(() => typeof (TransientObject3))}); resolver.AddType(); resolver.AddType(new Lazy(() => typeof(TransientObject2))); Resolution.Freeze(); Assert.IsFalse(resolver.HasResolvedTypes); var instances1 = resolver.Objects; Assert.IsTrue(resolver.HasResolvedTypes); Assert.AreEqual(3, instances1.Count()); Assert.IsTrue(instances1.Select(x => x.GetType()).ContainsAll(new []{typeof(TransientObject1), typeof(TransientObject2), typeof(TransientObject3)})); } [Test] public void Type_List_Delegates_Combination() { Func> types = () => new[] { typeof(TransientObject3), typeof(TransientObject2) }; var resolver = new LazyResolver(types); resolver.AddTypeListDelegate(() => new[] { typeof(TransientObject1)}); Resolution.Freeze(); var instances1 = resolver.Objects; Assert.AreEqual(3, instances1.Count()); Assert.IsTrue(instances1.Select(x => x.GetType()).ContainsAll(new[] { typeof(TransientObject1), typeof(TransientObject2), typeof(TransientObject3) })); } [Test] public void Type_List_Delegates_And_Lazy_Type_Combination() { Func> types = () => new[] { typeof(TransientObject3) }; var resolver = new LazyResolver(types); resolver.AddType(new Lazy(() => typeof(TransientObject2))); resolver.AddType(); Resolution.Freeze(); var instances1 = resolver.Objects; Assert.AreEqual(3, instances1.Count()); Assert.IsTrue(instances1.Select(x => x.GetType()).ContainsAll(new[] { typeof(TransientObject1), typeof(TransientObject2), typeof(TransientObject3) })); } [Test] public void Throws_If_Duplication() { Func> types = () => new[] { typeof(TransientObject3), typeof(TransientObject2), typeof(TransientObject1) }; var resolver = new LazyResolver(types); //duplicate, but will not throw here resolver.AddType(); Resolution.Freeze(); Assert.Throws(() => { var instances = resolver.Objects; }); } #region Test classes private interface ITestInterface { } private class TransientObject1 : ITestInterface { } private class TransientObject2 : ITestInterface { } private class TransientObject3 : ITestInterface { } private sealed class LazyResolver : LazyManyObjectsResolverBase { public LazyResolver() : base(ObjectLifetimeScope.Transient) { } public LazyResolver(IEnumerable> values) :base (values, ObjectLifetimeScope.Transient) { } public LazyResolver(Func> typeList) : base(typeList, ObjectLifetimeScope.Transient) { } public IEnumerable Objects { get { return Values; } } } #endregion } }