File: string_test.py

package info (click to toggle)
pyvlx 0.2.26-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 4,464 kB
  • sloc: python: 7,901; makefile: 39; sh: 5
file content (44 lines) | stat: -rw-r--r-- 1,574 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""Unit tests string_helper module."""
import unittest

from pyvlx.exception import PyVLXException
from pyvlx.string_helper import bytes_to_string, string_to_bytes


class TestString(unittest.TestCase):
    """Test class for String encoding/decoding."""

    # pylint: disable=too-many-public-methods,invalid-name

    def test_encoding(self) -> None:
        """Test simple encoding."""
        self.assertEqual(string_to_bytes("fnord", 10), b"fnord" + bytes(5))

    def test_encoding_without_padding(self) -> None:
        """Test encoding with exact match of size."""
        self.assertEqual(string_to_bytes("fnord", 5), b"fnord")

    def test_encoding_failure(self) -> None:
        """Test error while encoding."""
        with self.assertRaises(PyVLXException):
            string_to_bytes("fnord", 4)

    def test_decoding(self) -> None:
        """Test decoding of string."""
        self.assertEqual(bytes_to_string(b"fnord" + bytes(5)), "fnord")

    def test_decoding_without_padding(self) -> None:
        """Test decoding of string without padding."""
        self.assertEqual(bytes_to_string(b"fnord"), "fnord")

    def test_encode_utf8(self) -> None:
        """Test encoding a string with special characters."""
        self.assertEqual(
            string_to_bytes("Fenster Büro", 16), b"Fenster B\xc3\xbcro\x00\x00\x00"
        )

    def test_decode_utf8(self) -> None:
        """Test decoding a string with special characters."""
        self.assertEqual(
            bytes_to_string(b"Fenster B\xc3\xbcro\x00\x00\x00"), "Fenster Büro"
        )