Skip to content

Improve failure description - #6557

Closed
sebastianbergmann wants to merge 2 commits into
12.5from
feature/improve-failure-description-for-StringMatchesFormatDescription-constraint
Closed

sebastianbergmann wants to merge 2 commits into
12.5from
feature/improve-failure-description-for-StringMatchesFormatDescription-constraint

Conversation

@sebastianbergmann

Copy link
Copy Markdown
Owner

Improve the failure description for the StringMatchesFormatDescription constraint when %a/%A multiline placeholders match content spanning multiple lines.

Previously, additionalFailureDescription() compared expected and actual output line-by-line using a single shared index. When %a or %A consumed multiple actual lines, the arrays went out of sync and all subsequent lines appeared as false diffs — burying the real failure in noise.

This was attempted before in 72ea2b5 by modifying regularExpressionForFormatDescription() to remove the $ end-of-string anchor for multiline patterns.

That approach caused regression #6200 because regularExpressionForFormatDescription() is shared with matches(), which changed test pass/fail outcomes. It was reverted in 9bd312d.

This implementation takes a different approach that avoids the regression:

  • Rewrites only additionalFailureDescription()
  • matches() and regularExpressionForFormatDescription() are unchanged
  • Uses a two-pointer algorithm with anchor lookahead: when a %a/%A line is encountered, find the next non-multiline expected line (the "anchor") and search for it in the actual output to determine how many lines the placeholder consumed
  • Builds a fresh synced array instead of mutating in-place with array_splice()

Before (multiline %A causes full desync):

 ## before first A
 some multiline
+text for
+A to match
 ## after first A
 *
 ## before second A
+more multiline text
+for A to match
+## after second A
 *
-## after second A
+Foo: s match
 *
-Foo: %s
+Additional Text that is not matched

After (only the real mismatch is shown):

 ## after second A
 *
 Foo: s match
+*
+Additional Text that is not matched

@sebastianbergmann sebastianbergmann self-assigned this Mar 31, 2026
@sebastianbergmann sebastianbergmann added type/enhancement A new idea that should be implemented feature/phpt End-to-end tests in PHPT format feature/assertion Issues related to assertions and expectations labels Mar 31, 2026
@sebastianbergmann
sebastianbergmann changed the base branch from main to 12.5 March 31, 2026 07:52
@sebastianbergmann

Copy link
Copy Markdown
Owner Author

@sebastianfeldmann @edorian Can you have a look, please? Thank you!

@codecov

codecov Bot commented Mar 31, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.94%. Comparing base (aeb6899) to head (20b3fec).
⚠️ Report is 13 commits behind head on 12.5.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff            @@
##               12.5    #6557   +/-   ##
=========================================
  Coverage     95.93%   95.94%           
- Complexity     7462     7476   +14     
=========================================
  Files           790      790           
  Lines         22800    22842   +42     
=========================================
+ Hits          21874    21916   +42     
  Misses          926      926           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

findAnchorInActual() only returns a position where the literal or regular expression check already passed, so the conditional is unnecessary.
@edorian

edorian commented Apr 1, 2026

Copy link
Copy Markdown
Contributor

Tested it against my .phpt test suites and everything works well.

The "%A" match failures that are common for us that I remembered and tested didn't change in output.

The tests look a lot nicer and the algo looks a lot more sensible than before.

All around 👍 from me @sebastianbergmann

@sebastianbergmann

Copy link
Copy Markdown
Owner Author

Cherry-picked into main.

@sebastianfeldmann

Copy link
Copy Markdown
Contributor

Nice idea with the freshly synced array 👍

All tests in my local branch passed as well. I will check if it makes sense to still commit the tests.

@sebastianbergmann
sebastianbergmann deleted the feature/improve-failure-description-for-StringMatchesFormatDescription-constraint branch April 6, 2026 13:18
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators May 7, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

feature/assertion Issues related to assertions and expectations feature/phpt End-to-end tests in PHPT format type/enhancement A new idea that should be implemented

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants