1 | n/a | import os |
---|
2 | n/a | import unittest |
---|
3 | n/a | import collections |
---|
4 | n/a | import email |
---|
5 | n/a | from email.message import Message |
---|
6 | n/a | from email._policybase import compat32 |
---|
7 | n/a | from test.support import load_package_tests |
---|
8 | n/a | from test.test_email import __file__ as landmark |
---|
9 | n/a | |
---|
10 | n/a | # Load all tests in package |
---|
11 | n/a | def load_tests(*args): |
---|
12 | n/a | return load_package_tests(os.path.dirname(__file__), *args) |
---|
13 | n/a | |
---|
14 | n/a | |
---|
15 | n/a | # helper code used by a number of test modules. |
---|
16 | n/a | |
---|
17 | n/a | def openfile(filename, *args, **kws): |
---|
18 | n/a | path = os.path.join(os.path.dirname(landmark), 'data', filename) |
---|
19 | n/a | return open(path, *args, **kws) |
---|
20 | n/a | |
---|
21 | n/a | |
---|
22 | n/a | # Base test class |
---|
23 | n/a | class TestEmailBase(unittest.TestCase): |
---|
24 | n/a | |
---|
25 | n/a | maxDiff = None |
---|
26 | n/a | # Currently the default policy is compat32. By setting that as the default |
---|
27 | n/a | # here we make minimal changes in the test_email tests compared to their |
---|
28 | n/a | # pre-3.3 state. |
---|
29 | n/a | policy = compat32 |
---|
30 | n/a | # Likewise, the default message object is Message. |
---|
31 | n/a | message = Message |
---|
32 | n/a | |
---|
33 | n/a | def __init__(self, *args, **kw): |
---|
34 | n/a | super().__init__(*args, **kw) |
---|
35 | n/a | self.addTypeEqualityFunc(bytes, self.assertBytesEqual) |
---|
36 | n/a | |
---|
37 | n/a | # Backward compatibility to minimize test_email test changes. |
---|
38 | n/a | ndiffAssertEqual = unittest.TestCase.assertEqual |
---|
39 | n/a | |
---|
40 | n/a | def _msgobj(self, filename): |
---|
41 | n/a | with openfile(filename) as fp: |
---|
42 | n/a | return email.message_from_file(fp, policy=self.policy) |
---|
43 | n/a | |
---|
44 | n/a | def _str_msg(self, string, message=None, policy=None): |
---|
45 | n/a | if policy is None: |
---|
46 | n/a | policy = self.policy |
---|
47 | n/a | if message is None: |
---|
48 | n/a | message = self.message |
---|
49 | n/a | return email.message_from_string(string, message, policy=policy) |
---|
50 | n/a | |
---|
51 | n/a | def _bytes_msg(self, bytestring, message=None, policy=None): |
---|
52 | n/a | if policy is None: |
---|
53 | n/a | policy = self.policy |
---|
54 | n/a | if message is None: |
---|
55 | n/a | message = self.message |
---|
56 | n/a | return email.message_from_bytes(bytestring, message, policy=policy) |
---|
57 | n/a | |
---|
58 | n/a | def _make_message(self): |
---|
59 | n/a | return self.message(policy=self.policy) |
---|
60 | n/a | |
---|
61 | n/a | def _bytes_repr(self, b): |
---|
62 | n/a | return [repr(x) for x in b.splitlines(keepends=True)] |
---|
63 | n/a | |
---|
64 | n/a | def assertBytesEqual(self, first, second, msg): |
---|
65 | n/a | """Our byte strings are really encoded strings; improve diff output""" |
---|
66 | n/a | self.assertEqual(self._bytes_repr(first), self._bytes_repr(second)) |
---|
67 | n/a | |
---|
68 | n/a | def assertDefectsEqual(self, actual, expected): |
---|
69 | n/a | self.assertEqual(len(actual), len(expected), actual) |
---|
70 | n/a | for i in range(len(actual)): |
---|
71 | n/a | self.assertIsInstance(actual[i], expected[i], |
---|
72 | n/a | 'item {}'.format(i)) |
---|
73 | n/a | |
---|
74 | n/a | |
---|
75 | n/a | def parameterize(cls): |
---|
76 | n/a | """A test method parameterization class decorator. |
---|
77 | n/a | |
---|
78 | n/a | Parameters are specified as the value of a class attribute that ends with |
---|
79 | n/a | the string '_params'. Call the portion before '_params' the prefix. Then |
---|
80 | n/a | a method to be parameterized must have the same prefix, the string |
---|
81 | n/a | '_as_', and an arbitrary suffix. |
---|
82 | n/a | |
---|
83 | n/a | The value of the _params attribute may be either a dictionary or a list. |
---|
84 | n/a | The values in the dictionary and the elements of the list may either be |
---|
85 | n/a | single values, or a list. If single values, they are turned into single |
---|
86 | n/a | element tuples. However derived, the resulting sequence is passed via |
---|
87 | n/a | *args to the parameterized test function. |
---|
88 | n/a | |
---|
89 | n/a | In a _params dictionary, the keys become part of the name of the generated |
---|
90 | n/a | tests. In a _params list, the values in the list are converted into a |
---|
91 | n/a | string by joining the string values of the elements of the tuple by '_' and |
---|
92 | n/a | converting any blanks into '_'s, and this become part of the name. |
---|
93 | n/a | The full name of a generated test is a 'test_' prefix, the portion of the |
---|
94 | n/a | test function name after the '_as_' separator, plus an '_', plus the name |
---|
95 | n/a | derived as explained above. |
---|
96 | n/a | |
---|
97 | n/a | For example, if we have: |
---|
98 | n/a | |
---|
99 | n/a | count_params = range(2) |
---|
100 | n/a | |
---|
101 | n/a | def count_as_foo_arg(self, foo): |
---|
102 | n/a | self.assertEqual(foo+1, myfunc(foo)) |
---|
103 | n/a | |
---|
104 | n/a | we will get parameterized test methods named: |
---|
105 | n/a | test_foo_arg_0 |
---|
106 | n/a | test_foo_arg_1 |
---|
107 | n/a | test_foo_arg_2 |
---|
108 | n/a | |
---|
109 | n/a | Or we could have: |
---|
110 | n/a | |
---|
111 | n/a | example_params = {'foo': ('bar', 1), 'bing': ('bang', 2)} |
---|
112 | n/a | |
---|
113 | n/a | def example_as_myfunc_input(self, name, count): |
---|
114 | n/a | self.assertEqual(name+str(count), myfunc(name, count)) |
---|
115 | n/a | |
---|
116 | n/a | and get: |
---|
117 | n/a | test_myfunc_input_foo |
---|
118 | n/a | test_myfunc_input_bing |
---|
119 | n/a | |
---|
120 | n/a | Note: if and only if the generated test name is a valid identifier can it |
---|
121 | n/a | be used to select the test individually from the unittest command line. |
---|
122 | n/a | |
---|
123 | n/a | The values in the params dict can be a single value, a tuple, or a |
---|
124 | n/a | dict. If a single value of a tuple, it is passed to the test function |
---|
125 | n/a | as positional arguments. If a dict, it is a passed via **kw. |
---|
126 | n/a | |
---|
127 | n/a | """ |
---|
128 | n/a | paramdicts = {} |
---|
129 | n/a | testers = collections.defaultdict(list) |
---|
130 | n/a | for name, attr in cls.__dict__.items(): |
---|
131 | n/a | if name.endswith('_params'): |
---|
132 | n/a | if not hasattr(attr, 'keys'): |
---|
133 | n/a | d = {} |
---|
134 | n/a | for x in attr: |
---|
135 | n/a | if not hasattr(x, '__iter__'): |
---|
136 | n/a | x = (x,) |
---|
137 | n/a | n = '_'.join(str(v) for v in x).replace(' ', '_') |
---|
138 | n/a | d[n] = x |
---|
139 | n/a | attr = d |
---|
140 | n/a | paramdicts[name[:-7] + '_as_'] = attr |
---|
141 | n/a | if '_as_' in name: |
---|
142 | n/a | testers[name.split('_as_')[0] + '_as_'].append(name) |
---|
143 | n/a | testfuncs = {} |
---|
144 | n/a | for name in paramdicts: |
---|
145 | n/a | if name not in testers: |
---|
146 | n/a | raise ValueError("No tester found for {}".format(name)) |
---|
147 | n/a | for name in testers: |
---|
148 | n/a | if name not in paramdicts: |
---|
149 | n/a | raise ValueError("No params found for {}".format(name)) |
---|
150 | n/a | for name, attr in cls.__dict__.items(): |
---|
151 | n/a | for paramsname, paramsdict in paramdicts.items(): |
---|
152 | n/a | if name.startswith(paramsname): |
---|
153 | n/a | testnameroot = 'test_' + name[len(paramsname):] |
---|
154 | n/a | for paramname, params in paramsdict.items(): |
---|
155 | n/a | if hasattr(params, 'keys'): |
---|
156 | n/a | test = (lambda self, name=name, params=params: |
---|
157 | n/a | getattr(self, name)(**params)) |
---|
158 | n/a | else: |
---|
159 | n/a | test = (lambda self, name=name, params=params: |
---|
160 | n/a | getattr(self, name)(*params)) |
---|
161 | n/a | testname = testnameroot + '_' + paramname |
---|
162 | n/a | test.__name__ = testname |
---|
163 | n/a | testfuncs[testname] = test |
---|
164 | n/a | for key, value in testfuncs.items(): |
---|
165 | n/a | setattr(cls, key, value) |
---|
166 | n/a | return cls |
---|