diff --git a/src/Microsoft.AspNet.Mvc.Razor/MvcRazorMvcViewOptionsSetup.cs b/src/Microsoft.AspNet.Mvc.Razor/MvcRazorMvcViewOptionsSetup.cs index 5e9cf99e52..4f9a540504 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/MvcRazorMvcViewOptionsSetup.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/MvcRazorMvcViewOptionsSetup.cs @@ -1,21 +1,39 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; +using Microsoft.Framework.DependencyInjection; +using Microsoft.Framework.Internal; using Microsoft.Framework.OptionsModel; namespace Microsoft.AspNet.Mvc.Razor { + /// + /// Configures to use . + /// public class MvcRazorMvcViewOptionsSetup : ConfigureOptions { - public MvcRazorMvcViewOptionsSetup() - : base(ConfigureMvc) + /// + /// Initializes a new instance of . + /// + /// The application's . + public MvcRazorMvcViewOptionsSetup(IServiceProvider serviceProvider) + : base(options => ConfigureMvc(serviceProvider, options)) { Order = DefaultOrder.DefaultFrameworkSortOrder; } - public static void ConfigureMvc(MvcViewOptions options) + /// + /// Configures to use . + /// + /// The application's . + /// The to configure. + public static void ConfigureMvc( + [NotNull] IServiceProvider serviceProvider, + [NotNull] MvcViewOptions options) { - options.ViewEngines.Add(typeof(RazorViewEngine)); + var razorViewEngine = ActivatorUtilities.CreateInstance(serviceProvider); + options.ViewEngines.Add(razorViewEngine); } } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.ViewFeatures/DependencyInjection/MvcViewFeaturesMvcBuilderExtensions.cs b/src/Microsoft.AspNet.Mvc.ViewFeatures/DependencyInjection/MvcViewFeaturesMvcBuilderExtensions.cs index 5e500da126..b49c61e781 100644 --- a/src/Microsoft.AspNet.Mvc.ViewFeatures/DependencyInjection/MvcViewFeaturesMvcBuilderExtensions.cs +++ b/src/Microsoft.AspNet.Mvc.ViewFeatures/DependencyInjection/MvcViewFeaturesMvcBuilderExtensions.cs @@ -55,9 +55,7 @@ internal static void AddViewServices(IServiceCollection services) // // View Engine and related infrastructure // - // The provider is inexpensive to initialize and provides ViewEngines that may require request - // specific services. - services.TryAddScoped(); + services.TryAddSingleton(); // Support for activating ViewDataDictionary services.TryAddEnumerable( diff --git a/src/Microsoft.AspNet.Mvc.ViewFeatures/MvcViewOptions.cs b/src/Microsoft.AspNet.Mvc.ViewFeatures/MvcViewOptions.cs index a7fdec120b..e6e596d559 100644 --- a/src/Microsoft.AspNet.Mvc.ViewFeatures/MvcViewOptions.cs +++ b/src/Microsoft.AspNet.Mvc.ViewFeatures/MvcViewOptions.cs @@ -19,10 +19,9 @@ public class MvcViewOptions public HtmlHelperOptions HtmlHelperOptions { get;[param: NotNull] set; } = new HtmlHelperOptions(); /// - /// Gets a list of descriptors that represent used - /// by this application. + /// Gets a list s used by this application. /// - public IList ViewEngines { get; } = new List(); + public IList ViewEngines { get; } = new List(); /// /// Gets a list of instances. diff --git a/src/Microsoft.AspNet.Mvc.ViewFeatures/MvcViewOptionsSetup.cs b/src/Microsoft.AspNet.Mvc.ViewFeatures/MvcViewOptionsSetup.cs index b5c5110213..269c88dc22 100644 --- a/src/Microsoft.AspNet.Mvc.ViewFeatures/MvcViewOptionsSetup.cs +++ b/src/Microsoft.AspNet.Mvc.ViewFeatures/MvcViewOptionsSetup.cs @@ -7,10 +7,13 @@ namespace Microsoft.AspNet.Mvc { /// - /// Sets up default options for . + /// Sets up default options for . /// public class MvcViewOptionsSetup : ConfigureOptions { + /// + /// Initializes a new instance of . + /// public MvcViewOptionsSetup() : base(ConfigureMvc) { diff --git a/src/Microsoft.AspNet.Mvc.ViewFeatures/Rendering/ViewEngine/CompositeViewEngine.cs b/src/Microsoft.AspNet.Mvc.ViewFeatures/Rendering/ViewEngine/CompositeViewEngine.cs index 2d8bc0da0f..4ce440fb9f 100644 --- a/src/Microsoft.AspNet.Mvc.ViewFeatures/Rendering/ViewEngine/CompositeViewEngine.cs +++ b/src/Microsoft.AspNet.Mvc.ViewFeatures/Rendering/ViewEngine/CompositeViewEngine.cs @@ -1,7 +1,6 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using System; using System.Collections.Generic; using System.Linq; using Microsoft.Framework.Internal; @@ -15,41 +14,14 @@ public class CompositeViewEngine : ICompositeViewEngine /// /// Initializes a new instance of . /// - /// The options accessor for . - /// As instance that creates - /// an instance of type . - /// A instance that retrieves services from the - /// service collection. - public CompositeViewEngine( - IOptions optionsAccessor, - ITypeActivatorCache typeActivatorCache, - IServiceProvider serviceProvider) + /// The options accessor for . + public CompositeViewEngine(IOptions optionsAccessor) { - var viewEngines = new List(); - foreach (var descriptor in optionsAccessor.Options.ViewEngines) - { - IViewEngine viewEngine; - if (descriptor.ViewEngine != null) - { - viewEngine = descriptor.ViewEngine; - } - else - { - viewEngine = typeActivatorCache.CreateInstance( - serviceProvider, - descriptor.ViewEngineType); - } - - viewEngines.Add(viewEngine); - } - - ViewEngines = viewEngines; + ViewEngines = optionsAccessor.Options.ViewEngines.ToArray(); } - /// - /// Gets the list of this instance of delegates to. - /// - public IReadOnlyList ViewEngines { get; } + /// + public IReadOnlyList ViewEngines { get; } /// public ViewEngineResult FindPartialView([NotNull] ActionContext context, diff --git a/src/Microsoft.AspNet.Mvc.ViewFeatures/Rendering/ViewEngine/ICompositeViewEngine.cs b/src/Microsoft.AspNet.Mvc.ViewFeatures/Rendering/ViewEngine/ICompositeViewEngine.cs index 7213852af8..ad8fddcc73 100644 --- a/src/Microsoft.AspNet.Mvc.ViewFeatures/Rendering/ViewEngine/ICompositeViewEngine.cs +++ b/src/Microsoft.AspNet.Mvc.ViewFeatures/Rendering/ViewEngine/ICompositeViewEngine.cs @@ -1,6 +1,8 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System.Collections.Generic; + namespace Microsoft.AspNet.Mvc.Rendering { /// @@ -8,5 +10,10 @@ namespace Microsoft.AspNet.Mvc.Rendering /// public interface ICompositeViewEngine : IViewEngine { + /// + /// Gets the list of this instance of delegates + /// to. + /// + IReadOnlyList ViewEngines { get; } } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewEngineDescriptor.cs b/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewEngineDescriptor.cs deleted file mode 100644 index 5d78e60164..0000000000 --- a/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewEngineDescriptor.cs +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Reflection; -using Microsoft.AspNet.Mvc.Rendering; -using Microsoft.AspNet.Mvc.ViewFeatures; -using Microsoft.Framework.Internal; - -namespace Microsoft.AspNet.Mvc -{ - /// - /// Encapsulates information that describes an . - /// - public class ViewEngineDescriptor - { - /// - /// Creates a new instance of . - /// - /// The type that the descriptor represents. - public ViewEngineDescriptor([NotNull] Type type) - { - if (!typeof(IViewEngine).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo())) - { - var message = Resources.FormatTypeMustDeriveFromType(type.FullName, typeof(IViewEngine).FullName); - throw new ArgumentException(message, nameof(type)); - } - - ViewEngineType = type; - } - - /// - /// Creates a new instance of using the specified type. - /// - /// An instance of that the descriptor represents. - public ViewEngineDescriptor([NotNull] IViewEngine viewEngine) - { - ViewEngine = viewEngine; - ViewEngineType = viewEngine.GetType(); - } - - /// - /// Gets the type of the described by this . - /// - public Type ViewEngineType { get; } - - /// - /// Gets the instance described by this . - /// - public IViewEngine ViewEngine { get; } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewEngineDescriptorExtensions.cs b/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewEngineDescriptorExtensions.cs deleted file mode 100644 index b6e853622f..0000000000 --- a/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewEngineDescriptorExtensions.cs +++ /dev/null @@ -1,140 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using System.Linq; -using Microsoft.AspNet.Mvc.Rendering; -using Microsoft.Framework.Internal; - -namespace Microsoft.AspNet.Mvc -{ - /// - /// Extension methods for adding view engines to a descriptor collection. - /// - public static class ViewEngineDescriptorExtensions - { - /// - /// Adds a type representing a to a descriptor collection. - /// - /// A list of ViewEngineDescriptors - /// Type representing an . - /// ViewEngineDescriptor representing the added instance. - public static ViewEngineDescriptor Add( - [NotNull] this IList descriptors, - [NotNull] Type viewEngineType) - { - var descriptor = new ViewEngineDescriptor(viewEngineType); - descriptors.Add(descriptor); - return descriptor; - } - - /// - /// Inserts a type representing a to a descriptor collection. - /// - /// A list of ViewEngineDescriptors - /// Type representing an . - /// ViewEngineDescriptor representing the inserted instance. - public static ViewEngineDescriptor Insert( - [NotNull] this IList descriptors, - int index, - [NotNull] Type viewEngineType) - { - if (index < 0 || index > descriptors.Count) - { - throw new ArgumentOutOfRangeException(nameof(index)); - } - - var descriptor = new ViewEngineDescriptor(viewEngineType); - descriptors.Insert(index, descriptor); - return descriptor; - } - - /// - /// Adds an to a descriptor collection. - /// - /// A list of ViewEngineDescriptors - /// An instance. - /// ViewEngineDescriptor representing the added instance. - public static ViewEngineDescriptor Add( - [NotNull] this IList descriptors, - [NotNull] IViewEngine viewEngine) - { - var descriptor = new ViewEngineDescriptor(viewEngine); - descriptors.Add(descriptor); - return descriptor; - } - - /// - /// Insert an to a descriptor collection. - /// - /// A list of ViewEngineDescriptors - /// An instance. - /// ViewEngineDescriptor representing the added instance. - public static ViewEngineDescriptor Insert( - [NotNull] this IList descriptors, - int index, - [NotNull] IViewEngine viewEngine) - { - if (index < 0 || index > descriptors.Count) - { - throw new ArgumentOutOfRangeException(nameof(index)); - } - - var descriptor = new ViewEngineDescriptor(viewEngine); - descriptors.Insert(index, descriptor); - return descriptor; - } - - /// - /// Returns the only instance of from . - /// - /// The type of the instance to find. - /// The to search. - /// The only instance of in . - /// - /// Thrown if there is not exactly one in . - /// - public static TInstance InstanceOf( - [NotNull] this IList descriptors) - { - return descriptors - .Select(descriptor => descriptor.ViewEngine) - .OfType() - .Single(); - } - - /// - /// Returns the only instance of from - /// or null if the sequence is empty. - /// - /// The type of the instance to find. - /// The to search. - /// - /// Thrown if there is more than one in . - /// - public static TInstance InstanceOfOrDefault( - [NotNull] this IList descriptors) - { - return descriptors - .Select(descriptor => descriptor.ViewEngine) - .OfType() - .SingleOrDefault(); - } - - /// - /// Returns all instances of from . - /// - /// The type of the instances to find. - /// The to search. - /// An IEnumerable of that contains instances from - /// . - public static IEnumerable InstancesOf( - [NotNull] this IList descriptors) - { - return descriptors - .Select(descriptor => descriptor.ViewEngine) - .OfType(); - } - } -} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Test/MvcOptionsSetupTest.cs b/test/Microsoft.AspNet.Mvc.Test/MvcOptionsSetupTest.cs index 068b691f5e..459b783d21 100644 --- a/test/Microsoft.AspNet.Mvc.Test/MvcOptionsSetupTest.cs +++ b/test/Microsoft.AspNet.Mvc.Test/MvcOptionsSetupTest.cs @@ -23,8 +23,8 @@ public void Setup_SetsUpViewEngines() var options = GetOptions(); // Assert - Assert.Equal(1, options.ViewEngines.Count); - Assert.Equal(typeof(RazorViewEngine), options.ViewEngines[0].ViewEngineType); + var viewEngine = Assert.Single(options.ViewEngines); + Assert.IsType(viewEngine); } [Fact] diff --git a/test/Microsoft.AspNet.Mvc.ViewFeatures.Test/Rendering/ViewEngine/CompositeViewEngineTest.cs b/test/Microsoft.AspNet.Mvc.ViewFeatures.Test/Rendering/ViewEngine/CompositeViewEngineTest.cs index 552ffa27bd..3f891bf458 100644 --- a/test/Microsoft.AspNet.Mvc.ViewFeatures.Test/Rendering/ViewEngine/CompositeViewEngineTest.cs +++ b/test/Microsoft.AspNet.Mvc.ViewFeatures.Test/Rendering/ViewEngine/CompositeViewEngineTest.cs @@ -13,35 +13,21 @@ namespace Microsoft.AspNet.Mvc.Rendering public class CompositeViewEngineTest { [Fact] - public void ViewEngines_ReturnsInstantiatedListOfViewEngines() + public void ViewEngines_UsesListOfViewEnginesFromOptions() { // Arrange - var service = Mock.Of(); - var viewEngine = Mock.Of(); - var type = typeof(TestViewEngine); - var serviceProvider = new Mock(); - serviceProvider.Setup(p => p.GetService(typeof(ITestService))) - .Returns(service); - var typeActivatorCache = new DefaultTypeActivatorCache(); + var viewEngine1 = Mock.Of(); + var viewEngine2 = Mock.Of(); var optionsAccessor = new MockMvcViewOptionsAccessor(); - optionsAccessor.Options.ViewEngines.Add(viewEngine); - optionsAccessor.Options.ViewEngines.Add(type); - var compositeViewEngine = new CompositeViewEngine( - optionsAccessor, - typeActivatorCache, - serviceProvider.Object); + optionsAccessor.Options.ViewEngines.Add(viewEngine1); + optionsAccessor.Options.ViewEngines.Add(viewEngine2); + var compositeViewEngine = new CompositeViewEngine(optionsAccessor); // Act var result = compositeViewEngine.ViewEngines; // Assert - Assert.Collection(result, - actual => Assert.Same(viewEngine, actual), - actual => - { - var testViewEngine = Assert.IsType(actual); - Assert.Same(service, testViewEngine.Service); - }); + Assert.Equal(new[] { viewEngine1, viewEngine2 }, result); } [Fact] @@ -51,10 +37,7 @@ public void FindView_ReturnsNotFoundResult_WhenNoViewEnginesAreRegistered() var viewName = "test-view"; var actionContext = GetActionContext(); var optionsAccessor = new MockMvcViewOptionsAccessor(); - var compositeViewEngine = new CompositeViewEngine( - optionsAccessor, - Mock.Of(), - Mock.Of()); + var compositeViewEngine = new CompositeViewEngine(optionsAccessor); // Act var result = compositeViewEngine.FindView(actionContext, viewName); @@ -75,10 +58,7 @@ public void FindView_ReturnsNotFoundResult_WhenExactlyOneViewEngineIsRegisteredW .Returns(ViewEngineResult.NotFound(viewName, new[] { "controller/test-view" })); var optionsAccessor = new MockMvcViewOptionsAccessor(); optionsAccessor.Options.ViewEngines.Add(engine.Object); - var compositeViewEngine = new CompositeViewEngine( - optionsAccessor, - Mock.Of(), - Mock.Of()); + var compositeViewEngine = new CompositeViewEngine(optionsAccessor); // Act var result = compositeViewEngine.FindView(GetActionContext(), viewName); @@ -99,10 +79,7 @@ public void FindView_ReturnsView_WhenExactlyOneViewEngineIsRegisteredWhichReturn .Returns(ViewEngineResult.Found(viewName, view)); var optionsAccessor = new MockMvcViewOptionsAccessor(); optionsAccessor.Options.ViewEngines.Add(engine.Object); - var compositeViewEngine = new CompositeViewEngine( - optionsAccessor, - Mock.Of(), - Mock.Of()); + var compositeViewEngine = new CompositeViewEngine(optionsAccessor); // Act var result = compositeViewEngine.FindView(GetActionContext(), viewName); @@ -133,10 +110,7 @@ public void FindView_ReturnsViewFromFirstViewEngineWithFoundResult() optionsAccessor.Options.ViewEngines.Add(engine1.Object); optionsAccessor.Options.ViewEngines.Add(engine2.Object); optionsAccessor.Options.ViewEngines.Add(engine3.Object); - var compositeViewEngine = new CompositeViewEngine( - optionsAccessor, - Mock.Of(), - Mock.Of()); + var compositeViewEngine = new CompositeViewEngine(optionsAccessor); // Act var result = compositeViewEngine.FindView(GetActionContext(), viewName); @@ -166,10 +140,7 @@ public void FindView_ReturnsNotFound_IfAllViewEnginesReturnNotFound() optionsAccessor.Options.ViewEngines.Add(engine1.Object); optionsAccessor.Options.ViewEngines.Add(engine2.Object); optionsAccessor.Options.ViewEngines.Add(engine3.Object); - var compositeViewEngine = new CompositeViewEngine( - optionsAccessor, - Mock.Of(), - Mock.Of()); + var compositeViewEngine = new CompositeViewEngine(optionsAccessor); // Act var result = compositeViewEngine.FindView(GetActionContext(), viewName); @@ -185,10 +156,7 @@ public void FindPartialView_ReturnsNotFoundResult_WhenNoViewEnginesAreRegistered // Arrange var viewName = "my-partial-view"; var optionsAccessor = new MockMvcViewOptionsAccessor(); - var compositeViewEngine = new CompositeViewEngine( - optionsAccessor, - Mock.Of(), - Mock.Of()); + var compositeViewEngine = new CompositeViewEngine(optionsAccessor); // Act var result = compositeViewEngine.FindPartialView(GetActionContext(), viewName); @@ -208,10 +176,7 @@ public void FindPartialView_ReturnsNotFoundResult_WhenExactlyOneViewEngineIsRegi .Returns(ViewEngineResult.NotFound(viewName, new[] { "Shared/partial-view" })); var optionsAccessor = new MockMvcViewOptionsAccessor(); optionsAccessor.Options.ViewEngines.Add(engine.Object); - var compositeViewEngine = new CompositeViewEngine( - optionsAccessor, - Mock.Of(), - Mock.Of()); + var compositeViewEngine = new CompositeViewEngine(optionsAccessor); // Act var result = compositeViewEngine.FindPartialView(GetActionContext(), viewName); @@ -232,10 +197,7 @@ public void FindPartialView_ReturnsView_WhenExactlyOneViewEngineIsRegisteredWhic .Returns(ViewEngineResult.Found(viewName, view)); var optionsAccessor = new MockMvcViewOptionsAccessor(); optionsAccessor.Options.ViewEngines.Add(engine.Object); - var compositeViewEngine = new CompositeViewEngine( - optionsAccessor, - Mock.Of(), - Mock.Of()); + var compositeViewEngine = new CompositeViewEngine(optionsAccessor); // Act var result = compositeViewEngine.FindPartialView(GetActionContext(), viewName); @@ -266,10 +228,7 @@ public void FindPartialView_ReturnsViewFromFirstViewEngineWithFoundResult() optionsAccessor.Options.ViewEngines.Add(engine1.Object); optionsAccessor.Options.ViewEngines.Add(engine2.Object); optionsAccessor.Options.ViewEngines.Add(engine3.Object); - var compositeViewEngine = new CompositeViewEngine( - optionsAccessor, - Mock.Of(), - Mock.Of()); + var compositeViewEngine = new CompositeViewEngine(optionsAccessor); // Act var result = compositeViewEngine.FindPartialView(GetActionContext(), viewName); @@ -299,10 +258,7 @@ public void FindPartialView_ReturnsNotFound_IfAllViewEnginesReturnNotFound() optionsAccessor.Options.ViewEngines.Add(engine1.Object); optionsAccessor.Options.ViewEngines.Add(engine2.Object); optionsAccessor.Options.ViewEngines.Add(engine3.Object); - var compositeViewEngine = new CompositeViewEngine( - optionsAccessor, - Mock.Of(), - Mock.Of()); + var compositeViewEngine = new CompositeViewEngine(optionsAccessor); // Act var result = compositeViewEngine.FindPartialView(GetActionContext(), viewName); diff --git a/test/Microsoft.AspNet.Mvc.ViewFeatures.Test/ViewEngineDescriptorExtensionsTest.cs b/test/Microsoft.AspNet.Mvc.ViewFeatures.Test/ViewEngineDescriptorExtensionsTest.cs deleted file mode 100644 index f4563805a7..0000000000 --- a/test/Microsoft.AspNet.Mvc.ViewFeatures.Test/ViewEngineDescriptorExtensionsTest.cs +++ /dev/null @@ -1,210 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using System.Linq; -using Microsoft.AspNet.Mvc.Rendering; -using Moq; -using Xunit; - -namespace Microsoft.AspNet.Mvc -{ - public class ViewEngineDescriptorExtensionsTest - { - [Theory] - [InlineData(-1)] - [InlineData(5)] - public void Insert_WithType_ThrowsIfIndexIsOutOfBounds(int index) - { - // Arrange - var collection = new List - { - new ViewEngineDescriptor(Mock.Of()), - new ViewEngineDescriptor(Mock.Of()) - }; - - // Act & Assert - Assert.Throws("index", () => collection.Insert(index, typeof(IViewEngine))); - } - - [Theory] - [InlineData(-2)] - [InlineData(3)] - public void Insert_WithInstance_ThrowsIfIndexIsOutOfBounds(int index) - { - // Arrange - var collection = new List - { - new ViewEngineDescriptor(Mock.Of()), - new ViewEngineDescriptor(Mock.Of()) - }; - var viewEngine = Mock.Of(); - - // Act & Assert - Assert.Throws("index", () => collection.Insert(index, viewEngine)); - } - - [InlineData] - public void ViewEngineDescriptors_AddsTypesAndInstances() - { - // Arrange - var viewEngine = Mock.Of(); - var type = typeof(TestViewEngine); - var collection = new List(); - - // Act - collection.Add(viewEngine); - collection.Insert(0, type); - - // Assert - Assert.Equal(2, collection.Count); - Assert.IsType(collection[0].ViewEngine); - Assert.Same(viewEngine, collection[0].ViewEngineType); - } - - [Fact] - public void InputviewEngines_InstanceOf_ThrowsInvalidOperationExceptionIfMoreThanOnceInstance() - { - // Arrange - var viewEngines = new MvcViewOptions().ViewEngines; - viewEngines.Add(new TestViewEngine()); - viewEngines.Add(Mock.Of()); - viewEngines.Add(new TestViewEngine()); - - // Act & Assert - Assert.Throws(() => viewEngines.InstanceOf()); - } - - [Fact] - public void InputviewEngines_InstanceOf_ThrowsInvalidOperationExceptionIfNoInstance() - { - // Arrange - var viewEngines = new MvcViewOptions().ViewEngines; - viewEngines.Add(Mock.Of()); - viewEngines.Add(typeof(TestViewEngine)); - - // Act & Assert - Assert.Throws(() => viewEngines.InstanceOf()); - } - - [Fact] - public void InputviewEngines_InstanceOf_ReturnsInstanceOfIInputFormatterIfOneExists() - { - // Arrange - var viewEngines = new MvcViewOptions().ViewEngines; - viewEngines.Add(Mock.Of()); - var testEngine = new TestViewEngine(); - viewEngines.Add(testEngine); - viewEngines.Add(typeof(TestViewEngine)); - - // Act - var formatter = viewEngines.InstanceOf(); - - // Assert - Assert.NotNull(formatter); - Assert.IsType(formatter); - Assert.Same(testEngine, formatter); - } - - [Fact] - public void InputviewEngines_InstanceOfOrDefault_ThrowsInvalidOperationExceptionIfMoreThanOnceInstance() - { - // Arrange - var viewEngines = new MvcViewOptions().ViewEngines; - viewEngines.Add(new TestViewEngine()); - viewEngines.Add(Mock.Of()); - viewEngines.Add(new TestViewEngine()); - - // Act & Assert - Assert.Throws(() => viewEngines.InstanceOfOrDefault()); - } - - [Fact] - public void InputviewEngines_InstanceOfOrDefault_ReturnsNullIfNoInstance() - { - // Arrange - var viewEngines = new MvcViewOptions().ViewEngines; - viewEngines.Add(Mock.Of()); - viewEngines.Add(typeof(TestViewEngine)); - - // Act - var formatter = viewEngines.InstanceOfOrDefault(); - - // Assert - Assert.Null(formatter); - } - - [Fact] - public void InputviewEngines_InstanceOfOrDefault_ReturnsInstanceOfIInputFormatterIfOneExists() - { - // Arrange - var viewEngines = new MvcViewOptions().ViewEngines; - viewEngines.Add(Mock.Of()); - viewEngines.Add(typeof(TestViewEngine)); - var testEngine = new TestViewEngine(); - viewEngines.Add(testEngine); - - // Act - var formatter = viewEngines.InstanceOfOrDefault(); - - // Assert - Assert.NotNull(formatter); - Assert.IsType(formatter); - Assert.Same(testEngine, formatter); - } - - [Fact] - public void InputviewEngines_InstancesOf_ReturnsEmptyCollectionIfNoneExist() - { - // Arrange - var viewEngines = new MvcViewOptions().ViewEngines; - viewEngines.Add(Mock.Of()); - viewEngines.Add(typeof(TestViewEngine)); - - // Act - var result = viewEngines.InstancesOf(); - - // Assert - Assert.Empty(result); - } - - [Fact] - public void InputviewEngines_InstancesOf_ReturnsNonEmptyCollectionIfSomeExist() - { - // Arrange - var viewEngines = new MvcViewOptions().ViewEngines; - viewEngines.Add(typeof(TestViewEngine)); - var viewEngine1 = new TestViewEngine(); - var viewEngine2 = Mock.Of(); - var viewEngine3 = new TestViewEngine(); - var viewEngine4 = Mock.Of(); - viewEngines.Add(viewEngine1); - viewEngines.Add(viewEngine2); - viewEngines.Add(viewEngine3); - viewEngines.Add(viewEngine4); - - var expectedviewEngines = new List { viewEngine1, viewEngine3 }; - - // Act - var result = viewEngines.InstancesOf().ToList(); - - // Assert - Assert.NotEmpty(result); - Assert.Equal(result, expectedviewEngines); - } - - private class TestViewEngine : IViewEngine - { - public ViewEngineResult FindPartialView(ActionContext context, string partialViewName) - { - throw new NotImplementedException(); - } - - public ViewEngineResult FindView(ActionContext context, string viewName) - { - throw new NotImplementedException(); - } - } - } -} diff --git a/test/Microsoft.AspNet.Mvc.ViewFeatures.Test/ViewEngineDescriptorTest.cs b/test/Microsoft.AspNet.Mvc.ViewFeatures.Test/ViewEngineDescriptorTest.cs deleted file mode 100644 index 331c0324bf..0000000000 --- a/test/Microsoft.AspNet.Mvc.ViewFeatures.Test/ViewEngineDescriptorTest.cs +++ /dev/null @@ -1,67 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using Microsoft.AspNet.Mvc.Rendering; -using Microsoft.AspNet.Testing; -using Xunit; - -namespace Microsoft.AspNet.Mvc.OptionDescriptors -{ - public class ViewEngineDescriptorTest - { - [Fact] - public void ConstructorThrows_IfTypeIsNotViewEngine() - { - // Arrange - var viewEngineType = typeof(IViewEngine).FullName; - var type = typeof(string); - var expected = string.Format("The type '{0}' must derive from '{1}'.", - type.FullName, viewEngineType); - - // Act & Assert - ExceptionAssert.ThrowsArgument(() => new ViewEngineDescriptor(type), "type", expected); - } - - [Fact] - public void ConstructorSetsViewEngineType() - { - // Arrange - var type = typeof(TestViewEngine); - - // Act - var descriptor = new ViewEngineDescriptor(type); - - // Assert - Assert.Equal(type, descriptor.ViewEngineType); - Assert.Null(descriptor.ViewEngine); - } - - [Fact] - public void ConstructorSetsViewEngineAndViewEngineType() - { - // Arrange - var viewEngine = new TestViewEngine(); - - // Act - var descriptor = new ViewEngineDescriptor(viewEngine); - - // Assert - Assert.Same(viewEngine, descriptor.ViewEngine); - Assert.Equal(viewEngine.GetType(), descriptor.ViewEngineType); - } - - private class TestViewEngine : IViewEngine - { - public ViewEngineResult FindPartialView(ActionContext context, string partialViewName) - { - throw new NotImplementedException(); - } - - public ViewEngineResult FindView(ActionContext context, string viewName) - { - throw new NotImplementedException(); - } - } - } -} \ No newline at end of file diff --git a/test/WebSites/CompositeViewEngineWebSite/Startup.cs b/test/WebSites/CompositeViewEngineWebSite/Startup.cs index 1d5ba04d61..5ecf695c1b 100644 --- a/test/WebSites/CompositeViewEngineWebSite/Startup.cs +++ b/test/WebSites/CompositeViewEngineWebSite/Startup.cs @@ -2,7 +2,6 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using Microsoft.AspNet.Builder; -using Microsoft.AspNet.Mvc; using Microsoft.Framework.DependencyInjection; namespace CompositeViewEngineWebSite @@ -16,7 +15,7 @@ public void ConfigureServices(IServiceCollection services) services.AddMvc() .ConfigureMvcViews(options => { - options.ViewEngines.Insert(0, typeof(TestViewEngine)); + options.ViewEngines.Insert(0, new TestViewEngine()); }); }