Python / Doctest - doctesting复杂字符串

时间:2014-10-23 16:08:07

标签: python doctest

我是一名正在完成作业的学生,​​我应该对所有内部职能进行博士学位。我的函数使用复杂的字符串并返回复杂的字符串,所以我不知道该怎么做。例如我的函数" ProcessImports()"可以拿字符串:

%@import
blahblah
%@

返回:

\begin{shadedquoteBlueBar}
\fontsize{9pt}{9pt}
\begin{Verbatim}
blahblah}
\end{Verbatim}
\end{shadedquoteBlueBar}
\noindent

我将如何围绕doctesting这个?我没有看到任何使用doctest的例子,不会返回类/结构或数字或其他简单的表示(例如," \ n"似乎不起作用)

这里的函数ProcessImports(): http://pastebin.com/3JjnyKjK

任何帮助将不胜感激!

编辑:可能想忽略顶部尝试的doctest。只是我试图弄清楚是否可以让它发挥作用并且失败可怕。

1 个答案:

答案 0 :(得分:0)

以下示例显示了一种成功的方法;请记住,doctest只需要"看起来像"口译员会议:

from textwrap import dedent

def function(s):
    """Testing doctests.

    >>> print function('''%@import
    ... blahblah
    ... %@''')
    <BLANKLINE>
    \\begin{shadedquoteBlueBar}
    \\fontsize{9pt}{9pt}
    \\begin{Verbatim}
    blahblah}
    \\end{Verbatim}
    \\end{shadedquoteBlueBar}
    \\noindent

    """
    s = dedent(r"""
               \begin{shadedquoteBlueBar}
               \fontsize{9pt}{9pt}
               \begin{Verbatim}
               blahblah}
               \end{Verbatim}
               \end{shadedquoteBlueBar}
               \noindent""")
    return s

注意文档字符串中函数输入的连续字符...