testfx icon indicating copy to clipboard operation
testfx copied to clipboard

Assert.AreEqual string: consider replacing whitespaces by a symbol

Open Evangelink opened this issue 3 months ago • 3 comments

Describe the bug

When reviewing diffs for Assert.AreEqual of strings, it's sometimes uneasy to understand how many spaces there are. Let's take:

Assert.AreEqual failed. Expected string length 199 but was 139. 'expected' expression: 'Normalize(expected)', 'actual' expression: 'Normalize(actual)'.
Expected: "...n 1; }␊    #endregion␊␊    #region ..."
But was:  "...n 1; }␊    #endregion␊    #endregion␊}"

Expected behavior

Space is replaced with a symbol:

  • - Space (U+2423 OPEN BOX)
  • · - Middle dot

Nit: if that's not already handled, also consider replacing tabs:

- Tab (can also use or )

Evangelink avatar Oct 10 '25 13:10 Evangelink

Since v4 we replace common formatting characters with their visible version. Except for spaces and tabs. We don't control how they are rendered, we just change the character to visible.

Space is not replaced because it impacts readability of longer text when spaces are not the difference.

Tab is not replaced because it would break formatting of text. But in this case it probably does not matter as much since it is 1 line only.

We do however have the indicator of where the difference is, which should make it even more obvious where the difference is.

Image

nohwnd avatar Oct 10 '25 13:10 nohwnd

I think we could have a heuristic here specific to multi-line diffing, similar to how the diffing from Roslyn testing library works. I think Roslyn testing uses https://github.com/mmanela/diffplex. I'm not sure how feasible it is to add such extra dependency to MSTest. Or alternatively, we copy the needed parts from DiffPlex with license notice (we might want to confirm with CELA about the licensing stuff and whether we can copy from DiffPlex).

We might even consider if DiffPlex provides good diffing for single-line and use that instead.

Youssef1313 avatar Oct 12 '25 09:10 Youssef1313

Related: https://github.com/microsoft/testfx/issues/6771

Youssef1313 avatar Nov 01 '25 22:11 Youssef1313