Skip to content

Improve assertions error messages with structured format#7444

Open
Evangelink wants to merge 17 commits intomainfrom
dev/amauryleve/rework-assert
Open

Improve assertions error messages with structured format#7444
Evangelink wants to merge 17 commits intomainfrom
dev/amauryleve/rework-assert

Conversation

@Evangelink
Copy link
Member

@Evangelink Evangelink commented Feb 20, 2026

Fix #7421

Copilot AI review requested due to automatic review settings February 20, 2026 21:33
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request modernizes assertion error messages by introducing a structured, multi-line format that improves readability and developer experience. The changes replace old-style concatenated messages with formatted parameter displays using raw string literals.

Changes:

  • Introduced structured error message formatting with parameter names, expressions, and values on separate lines
  • Added helper methods for value formatting, expression truncation, and redundancy detection
  • Updated all assertion methods to use the new formatting approach
  • Removed obsolete resource strings and added new simplified ones
  • Updated all test expectations to match the new message format

Reviewed changes

Copilot reviewed 41 out of 41 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
Assert.cs Core formatting infrastructure: FormatValue, FormatParameter, truncation logic
Assert.ThrowsException.cs Updated to new structured format for exception type mismatches
Assert.StartsWith/EndsWith/Matches.cs String assertion methods using new format
Assert.IsTrue/IsFalse.cs Boolean assertion methods with condition parameter display
Assert.IsNull/IsNotNull.cs Null checking assertions with value display
Assert.IsInstanceOfType/IsExactInstanceOfType.cs Type checking with structured type comparison
Assert.IComparable.cs Comparison assertions (greater/less than, positive/negative)
Assert.Count/Contains.cs Collection assertions with expression parameters
Assert.AreSame.cs Reference equality with hash code display for disambiguation
FrameworkMessages.resx Resource string simplification and new message keys
xlf files Localization files marked with untranslated entries
Test files Updated expectations for all assertion error messages

@Evangelink Evangelink force-pushed the dev/amauryleve/rework-assert branch 2 times, most recently from a55c762 to 49018c6 Compare February 22, 2026 10:31
Copilot AI review requested due to automatic review settings February 22, 2026 10:31
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 44 out of 44 changed files in this pull request and generated 7 comments.

@Evangelink Evangelink force-pushed the dev/amauryleve/rework-assert branch from 49018c6 to 63e8257 Compare February 22, 2026 10:38
Copilot AI review requested due to automatic review settings February 22, 2026 12:56
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 44 out of 44 changed files in this pull request and generated no new comments.

@nohwnd
Copy link
Member

nohwnd commented Feb 23, 2026

looks great

@Evangelink Evangelink marked this pull request as ready for review February 23, 2026 14:05
Copilot AI review requested due to automatic review settings February 23, 2026 14:05
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 46 out of 46 changed files in this pull request and generated 4 comments.

Comment on lines +12 to +127
public void MatchesRegex_WhenValueMatchesPattern_ShouldPass()
=> Assert.MatchesRegex(@"\d+", "abc123");

public void MatchesRegex_WhenValueDoesNotMatchPattern_ShouldFail()
{
Action action = () => Assert.MatchesRegex(@"\d+", "abc");
action.Should().Throw<AssertFailedException>()
.WithMessage("""
Assert.MatchesRegex failed.
String does not match expected pattern.
pattern: \d+
value: "abc"
""");
}

public void DoesNotMatchRegex_WhenValueDoesNotMatchPattern_ShouldPass()
=> Assert.DoesNotMatchRegex(@"\d+", "abc");

public void DoesNotMatchRegex_WhenValueMatchesPattern_ShouldFail()
{
Action action = () => Assert.DoesNotMatchRegex(@"\d+", "abc123");
action.Should().Throw<AssertFailedException>()
.WithMessage("""
Assert.DoesNotMatchRegex failed.
String matches pattern but should not.
pattern: \d+
value: "abc123"
""");
}

#endregion

#region MatchesRegex/DoesNotMatchRegex truncation and newline escaping

public void MatchesRegex_WithLongExpression_ShouldTruncateExpression()
{
string aVeryLongVariableNameThatExceedsOneHundredCharactersInLengthToTestTruncationBehaviorOfExpressionDisplayXYZ = "hello";

Action action = () => Assert.MatchesRegex(@"\d+", aVeryLongVariableNameThatExceedsOneHundredCharactersInLengthToTestTruncationBehaviorOfExpressionDisplayXYZ);
action.Should().Throw<AssertFailedException>()
.WithMessage("""
Assert.MatchesRegex failed.
String does not match expected pattern.
pattern: \d+
value (aVeryLongVariableNameThatExceedsOneHundredCharactersInLengthToTestTruncationBehaviorOfExpressionDisp...): "hello"
""");
}

public void MatchesRegex_WithLongValue_ShouldTruncateValue()
{
string longValue = new string('x', 300);

Action action = () => Assert.MatchesRegex(@"\d+", longValue);
action.Should().Throw<AssertFailedException>()
.WithMessage($"""
Assert.MatchesRegex failed.
String does not match expected pattern.
pattern: \d+
value (longValue): "{new string('x', 255)}... (302 chars)
""");
}

public void MatchesRegex_WithNewlineInValue_ShouldEscapeNewlines()
{
Action action = () => Assert.MatchesRegex(@"^\d+$", "hello\r\nworld");
action.Should().Throw<AssertFailedException>()
.WithMessage("""
Assert.MatchesRegex failed.
String does not match expected pattern.
pattern: ^\d+$
value: "hello\r\nworld"
""");
}

public void DoesNotMatchRegex_WithLongExpression_ShouldTruncateExpression()
{
string aVeryLongVariableNameThatExceedsOneHundredCharactersInLengthToTestTruncationBehaviorOfExpressionDisplayXYZ = "abc123";

Action action = () => Assert.DoesNotMatchRegex(@"\d+", aVeryLongVariableNameThatExceedsOneHundredCharactersInLengthToTestTruncationBehaviorOfExpressionDisplayXYZ);
action.Should().Throw<AssertFailedException>()
.WithMessage("""
Assert.DoesNotMatchRegex failed.
String matches pattern but should not.
pattern: \d+
value (aVeryLongVariableNameThatExceedsOneHundredCharactersInLengthToTestTruncationBehaviorOfExpressionDisp...): "abc123"
""");
}

public void DoesNotMatchRegex_WithLongValue_ShouldTruncateValue()
{
string longValue = new string('1', 300);

Action action = () => Assert.DoesNotMatchRegex(@"\d+", longValue);
action.Should().Throw<AssertFailedException>()
.WithMessage($"""
Assert.DoesNotMatchRegex failed.
String matches pattern but should not.
pattern: \d+
value (longValue): "{new string('1', 255)}... (302 chars)
""");
}

public void DoesNotMatchRegex_WithNewlineInValue_ShouldEscapeNewlines()
{
Action action = () => Assert.DoesNotMatchRegex(@"hello", "hello\r\nworld");
action.Should().Throw<AssertFailedException>()
.WithMessage("""
Assert.DoesNotMatchRegex failed.
String matches pattern but should not.
pattern: hello
value: "hello\r\nworld"
""");
}

#endregion
}
Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All test methods in this new file are missing the [TestMethod] attribute. Without this attribute, none of these tests will be discovered and executed by the test runner.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve AreSame error messages in case of null values

3 participants