From 7c5a16c1059b45b2652ea903d19b57c763471a9c Mon Sep 17 00:00:00 2001 From: Pranav K Date: Fri, 30 Jun 2017 16:27:51 -0700 Subject: [PATCH] Add RemoveType methods of PageConventionCollection --- .../PageConventionCollection.cs | 43 ++++++++++++++++++ .../PageConventionCollectionTest.cs | 44 +++++++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/src/Microsoft.AspNetCore.Mvc.RazorPages/ApplicationModels/PageConventionCollection.cs b/src/Microsoft.AspNetCore.Mvc.RazorPages/ApplicationModels/PageConventionCollection.cs index 51c36fcad0..816ce5861e 100644 --- a/src/Microsoft.AspNetCore.Mvc.RazorPages/ApplicationModels/PageConventionCollection.cs +++ b/src/Microsoft.AspNetCore.Mvc.RazorPages/ApplicationModels/PageConventionCollection.cs @@ -2,6 +2,7 @@ // 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.Collections.ObjectModel; using Microsoft.AspNetCore.Mvc.RazorPages; @@ -9,6 +10,23 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationModels { public class PageConventionCollection : Collection { + /// + /// Initializes a new instance of the class that is empty. + /// + public PageConventionCollection() + { + } + + /// + /// Initializes a new instance of the class + /// as a wrapper for the specified list. + /// + /// The list that is wrapped by the new collection. + public PageConventionCollection(IList conventions) + : base(conventions) + { + } + /// /// Creates and adds an that invokes an action on the /// for the page with the speciifed name. @@ -87,6 +105,31 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationModels return Add(new FolderRouteModelConvention(folderPath, action)); } + /// + /// Removes all instances of the specified type. + /// + /// The type to remove. + public void RemoveType() where TPageConvention : IPageConvention + { + RemoveType(typeof(TPageConvention)); + } + + /// + /// Removes all instances of the specified type. + /// + /// The type to remove. + public void RemoveType(Type pageConventionType) + { + for (var i = Count - 1; i >= 0; i--) + { + var pageConvention = this[i]; + if (pageConvention.GetType() == pageConventionType) + { + RemoveAt(i); + } + } + } + // Internal for unit testing internal static void EnsureValidPageName(string pageName) { diff --git a/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/ApplicationModels/PageConventionCollectionTest.cs b/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/ApplicationModels/PageConventionCollectionTest.cs index ee6b4603a1..c1bc7e1367 100644 --- a/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/ApplicationModels/PageConventionCollectionTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.RazorPages.Test/ApplicationModels/PageConventionCollectionTest.cs @@ -72,5 +72,49 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationModels "folderPath", "Path must be a root relative path that starts with a forward slash '/'."); } + + [Fact] + public void RemoveType_RemovesAllOfType() + { + // Arrange + var collection = new PageConventionCollection + { + new FooPageConvention(), + new BarPageConvention(), + new FooPageConvention() + }; + + // Act + collection.RemoveType(typeof(FooPageConvention)); + + // Assert + Assert.Collection( + collection, + convention => Assert.IsType(convention)); + } + + [Fact] + public void GenericRemoveType_RemovesAllOfType() + { + // Arrange + var collection = new PageConventionCollection + { + new FooPageConvention(), + new BarPageConvention(), + new FooPageConvention() + }; + + // Act + collection.RemoveType(); + + // Assert + Assert.Collection( + collection, + convention => Assert.IsType(convention)); + } + + private class FooPageConvention : IPageConvention { } + + private class BarPageConvention : IPageConvention { } } }