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
20 changes: 20 additions & 0 deletions Extensions.Test/CollectionExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,26 @@
CollectionAssert.AreEqual(new List<string?> { "a", null, "b", null }, collection);
}

[TestMethod]
public void AddFromItselfDoublesTheCollection()
{
List<int> collection = [1, 2];

collection.AddFrom(collection);

CollectionAssert.AreEqual(new List<int> { 1, 2, 1, 2 }, collection);

Check warning on line 107 in Extensions.Test/CollectionExtensionsTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.AreSequenceEqual' instead of 'CollectionAssert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_Extensions&issues=AaFLLbf1Av_O3Ug5T5J-&open=AaFLLbf1Av_O3Ug5T5J-&pullRequest=168
}

[TestMethod]
public void AddFromALazyQueryOverItselfAddsTheQueriedItems()
{
List<int> collection = [1, 2];

collection.AddFrom(collection.Where(x => x > 0));

CollectionAssert.AreEqual(new List<int> { 1, 2, 1, 2 }, collection);

Check warning on line 117 in Extensions.Test/CollectionExtensionsTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.AreSequenceEqual' instead of 'CollectionAssert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_Extensions&issues=AaFLLbf1Av_O3Ug5T5J_&open=AaFLLbf1Av_O3Ug5T5J_&pullRequest=168
}

[TestMethod]
public void AnyNullEmptyCollectionReturnsFalse()
{
Expand Down
5 changes: 4 additions & 1 deletion Extensions/CollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ public static void AddFrom<T>(this ICollection<T> collection, IEnumerable<T> ite
}
#pragma warning restore KTSU0004 // Use Ensure.NotNull instead of manual null check

foreach (T? item in items)
// Materialize first: items may be the collection itself or a lazy query over it, whose enumerator
// the first Add would invalidate
T[] snapshot = [.. items];
foreach (T? item in snapshot)
{
collection.Add(item);
}
Expand Down
Loading