Given the following python script:
# dedupe.py
import re
def dedupe_whitespace(s,spacechars=\'\\t \'):
\"\"\"Merge repeated whitespace characters.
Examp
You must set the NORMALIZE_WHITESPACE. Or, alternatively, capture the output and compare it to the expected value:
def dedupe_whitespace(s,spacechars='\t '):
"""Merge repeated whitespace characters.
Example:
>>> output = dedupe_whitespace(r"Black\t\tGround") #doctest: +REPORT_NDIFF
>>> output == 'Black\tGround'
True
"""
From the doctest
documentation section How are Docstring Examples Recognized?:
All hard tab characters are expanded to spaces, using 8-column tab stops. Tabs in output generated by the tested code are not modified. Because any hard tabs in the sample output are expanded, this means that if the code output includes hard tabs, the only way the doctest can pass is if the NORMALIZE_WHITESPACE option or directive is in effect. Alternatively, the test can be rewritten to capture the output and compare it to an expected value as part of the test. This handling of tabs in the source was arrived at through trial and error, and has proven to be the least error prone way of handling them. It is possible to use a different algorithm for handling tabs by writing a custom
DocTestParser
class.
Edit: My mistake, I understood the docs the other way around. Tabs are being expanded to 8 spaces at both the string argument passed to dedupe_whitespace
and the string literal being compared on the next line, so output
contains:
"Black Ground"
and is being compared to:
"Black Ground"
I can't find a way to overcome this limitation without writing your own DocTestParser
or testing for deduplicated spaces instead of tabs.