TestElementString   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 99
rs 10
wmc 17

5 Methods

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