wolverdude / GenSON

GenSON is a powerful, user-friendly JSON Schema generator built in Python.
MIT License
584 stars 64 forks source link

Ignore whitespace differences when testing error messages #75

Open jdayton3 opened 6 months ago

jdayton3 commented 6 months ago

Problem

When I run the unit tests locally, I get failures for test_bin.py's TestError tests. It looks to be because my detected terminal width is configured differently than is expected, so the "usage" portion of the error message wraps differently than expected:

py37: commands[0]> coverage run --source=genson -m unittest
.......................................FFF.............................................................
======================================================================
FAIL: test_no_input (test.test_bin.TestError)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:\repos\GenSON\test\test_bin.py", line 81, in test_no_input
    'noting to do - no schemas or objects given'))
AssertionError: 'usag[59 chars]CES]\r\n              [-s SCHEMA] [-$ SCHEMA_U[85 chars]\r\n' != 'usag[59 chars]CES]\n              [-s SCHEMA] [-$ SCHEMA_URI[77 chars]en\n'
- usage: genson [-h] [--version] [-d DELIM] [-e ENCODING] [-i SPACES]
?                                                                    -
+ usage: genson [-h] [--version] [-d DELIM] [-e ENCODING] [-i SPACES]
-               [-s SCHEMA] [-$ SCHEMA_URI]
?                                          -
+               [-s SCHEMA] [-$ SCHEMA_URI]
-               ...
?                  -
+               ...
- genson: error: noting to do - no schemas or objects given
?                                                          -
+ genson: error: noting to do - no schemas or objects given

Proposed solution

Add a method TestError.assertEqualIgnoreWhitespace() that replaces any chunk of whitespace with a single space before comparing two strings, and call this method when comparing stderr to the expected output.

Also considered

I looked through the Python unittest documentation, and I was unable to find an existing method that would ignore differences in whitespace when asserting equality between two strings.

I considered using a regular expression to standardize whitespace differences (one of the proposed solutions on this StackOverflow question), but that would add an import and (in my opinion) some unnecessary complexity.