Add basic API tests
[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}
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}
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):
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}
69 res = normand.parse('11 <june> 22 (77 <aug> 88) * 2 <kilo> 33', init_labels=labels.copy())
70 labels['june'] = 1
71 labels['kilo'] = 6
72 assert res.labels == labels.copy()
73
74
75 def test_final_vars():
76 variables = {'yo': 0x88, 'meow': -123.45}
77 res = normand.parse('11 {yo = 18.2} 22 (77 {zoom = ICITTE} 88) * 2 33', init_variables=variables.copy())
78 variables['yo'] = 18.2
79 variables['zoom'] = 5
80 assert res.variables == variables.copy()
81
82
83 def test_final_offset():
84 res = normand.parse('11 22 33 <32> 44 55')
85 assert res.offset == 34
86
87
88 def test_final_byte_order_none():
89 res = normand.parse('11 22 33 {-19:8} 44 55')
90 assert res.byte_order is None
91
92
93 def test_final_byte_order_be():
94 res = normand.parse('11 22 {le} 33 {-19:8} 44 ( {be} 88 ) * 3 55')
95 assert res.byte_order == normand.ByteOrder.BE
96
97
98 def test_final_byte_order_le():
99 res = normand.parse('11 22 {be} 33 {-19:8} 44 ( {le} 88 ) * 3 55')
100 assert res.byte_order == normand.ByteOrder.LE
101
102
103 def test_no_data():
104 res = normand.parse('# nothing to see here!\n')
105 assert len(res.data) == 0
106 assert len(res.labels) == 0
107 assert len(res.variables) == 0
108 assert res.offset == 0
This page took 0.03973 seconds and 4 git commands to generate.