File: doctests.py

package info (click to toggle)
jaydebeapi 1.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 292 kB
  • sloc: python: 963; java: 188; xml: 62; sh: 53; sql: 27; makefile: 3
file content (53 lines) | stat: -rw-r--r-- 1,538 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python
"""Run doctests."""

import doctest
import platform
import re
import sys

try:
    import unittest2 as unittest
except ImportError:
    import unittest

def _is_new_jpype():
    if platform.python_implementation() != 'Jython':
        import jpype
        try:
            ver_match = re.match('\d+\.\d+', jpype.__version__)
            if ver_match:
                jpype_ver = float(ver_match.group(0))
                if jpype_ver >= 0.7:
                    return True
        except ValueError:
            pass
        return False

class Py23DocChecker(doctest.OutputChecker):
    """Doctest checker to avoid Python 2/3 unicode comparison
    issues. Code mostly taken from Dirkjan Ochtman"""
    def check_output(self, want, got, optionflags):
        if sys.version_info[0] > 2 or _is_new_jpype():
            # new python has unicode as default
            # new JPype does not automatically convert to unicode on Python 2
            want = re.sub("u'(.*?)'", "'\\1'", want)
            want = re.sub('u"(.*?)"', '"\\1"', want)
        return doctest.OutputChecker.check_output(self, want, got, optionflags)

def suite():
    suite = unittest.TestSuite()
    suite.addTest(doctest.DocFileSuite('../README.rst',
                                       checker=Py23DocChecker()))
    return suite;

def main():
    runner = unittest.TextTestRunner()
    result = runner.run(suite())
    if result.wasSuccessful():
        return 0
    else:
        return 1

if __name__ == '__main__':
    sys.exit(main())