Skip to content
Merged
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
27 changes: 21 additions & 6 deletions src/TestFramework/TestFramework/Assertions/Assert.Contains.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,23 +154,38 @@ public static T ContainsSingle<T>(IEnumerable<T> collection, string? message = "
/// <returns>The item that matches the predicate.</returns>
public static T ContainsSingle<T>(Func<T, bool> predicate, IEnumerable<T> collection, string? message = "", [CallerArgumentExpression(nameof(predicate))] string predicateExpression = "", [CallerArgumentExpression(nameof(collection))] string collectionExpression = "")
{
var matchingElements = collection.Where(predicate).ToList();
int actualCount = matchingElements.Count;
T firstMatch = default!;
int matchCount = 0;

if (actualCount == 1)
foreach (T item in collection)
{
return matchingElements[0];
if (!predicate(item))
{
continue;
}

if (matchCount == 0)
{
firstMatch = item;
}

matchCount++;
}

if (matchCount == 1)
{
return firstMatch;
}

if (string.IsNullOrEmpty(predicateExpression))
{
string userMessage = BuildUserMessageForCollectionExpression(message, collectionExpression);
ThrowAssertContainsSingleFailed(actualCount, userMessage);
ThrowAssertContainsSingleFailed(matchCount, userMessage);
}
else
{
string userMessage = BuildUserMessageForPredicateExpressionAndCollectionExpression(message, predicateExpression, collectionExpression);
ThrowAssertSingleMatchFailed(actualCount, userMessage);
ThrowAssertSingleMatchFailed(matchCount, userMessage);
}

// Unreachable code but compiler cannot work it out
Expand Down
Loading