Search code examples
pythondoctest

Python/Doctest - doctesting complex strings


I'm a student working on an assignment where I'm supposed to doctest all internal functions. My functions use complex strings and return complex strings as well, so I have no idea how to do it. For example my function "ProcessImports()" can take the string:

%@import
blahblah
%@

And return:

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

How would I go around doctesting this? I've not seen any examples of using doctest that don't return classes/structs or numbers or other simple representations ("\n" doesn't seem to work, for example).

Here's the function ProcessImports(): http://pastebin.com/3JjnyKjK

Any help would be appreciated!

Edit: might wanna ignore the attempted doctest at the top. That's just me trying to mess around to find out if I can make it work and failing horribly at it.


Solution

  • The following example shows one successful approach; bear in mind that the doctest just needs to "look like" an interpreter session:

    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
    

    Note the continuation characters ... on the input to the function within the docstring.