Skip to content

Commit c4de698

Browse files
Merge pull request #4 from michealroberts/feature/errors/SerialWriteError
feat: add SerialWriteError to errors in samps module
2 parents 3e76586 + 7776a60 commit c4de698

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

src/samps/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# **************************************************************************************
77

8-
from .errors import SerialReadError
8+
from .errors import SerialReadError, SerialWriteError
99

1010
# **************************************************************************************
1111

@@ -19,6 +19,7 @@
1919

2020
__all__: list[str] = [
2121
"SerialReadError",
22+
"SerialWriteError",
2223
]
2324

2425
# **************************************************************************************

src/samps/errors.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,15 @@ def __init__(self, message: str) -> None:
1616

1717

1818
# **************************************************************************************
19+
20+
21+
class SerialWriteError(Exception):
22+
"""
23+
Exception class for serial write errors.
24+
"""
25+
26+
def __init__(self, message: str) -> None:
27+
super().__init__(message)
28+
29+
30+
# **************************************************************************************

test/test_errors.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import unittest
99

10-
from samps import SerialReadError
10+
from samps import SerialReadError, SerialWriteError
1111

1212
# **************************************************************************************
1313

@@ -35,3 +35,28 @@ def test_raising_error(self):
3535

3636

3737
# **************************************************************************************
38+
39+
40+
class TestSerialWriteError(unittest.TestCase):
41+
def test_inheritance(self):
42+
"""Test that SerialWriteError inherits from the built-in Exception class."""
43+
self.assertTrue(issubclass(SerialWriteError, Exception))
44+
45+
def test_error_message(self):
46+
"""Test that the error message is correctly set and retrieved."""
47+
test_message = "This is a test error message."
48+
error = SerialWriteError(test_message)
49+
self.assertEqual(str(error), test_message)
50+
51+
def test_raising_error(self):
52+
"""
53+
Test that raising SerialWriteError with a specific message
54+
results in that message being available on the exception.
55+
"""
56+
test_message = "An error occurred."
57+
with self.assertRaises(SerialWriteError) as context:
58+
raise SerialWriteError(test_message)
59+
self.assertEqual(str(context.exception), test_message)
60+
61+
62+
# **************************************************************************************

0 commit comments

Comments
 (0)