-
Notifications
You must be signed in to change notification settings - Fork 293
Apply structured assertion messages (RFC 012) to Assert.HasCount / IsEmpty / IsNotEmpty #8262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Evangelink
wants to merge
5
commits into
main
Choose a base branch
from
dev/amauryleve/assert-count-rfc012
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ad180be
Apply structured assertion messages (RFC 012) to Assert.HasCount / Is…
Evangelink 843346d
Fix Assert.Count call-site formatting
9ff9009
Merge origin/main into dev/amauryleve/assert-count-rfc012
Copilot 201784a
Apply suggestion from @Evangelink
Evangelink ef50f91
Address review: culture-aware actual count and WithExpectedAndActual …
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,8 +47,7 @@ internal void ComputeAssertion(string assertionName, string collectionExpression | |
| { | ||
| if (_builder is not null) | ||
| { | ||
| _builder.Insert(0, string.Format(CultureInfo.CurrentCulture, FrameworkMessages.CallerArgumentExpressionSingleParameterMessage, "collection", collectionExpression) + " "); | ||
| ReportAssertCountFailed(assertionName, _expectedCount, _actualCount, _builder.ToString()); | ||
| ReportAssertCountFailed(assertionName, _expectedCount, _actualCount, _builder.ToString(), collectionExpression); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -115,8 +114,7 @@ internal void ComputeAssertion(string collectionExpression) | |
| { | ||
| if (_builder is not null) | ||
| { | ||
| _builder.Insert(0, string.Format(CultureInfo.CurrentCulture, FrameworkMessages.CallerArgumentExpressionSingleParameterMessage, "collection", collectionExpression) + " "); | ||
| ReportAssertIsNotEmptyFailed(_builder.ToString()); | ||
| ReportAssertIsNotEmptyFailed(_builder.ToString(), collectionExpression); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -207,8 +205,7 @@ public static void IsNotEmpty<T>(IEnumerable<T> collection, string? message = "" | |
| return; | ||
| } | ||
|
|
||
| string userMessage = BuildUserMessageForCollectionExpression(message, collectionExpression); | ||
| ReportAssertIsNotEmptyFailed(userMessage); | ||
| ReportAssertIsNotEmptyFailed(message, collectionExpression); | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
@@ -229,8 +226,7 @@ public static void IsNotEmpty(IEnumerable collection, string? message = "", [Cal | |
| return; | ||
| } | ||
|
|
||
| string userMessage = BuildUserMessageForCollectionExpression(message, collectionExpression); | ||
| ReportAssertIsNotEmptyFailed(userMessage); | ||
| ReportAssertIsNotEmptyFailed(message, collectionExpression); | ||
| } | ||
| #endregion // IsNotEmpty | ||
|
|
||
|
|
@@ -252,7 +248,7 @@ public static void HasCount<T>(int expected, IEnumerable<T> collection, [Interpo | |
| #pragma warning restore IDE0060 // Remove unused parameter | ||
| { | ||
| TelemetryCollector.TrackAssertionCall("Assert.HasCount"); | ||
| message.ComputeAssertion("HasCount", collectionExpression); | ||
| message.ComputeAssertion(nameof(HasCount), collectionExpression); | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
@@ -267,7 +263,7 @@ public static void HasCount<T>(int expected, IEnumerable<T> collection, [Interpo | |
| /// Users shouldn't pass a value for this parameter. | ||
| /// </param> | ||
| public static void HasCount<T>(int expected, IEnumerable<T> collection, string? message = "", [CallerArgumentExpression(nameof(collection))] string collectionExpression = "") | ||
| => HasCount("HasCount", expected, collection, message, collectionExpression); | ||
| => HasCount(nameof(HasCount), expected, collection, message, collectionExpression); | ||
|
|
||
| /// <summary> | ||
| /// Tests whether the collection has the expected count/length. | ||
|
|
@@ -280,7 +276,7 @@ public static void HasCount<T>(int expected, IEnumerable<T> collection, string? | |
| /// Users shouldn't pass a value for this parameter. | ||
| /// </param> | ||
| public static void HasCount(int expected, IEnumerable collection, string? message = "", [CallerArgumentExpression(nameof(collection))] string collectionExpression = "") | ||
| => HasCount("HasCount", expected, collection, message, collectionExpression); | ||
| => HasCount(nameof(HasCount), expected, collection, message, collectionExpression); | ||
|
|
||
| #endregion // HasCount | ||
|
|
||
|
|
@@ -301,7 +297,7 @@ public static void IsEmpty<T>(IEnumerable<T> collection, [InterpolatedStringHand | |
| #pragma warning restore IDE0060 // Remove unused parameter | ||
| { | ||
| TelemetryCollector.TrackAssertionCall("Assert.IsEmpty"); | ||
| message.ComputeAssertion("IsEmpty", collectionExpression); | ||
| message.ComputeAssertion(nameof(IsEmpty), collectionExpression); | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
@@ -315,7 +311,7 @@ public static void IsEmpty<T>(IEnumerable<T> collection, [InterpolatedStringHand | |
| /// Users shouldn't pass a value for this parameter. | ||
| /// </param> | ||
| public static void IsEmpty<T>(IEnumerable<T> collection, string? message = "", [CallerArgumentExpression(nameof(collection))] string collectionExpression = "") | ||
| => HasCount("IsEmpty", 0, collection, message, collectionExpression); | ||
| => HasCount(nameof(IsEmpty), 0, collection, message, collectionExpression); | ||
|
|
||
| /// <summary> | ||
| /// Tests that the collection is empty. | ||
|
|
@@ -327,7 +323,7 @@ public static void IsEmpty<T>(IEnumerable<T> collection, string? message = "", [ | |
| /// Users shouldn't pass a value for this parameter. | ||
| /// </param> | ||
| public static void IsEmpty(IEnumerable collection, string? message = "", [CallerArgumentExpression(nameof(collection))] string collectionExpression = "") | ||
| => HasCount("IsEmpty", 0, collection, message, collectionExpression); | ||
| => HasCount(nameof(IsEmpty), 0, collection, message, collectionExpression); | ||
|
|
||
| #endregion // IsEmpty | ||
|
|
||
|
|
@@ -343,8 +339,7 @@ private static void HasCount<T>(string assertionName, int expected, IEnumerable< | |
| return; | ||
| } | ||
|
|
||
| string userMessage = BuildUserMessageForCollectionExpression(message, collectionExpression); | ||
| ReportAssertCountFailed(assertionName, expected, actualCount, userMessage); | ||
| ReportAssertCountFailed(assertionName, expected, actualCount, message, collectionExpression); | ||
| } | ||
|
|
||
| private static void HasCount(string assertionName, int expected, IEnumerable collection, string? message, string collectionExpression) | ||
|
|
@@ -359,24 +354,42 @@ private static string GetTrackedAssertionName(string assertionName) | |
| }; | ||
|
|
||
| [DoesNotReturn] | ||
| private static void ReportAssertCountFailed(string assertionName, int expectedCount, int actualCount, string userMessage) | ||
| private static void ReportAssertCountFailed(string assertionName, int expectedCount, int actualCount, string? userMessage, string collectionExpression) | ||
| { | ||
| string finalMessage = string.Format( | ||
| CultureInfo.CurrentCulture, | ||
| FrameworkMessages.HasCountFailMsg, | ||
| userMessage, | ||
| expectedCount, | ||
| actualCount); | ||
| ReportAssertFailed($"Assert.{assertionName}", finalMessage); | ||
| bool isEmptyAssertion = string.Equals(assertionName, nameof(IsEmpty), StringComparison.Ordinal); | ||
| string summary = isEmptyAssertion | ||
| ? FrameworkMessages.IsEmptyFailedSummary | ||
| : FrameworkMessages.HasCountFailedSummary; | ||
|
|
||
| string expectedEvidenceText = expectedCount.ToString(CultureInfo.CurrentCulture); | ||
| string expectedCallSiteText = expectedCount.ToString(CultureInfo.InvariantCulture); | ||
| string actualText = actualCount.ToString(CultureInfo.CurrentCulture); | ||
| EvidenceBlock evidence = EvidenceBlock.Create() | ||
| .AddLine("expected count:", expectedEvidenceText) | ||
| .AddLine("actual count:", actualText); | ||
|
|
||
| StructuredAssertionMessage structured = new(summary); | ||
| structured.WithUserMessage(userMessage); | ||
| structured.WithEvidence(evidence); | ||
| structured.WithExpectedAndActual(expectedEvidenceText, actualText); | ||
| structured.WithCallSiteExpression(isEmptyAssertion | ||
| ? FormatCallSiteExpression($"Assert.{assertionName}", collectionExpression, "<collection>") | ||
| : FormatCallSiteExpression($"Assert.{assertionName}", expectedCallSiteText, collectionExpression, "<expected>", "<collection>")); | ||
|
|
||
| ReportAssertFailed(structured); | ||
| } | ||
|
|
||
| [DoesNotReturn] | ||
| private static void ReportAssertIsNotEmptyFailed(string userMessage) | ||
| private static void ReportAssertIsNotEmptyFailed(string? userMessage, string collectionExpression) | ||
| { | ||
| string finalMessage = string.Format( | ||
| CultureInfo.CurrentCulture, | ||
| FrameworkMessages.IsNotEmptyFailMsg, | ||
| userMessage); | ||
| ReportAssertFailed("Assert.IsNotEmpty", finalMessage); | ||
| EvidenceBlock evidence = EvidenceBlock.Create() | ||
| .AddLine("actual count:", "0"); | ||
|
|
||
| StructuredAssertionMessage structured = new(FrameworkMessages.IsNotEmptyFailedSummary); | ||
| structured.WithUserMessage(userMessage); | ||
| structured.WithEvidence(evidence); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done — added |
||
| structured.WithCallSiteExpression(FormatCallSiteExpression("Assert.IsNotEmpty", collectionExpression, "<collection>")); | ||
|
|
||
| ReportAssertFailed(structured); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done — the evidence value is now formatted via
0.ToString(CultureInfo.CurrentCulture)and reused for the newWithExpectedAndActual(...)call so the evidence respects non-Latin digit cultures, matching the other count-based assertions.