PageRenderTime 224ms CodeModel.GetById 120ms app.highlight 5ms RepoModel.GetById 96ms app.codeStats 0ms

/Lib/test/test_doctest2.py

http://unladen-swallow.googlecode.com/
Python | 127 lines | 126 code | 0 blank | 1 comment | 1 complexity | 71d68d27a3d2cc462131ff8fcba4191a MD5 | raw file
  1# -*- coding: utf-8 -*-
  2u"""A module to test whether doctest recognizes some 2.2 features,
  3like static and class methods.
  4
  5>>> print 'yup'  # 1
  6yup
  7
  8We include some (random) encoded (utf-8) text in the text surrounding
  9the example.  It should be ignored:
 10
 11ЉЊЈЁЂ
 12
 13"""
 14
 15import sys
 16from test import test_support
 17
 18class C(object):
 19    u"""Class C.
 20
 21    >>> print C()  # 2
 22    42
 23
 24
 25    We include some (random) encoded (utf-8) text in the text surrounding
 26    the example.  It should be ignored:
 27
 28        ЉЊЈЁЂ
 29
 30    """
 31
 32    def __init__(self):
 33        """C.__init__.
 34
 35        >>> print C() # 3
 36        42
 37        """
 38
 39    def __str__(self):
 40        """
 41        >>> print C() # 4
 42        42
 43        """
 44        return "42"
 45
 46    class D(object):
 47        """A nested D class.
 48
 49        >>> print "In D!"   # 5
 50        In D!
 51        """
 52
 53        def nested(self):
 54            """
 55            >>> print 3 # 6
 56            3
 57            """
 58
 59    def getx(self):
 60        """
 61        >>> c = C()    # 7
 62        >>> c.x = 12   # 8
 63        >>> print c.x  # 9
 64        -12
 65        """
 66        return -self._x
 67
 68    def setx(self, value):
 69        """
 70        >>> c = C()     # 10
 71        >>> c.x = 12    # 11
 72        >>> print c.x   # 12
 73        -12
 74        """
 75        self._x = value
 76
 77    x = property(getx, setx, doc="""\
 78        >>> c = C()    # 13
 79        >>> c.x = 12   # 14
 80        >>> print c.x  # 15
 81        -12
 82        """)
 83
 84    @staticmethod
 85    def statm():
 86        """
 87        A static method.
 88
 89        >>> print C.statm()    # 16
 90        666
 91        >>> print C().statm()  # 17
 92        666
 93        """
 94        return 666
 95
 96    @classmethod
 97    def clsm(cls, val):
 98        """
 99        A class method.
100
101        >>> print C.clsm(22)    # 18
102        22
103        >>> print C().clsm(23)  # 19
104        23
105        """
106        return val
107
108def test_main():
109    from test import test_doctest2
110    if sys.flags.optimize >= 2:
111        print >>sys.stderr, "test_doctest2 --",
112        print >>sys.stderr, "skipping some tests due to -O flag."
113        sys.stderr.flush()
114        EXPECTED = 3
115    else:
116        EXPECTED = 19
117    f, t = test_support.run_doctest(test_doctest2)
118    if t != EXPECTED:
119        raise test_support.TestFailed("expected %d tests to run, not %d" %
120                                      (EXPECTED, t))
121
122# Pollute the namespace with a bunch of imported functions and classes,
123# to make sure they don't get tested.
124from doctest import *
125
126if __name__ == '__main__':
127    test_main()