Implement REUSE with SPDX identifiers
[normand.git] / tests / test_api.py
1 # SPDX-FileCopyrightText: 2023 Philippe Proulx <eeppeliteloop@gmail.com>
2 # SPDX-License-Identifier: MIT
3
4 import normand
5
6
7 def test_init_labels():
8 labels = {"yo": 0x88, "meow": 123} # type: normand.LabelsT
9 res = normand.parse("11 22 [yo:8] 33", init_labels=labels.copy())
10 assert res.data == bytearray([0x11, 0x22, 0x88, 0x33])
11 assert res.labels == labels.copy()
12
13
14 def test_init_vars():
15 variables = {"zoom": 0x88, "bateau": -123.45} # type: normand.VariablesT
16 res = normand.parse("11 22 [zoom:8] 33", init_variables=variables.copy())
17 assert res.data == bytearray([0x11, 0x22, 0x88, 0x33])
18 assert res.variables == variables.copy()
19
20
21 def test_init_offset():
22 res = normand.parse("11 22 [ICITTE:8] 33", init_offset=0x23)
23 assert res.data == bytearray([0x11, 0x22, 0x25, 0x33])
24 assert res.offset == 0x27
25
26
27 def _test_init_bo(bo: normand.ByteOrder):
28 h_byte = 0xF8
29 l_byte = 0x37
30
31 if bo == normand.ByteOrder.LE:
32 h_byte, l_byte = l_byte, h_byte
33
34 res = normand.parse("11 22 [-1993:16] 33", init_byte_order=bo)
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():
48 labels = {"yo": 0x88, "meow": 123} # type: normand.LabelsT
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
54 assert res.labels == labels.copy()
55
56
57 def test_final_vars():
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
65 assert res.variables == variables.copy()
66
67
68 def test_final_offset():
69 res = normand.parse("11 22 33 <32> 44 55")
70 assert res.offset == 34
71
72
73 def test_final_byte_order_none():
74 res = normand.parse("11 22 33 [-19:8] 44 55")
75 assert res.byte_order is None
76
77
78 def test_final_byte_order_be():
79 res = normand.parse("11 22 !le 33 [-19:8] 44 ( !be 88 ) * 3 55")
80 assert res.byte_order == normand.ByteOrder.BE
81
82
83 def test_final_byte_order_le():
84 res = normand.parse("11 22 !be 33 [-19:8] 44 ( !le 88 ) * 3 55")
85 assert res.byte_order == normand.ByteOrder.LE
86
87
88 def test_no_data():
89 res = normand.parse("# nothing to see here!\n")
90 assert len(res.data) == 0
91 assert len(res.labels) == 0
92 assert len(res.variables) == 0
93 assert res.offset == 0
This page took 0.031525 seconds and 4 git commands to generate.