luaunit icon indicating copy to clipboard operation
luaunit copied to clipboard

Possible assertEquals output enhancement

Open premek opened this issue 9 years ago • 4 comments

When comparing very long strings where only a small part of it is different it is quite difficult to see in the output what the difference is.

  • It may be useful to omit some part of the string at the beginning and at the end of the string that is equal
  • mark the part of the string that is different with [...] for example
  • use colors if terminal supports it? Or enable colors with commandline param

FYI JUnit does it this way (note the three dots and braces):

assertEquals("1111111111111111111111111111111111111111111114111111111111111","1111111111111111111111111111111111111111111111411111111111111");
expected:<...11111111111111111111[41]11111111111111> but was:<...11111111111111111111[14]11111111111111>

Something similar would be very useful when comparing big tables.

premek avatar Dec 05 '16 15:12 premek

I have something along those lines in mind for comparing strings. I was also thinking about a full diff but abbreviatting the output is also an interesting path.

For lists, the git version of LuaUnit (unreleased at the moment) does some diff analysis of the list to simplify the analysis. See https://github.com/bluebird75/luaunit/blob/master/test/ref/some_lists_comparisons.txt .

I'll have a look a JUnit for more inspiration.

Do you have any real world example of comparison implying big lists and/or big strings ? Such data would be really useful.

bluebird75 avatar Dec 06 '16 12:12 bluebird75

yes, i just started to use it to test a simple text parser. Some example data here: https://github.com/premek/pink/blob/29b40f7a968db0fe38eb8230d10803fafba46c92/test/branching.lua

In my case, if the test fails, it is usually just one node missing or nested on a different level. Or a space missing in one of the strings etc.

premek avatar Dec 06 '16 13:12 premek

We could possibly re-use some code/logic from the "list" comparison (mismatchFormattingPureList) to help with this. I'm thinking of breaking down multiple line strings into a table of strings, and single-line strings into a table of characters. A list_compare() helper could then work with these (sequential) tables...

Regards, NiteHawk

n1tehawk avatar Jan 24 '17 15:01 n1tehawk

Why not...

The most challenging part is to make the output readable, especially in case of multi-line strings. The output should probably be different for single line comparison and multi-line strings. I was thinking about using the unified diff syntax.

Example for one line comparison :

Expected : the dog jumps over the cat
               ---
               +++
Received : the fox jumps over the cat

Example for multi-line comparison

Expected (---) / Received (+++)
The
- dog
+ fox
jumps
over
the
cat

I have in mind for a long time to use a diff library to perform really intelligent string and lists comparisons. See : https://github.com/paulgb/simplediff/tree/master/lua

bluebird75 avatar Feb 13 '17 21:02 bluebird75