diff options
Diffstat (limited to 'tests/test_error.py')
-rw-r--r-- | tests/test_error.py | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/tests/test_error.py b/tests/test_error.py new file mode 100644 index 00000000..82b00c24 --- /dev/null +++ b/tests/test_error.py | |||
@@ -0,0 +1,53 @@ | |||
1 | # Copyright 2021 The Android Open Source Project | ||
2 | # | ||
3 | # Licensed under the Apache License, Version 2.0 (the "License"); | ||
4 | # you may not use this file except in compliance with the License. | ||
5 | # You may obtain a copy of the License at | ||
6 | # | ||
7 | # http://www.apache.org/licenses/LICENSE-2.0 | ||
8 | # | ||
9 | # Unless required by applicable law or agreed to in writing, software | ||
10 | # distributed under the License is distributed on an "AS IS" BASIS, | ||
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
12 | # See the License for the specific language governing permissions and | ||
13 | # limitations under the License. | ||
14 | |||
15 | """Unittests for the error.py module.""" | ||
16 | |||
17 | import inspect | ||
18 | import pickle | ||
19 | import unittest | ||
20 | |||
21 | import error | ||
22 | |||
23 | |||
24 | class PickleTests(unittest.TestCase): | ||
25 | """Make sure all our custom exceptions can be pickled.""" | ||
26 | |||
27 | def getExceptions(self): | ||
28 | """Return all our custom exceptions.""" | ||
29 | for name in dir(error): | ||
30 | cls = getattr(error, name) | ||
31 | if isinstance(cls, type) and issubclass(cls, Exception): | ||
32 | yield cls | ||
33 | |||
34 | def testExceptionLookup(self): | ||
35 | """Make sure our introspection logic works.""" | ||
36 | classes = list(self.getExceptions()) | ||
37 | self.assertIn(error.HookError, classes) | ||
38 | # Don't assert the exact number to avoid being a change-detector test. | ||
39 | self.assertGreater(len(classes), 10) | ||
40 | |||
41 | def testPickle(self): | ||
42 | """Try to pickle all the exceptions.""" | ||
43 | for cls in self.getExceptions(): | ||
44 | args = inspect.getfullargspec(cls.__init__).args[1:] | ||
45 | obj = cls(*args) | ||
46 | p = pickle.dumps(obj) | ||
47 | try: | ||
48 | newobj = pickle.loads(p) | ||
49 | except Exception as e: # pylint: disable=broad-except | ||
50 | self.fail('Class %s is unable to be pickled: %s\n' | ||
51 | 'Incomplete super().__init__(...) call?' % (cls, e)) | ||
52 | self.assertIsInstance(newobj, cls) | ||
53 | self.assertEqual(str(obj), str(newobj)) | ||