6a035640b7023b99a85bf8c4c15ddb60bfc49967
[normand.git] / tests / test_api.py
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():
28 labels = {"yo": 0x88, "meow": 123} # type: normand.LabelsT
29 res = normand.parse("11 22 [yo:8] 33", init_labels=labels.copy())
30 assert res.data == bytearray([0x11, 0x22, 0x88, 0x33])
31 assert res.labels == labels.copy()
32
33
34 def test_init_vars():
35 variables = {"zoom": 0x88, "bateau": -123.45} # type: normand.VariablesT
36 res = normand.parse("11 22 [zoom:8] 33", init_variables=variables.copy())
37 assert res.data == bytearray([0x11, 0x22, 0x88, 0x33])
38 assert res.variables == variables.copy()
39
40
41 def test_init_offset():
42 res = normand.parse("11 22 [ICITTE:8] 33", init_offset=0x23)
43 assert res.data == bytearray([0x11, 0x22, 0x25, 0x33])
44 assert res.offset == 0x27
45
46
47 def _test_init_bo(bo: normand.ByteOrder):
48 h_byte = 0xF8
49 l_byte = 0x37
50
51 if bo == normand.ByteOrder.LE:
52 h_byte, l_byte = l_byte, h_byte
53
54 res = normand.parse("11 22 [-1993:16] 33", init_byte_order=bo)
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():
68 labels = {"yo": 0x88, "meow": 123} # type: normand.LabelsT
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
74 assert res.labels == labels.copy()
75
76
77 def test_final_vars():
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
85 assert res.variables == variables.copy()
86
87
88 def test_final_offset():
89 res = normand.parse("11 22 33 <32> 44 55")
90 assert res.offset == 34
91
92
93 def test_final_byte_order_none():
94 res = normand.parse("11 22 33 [-19:8] 44 55")
95 assert res.byte_order is None
96
97
98 def test_final_byte_order_be():
99 res = normand.parse("11 22 !le 33 [-19:8] 44 ( !be 88 ) * 3 55")
100 assert res.byte_order == normand.ByteOrder.BE
101
102
103 def test_final_byte_order_le():
104 res = normand.parse("11 22 !be 33 [-19:8] 44 ( !le 88 ) * 3 55")
105 assert res.byte_order == normand.ByteOrder.LE
106
107
108 def test_no_data():
109 res = normand.parse("# nothing to see here!\n")
110 assert len(res.data) == 0
111 assert len(res.labels) == 0
112 assert len(res.variables) == 0
113 assert res.offset == 0
This page took 0.032472 seconds and 3 git commands to generate.