Completed
Push — master ( 6737d1...1af191 )
by
unknown
09:46
created

TestElementString.test_alignment()   A

Complexity

Conditions 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
1
#!/usr/bin/env python3
2
3
"""Tests for the elementstring class"""
4
5
import pytest
6
import unittest
7
8
from starstruct.message import Message
9
from starstruct.elementstring import ElementString
10
11
12
# pylint: disable=line-too-long,invalid-name
13
class TestElementString(unittest.TestCase):
14
    """ElementString module tests"""
15
16
    def test_valid(self):
17
        """Test field formats that are valid ElementString elements."""
18
19
        test_fields = [
20
            ('a', 'c'),     # single character
21
            ('b', '2c'),    # 2 char string
22
            ('c', '10s'),   # 10 char string (variable)
23
            ('d', '5p'),    # 5 char string (fixed)
24
        ]
25
26
        for field in test_fields:
27
            with self.subTest(field):  # pylint: disable=no-member
28
                out = ElementString.valid(field)
29
                self.assertTrue(out)
30
31
    def test_not_valid(self):
32
        """Test field formats that are not valid ElementString elements."""
33
34
        test_fields = [
35
            ('a', '4x'),    # 4 pad bytes
36
            ('b', 'z'),     # invalid
37
            ('c', '1'),     # invalid
38
            ('d', '9S'),    # invalid (must be lowercase)
39
            ('e', '/'),     # invalid
40
            ('a', 'b'),     # signed byte: -128, 127
41
            ('b', 'H'),     # unsigned short: 0, 65535
42
            ('d', 'L'),     # unsigned long: 0, 2^32-1
43
            ('e', '?'),     # bool: 0, 1
44
        ]
45
46
        for field in test_fields:
47
            with self.subTest(field):  # pylint: disable=no-member
48
                out = ElementString.valid(field)
49
                self.assertFalse(out)
50
51
    def test_make_andk_pack(self):
52
        """Test field formats that are valid ElementString elements."""
53
        TestStruct = Message('TestStruct', [
54
            ('a', 'c'),     # single character
55
            ('b', '2c'),    # 2 char string
56
            ('c', '10s'),   # 10 char string (variable)
57
            ('d', '9p'),    # 9 ( - 1 ) char string (fixed)
58
            ('e', '5c'),
59
        ])
60
61
        test_data = {
62
            'a': 'i',
63
            'b': 'hi',
64
            'c': 'short',
65
            'd': 'long',
66
            'e': ['l', 'i', 's', 't'],
67
        }
68
69
        made = TestStruct.make(test_data)
70
71
        assert made.a == ['i']
72
        assert made.b == ['h', 'i']
73
        assert made.c == 'short'
74
        assert made.d == 'long\x00\x00\x00\x00'
75
        assert made.e == ['l', 'i', 's', 't', '\x00']
76
77
        packed = TestStruct.pack(test_data)
78
        unpacked = TestStruct.unpack(packed)
79
80
        assert made == unpacked
81
82
    def test_alignment(self):
83
        """Test field formats that are valid ElementString elements."""
84
        TestStruct = Message('TestStruct', [
85
            ('a', 'c'),     # single character
86
            ('b', '2c'),    # 2 char string
87
        ])
88
89
        test_data = {
90
            'a': 'a',
91
            'b': 'no',
92
        }
93
94
        TestStruct.update(alignment=4)
95
        packed = TestStruct.pack(test_data)
96
        assert packed == b'a\x00\x00\x00no\x00\x00'
97
98
    def test_bad_values(self):
99
        """Test field formats that are valid ElementString elements."""
100
        TestStruct = Message('TestStruct', [
101
            ('a', 'c'),     # single character
102
            ('b', '2c'),    # 2 char string
103
        ])
104
105
        test_data = {
106
            'a': [5],
107
            'b': 'no',
108
        }
109
110
        with pytest.raises(TypeError):
111
            TestStruct.make(test_data)
112