Commit | Line | Data |
---|---|---|
d2d06893 MJ |
1 | # SPDX-FileCopyrightText: 2023 Philippe Proulx <eeppeliteloop@gmail.com> |
2 | # SPDX-License-Identifier: MIT | |
0dcc2789 PP |
3 | |
4 | import normand | |
5 | ||
6 | ||
7 | def test_init_labels(): | |
cd33dfe6 | 8 | labels = {"yo": 0x88, "meow": 123} # type: normand.LabelsT |
ee724c95 | 9 | res = normand.parse("11 22 [yo:8] 33", init_labels=labels.copy()) |
0dcc2789 PP |
10 | assert res.data == bytearray([0x11, 0x22, 0x88, 0x33]) |
11 | assert res.labels == labels.copy() | |
12 | ||
13 | ||
14 | def test_init_vars(): | |
cd33dfe6 | 15 | variables = {"zoom": 0x88, "bateau": -123.45} # type: normand.VariablesT |
ee724c95 | 16 | res = normand.parse("11 22 [zoom:8] 33", init_variables=variables.copy()) |
0dcc2789 PP |
17 | assert res.data == bytearray([0x11, 0x22, 0x88, 0x33]) |
18 | assert res.variables == variables.copy() | |
19 | ||
20 | ||
21 | def test_init_offset(): | |
ee724c95 | 22 | res = normand.parse("11 22 [ICITTE:8] 33", init_offset=0x23) |
0dcc2789 PP |
23 | assert res.data == bytearray([0x11, 0x22, 0x25, 0x33]) |
24 | assert res.offset == 0x27 | |
25 | ||
26 | ||
eb2f968a PP |
27 | def _test_init_bo(bo: normand.ByteOrder): |
28 | h_byte = 0xF8 | |
0dcc2789 PP |
29 | l_byte = 0x37 |
30 | ||
31 | if bo == normand.ByteOrder.LE: | |
32 | h_byte, l_byte = l_byte, h_byte | |
33 | ||
ee724c95 | 34 | res = normand.parse("11 22 [-1993:16] 33", init_byte_order=bo) |
0dcc2789 PP |
35 | assert res.data == bytearray([0x11, 0x22, h_byte, l_byte, 0x33]) |
36 | assert res.byte_order == bo | |
37 | ||
38 | ||
39 | def test_init_bo_be(): | |
40 | _test_init_bo(normand.ByteOrder.BE) | |
41 | ||
42 | ||
43 | def test_init_bo_le(): | |
44 | _test_init_bo(normand.ByteOrder.LE) | |
45 | ||
46 | ||
47 | def test_final_labels(): | |
cd33dfe6 | 48 | labels = {"yo": 0x88, "meow": 123} # type: normand.LabelsT |
eb2f968a PP |
49 | res = normand.parse( |
50 | "11 <june> 22 (77 <aug> 88) * 2 <kilo> 33", init_labels=labels.copy() | |
51 | ) | |
52 | labels["june"] = 1 | |
53 | labels["kilo"] = 6 | |
0dcc2789 PP |
54 | assert res.labels == labels.copy() |
55 | ||
56 | ||
57 | def test_final_vars(): | |
eb2f968a PP |
58 | variables = {"yo": 0x88, "meow": -123.45} |
59 | res = normand.parse( | |
60 | "11 {yo = 18.2} 22 (77 {zoom = ICITTE} 88) * 2 33", | |
61 | init_variables=variables.copy(), | |
62 | ) | |
63 | variables["yo"] = 18.2 | |
64 | variables["zoom"] = 5 | |
0dcc2789 PP |
65 | assert res.variables == variables.copy() |
66 | ||
67 | ||
68 | def test_final_offset(): | |
eb2f968a | 69 | res = normand.parse("11 22 33 <32> 44 55") |
0dcc2789 PP |
70 | assert res.offset == 34 |
71 | ||
72 | ||
73 | def test_final_byte_order_none(): | |
ee724c95 | 74 | res = normand.parse("11 22 33 [-19:8] 44 55") |
0dcc2789 PP |
75 | assert res.byte_order is None |
76 | ||
77 | ||
78 | def test_final_byte_order_be(): | |
ee724c95 | 79 | res = normand.parse("11 22 !le 33 [-19:8] 44 ( !be 88 ) * 3 55") |
0dcc2789 PP |
80 | assert res.byte_order == normand.ByteOrder.BE |
81 | ||
82 | ||
83 | def test_final_byte_order_le(): | |
ee724c95 | 84 | res = normand.parse("11 22 !be 33 [-19:8] 44 ( !le 88 ) * 3 55") |
0dcc2789 PP |
85 | assert res.byte_order == normand.ByteOrder.LE |
86 | ||
87 | ||
88 | def test_no_data(): | |
eb2f968a | 89 | res = normand.parse("# nothing to see here!\n") |
0dcc2789 PP |
90 | assert len(res.data) == 0 |
91 | assert len(res.labels) == 0 | |
92 | assert len(res.variables) == 0 | |
93 | assert res.offset == 0 |