-
Notifications
You must be signed in to change notification settings - Fork 301
Add structured assertion message infrastructure (RFC 012) #8170
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
Merged
Evangelink
merged 5 commits into
main
from
dev/amauryleve/structured-assertion-messages-infrastructure
May 13, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
119afd5
Add structured assertion message infrastructure (RFC 012)
Evangelink c79782b
Address PR review comments: fix culture-dependent test, add pragma ra…
Evangelink 33b4249
Fix CA1305: use CultureInfo.CurrentCulture in ToString call
Evangelink 14378fe
Merge remote-tracking branch 'origin/main' into dev/amauryleve/struct…
Evangelink d57da84
Address review comments: EvidenceBlock as class, multiple evidence bl…
Evangelink 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
109 changes: 109 additions & 0 deletions
109
src/TestFramework/TestFramework/Assertions/AssertionValueRenderer.cs
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 |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| namespace Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| /// <summary> | ||
| /// Renders values for display in structured assertion messages following the RFC 012 value rendering rules. | ||
| /// </summary> | ||
| internal static class AssertionValueRenderer | ||
| { | ||
| /// <summary> | ||
| /// Renders a value as a string suitable for display in the evidence block. | ||
| /// </summary> | ||
| internal static string RenderValue(object? value) | ||
| => value switch | ||
| { | ||
| null => "null", | ||
| string s => RenderString(s), | ||
| bool b => b ? "true" : "false", | ||
| char c => RenderChar(c), | ||
| IEnumerable enumerable => RenderCollection(enumerable), | ||
| _ => value.ToString() ?? value.GetType().FullName ?? value.GetType().Name, | ||
| }; | ||
|
|
||
| /// <summary> | ||
| /// Renders a string value with double quotes and escape sequences for control characters. | ||
| /// </summary> | ||
| private static string RenderString(string value) | ||
| { | ||
| StringBuilder sb = new(value.Length + 2); | ||
| sb.Append('"'); | ||
| foreach (char c in value) | ||
| { | ||
| switch (c) | ||
| { | ||
| case '"': | ||
| sb.Append("\\\""); | ||
| break; | ||
| case '\\': | ||
| sb.Append("\\\\"); | ||
| break; | ||
| case '\n': | ||
| sb.Append("\\n"); | ||
| break; | ||
| case '\r': | ||
| sb.Append("\\r"); | ||
| break; | ||
| case '\t': | ||
| sb.Append("\\t"); | ||
| break; | ||
| case '\0': | ||
| sb.Append("\\0"); | ||
| break; | ||
| default: | ||
| if (char.IsControl(c)) | ||
| { | ||
| sb.Append("\\u"); | ||
| sb.Append(((int)c).ToString("X4", CultureInfo.InvariantCulture)); | ||
| } | ||
| else | ||
| { | ||
| sb.Append(c); | ||
| } | ||
|
|
||
| break; | ||
| } | ||
| } | ||
|
|
||
| sb.Append('"'); | ||
| return sb.ToString(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Renders a char value with single quotes and escape sequences. | ||
| /// </summary> | ||
| private static string RenderChar(char value) => | ||
| value switch | ||
| { | ||
| '\n' => "'\\n'", | ||
| '\r' => "'\\r'", | ||
| '\t' => "'\\t'", | ||
| '\0' => "'\\0'", | ||
| _ when char.IsControl(value) => $"'\\u{(int)value:X4}'", | ||
| _ => $"'{value}'", | ||
| }; | ||
|
|
||
| /// <summary> | ||
| /// Renders a collection in JSON-style array notation. | ||
| /// </summary> | ||
| private static string RenderCollection(IEnumerable enumerable) | ||
| { | ||
| StringBuilder sb = new(); | ||
| sb.Append('['); | ||
| bool first = true; | ||
| foreach (object? item in enumerable) | ||
| { | ||
| if (!first) | ||
| { | ||
| sb.Append(", "); | ||
| } | ||
|
|
||
| sb.Append(RenderValue(item)); | ||
| first = false; | ||
| } | ||
|
|
||
| sb.Append(']'); | ||
| return sb.ToString(); | ||
| } | ||
| } |
62 changes: 62 additions & 0 deletions
62
src/TestFramework/TestFramework/Assertions/EvidenceBlock.cs
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 |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| namespace Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| /// <summary> | ||
| /// Represents the evidence block of a structured assertion message, containing labeled value lines | ||
| /// such as expected/actual values and assertion-specific details. | ||
| /// </summary> | ||
| internal sealed class EvidenceBlock | ||
| { | ||
| private readonly List<EvidenceLine> _lines = []; | ||
|
|
||
| internal static EvidenceBlock Create() => new(); | ||
|
|
||
| internal IReadOnlyList<EvidenceLine> Lines => _lines; | ||
|
|
||
| internal EvidenceBlock AddLine(string label, string value) | ||
| { | ||
| _lines.Add(new EvidenceLine(label, value)); | ||
| return this; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Formats the evidence block as aligned label: value lines. | ||
| /// Labels are right-padded so all values start at the same column. | ||
| /// </summary> | ||
| internal string Format() | ||
| { | ||
| if (_lines.Count == 0) | ||
| { | ||
| return string.Empty; | ||
| } | ||
|
|
||
| int maxLabelLength = 0; | ||
| foreach (EvidenceLine line in _lines) | ||
| { | ||
| if (line.Label.Length > maxLabelLength) | ||
| { | ||
| maxLabelLength = line.Label.Length; | ||
| } | ||
| } | ||
|
|
||
| StringBuilder sb = new(); | ||
| for (int i = 0; i < _lines.Count; i++) | ||
| { | ||
| if (i > 0) | ||
| { | ||
| sb.Append(Environment.NewLine); | ||
| } | ||
|
|
||
| EvidenceLine line = _lines[i]; | ||
|
|
||
| // Pad label (which includes trailing colon) to align values, then append a space and value | ||
| sb.Append(line.Label.PadRight(maxLabelLength)); | ||
| sb.Append(' '); | ||
| sb.Append(line.Value); | ||
| } | ||
|
|
||
| return sb.ToString(); | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| namespace Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| /// <summary> | ||
| /// Represents a single labeled line in the evidence block of a structured assertion message. | ||
| /// </summary> | ||
| internal readonly record struct EvidenceLine(string Label, string Value); |
129 changes: 129 additions & 0 deletions
129
src/TestFramework/TestFramework/Assertions/StructuredAssertionMessage.cs
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 |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| namespace Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| /// <summary> | ||
| /// Builds a structured assertion failure message following the format: | ||
| /// <code> | ||
| /// Assertion failed. <summary> | ||
| /// <user message> | ||
| /// | ||
| /// <evidence block> | ||
| /// | ||
| /// <call-site expression> | ||
| /// </code> | ||
| /// </summary> | ||
| internal sealed class StructuredAssertionMessage | ||
| { | ||
| private const string AssertionPrefix = "Assertion failed."; | ||
|
|
||
| private readonly string _summary; | ||
| private readonly List<string> _additionalSummaryLines = []; | ||
| private readonly List<EvidenceBlock> _evidenceBlocks = []; | ||
| private string? _userMessage; | ||
| private string? _callSiteExpression; | ||
|
|
||
| internal StructuredAssertionMessage(string summary) | ||
| { | ||
| _summary = summary; | ||
| } | ||
|
|
||
| internal string? ExpectedText { get; private set; } | ||
|
|
||
| internal string? ActualText { get; private set; } | ||
|
|
||
| internal StructuredAssertionMessage WithAdditionalSummaryLine(string line) | ||
| { | ||
| _additionalSummaryLines.Add(line); | ||
| return this; | ||
| } | ||
|
|
||
| internal StructuredAssertionMessage WithUserMessage(string? userMessage) | ||
| { | ||
| if (!string.IsNullOrWhiteSpace(userMessage)) | ||
| { | ||
| _userMessage = userMessage; | ||
| } | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| internal StructuredAssertionMessage WithEvidence(EvidenceBlock evidenceBlock) | ||
| { | ||
| _evidenceBlocks.Add(evidenceBlock); | ||
| return this; | ||
| } | ||
|
Evangelink marked this conversation as resolved.
|
||
|
|
||
| internal StructuredAssertionMessage WithExpectedAndActual(string? expectedText, string? actualText) | ||
| { | ||
| ExpectedText = expectedText; | ||
| ActualText = actualText; | ||
| return this; | ||
| } | ||
|
|
||
| internal StructuredAssertionMessage WithCallSiteExpression(string? callSiteExpression) | ||
| { | ||
| if (!string.IsNullOrWhiteSpace(callSiteExpression)) | ||
| { | ||
| _callSiteExpression = callSiteExpression; | ||
| } | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Formats the structured message as a multi-line string following the RFC 012 layout. | ||
| /// </summary> | ||
| internal string Format() | ||
| { | ||
| StringBuilder sb = new(); | ||
|
|
||
| // Line 1: Assertion prefix + summary | ||
| sb.Append(AssertionPrefix); | ||
| if (!string.IsNullOrEmpty(_summary)) | ||
| { | ||
| sb.Append(' '); | ||
| sb.Append(_summary); | ||
| } | ||
|
|
||
| // Additional summary lines | ||
| foreach (string additionalLine in _additionalSummaryLines) | ||
| { | ||
| sb.Append(Environment.NewLine); | ||
| sb.Append(additionalLine); | ||
| } | ||
|
|
||
| // User message (on its own line, no label) | ||
| if (_userMessage is not null) | ||
| { | ||
| sb.Append(Environment.NewLine); | ||
| sb.Append(_userMessage); | ||
| } | ||
|
|
||
| // Evidence blocks (each separated by blank line) | ||
| foreach (EvidenceBlock evidence in _evidenceBlocks) | ||
| { | ||
| string formattedEvidence = evidence.Format(); | ||
| if (!string.IsNullOrEmpty(formattedEvidence)) | ||
| { | ||
| sb.Append(Environment.NewLine); | ||
| sb.Append(Environment.NewLine); | ||
| sb.Append(formattedEvidence); | ||
| } | ||
| } | ||
|
|
||
| // Call-site expression (separated by blank line) | ||
| if (_callSiteExpression is not null) | ||
| { | ||
| sb.Append(Environment.NewLine); | ||
| sb.Append(Environment.NewLine); | ||
| sb.Append(_callSiteExpression); | ||
| } | ||
|
|
||
| return sb.ToString(); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public override string ToString() => Format(); | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.