|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
1 | 4 | from collections import namedtuple
|
2 | 5 |
|
3 | 6 |
|
4 | 7 | class TestPath(namedtuple('TestPath', 'root relfile func sub')):
|
5 | 8 | """Where to find a single test."""
|
6 | 9 |
|
| 10 | + def __new__(cls, root, relfile, func, sub=None): |
| 11 | + self = super(TestPath, cls).__new__( |
| 12 | + cls, |
| 13 | + str(root) if root else None, |
| 14 | + str(relfile) if relfile else None, |
| 15 | + str(func) if func else None, |
| 16 | + [str(s) for s in sub] if sub else None, |
| 17 | + ) |
| 18 | + return self |
| 19 | + |
| 20 | + def __init__(self, *args, **kwargs): |
| 21 | + if self.root is None: |
| 22 | + raise TypeError('missing id') |
| 23 | + if self.relfile is None: |
| 24 | + raise TypeError('missing kind') |
| 25 | + # self.func may be None (e.g. for doctests). |
| 26 | + # self.sub may be None. |
| 27 | + |
| 28 | + |
| 29 | +class ParentInfo(namedtuple('ParentInfo', 'id kind name root parentid')): |
| 30 | + |
| 31 | + KINDS = ('folder', 'file', 'suite', 'function', 'subtest') |
| 32 | + |
| 33 | + def __new__(cls, id, kind, name, root=None, parentid=None): |
| 34 | + self = super(ParentInfo, cls).__new__( |
| 35 | + cls, |
| 36 | + str(id) if id else None, |
| 37 | + str(kind) if kind else None, |
| 38 | + str(name) if name else None, |
| 39 | + str(root) if root else None, |
| 40 | + str(parentid) if parentid else None, |
| 41 | + ) |
| 42 | + return self |
| 43 | + |
| 44 | + def __init__(self, *args, **kwargs): |
| 45 | + if self.id is None: |
| 46 | + raise TypeError('missing id') |
| 47 | + if self.kind is None: |
| 48 | + raise TypeError('missing kind') |
| 49 | + if self.kind not in self.KINDS: |
| 50 | + raise ValueError('unsupported kind {!r}'.format(self.kind)) |
| 51 | + if self.name is None: |
| 52 | + raise TypeError('missing name') |
| 53 | + if self.root is None: |
| 54 | + if self.parentid is not None or self.kind != 'folder': |
| 55 | + raise TypeError('missing root') |
| 56 | + elif self.parentid is None: |
| 57 | + raise TypeError('missing parentid') |
| 58 | + |
7 | 59 |
|
8 |
| -class TestInfo(namedtuple('TestInfo', 'id name path lineno markers')): |
| 60 | +class TestInfo(namedtuple('TestInfo', 'id name path source markers parentid kind')): |
9 | 61 | """Info for a single test."""
|
| 62 | + |
| 63 | + MARKERS = ('skip', 'skip-if', 'expected-failure') |
| 64 | + KINDS = ('function', 'doctest') |
| 65 | + |
| 66 | + def __new__(cls, id, name, path, source, markers, parentid, kind='function'): |
| 67 | + self = super(TestInfo, cls).__new__( |
| 68 | + cls, |
| 69 | + str(id) if id else None, |
| 70 | + str(name) if name else None, |
| 71 | + path or None, |
| 72 | + str(source) if source else None, |
| 73 | + [str(marker) for marker in markers or ()], |
| 74 | + str(parentid) if parentid else None, |
| 75 | + str(kind) if kind else None, |
| 76 | + ) |
| 77 | + return self |
| 78 | + |
| 79 | + def __init__(self, *args, **kwargs): |
| 80 | + if self.id is None: |
| 81 | + raise TypeError('missing id') |
| 82 | + if self.name is None: |
| 83 | + raise TypeError('missing name') |
| 84 | + if self.path is None: |
| 85 | + raise TypeError('missing path') |
| 86 | + if self.source is None: |
| 87 | + raise TypeError('missing source') |
| 88 | + else: |
| 89 | + srcfile, _, lineno = self.source.rpartition(':') |
| 90 | + if not srcfile or not lineno or int(lineno) < 0: |
| 91 | + raise ValueError('bad source {!r}'.format(self.source)) |
| 92 | + if self.markers: |
| 93 | + badmarkers = [m for m in self.markers if m not in self.MARKERS] |
| 94 | + if badmarkers: |
| 95 | + raise ValueError('unsupported markers {!r}'.format(badmarkers)) |
| 96 | + if self.parentid is None: |
| 97 | + raise TypeError('missing parentid') |
| 98 | + if self.kind is None: |
| 99 | + raise TypeError('missing kind') |
| 100 | + elif self.kind not in self.KINDS: |
| 101 | + raise ValueError('unsupported kind {!r}'.format(self.kind)) |
| 102 | + |
| 103 | + |
| 104 | + @property |
| 105 | + def root(self): |
| 106 | + return self.path.root |
| 107 | + |
| 108 | + @property |
| 109 | + def srcfile(self): |
| 110 | + return self.source.rpartition(':')[0] |
| 111 | + |
| 112 | + @property |
| 113 | + def lineno(self): |
| 114 | + return int(self.source.rpartition(':')[-1]) |
0 commit comments