Commit | Line | Data |
---|---|---|
0dcc2789 PP |
1 | # The MIT License (MIT) |
2 | # | |
3 | # Copyright (c) 2023 Philippe Proulx <eeppeliteloop@gmail.com> | |
4 | # | |
5 | # Permission is hereby granted, free of charge, to any person obtaining | |
6 | # a copy of this software and associated documentation files (the | |
7 | # "Software"), to deal in the Software without restriction, including | |
8 | # without limitation the rights to use, copy, modify, merge, publish, | |
9 | # distribute, sublicense, and/or sell copies of the Software, and to | |
10 | # permit persons to whom the Software is furnished to do so, subject to | |
11 | # the following conditions: | |
12 | # | |
13 | # The above copyright notice and this permission notice shall be | |
14 | # included in all copies or substantial portions of the Software. | |
15 | # | |
16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
17 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
18 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
19 | # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | |
20 | # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |
21 | # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
22 | # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
23 | ||
24 | import normand | |
25 | ||
26 | ||
27 | def test_init_labels(): | |
cd33dfe6 | 28 | labels = {"yo": 0x88, "meow": 123} # type: normand.LabelsT |
eb2f968a | 29 | res = normand.parse("11 22 {yo:8} 33", init_labels=labels.copy()) |
0dcc2789 PP |
30 | assert res.data == bytearray([0x11, 0x22, 0x88, 0x33]) |
31 | assert res.labels == labels.copy() | |
32 | ||
33 | ||
34 | def test_init_vars(): | |
cd33dfe6 | 35 | variables = {"zoom": 0x88, "bateau": -123.45} # type: normand.VariablesT |
eb2f968a | 36 | res = normand.parse("11 22 {zoom:8} 33", init_variables=variables.copy()) |
0dcc2789 PP |
37 | assert res.data == bytearray([0x11, 0x22, 0x88, 0x33]) |
38 | assert res.variables == variables.copy() | |
39 | ||
40 | ||
41 | def test_init_offset(): | |
eb2f968a | 42 | res = normand.parse("11 22 {ICITTE:8} 33", init_offset=0x23) |
0dcc2789 PP |
43 | assert res.data == bytearray([0x11, 0x22, 0x25, 0x33]) |
44 | assert res.offset == 0x27 | |
45 | ||
46 | ||
eb2f968a PP |
47 | def _test_init_bo(bo: normand.ByteOrder): |
48 | h_byte = 0xF8 | |
0dcc2789 PP |
49 | l_byte = 0x37 |
50 | ||
51 | if bo == normand.ByteOrder.LE: | |
52 | h_byte, l_byte = l_byte, h_byte | |
53 | ||
eb2f968a | 54 | res = normand.parse("11 22 {-1993:16} 33", init_byte_order=bo) |
0dcc2789 PP |
55 | assert res.data == bytearray([0x11, 0x22, h_byte, l_byte, 0x33]) |
56 | assert res.byte_order == bo | |
57 | ||
58 | ||
59 | def test_init_bo_be(): | |
60 | _test_init_bo(normand.ByteOrder.BE) | |
61 | ||
62 | ||
63 | def test_init_bo_le(): | |
64 | _test_init_bo(normand.ByteOrder.LE) | |
65 | ||
66 | ||
67 | def test_final_labels(): | |
cd33dfe6 | 68 | labels = {"yo": 0x88, "meow": 123} # type: normand.LabelsT |
eb2f968a PP |
69 | res = normand.parse( |
70 | "11 <june> 22 (77 <aug> 88) * 2 <kilo> 33", init_labels=labels.copy() | |
71 | ) | |
72 | labels["june"] = 1 | |
73 | labels["kilo"] = 6 | |
0dcc2789 PP |
74 | assert res.labels == labels.copy() |
75 | ||
76 | ||
77 | def test_final_vars(): | |
eb2f968a PP |
78 | variables = {"yo": 0x88, "meow": -123.45} |
79 | res = normand.parse( | |
80 | "11 {yo = 18.2} 22 (77 {zoom = ICITTE} 88) * 2 33", | |
81 | init_variables=variables.copy(), | |
82 | ) | |
83 | variables["yo"] = 18.2 | |
84 | variables["zoom"] = 5 | |
0dcc2789 PP |
85 | assert res.variables == variables.copy() |
86 | ||
87 | ||
88 | def test_final_offset(): | |
eb2f968a | 89 | res = normand.parse("11 22 33 <32> 44 55") |
0dcc2789 PP |
90 | assert res.offset == 34 |
91 | ||
92 | ||
93 | def test_final_byte_order_none(): | |
eb2f968a | 94 | res = normand.parse("11 22 33 {-19:8} 44 55") |
0dcc2789 PP |
95 | assert res.byte_order is None |
96 | ||
97 | ||
98 | def test_final_byte_order_be(): | |
eb2f968a | 99 | res = normand.parse("11 22 {le} 33 {-19:8} 44 ( {be} 88 ) * 3 55") |
0dcc2789 PP |
100 | assert res.byte_order == normand.ByteOrder.BE |
101 | ||
102 | ||
103 | def test_final_byte_order_le(): | |
eb2f968a | 104 | res = normand.parse("11 22 {be} 33 {-19:8} 44 ( {le} 88 ) * 3 55") |
0dcc2789 PP |
105 | assert res.byte_order == normand.ByteOrder.LE |
106 | ||
107 | ||
108 | def test_no_data(): | |
eb2f968a | 109 | res = normand.parse("# nothing to see here!\n") |
0dcc2789 PP |
110 | assert len(res.data) == 0 |
111 | assert len(res.labels) == 0 | |
112 | assert len(res.variables) == 0 | |
113 | assert res.offset == 0 |