From ead7115cbe76b34061f83ba38f94a9c213922043 Mon Sep 17 00:00:00 2001 From: Nils Andresen Date: Fri, 10 Nov 2023 22:59:05 +0100 Subject: [PATCH] (#1313) Add new test to TypeRegistrarBaseTests to assert the assumptions we're making in the code. --- .../Cli/TypeRegistrarBaseTests.cs | 84 ++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/src/Spectre.Console.Testing/Cli/TypeRegistrarBaseTests.cs b/src/Spectre.Console.Testing/Cli/TypeRegistrarBaseTests.cs index b1baa38..753c4d2 100644 --- a/src/Spectre.Console.Testing/Cli/TypeRegistrarBaseTests.cs +++ b/src/Spectre.Console.Testing/Cli/TypeRegistrarBaseTests.cs @@ -23,13 +23,16 @@ public sealed class TypeRegistrarBaseTests /// This exception is raised, if a test fails. public void RunAllTests() { - var testCases = new Action[] + var testCases = new[] { RegistrationsCanBeResolved, InstanceRegistrationsCanBeResolved, LazyRegistrationsCanBeResolved, ResolvingNotRegisteredServiceReturnsNull, ResolvingNullTypeReturnsNull, + ResolvingSingleInstanceOfMultipleRegistrationsResolvesTheFirstOne, + ResolvingAnEnumerableOfInstancesDoesNotReturnNull, + ResolvingAnEnumerableOfInstancesOfMultipleRegistrationsResolvesAllRegistrations, }; foreach (var test in testCases) @@ -38,6 +41,80 @@ public sealed class TypeRegistrarBaseTests } } + private void ResolvingAnEnumerableOfInstancesDoesNotReturnNull(ITypeRegistrar registrar) + { + // Given + var resolver = registrar.Build(); + + // When + var actual = resolver.Resolve(typeof(IEnumerable)) as IEnumerable; + + // Then + if (actual == null) + { + throw new TestFailedException( + "Expected an IEnumerable never to resolve to null."); + } + } + + private void ResolvingAnEnumerableOfInstancesOfMultipleRegistrationsResolvesAllRegistrations(ITypeRegistrar registrar) + { + // Given + var theLastRegistration = new AnotherMockService("last"); + registrar.RegisterLazy(typeof(IMockService), () => new AnotherMockService("first")); + registrar.Register(typeof(IMockService), typeof(MockService)); + registrar.RegisterInstance(typeof(IMockService), theLastRegistration); + var resolver = registrar.Build(); + + // When + var actual = (resolver.Resolve(typeof(IEnumerable)) as IEnumerable)!.ToList(); + + // Then + if (actual.Count != 3) + { + throw new TestFailedException( + "Expected the resolver to resolve a list with exactly 3 elements."); + } + + if (actual.Count(x => x.GetType() == typeof(AnotherMockService)) != 2) + { + throw new TestFailedException( + $"Expected the resolver to resolve a list with exactly 2 elements of type {nameof(AnotherMockService)}."); + } + + if (actual.Count(x => x.GetType() == typeof(MockService)) != 1) + { + throw new TestFailedException( + $"Expected the resolver to resolve a list with exactly one element of type {nameof(MockService)}."); + } + + if (!actual.Contains(theLastRegistration)) + { + throw new TestFailedException( + "Expected the resolver to resolve the known instance that was registered."); + } + } + + private void ResolvingSingleInstanceOfMultipleRegistrationsResolvesTheFirstOne(ITypeRegistrar registrar) + { + // Given + var theLastRegistration = new AnotherMockService("last"); + registrar.RegisterLazy(typeof(IMockService), () => new AnotherMockService("first")); + registrar.Register(typeof(IMockService), typeof(MockService)); + registrar.RegisterInstance(typeof(IMockService), theLastRegistration); + var resolver = registrar.Build(); + + // When + var actual = resolver.Resolve(typeof(IMockService)); + + // Then + if (!ReferenceEquals(actual, theLastRegistration)) + { + throw new TestFailedException( + "Expected the resolver to resolve the first registered instance of multiple registrations."); + } + } + private static void ResolvingNullTypeReturnsNull(ITypeRegistrar registrar) { // Given no registration @@ -167,6 +244,11 @@ public sealed class TypeRegistrarBaseTests { } + private class AnotherMockService : IMockService + { + public AnotherMockService(string _){} + } + /// /// Exception, to be raised when a test fails. ///