File: common.py

package info (click to toggle)
pytagsfs 0.6.0-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 628 kB
  • ctags: 839
  • sloc: python: 4,554; xml: 523; sh: 73; makefile: 35
file content (53 lines) | stat: -rw-r--r-- 1,860 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
# Copyright (c) 2005-2008 Forest Bond.
# This file is part of the pytagsfs software package.
#
# pytagsfs is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License version 2 as published by the Free
# Software Foundation.
#
# A copy of the license has been included in the COPYING file.

# A lot of this code was ripped out of sclapp's test/common.py

import doctest

from sclapp.util import importName

# For some reason, it is important that the class is defined in a separate
# namespace.  If it is not defined in a separate namespace (i.e., directly in
# getAllDocTestCases, below), the same doctest is used for all test cases.
# This is pretty strange, but it's probably best to separate this, anyway.
def _makeDocTestCase(test):
    class _DocTestCase(doctest.DocTestCase):
        def __init__(self, *args, **kwargs):
            doctest.DocTestCase.__init__(self, test)
    _DocTestCase.__name__ = '%s_TestCase' % test.name.split('.')[-1]
    return _DocTestCase

def getAllDocTestCases(name):
    mod = importName(name)

    finder = doctest.DocTestFinder()
    tests = finder.find(mod)

    doc_test_cases = [ ]
    for test in tests:
        doc_test_cases.append(_makeDocTestCase(test))
    return doc_test_cases

def defineDocTestCases(dest_name, src_name = None):
    if src_name is None:
        src_name = dest_name

    dest_mod = importName(dest_name)
    for test_case in getAllDocTestCases(src_name):
        test_case.__module__ = dest_mod
        dest_mod.__dict__[test_case.__name__] = test_case

def copy_private_inherited_test_methods(cls):
    for base_cls in cls.__bases__:
        for name in dir(base_cls):
            if name.startswith('_test'):
                value = getattr(base_cls, name)
                new_name = name[1:]
                setattr(cls, new_name, value)