Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions PowerKit.Tests/CollectionExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,58 @@ namespace PowerKit.Tests;

public class CollectionExtensionsTests
{
[Fact]
public void AddRange_Test()
{
// Arrange
var collection = (ICollection<int>)new List<int> { 1, 2, 3 };

// Act
collection.AddRange([4, 5, 6]);

// Assert
collection.Should().Equal(1, 2, 3, 4, 5, 6);
}

[Fact]
public void AddRange_SameCollection_Test()
{
// Arrange
var collection = (ICollection<int>)new List<int> { 1, 2, 3 };

// Act
collection.AddRange(collection);

// Assert
collection.Should().Equal(1, 2, 3, 1, 2, 3);
}

[Fact]
public void RemoveRange_Test()
{
// Arrange
var collection = (ICollection<int>)new List<int> { 1, 2, 3, 4, 5 };

// Act
collection.RemoveRange([2, 4]);

// Assert
collection.Should().Equal(1, 3, 5);
}
Comment thread
Tyrrrz marked this conversation as resolved.

[Fact]
public void RemoveRange_SameCollection_Test()
{
// Arrange
var collection = (ICollection<int>)new List<int> { 1, 2, 3, 4, 5 };

// Act
collection.RemoveRange(collection);

// Assert
collection.Should().BeEmpty();
}

[Fact]
public void RemoveAll_Test()
{
Expand Down
18 changes: 18 additions & 0 deletions PowerKit/Extensions/CollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@ internal static class CollectionExtensions
{
extension<T>(ICollection<T> source)
{
/// <summary>
/// Adds all elements from the specified sequence to the collection.
/// </summary>
public void AddRange(IEnumerable<T> items)
{
foreach (var item in items.ToArray())
source.Add(item);
}

/// <summary>
/// Removes all elements in the specified sequence from the collection.
/// </summary>
public void RemoveRange(IEnumerable<T> items)
{
foreach (var item in items.ToArray())
source.Remove(item);
}

/// <summary>
/// Removes all elements from the collection that match the specified predicate.
/// </summary>
Expand Down