| 1 |
#!/usr/bin/env python |
| 2 |
from io import BytesIO |
| 3 |
import typing |
| 4 |
import unittest |
| 5 |
|
| 6 |
from asammdf.blocks.utils import MdfException |
| 7 |
from asammdf.blocks.v4_blocks import TextBlock |
| 8 |
|
| 9 |
|
| 10 |
class TestATBLOCK(unittest.TestCase): |
| 11 |
plain_text: str |
| 12 |
plain_bytes: bytes |
| 13 |
plain_stream: BytesIO |
| 14 |
meta_text: str |
| 15 |
meta_bytes: bytes |
| 16 |
meta_stream: BytesIO |
| 17 |
|
| 18 |
@classmethod |
| 19 |
def setUpClass(cls) -> None: |
| 20 |
cls.plain_text = "sample text" |
| 21 |
cls.plain_bytes = b"##TX\x00\x00\x00\x00(\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00sample text\x00\x00\x00\x00\x00" |
| 22 |
cls.plain_stream = BytesIO() |
| 23 |
cls.plain_stream.write(cls.plain_bytes) |
| 24 |
|
| 25 |
cls.meta_text = "<CN>sample text</CN>" |
| 26 |
cls.meta_bytes = b"##MD\x00\x00\x00\x000\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00<CN>sample text</CN>\x00\x00\x00\x00" |
| 27 |
cls.meta_stream = BytesIO() |
| 28 |
cls.meta_stream.write(cls.meta_bytes) |
| 29 |
|
| 30 |
def test_read(self) -> None: |
| 31 |
self.plain_stream.seek(0) |
| 32 |
|
| 33 |
block = TextBlock(address=0, stream=self.plain_stream) |
| 34 |
|
| 35 |
self.assertEqual(block.id, b"##TX") |
| 36 |
self.assertIsInstance(block.text, bytes) |
| 37 |
block.text = typing.cast(bytes, block.text) |
| 38 |
self.assertEqual(block.text.strip(b"\0").decode("utf-8"), self.plain_text) |
| 39 |
|
| 40 |
self.meta_stream.seek(0) |
| 41 |
|
| 42 |
block = TextBlock(address=0, stream=self.meta_stream) |
| 43 |
|
| 44 |
self.assertEqual(block.id, b"##MD") |
| 45 |
self.assertIsInstance(block.text, bytes) |
| 46 |
block.text = typing.cast(bytes, block.text) |
| 47 |
self.assertEqual(block.text.strip(b"\0").decode("utf-8"), self.meta_text) |
| 48 |
|
| 49 |
def test_read_wrong_id(self) -> None: |
| 50 |
stream = BytesIO(self.plain_bytes) |
| 51 |
stream.seek(0) |
| 52 |
stream.write(b"_NOK") |
| 53 |
|
| 54 |
with self.assertRaises(MdfException): |
| 55 |
TextBlock(address=0, stream=stream) |
| 56 |
|
| 57 |
def test_bytes(self) -> None: |
| 58 |
block = TextBlock(text=self.plain_text, meta=False) |
| 59 |
self.assertEqual(bytes(block), self.plain_bytes) |
| 60 |
|
| 61 |
block = TextBlock(text=self.meta_text, meta=True) |
| 62 |
self.assertEqual(bytes(block), self.meta_bytes) |
| 63 |
|
| 64 |
|
| 65 |
if __name__ == "__main__": |
| 66 |
unittest.main() |