Coverage for tests/test_Exception_2.py: 19%
63 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-12-14 03:22 -0800
« prev ^ index » next coverage.py v6.5.0, created at 2022-12-14 03:22 -0800
1# This file is part of pex_exceptions.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <https://www.gnu.org/licenses/>.
22import unittest
24import lsst.pex.exceptions
25import _testLib as testLib
28class ExceptionTestCase(unittest.TestCase):
29 """A test case for C++/Python LsstCppExceptions."""
31 def testBaseTranslation(self):
32 self.assertRaises(lsst.pex.exceptions.Exception, testLib.failException1, "message1")
33 try:
34 testLib.failException1("message2")
35 except lsst.pex.exceptions.Exception as err:
36 self.assertEqual(err.what(), "message2")
37 self.assertEqual(repr(err), "Exception('message2')")
38 else:
39 self.fail("Expected Exception not raised")
41 def testDerivedTranslation(self):
42 self.assertRaises(lsst.pex.exceptions.RuntimeError, testLib.failRuntimeError1, "message1")
43 self.assertRaises(lsst.pex.exceptions.Exception, testLib.failRuntimeError1, "message1")
44 try:
45 testLib.failRuntimeError1("message2")
46 except lsst.pex.exceptions.RuntimeError as err:
47 self.assertEqual(err.what(), "message2")
48 self.assertEqual(repr(err), "RuntimeError('message2')")
49 else:
50 self.fail("Expected Exception not raised")
52 def testAddMessage(self):
53 try:
54 testLib.failLogicError2("message1", "message2")
55 except lsst.pex.exceptions.LogicError as err:
56 self.assertEqual(err.what(), "message1 {0}; message2 {1}")
57 self.assertEqual(repr(err), "LogicError('message1 {0}; message2 {1}')")
58 else:
59 self.fail("Expected Exception not raised")
61 def testPythonRaise(self):
62 try:
63 raise lsst.pex.exceptions.LogicError("message1")
64 except lsst.pex.exceptions.Exception as err:
65 self.assertIsInstance(err, lsst.pex.exceptions.LogicError)
66 self.assertEqual(err.what(), "message1")
67 self.assertEqual(repr(err), "LogicError('message1')")
68 self.assertEqual(str(err), "message1")
70 def testCustom(self):
71 self.assertRaises(lsst.pex.exceptions.Exception, testLib.failTestError1, "message1")
72 self.assertRaises(lsst.pex.exceptions.RuntimeError, testLib.failTestError1, "message1")
73 self.assertRaises(RuntimeError, testLib.failTestError1, "message1")
74 self.assertRaises(testLib.TestError, testLib.failTestError1, "message1")
75 try:
76 testLib.failTestError1("message2")
77 except lsst.pex.exceptions.Exception as err:
78 self.assertEqual(err.what(), "message2")
79 self.assertEqual(repr(err), "TestError('message2')")
80 else:
81 self.fail("Expected Exception not raised")
83 def checkHierarchy(self, method, classes):
84 for cls in classes:
85 self.assertRaises(cls, method, "message")
87 def testHierarchy(self):
88 self.checkHierarchy(testLib.failLogicError1,
89 [lsst.pex.exceptions.LogicError,
90 lsst.pex.exceptions.Exception])
91 self.checkHierarchy(testLib.failInvalidParameterError1,
92 [lsst.pex.exceptions.InvalidParameterError,
93 lsst.pex.exceptions.LogicError,
94 lsst.pex.exceptions.Exception])
95 self.checkHierarchy(testLib.failLengthError1,
96 [lsst.pex.exceptions.LengthError,
97 lsst.pex.exceptions.LogicError,
98 lsst.pex.exceptions.Exception])
99 self.checkHierarchy(testLib.failOutOfRangeError1,
100 [lsst.pex.exceptions.OutOfRangeError,
101 lsst.pex.exceptions.LogicError,
102 lsst.pex.exceptions.Exception])
103 self.checkHierarchy(testLib.failRuntimeError1,
104 [lsst.pex.exceptions.RuntimeError,
105 lsst.pex.exceptions.Exception])
106 self.checkHierarchy(testLib.failOverflowError1,
107 [lsst.pex.exceptions.OverflowError,
108 lsst.pex.exceptions.RuntimeError,
109 lsst.pex.exceptions.Exception,
110 OverflowError])
111 self.checkHierarchy(testLib.failUnderflowError1,
112 [lsst.pex.exceptions.UnderflowError,
113 lsst.pex.exceptions.RuntimeError,
114 lsst.pex.exceptions.Exception,
115 ArithmeticError])
116 self.checkHierarchy(testLib.failNotFoundError1,
117 [lsst.pex.exceptions.NotFoundError,
118 lsst.pex.exceptions.Exception,
119 LookupError])
120 self.checkHierarchy(testLib.failIoError1,
121 [lsst.pex.exceptions.IoError,
122 lsst.pex.exceptions.RuntimeError,
123 lsst.pex.exceptions.Exception,
124 IOError])
125 self.checkHierarchy(testLib.failTypeError1,
126 [lsst.pex.exceptions.TypeError,
127 lsst.pex.exceptions.LogicError,
128 lsst.pex.exceptions.Exception,
129 TypeError])
132if __name__ == '__main__': 132 ↛ 133line 132 didn't jump to line 133, because the condition on line 132 was never true
133 unittest.main()