Add RemoveType methods of PageConventionCollection

This commit is contained in:
Pranav K 2017-06-30 16:27:51 -07:00
parent bc4328de16
commit 7c5a16c105
2 changed files with 87 additions and 0 deletions

View File

@ -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<IPageConvention>
{
/// <summary>
/// Initializes a new instance of the <see cref="PageConventionCollection"/> class that is empty.
/// </summary>
public PageConventionCollection()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="PageConventionCollection"/> class
/// as a wrapper for the specified list.
/// </summary>
/// <param name="conventions">The list that is wrapped by the new collection.</param>
public PageConventionCollection(IList<IPageConvention> conventions)
: base(conventions)
{
}
/// <summary>
/// Creates and adds an <see cref="IPageApplicationModelConvention"/> that invokes an action on the
/// <see cref="PageApplicationModel"/> for the page with the speciifed name.
@ -87,6 +105,31 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationModels
return Add(new FolderRouteModelConvention(folderPath, action));
}
/// <summary>
/// Removes all <see cref="IPageConvention"/> instances of the specified type.
/// </summary>
/// <typeparam name="TPageConvention">The type to remove.</typeparam>
public void RemoveType<TPageConvention>() where TPageConvention : IPageConvention
{
RemoveType(typeof(TPageConvention));
}
/// <summary>
/// Removes all <see cref="IPageConvention"/> instances of the specified type.
/// </summary>
/// <param name="pageConventionType">The type to remove.</param>
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)
{

View File

@ -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<BarPageConvention>(convention));
}
[Fact]
public void GenericRemoveType_RemovesAllOfType()
{
// Arrange
var collection = new PageConventionCollection
{
new FooPageConvention(),
new BarPageConvention(),
new FooPageConvention()
};
// Act
collection.RemoveType<FooPageConvention>();
// Assert
Assert.Collection(
collection,
convention => Assert.IsType<BarPageConvention>(convention));
}
private class FooPageConvention : IPageConvention { }
private class BarPageConvention : IPageConvention { }
}
}