File: tlv8_entrylist_tests.py

package info (click to toggle)
tlv8-python 0.10.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 216 kB
  • sloc: python: 1,518; sh: 7; makefile: 3
file content (149 lines) | stat: -rw-r--r-- 4,898 bytes parent folder | download
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#
# Copyright 2020 Joachim Lusiardi
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import unittest

import tlv8


class TestTLV8EntryList(unittest.TestCase):
    def test_entrylist_init(self):
        with self.assertRaises(ValueError) as error_context:
            tlv8.EntryList('not a list')
        self.assertEqual(str(error_context.exception), 'No valid list: not a list')

        with self.assertRaises(ValueError) as error_context:
            tlv8.EntryList(['not an entry'])
        self.assertEqual(str(error_context.exception), 'Not a valid tlv8.Entry: not an entry')

    def test_entrylist_append(self):
        el = tlv8.EntryList()
        with self.assertRaises(ValueError) as error_context:
            el.append('not a list')
        self.assertEqual(str(error_context.exception), 'Not an tlv8.Entry: not a list')

    def test_entrylist_compare_empty(self):
        el_1 = tlv8.EntryList()
        el_2 = tlv8.EntryList()
        self.assertEqual(el_1, el_2)

    def test_entrylist_compare_not_a_list(self):
        el_1 = tlv8.EntryList()
        self.assertNotEqual(el_1, 'not a entry list')

    def test_entrylist_compare_1_equal(self):
        el_1 = tlv8.EntryList([tlv8.Entry(1, 2)])
        el_2 = tlv8.EntryList([tlv8.Entry(1, 2)])
        self.assertEqual(el_1, el_2)

    def test_entrylist_compare_1_not_equal(self):
        el_1 = tlv8.EntryList([tlv8.Entry(1, 2)])
        el_2 = tlv8.EntryList([tlv8.Entry(1, 3)])
        self.assertNotEqual(el_1, el_2)

    def test_entrylist_length(self):
        el = tlv8.EntryList()
        self.assertEqual(0, len(el))
        el.append(tlv8.Entry(1, 2))
        self.assertEqual(1, len(el))
        el.append(tlv8.Entry(1, 2))
        self.assertEqual(2, len(el))

    def test_entrylist_index(self):
        el = tlv8.EntryList()
        e1 = tlv8.Entry(1, 2)
        e2 = tlv8.Entry(3, 4)
        el.append(e1)
        el.append(e2)
        self.assertEqual(e2, el[1])

    def test_entrylist_decode(self):
        input_data = bytearray(b'\x02\x01\x23')
        result = tlv8.decode(input_data)
        self.assertIsInstance(result, tlv8.EntryList)
        self.assertEqual(result, tlv8.EntryList([tlv8.Entry(2, b'\x23')]))

    def test_entrylist_encode(self):
        el = tlv8.EntryList([tlv8.Entry(2, b'\x23')])
        result = el.encode()
        self.assertEqual(b'\x02\x01\x23', result)

    def test_entrylist_encode_same_sep_type(self):
        el = tlv8.EntryList([
            tlv8.Entry(2, b'\x23'),
            tlv8.Entry(2, b'\x42')
        ])
        result = el.encode(1)
        self.assertEqual(b'\x02\x01\x23\x01\x00\x02\x01\x42', result)

    def test_entrylist_assert_has(self):
        el = tlv8.EntryList([
            tlv8.Entry(2, b'\x23')
        ])
        with self.assertRaises(AssertionError) as error_context:
            el.assert_has(3, 'no bla bla')
        self.assertEqual(str(error_context.exception), 'no bla bla')

        el.assert_has(2, 'no bla bla')

    def test_entrylist_decode_nested(self):
        data = b'\x01\x04%\x06I@\x02\x0e\x03\x05hello\x04\x05world\x03\x01\x02'
        result = tlv8.decode(data, {
            1: tlv8.DataType.FLOAT,
            2: {
                3: tlv8.DataType.STRING,
                4: tlv8.DataType.STRING,
            },
            3: tlv8.DataType.INTEGER
        })
        self.assertIsInstance(result, tlv8.EntryList)
        self.assertIsInstance(result[1].data, tlv8.EntryList)

    def test_entrylist_by_id(self):
        el = tlv8.EntryList([
            tlv8.Entry(2, b'\x23'),
            tlv8.Entry(2, b'\x42')
        ])
        self.assertEqual(el.by_id(1), tlv8.EntryList())
        self.assertEqual(el.by_id(2), el)

    def test_entrylist_first_by_id(self):
        el = tlv8.EntryList([
            tlv8.Entry(2, b'\x23'),
            tlv8.Entry(2, b'\x42')
        ])
        self.assertEqual(el.first_by_id(1), None)
        self.assertEqual(el.first_by_id(2), el[0])

    def test_entrylist_format_string(self):
        el = tlv8.EntryList([
            tlv8.Entry(1, 1),
            tlv8.Entry(2, tlv8.EntryList([
                tlv8.Entry(4, 4),
                tlv8.Entry(5, 5)
            ])),
            tlv8.Entry(3, 3),
        ])
        result = tlv8.format_string(el)
        expected = """[
  <1, 1>,
  <2, [
    <4, 4>,
    <5, 5>,
  ]>,
  <3, 3>,
]"""
        self.assertEqual(result, expected)