1 | #!/usr/bin/env python3 |
||
2 | # -*- coding: utf-8 -*- |
||
3 | |||
4 | """Tests for the constant class""" |
||
5 | |||
6 | import unittest |
||
7 | |||
8 | from starstruct.message import Message |
||
9 | from starstruct.modes import Mode |
||
10 | |||
11 | |||
12 | class TestElementConstant(unittest.TestCase): |
||
13 | def test_one_element(self): |
||
14 | TestStruct = Message('TestStruct', [ |
||
15 | ('regular', 'B'), # Two regular messages |
||
16 | ('fill_in_later', 'H'), |
||
17 | ('ending_sequence', 'II', (0xAA, 0xBB)), # An ending sequence to a message |
||
18 | ]) |
||
19 | |||
20 | test_data = { |
||
21 | 'regular': 13, |
||
22 | 'fill_in_later': 4, |
||
23 | } |
||
24 | |||
25 | made = TestStruct.make(**test_data) |
||
0 ignored issues
–
show
|
|||
26 | assert made.regular == 13 |
||
27 | assert made.fill_in_later == 4 |
||
28 | assert made.ending_sequence == (0xAA, 0xBB) |
||
29 | |||
30 | def test_unpack(self): |
||
31 | TestStruct = Message('TestStruct', [ |
||
32 | ('regular', 'B'), # Two regular messages |
||
33 | ('fill_in_later', 'H'), |
||
34 | ('ending_sequence', 'II', (0xAB, 0xBA)), # An ending sequence to a message |
||
35 | ], Mode.Little) |
||
36 | test_data = { |
||
37 | 'regular': 8, |
||
38 | 'fill_in_later': 7, |
||
39 | } |
||
40 | |||
41 | test_bytes = b'\x08\x07\x00\xab\x00\x00\x00\xba\x00\x00\x00' |
||
42 | |||
43 | assert test_bytes == TestStruct.pack(**test_data) |
||
0 ignored issues
–
show
Usage of
* or ** arguments should usually be done with care.
Generally, there is nothing wrong with usage of For more information, we can recommend this blog post from Ned Batchelder including its comments which also touches this aspect. ![]() |
|||
44 | |||
45 | unpacked = TestStruct.unpack(test_bytes) |
||
46 | assert unpacked.regular == 8 |
||
47 | assert unpacked.fill_in_later == 7 |
||
48 | assert unpacked.ending_sequence == (0xAB, 0xBA) |
||
49 | |||
50 | def test_unpack_in_the_middle(self): |
||
51 | SomeMessage = Message('SomeMessage', [ |
||
52 | ('regular', 'B'), |
||
53 | ('irregular', 'B'), |
||
54 | ('confused', 'B'), |
||
55 | ]) |
||
56 | |||
57 | TestStruct = Message('TestStruct', [ |
||
58 | ('regular', 'B'), |
||
59 | ('middle_constant', 'II', (0xAB, 0xBA)), |
||
60 | ('a_variable_length', 'H', 'msg'), |
||
61 | ('msg', SomeMessage, 'a_variable_length') |
||
62 | ], Mode.Little) |
||
63 | |||
64 | test_data = { |
||
65 | 'regular': 8, |
||
66 | 'a_variable_length': 2, |
||
67 | 'msg': [ |
||
68 | {'regular': 4, 'irregular': 0, 'confused': 6}, |
||
69 | {'regular': 5, 'irregular': 2, 'confused': 4}, |
||
70 | ], |
||
71 | } |
||
72 | |||
73 | made = TestStruct.make(**test_data) |
||
0 ignored issues
–
show
Usage of
* or ** arguments should usually be done with care.
Generally, there is nothing wrong with usage of For more information, we can recommend this blog post from Ned Batchelder including its comments which also touches this aspect. ![]() |
|||
74 | assert made.regular == 8 |
||
75 | assert made.middle_constant == (0xAB, 0xBA) |
||
76 | |||
77 | packed = TestStruct.pack(test_data) |
||
78 | assert packed == b'\x08\xab\x00\x00\x00\xba\x00\x00\x00\x02\x00\x04\x00\x06\x05\x02\x04' |
||
79 | |||
80 | unpacked = TestStruct.unpack(packed) |
||
81 | assert unpacked.regular == 8 |
||
82 | assert unpacked.middle_constant == (0xAB, 0xBA) |
||
83 |
Generally, there is nothing wrong with usage of
*
or**
arguments. For readability of the code base, we suggest to not over-use these language constructs though.For more information, we can recommend this blog post from Ned Batchelder including its comments which also touches this aspect.