ยปCore Development>Code coverage>Lib/unittest/util.py

Python code coverage for Lib/unittest/util.py

#countcontent
1n/a"""Various utility functions."""
2n/a
3n/afrom collections import namedtuple, OrderedDict
4n/afrom os.path import commonprefix
5n/a
6n/a__unittest = True
7n/a
8n/a_MAX_LENGTH = 80
9n/a_PLACEHOLDER_LEN = 12
10n/a_MIN_BEGIN_LEN = 5
11n/a_MIN_END_LEN = 5
12n/a_MIN_COMMON_LEN = 5
13n/a_MIN_DIFF_LEN = _MAX_LENGTH - \
14n/a (_MIN_BEGIN_LEN + _PLACEHOLDER_LEN + _MIN_COMMON_LEN +
15n/a _PLACEHOLDER_LEN + _MIN_END_LEN)
16n/aassert _MIN_DIFF_LEN >= 0
17n/a
18n/adef _shorten(s, prefixlen, suffixlen):
19n/a skip = len(s) - prefixlen - suffixlen
20n/a if skip > _PLACEHOLDER_LEN:
21n/a s = '%s[%d chars]%s' % (s[:prefixlen], skip, s[len(s) - suffixlen:])
22n/a return s
23n/a
24n/adef _common_shorten_repr(*args):
25n/a args = tuple(map(safe_repr, args))
26n/a maxlen = max(map(len, args))
27n/a if maxlen <= _MAX_LENGTH:
28n/a return args
29n/a
30n/a prefix = commonprefix(args)
31n/a prefixlen = len(prefix)
32n/a
33n/a common_len = _MAX_LENGTH - \
34n/a (maxlen - prefixlen + _MIN_BEGIN_LEN + _PLACEHOLDER_LEN)
35n/a if common_len > _MIN_COMMON_LEN:
36n/a assert _MIN_BEGIN_LEN + _PLACEHOLDER_LEN + _MIN_COMMON_LEN + \
37n/a (maxlen - prefixlen) < _MAX_LENGTH
38n/a prefix = _shorten(prefix, _MIN_BEGIN_LEN, common_len)
39n/a return tuple(prefix + s[prefixlen:] for s in args)
40n/a
41n/a prefix = _shorten(prefix, _MIN_BEGIN_LEN, _MIN_COMMON_LEN)
42n/a return tuple(prefix + _shorten(s[prefixlen:], _MIN_DIFF_LEN, _MIN_END_LEN)
43n/a for s in args)
44n/a
45n/adef safe_repr(obj, short=False):
46n/a try:
47n/a result = repr(obj)
48n/a except Exception:
49n/a result = object.__repr__(obj)
50n/a if not short or len(result) < _MAX_LENGTH:
51n/a return result
52n/a return result[:_MAX_LENGTH] + ' [truncated]...'
53n/a
54n/adef strclass(cls):
55n/a return "%s.%s" % (cls.__module__, cls.__qualname__)
56n/a
57n/adef sorted_list_difference(expected, actual):
58n/a """Finds elements in only one or the other of two, sorted input lists.
59n/a
60n/a Returns a two-element tuple of lists. The first list contains those
61n/a elements in the "expected" list but not in the "actual" list, and the
62n/a second contains those elements in the "actual" list but not in the
63n/a "expected" list. Duplicate elements in either input list are ignored.
64n/a """
65n/a i = j = 0
66n/a missing = []
67n/a unexpected = []
68n/a while True:
69n/a try:
70n/a e = expected[i]
71n/a a = actual[j]
72n/a if e < a:
73n/a missing.append(e)
74n/a i += 1
75n/a while expected[i] == e:
76n/a i += 1
77n/a elif e > a:
78n/a unexpected.append(a)
79n/a j += 1
80n/a while actual[j] == a:
81n/a j += 1
82n/a else:
83n/a i += 1
84n/a try:
85n/a while expected[i] == e:
86n/a i += 1
87n/a finally:
88n/a j += 1
89n/a while actual[j] == a:
90n/a j += 1
91n/a except IndexError:
92n/a missing.extend(expected[i:])
93n/a unexpected.extend(actual[j:])
94n/a break
95n/a return missing, unexpected
96n/a
97n/a
98n/adef unorderable_list_difference(expected, actual):
99n/a """Same behavior as sorted_list_difference but
100n/a for lists of unorderable items (like dicts).
101n/a
102n/a As it does a linear search per item (remove) it
103n/a has O(n*n) performance."""
104n/a missing = []
105n/a while expected:
106n/a item = expected.pop()
107n/a try:
108n/a actual.remove(item)
109n/a except ValueError:
110n/a missing.append(item)
111n/a
112n/a # anything left in actual is unexpected
113n/a return missing, actual
114n/a
115n/adef three_way_cmp(x, y):
116n/a """Return -1 if x < y, 0 if x == y and 1 if x > y"""
117n/a return (x > y) - (x < y)
118n/a
119n/a_Mismatch = namedtuple('Mismatch', 'actual expected value')
120n/a
121n/adef _count_diff_all_purpose(actual, expected):
122n/a 'Returns list of (cnt_act, cnt_exp, elem) triples where the counts differ'
123n/a # elements need not be hashable
124n/a s, t = list(actual), list(expected)
125n/a m, n = len(s), len(t)
126n/a NULL = object()
127n/a result = []
128n/a for i, elem in enumerate(s):
129n/a if elem is NULL:
130n/a continue
131n/a cnt_s = cnt_t = 0
132n/a for j in range(i, m):
133n/a if s[j] == elem:
134n/a cnt_s += 1
135n/a s[j] = NULL
136n/a for j, other_elem in enumerate(t):
137n/a if other_elem == elem:
138n/a cnt_t += 1
139n/a t[j] = NULL
140n/a if cnt_s != cnt_t:
141n/a diff = _Mismatch(cnt_s, cnt_t, elem)
142n/a result.append(diff)
143n/a
144n/a for i, elem in enumerate(t):
145n/a if elem is NULL:
146n/a continue
147n/a cnt_t = 0
148n/a for j in range(i, n):
149n/a if t[j] == elem:
150n/a cnt_t += 1
151n/a t[j] = NULL
152n/a diff = _Mismatch(0, cnt_t, elem)
153n/a result.append(diff)
154n/a return result
155n/a
156n/adef _ordered_count(iterable):
157n/a 'Return dict of element counts, in the order they were first seen'
158n/a c = OrderedDict()
159n/a for elem in iterable:
160n/a c[elem] = c.get(elem, 0) + 1
161n/a return c
162n/a
163n/adef _count_diff_hashable(actual, expected):
164n/a 'Returns list of (cnt_act, cnt_exp, elem) triples where the counts differ'
165n/a # elements must be hashable
166n/a s, t = _ordered_count(actual), _ordered_count(expected)
167n/a result = []
168n/a for elem, cnt_s in s.items():
169n/a cnt_t = t.get(elem, 0)
170n/a if cnt_s != cnt_t:
171n/a diff = _Mismatch(cnt_s, cnt_t, elem)
172n/a result.append(diff)
173n/a for elem, cnt_t in t.items():
174n/a if elem not in s:
175n/a diff = _Mismatch(0, cnt_t, elem)
176n/a result.append(diff)
177n/a return result