// Copyright (c) .NET Foundation and contributors. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; using System.Collections.Generic; using Microsoft.Extensions.DependencyModel; using Microsoft.Extensions.DependencyModel.Resolution; using Moq; using Xunit; using FluentAssertions; namespace StreamForwarderTests { public class CompositeResolverTests { [Fact] public void ReturnsFirstSuccesfullResolve() { var fail = new Mock(); var success = new Mock(); success.Setup(r => r.TryResolveAssemblyPaths(It.IsAny(), It.IsAny>())) .Returns(true); var failTwo = new Mock(); var resolvers = new[] { fail.Object, success.Object, failTwo.Object }; var resolver = new CompositeCompilationAssemblyResolver(resolvers); var result = resolver.TryResolveAssemblyPaths(null, null); Assert.True(result); fail.Verify(r => r.TryResolveAssemblyPaths(It.IsAny(), It.IsAny>()), Times.Once()); success.Verify(r => r.TryResolveAssemblyPaths(It.IsAny(), It.IsAny>()), Times.Once()); failTwo.Verify(r => r.TryResolveAssemblyPaths(It.IsAny(), It.IsAny>()), Times.Never()); } [Fact] public void PassesLibraryToAllResolvers() { var fail = new Mock(); var failTwo = new Mock(); var resolvers = new[] { fail.Object, failTwo.Object }; var library = new CompilationLibrary(string.Empty, string.Empty, string.Empty, string.Empty, null, null, false); var resolver = new CompositeCompilationAssemblyResolver(resolvers); var result = resolver.TryResolveAssemblyPaths(library, null); fail.Verify(r => r.TryResolveAssemblyPaths(library, null), Times.Once()); failTwo.Verify(r => r.TryResolveAssemblyPaths(library, null), Times.Once()); } [Fact] public void PopulatedAssemblies() { var fail = new Mock(); var success = new Mock(); success.Setup(r => r.TryResolveAssemblyPaths(It.IsAny(), It.IsAny>())) .Returns(true) .Callback((CompilationLibrary l, List a) => { a.Add("Assembly"); }); var resolvers = new[] { fail.Object, success.Object }; var assemblies = new List(); var library = new CompilationLibrary(string.Empty, string.Empty, string.Empty, string.Empty, null, null, false); var resolver = new CompositeCompilationAssemblyResolver(resolvers); var result = resolver.TryResolveAssemblyPaths(library, assemblies); assemblies.Should().Contain("Assembly"); } } }