Browse Source

Added InsertAfter extension method.

pull/880/head
Halil ibrahim Kalkan 8 years ago
parent
commit
fb656134d8
  1. 23
      framework/src/Volo.Abp.Core/System/Collections/Generic/AbpListExtensions.cs
  2. 48
      framework/test/Volo.Abp.Core.Tests/System/Collections/Generic/AbpListExtensions_Tests.cs

23
framework/src/Volo.Abp.Core/System/Collections/Generic/AbpListExtensions.cs

@ -32,6 +32,18 @@ namespace System.Collections.Generic
source.Insert(source.Count, item); source.Insert(source.Count, item);
} }
public static void InsertAfter<T>(this IList<T> source, T existingItem, T item)
{
var index = source.IndexOf(existingItem);
if (index < 0)
{
source.AddFirst(item);
return;
}
source.Insert(index + 1, item);
}
public static void InsertAfter<T>(this IList<T> source, Predicate<T> selector, T item) public static void InsertAfter<T>(this IList<T> source, Predicate<T> selector, T item)
{ {
var index = source.FindIndex(selector); var index = source.FindIndex(selector);
@ -44,6 +56,17 @@ namespace System.Collections.Generic
source.Insert(index + 1, item); source.Insert(index + 1, item);
} }
public static void InsertBefore<T>(this IList<T> source, T existingItem, T item)
{
var index = source.IndexOf(existingItem);
if (index < 0)
{
source.AddLast(item);
return;
}
source.Insert(index, item);
}
public static void InsertBefore<T>(this IList<T> source, Predicate<T> selector, T item) public static void InsertBefore<T>(this IList<T> source, Predicate<T> selector, T item)
{ {

48
framework/test/Volo.Abp.Core.Tests/System/Collections/Generic/AbpListExtensions_Tests.cs

@ -11,6 +11,29 @@ namespace System.Collections.Generic
{ {
var list = Enumerable.Range(1, 3).ToList(); var list = Enumerable.Range(1, 3).ToList();
list.InsertAfter(2, 42);
list.Count.ShouldBe(4);
list[0].ShouldBe(1);
list[1].ShouldBe(2);
list[2].ShouldBe(42);
list[3].ShouldBe(3);
list.InsertAfter(3, 43);
list.Count.ShouldBe(5);
list[0].ShouldBe(1);
list[1].ShouldBe(2);
list[2].ShouldBe(42);
list[3].ShouldBe(3);
list[4].ShouldBe(43);
}
[Fact]
public void InsertAfter_With_Predicate()
{
var list = Enumerable.Range(1, 3).ToList();
list.InsertAfter(i => i == 2, 42); list.InsertAfter(i => i == 2, 42);
list.Count.ShouldBe(4); list.Count.ShouldBe(4);
@ -30,7 +53,7 @@ namespace System.Collections.Generic
} }
[Fact] [Fact]
public void InsertAfter_Should_Insert_To_First_If_Not_Found() public void InsertAfter_With_Predicate_Should_Insert_To_First_If_Not_Found()
{ {
var list = Enumerable.Range(1, 3).ToList(); var list = Enumerable.Range(1, 3).ToList();
@ -48,6 +71,29 @@ namespace System.Collections.Generic
{ {
var list = Enumerable.Range(1, 3).ToList(); var list = Enumerable.Range(1, 3).ToList();
list.InsertBefore(2, 42);
list.Count.ShouldBe(4);
list[0].ShouldBe(1);
list[1].ShouldBe(42);
list[2].ShouldBe(2);
list[3].ShouldBe(3);
list.InsertBefore(1, 43);
list.Count.ShouldBe(5);
list[0].ShouldBe(43);
list[1].ShouldBe(1);
list[2].ShouldBe(42);
list[3].ShouldBe(2);
list[4].ShouldBe(3);
}
[Fact]
public void InsertBefore_With_Predicate()
{
var list = Enumerable.Range(1, 3).ToList();
list.InsertBefore(i => i == 2, 42); list.InsertBefore(i => i == 2, 42);
list.Count.ShouldBe(4); list.Count.ShouldBe(4);

Loading…
Cancel
Save