Does unified diff suffice for testing-purposed string comparison?

39 Views Asked by At

If we are going to implement tests to compare some inline outputs from the program and the expected outputs, we want to enforce different levels of "strictness":

  • exact match
  • exact match after trimming
  • include occurrences (i.e. all lines in the expected output present in the actual output, in the same order)

All these levels of strictness can be easily implemented if a unified diff is not desired.

However, is it possible to use an existing unified diff generator library (Python difflib), combined with certain preprocessing and post-interpretation techniques to tell if the outputs satisfy the third criteria given above?

For example, if the expected output and the actual output are as follows:

Expected:

123
asd
fgh

Actual:

123
asd
test
fgh

If we only expect occurences, this is evaluated to be a match.

The diff will be something like this:

asd
+ test
fgh

One way that I have thought of is to check if we can only see additions in the diff. This is valid in the example provided, as the line "test" only presents in the actual output. But I can't tell if this applies to other cases.

As a followup, if this doesn't work, what will be a way to generate unified diff while only checking for occurrence with Python?

0

There are 0 best solutions below