Skip to content
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

Show location of difference when comparing two strings #907

Closed
wilka opened this issue Sep 2, 2018 · 3 comments · Fixed by #915
Closed

Show location of difference when comparing two strings #907

wilka opened this issue Sep 2, 2018 · 3 comments · Fixed by #915

Comments

@wilka
Copy link
Contributor

wilka commented Sep 2, 2018

When using string1.Should().Be(string2), if the strings differ it can be awkward to work out where the difference is for large string. E.g. if you're checking that building some json/xml/whatever-long-text works correctly.

Click for example test output for xml text
Expected xmlString1 to be 
"<CATALOG>
	<CD>
		<TITLE>Empire Burlesque</TITLE>
		<ARTIST>Bob Dylan</ARTIST>
		<COUNTRY>USA</COUNTRY>
		<COMPANY>Columbia</COMPANY>
		<PRICE>10.90</PRICE>
		<YEAR>1985</YEAR>
	</CD>
	<CD>
		<TITLE>Hide your heart</TITLE>
		<ARTIST>Bonnie Tyler</ARTIST>
		<COUNTRY>UK</COUNTRY>
		<COMPANY>CBS Records</COMPANY>
		<PRICE>9.90</PRICE>
		<YEAR>1988</YEAR>
	</CD>
	<CD>
		<TITLE>Greatest Hits</TITLE>
		<ARTIST>Dolly Parton</ARTIST>
		<COUNTRY>USA</COUNTRY>
		<COMPANY>RCA</COMPANY>
		<PRICE>19.90</PRICE>
		<YEAR>1982</YEAR>
	</CD>
	<CD>
		<TITLE>Still got the blues</TITLE>
		<ARTIST>Gary Moore</ARTIST>
		<COUNTRY>UK</COUNTRY>
		<COMPANY>Virgin records</COMPANY>
		<PRICE>10.20</PRICE>
		<YEAR>1990</YEAR>
	</CD>
</CATALOG>" with a length of 756, but 
"<CATALOG>
	<CD>
		<TITLE>Empire Burlesque</TITLE>
		<ARTIST>Bob Dylan</ARTIST>
		<COUNTRY>USA</COUNTRY>
		<COMPANY>Columbia</COMPANY>
		<PRICE>10.90</PRICE>
		<YEAR>1985</YEAR>
	</CD>
	<CD>
		<TITLE>Hide your heart</TITLE>
		<ARTIST>Bonnie Tyler</ARTIST>
		<COUNTRY>UK</COUNTRY>
		<COMPANY>CBS Records</COMPANY>
		<PRICE>9.90</PRICE>
		<YEAR>1988</YEAR>
	</CD>
	<CD>
		<TITLE>Greatest Hits</TITLE>
		<ARTIST>Dolly Parton</ARTIST>
		<COUNTRY>USA</COUNTRY>
		<COMPANY>RCA</COMPANY>
		<PRICE>9.90</PRICE>
		<YEAR>1982</YEAR>
	</CD>
	<CD>
		<TITLE>Still got the blues</TITLE>
		<ARTIST>Gary Moore</ARTIST>
		<COUNTRY>UK</COUNTRY>
		<COMPANY>Virgin records</COMPANY>
		<PRICE>10.20</PRICE>
		<YEAR>1990</YEAR>
	</CD>
</CATALOG>" has a length of 755.

At the moment, I'm copy & pasting from the test output into a diff tool so I can find where the difference is.

It would be great if the assert message also included a hint to where to the first difference.

Some ideas that I'd find handy (any of these could go after or before the current message showing the full actual vs expected text)

  • For multi-line strings, include the line number of the first difference
  • Include a snippet of the string, that shows a few characters before & after the index of the first difference (say 14 characters before the difference, and 6 characters after)
  • A unified diff style display of the two strings (like git would show)

The specific details of how to show what the differences are (or at least the first difference) aren't very important, anything that helps speed up tracking down the differences would be 👍

@adamfk
Copy link

adamfk commented Dec 18, 2022

Did the unified diff idea get implemented? I looked in the related PR and documentation, but didn't see anything.

@dennisdoomen
Copy link
Member

No, but we are experimenting with it as we speak. See #2050

@adamfk
Copy link

adamfk commented Dec 20, 2022

Thanks! I'll keep an eye on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants