Can I have an ellipsis at the beginning of the line in a Python doctest?

前端 未结 4 2033
我寻月下人不归
我寻月下人不归 2021-01-17 09:37

Python doctests are cool. Let me start with a simple example:

def foo():
  \"\"\"
  >>> foo()
  hello world
  \"\"\"
  print \"hello world\"
         


        
4条回答
  •  不思量自难忘°
    2021-01-17 10:13

    Here's a quick and dirty hack for you:

    def foo():
        """
        >>> foo() # doctest: +ELLIPSIS
        [...] world
        """
        print "hello world"
    
    if __name__ == "__main__":
        import doctest
    
        OC = doctest.OutputChecker
        class AEOutputChecker(OC):
            def check_output(self, want, got, optionflags):
                from re import sub
                if optionflags & doctest.ELLIPSIS:
                    want = sub(r'\[\.\.\.\]', '...', want)
                return OC.check_output(self, want, got, optionflags)
    
        doctest.OutputChecker = AEOutputChecker
        doctest.testmod()
    

    This still understands the normal ( ... ) ellipsis, but it adds a new one ( [...] ) that doesn't raise the line start ambiguity.

    It would be seriously hard for doctest to guess whether there is a line continuation pending or whether its a line start ellipsis - it can be done, in theory, if you subclass DocTestParser to do that work but it probably won't be fun.

    In complex situations you should probably roll your own DocTestRunner that would use the new OutputChecker and use that instead of the normal testmod but this should do in simple scenarios.

提交回复
热议问题