|
1
|
|
|
#!/usr/bin/env python3 |
|
2
|
|
|
# -*- coding: utf-8 -*- |
|
3
|
|
|
|
|
4
|
|
|
import enum |
|
5
|
|
|
import struct |
|
6
|
|
|
import unittest |
|
7
|
|
|
|
|
8
|
|
|
import pytest |
|
9
|
|
|
|
|
10
|
|
|
from starstruct.message import Message |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
class MyEnum(enum.Enum): |
|
14
|
|
|
"""Possible enum types.""" |
|
15
|
|
|
THIS = 0 |
|
16
|
|
|
THAT = 1 |
|
17
|
|
|
OTHER = 2 |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
MyNamed = Message('MyNamed', [ |
|
21
|
|
|
('type', 'B'), |
|
22
|
|
|
('name', '32s'), |
|
23
|
|
|
]) |
|
24
|
|
|
|
|
25
|
|
|
MyOtherNamed = Message('MyOtherNamed', [ |
|
26
|
|
|
('first', 'B'), |
|
27
|
|
|
('second', 'H'), |
|
28
|
|
|
('pad', '30x'), |
|
29
|
|
|
]) |
|
30
|
|
|
|
|
31
|
|
|
NotSameMessage = Message('WowNotEvenClose', [ |
|
32
|
|
|
('first_and_only', '4H') |
|
33
|
|
|
]) |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
# pylint: disable=no-self-use |
|
37
|
|
|
class TestLengthHelper(unittest.TestCase): |
|
38
|
|
|
"""Test the length for this class""" |
|
39
|
|
|
def test_empty_length(self): |
|
40
|
|
|
empty_message = Message('Empty', []) |
|
41
|
|
|
assert len(empty_message) == 0 |
|
42
|
|
|
|
|
43
|
|
|
def test_single_item(self): |
|
44
|
|
|
one_message = Message('One', [ |
|
45
|
|
|
('info', 'H'), |
|
46
|
|
|
]) |
|
47
|
|
|
assert len(one_message) == struct.calcsize('H') |
|
48
|
|
|
|
|
49
|
|
|
def test_comparison(self): |
|
50
|
|
|
assert len(MyNamed) == len(MyOtherNamed) |
|
51
|
|
|
assert len(MyNamed) != len(NotSameMessage) |
|
52
|
|
|
|
|
53
|
|
|
def test_bad_length_item(self): |
|
54
|
|
|
bad_message = Message('BadMessage', [ |
|
55
|
|
|
('type', 'B', MyEnum), |
|
56
|
|
|
('data', { |
|
57
|
|
|
MyEnum.THIS: MyNamed, |
|
58
|
|
|
MyEnum.THAT: NotSameMessage, |
|
59
|
|
|
MyEnum.OTHER: MyOtherNamed, |
|
60
|
|
|
}, 'type'), |
|
61
|
|
|
]) |
|
62
|
|
|
|
|
63
|
|
|
with self.assertRaises(AttributeError): |
|
64
|
|
|
print(len(bad_message)) |
|
65
|
|
|
|
|
66
|
|
|
def test_long_item(self): |
|
67
|
|
|
long_message = Message('ThisIsALongOne', [ |
|
68
|
|
|
('ID', 'B'), |
|
69
|
|
|
('delay', 'B'), |
|
70
|
|
|
('a_byte', 'B'), |
|
71
|
|
|
('type', 'B', MyEnum), |
|
72
|
|
|
('data', { |
|
73
|
|
|
MyEnum.THIS: MyNamed, |
|
74
|
|
|
MyEnum.THAT: MyNamed, |
|
75
|
|
|
MyEnum.OTHER: MyOtherNamed, |
|
76
|
|
|
}, 'type'), |
|
77
|
|
|
]) |
|
78
|
|
|
|
|
79
|
|
|
# Second size is from the MyNamed Enum above |
|
80
|
|
|
my_named_format = 'B32s' |
|
81
|
|
|
assert len(long_message) == struct.calcsize('BBBB' + my_named_format) |
|
82
|
|
|
|
|
83
|
|
|
@pytest.mark.skip(reason="don't know how to test this") |
|
84
|
|
|
def test_variable_length(self): |
|
85
|
|
|
dont_know_how_to_test = Message('DontKnow', [ |
|
86
|
|
|
('numNames', 'B', 'names'), |
|
87
|
|
|
('names', MyNamed, 'numNames'), |
|
88
|
|
|
]) |
|
89
|
|
|
|
|
90
|
|
|
# Not sure how to test this one yet |
|
91
|
|
|
# could do some multiples thing or just let it be. |
|
92
|
|
|
print(len(dont_know_how_to_test)) |
|
93
|
|
|
|