How can I include special characters (tab, newline) in a python doctest result string?

后端 未结 6 1506
甜味超标
甜味超标 2021-02-18 19:06

Given the following python script:

# dedupe.py
import re

def dedupe_whitespace(s,spacechars=\'\\t \'):
    \"\"\"Merge repeated whitespace characters.
    Examp         


        
6条回答
  •  时光说笑
    2021-02-18 19:38

    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.

提交回复
热议问题