|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import unittest |
| 4 | +import can |
| 5 | +from can.interfaces.socketcand import socketcand |
| 6 | + |
| 7 | + |
| 8 | +class TestConvertAsciiMessageToCanMessage(unittest.TestCase): |
| 9 | + def test_valid_frame_message(self): |
| 10 | + # Example: < frame 123 1680000000.0 01020304 > |
| 11 | + ascii_msg = "< frame 123 1680000000.0 01020304 >" |
| 12 | + msg = socketcand.convert_ascii_message_to_can_message(ascii_msg) |
| 13 | + self.assertIsInstance(msg, can.Message) |
| 14 | + self.assertEqual(msg.arbitration_id, 0x123) |
| 15 | + self.assertEqual(msg.timestamp, 1680000000.0) |
| 16 | + self.assertEqual(msg.data, bytearray([1, 2, 3, 4])) |
| 17 | + self.assertEqual(msg.dlc, 4) |
| 18 | + self.assertFalse(msg.is_extended_id) |
| 19 | + self.assertTrue(msg.is_rx) |
| 20 | + |
| 21 | + def test_valid_error_message(self): |
| 22 | + # Example: < error 1ABCDEF0 1680000001.0 > |
| 23 | + ascii_msg = "< error 1ABCDEF0 1680000001.0 >" |
| 24 | + msg = socketcand.convert_ascii_message_to_can_message(ascii_msg) |
| 25 | + self.assertIsInstance(msg, can.Message) |
| 26 | + self.assertEqual(msg.arbitration_id, 0x1ABCDEF0) |
| 27 | + self.assertEqual(msg.timestamp, 1680000001.0) |
| 28 | + self.assertEqual(msg.data, bytearray([0])) |
| 29 | + self.assertEqual(msg.dlc, 1) |
| 30 | + self.assertTrue(msg.is_extended_id) |
| 31 | + self.assertTrue(msg.is_error_frame) |
| 32 | + self.assertTrue(msg.is_rx) |
| 33 | + |
| 34 | + def test_invalid_message(self): |
| 35 | + ascii_msg = "< unknown 123 0.0 >" |
| 36 | + msg = socketcand.convert_ascii_message_to_can_message(ascii_msg) |
| 37 | + self.assertIsNone(msg) |
| 38 | + |
| 39 | + def test_missing_ending_character(self): |
| 40 | + ascii_msg = "< frame 123 1680000000.0 01020304" |
| 41 | + msg = socketcand.convert_ascii_message_to_can_message(ascii_msg) |
| 42 | + self.assertIsNone(msg) |
| 43 | + |
| 44 | + |
| 45 | +if __name__ == "__main__": |
| 46 | + unittest.main() |
0 commit comments