给定以下python脚本:
# dedupe.py
import re
def dedupe_whitespace(s,spacechars='\t '):
"""Merge repeated whitespace characters.
Example:
>>> dedupe_whitespace(r"Green\t\tGround") # doctest: +REPORT_NDIFF
'Green\tGround'
"""
for w in spacechars:
s = re.sub(r"("+w+"+)", w, s)
return s
该函数在python解释器中按预期工作:
$ python
>>> import dedupe
>>> dedupe.dedupe_whitespace('Purple\t\tHaze')
'Purple\tHaze'
>>> print dedupe.dedupe_whitespace('Blue\t\tSky')
Blue Sky
但是,doctest示例失败,因为制表符在与结果字符串进行比较之前被转换为空格:
>>> import doctest, dedupe
>>> doctest.testmod(dedupe)
给出
Failed example:
dedupe_whitespace(r"Green Ground") #doctest: +REPORT_NDIFF
Differences (ndiff with -expected +actual):
- 'Green Ground'
? -
+ 'Green Ground'
如何对doctest heredoc字符串中的制表符进行编码,以便正确执行测试结果比较?
https://stackoverflow.com/questions/8834916
复制相似问题