Assert.That: restore friendly method-call display from #6691#8364
Merged
Conversation
PR #8307 re-architected `Assert.That` and silently regressed two display fixes from the original #6691 work: 1. Any instance method named `Get(...)` was rendered as `obj[...]` in failure details, regardless of whether the receiver was an array. So `box.Get(1) == 0` produced `box[1] = 101` instead of `box.Get(1) = 101`. The original fix gated this on `callExpr.Object.Type.IsArray`. 2. Non-boolean method calls fell back to a raw `callExpr.ToString()` cleanup, losing the friendly `this.Method(...)` / `Type.Method(...)` / `receiver.Method(...)` rendering. Inherited methods on `this`, captured-this extension methods, and static methods on the same type all suffered. Re-introduces: * `IsMultiDimensionalArrayGetCall` — scopes the `Get -> obj[...]` special case to actual array receivers (matches multi-dim arrays like `int[,]` whose `Get` is runtime-synthesized on the array type itself, not on `System.Array`). * `GetMethodCallDisplayName` — builds the friendly receiver string for non-boolean method calls. * `IsCapturedThis` — recognizes the compiler-synthesized `<>4__this` field used in closures AND the no-closure `ConstantExpression` form, guarded by `ce.Type == ce.Value.GetType()` so a base-typed local isn't mislabeled as `this` (while inherited methods on `this` are still rendered correctly via `IsAssignableFrom`). Four regression tests added in `AssertTests.That.cs`: * `That_ArbitraryGetMethodOnNonArray_RendersAsMethodCall_NotIndexer` * `That_StaticMethodOnSameType_RendersWithTypeNamePrefix` * `That_InstanceMethodOnThis_RendersAsThisMethod` * `That_ExtensionMethodOnThis_RendersAsThisMethod` All 918 `AssertTests` pass on `net9.0`. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Restores friendlier method-call rendering in Assert.That failure details (regressions from #6691) and adds regression tests to lock in the message formatting.
Changes:
- Gate
array.Get(...)→array[...]rendering to true array receivers to avoid mis-rendering arbitraryGet(...)methods. - Add friendly receiver formatting for method calls (
this.Method(...),Type.Method(...), and extension methods onthis). - Add 4 regression tests covering the restored display behavior.
Show a summary per file
| File | Description |
|---|---|
| test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.That.cs | Adds regression tests for friendly receiver/method-call rendering. |
| src/TestFramework/TestFramework/Assertions/Assert.That.cs | Reintroduces friendly method-call display logic and corrects the Get-as-indexer special case. |
Copilot's findings
- Files reviewed: 2/2 changed files
- Comments generated: 6
- Resolve Assert.That.cs / AssertTests.That.cs merge conflicts (both sides additive). - Rename IsMultiDimensionalArrayGetCall -> IsArrayGetCall to match the IsArray gate; update doc to explain that the call only fires for multidim arrays in practice. - Static method-call rendering now uses a friendly type name (no namespace, '+' replaced with '.') so the displayed type matches the user-facing C# syntax described in the comment. - Fix 'mis-leading' typo in regression-test header. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
YuliiaKovalova
approved these changes
May 20, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Follow-up to #8306 / #8307 / #8352 restoring two friendly-display fixes from issue #6691 that PR #8307's architectural rewrite silently regressed.
Regressions repaired
1.
obj.Get(...)was rendered asobj[...]for any receiverHandleMethodCallExpressioncurrently matches any instance method namedGetand renders it asobject[args]. So a user assertionAssert.That(() => box.Get(1) == 0)on a plain class producesbox[1] = 101in the failure details, which is misleading — looks like an indexer, isn't.The original #6691 fix gated this on
callExpr.Object.Type.IsArray(the multi-dim-array case is exactly why this special case exists —int[,]exposes a runtime-synthesizedGeton the array type itself, not onSystem.Array). This PR re-introduces that gate asIsMultiDimensionalArrayGetCall.2.
this.Method(...)/Type.Method(...)rendering lostNon-boolean method calls fall back to raw
callExpr.ToString()cleanup, which renders:GetAnimal()AssertTests.GetAnimal()thisvalue(AssertTests).GetAnimalInstance()/ similarthis.GetAnimalInstance()thisthis.GetGreeting()Re-introduces
GetMethodCallDisplayName+IsCapturedThisfrom the original fix, with the no-closureConstantExpressionbranch guarded byce.Type == ce.Value.GetType()so a base-typed local isn't mislabeled asthis(while inherited methods onthisare still rendered correctly viaIsAssignableFrom).Tests
Four regression tests added in
AssertTests.That.cs:That_ArbitraryGetMethodOnNonArray_RendersAsMethodCall_NotIndexerThat_StaticMethodOnSameType_RendersWithTypeNamePrefixThat_InstanceMethodOnThis_RendersAsThisMethodThat_ExtensionMethodOnThis_RendersAsThisMethodThe tests assert on substrings rather than full-message matches so the exact parenthesization / argument rendering can still evolve.
Validation
dotnet build src/TestFramework/TestFramework/TestFramework.csproj -c Debug→ 0 warnings, 0 errors across netstandard2.0, net462, net8.0, net9.0.AssertTestspass on net9.0 (76That_*tests, including the 4 new ones).Notes
AssertExtensions.IsCapturedThissoAssert.That(() => this.SomeExtension() == x)renders asthis.SomeExtension(...)rather than<>4__this.SomeExtension(...)orTypeName.SomeExtension(...).