Skip to content

Commit 7dbf7f7

Browse files
authored
Add unit tests for common.py
1 parent a2f9f59 commit 7dbf7f7

File tree

1 file changed

+236
-0
lines changed

1 file changed

+236
-0
lines changed

tests/test_common.py

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
import dataclasses
2+
3+
import pytest
4+
5+
from genieutils.common import GenieClass, ByteHandler
6+
from genieutils.versions import Version
7+
8+
class TestGenieClass:
9+
@pytest.fixture
10+
def genie_class(self):
11+
return GenieClass()
12+
13+
def test_from_bytes(self, genie_class: GenieClass):
14+
with pytest.raises(NotImplementedError):
15+
genie_class.from_bytes(ByteHandler(b''))
16+
17+
def test_from_bytes_with_count(self, genie_class: GenieClass):
18+
with pytest.raises(NotImplementedError):
19+
genie_class.from_bytes_with_count(ByteHandler(b''), 0)
20+
21+
def test_to_bytes(self, genie_class: GenieClass):
22+
with pytest.raises(NotImplementedError):
23+
genie_class.to_bytes(Version.UNDEFINED)
24+
25+
def test_write_debug_string(self, genie_class: GenieClass):
26+
assert genie_class.write_debug_string("foobar") == b'\x60\x0A\x06\x00foobar'
27+
28+
def test_write_string(self, genie_class: GenieClass):
29+
assert genie_class.write_string(6, "foobar") == b'foobar'
30+
31+
def test_write_int_8(self, genie_class: GenieClass):
32+
assert genie_class.write_int_8(10) == b'\x0A'
33+
with pytest.raises(OverflowError):
34+
genie_class.write_int_8(1000)
35+
36+
def test_write_int_8_array(self, genie_class: GenieClass):
37+
assert genie_class.write_int_8_array([10, 10, 10]) == b'\x0A\x0A\x0A'
38+
with pytest.raises(OverflowError):
39+
genie_class.write_int_8_array([10, 10, 1000])
40+
41+
def test_write_int_16(self, genie_class: GenieClass):
42+
assert genie_class.write_int_16(10) == b'\x0A\x00'
43+
with pytest.raises(OverflowError):
44+
genie_class.write_int_16(70000)
45+
46+
def test_write_int_16_array(self, genie_class: GenieClass):
47+
assert genie_class.write_int_16_array([10, 10, 10]) == b'\x0A\x00\x0A\x00\x0A\x00'
48+
with pytest.raises(OverflowError):
49+
genie_class.write_int_16_array([10, 10, 70000])
50+
51+
def test_write_int_32(self, genie_class: GenieClass):
52+
assert genie_class.write_int_32(10) == b'\x0A\x00\x00\x00'
53+
with pytest.raises(OverflowError):
54+
genie_class.write_int_32(5000000000)
55+
56+
def test_write_int_32_array(self, genie_class: GenieClass):
57+
assert genie_class.write_int_32_array([10, 10, 10]) == b'\x0A\x00\x00\x00\x0A\x00\x00\x00\x0A\x00\x00\x00'
58+
with pytest.raises(OverflowError):
59+
genie_class.write_int_32_array([10, 10, 5000000000])
60+
61+
def test_write_float(self, genie_class: GenieClass):
62+
assert genie_class.write_float(10) == b'\x00\x00\x20\x41'
63+
64+
def test_write_float_array(self, genie_class: GenieClass):
65+
assert genie_class.write_float_array([10, 10, 10]) == b'\x00\x00\x20\x41\x00\x00\x20\x41\x00\x00\x20\x41'
66+
67+
def test_write_class(self, genie_class: GenieClass):
68+
with pytest.raises(NotImplementedError):
69+
genie_class.write_class(GenieClass(), Version.UNDEFINED)
70+
71+
def test_write_class_array(self, genie_class: GenieClass):
72+
with pytest.raises(NotImplementedError):
73+
genie_class.write_class(GenieClass(), Version.UNDEFINED)
74+
75+
76+
class TestByteHandler:
77+
@pytest.fixture
78+
def byte_handler_8(self):
79+
data = memoryview(b''.join([int.to_bytes(i) for i in range(32)]))
80+
return ByteHandler(data)
81+
82+
@pytest.fixture
83+
def byte_handler_16(self):
84+
data = memoryview(b''.join([int.to_bytes(i) + b'\x00' for i in range(32)]))
85+
return ByteHandler(data)
86+
87+
@pytest.fixture
88+
def byte_handler_32(self):
89+
data = memoryview(b''.join([int.to_bytes(i) + b'\x00\x00\x00' for i in range(32)]))
90+
return ByteHandler(data)
91+
92+
@pytest.fixture
93+
def byte_handler_float(self):
94+
data = memoryview(b'\x00\x00\x00\x00\x00\x00\x80\x3f\x00\x00\x00\x40' * 32)
95+
return ByteHandler(data)
96+
97+
@pytest.fixture
98+
def byte_handler_class(self):
99+
data = memoryview(b'\x80\x01\x00\x00\x08\x00\x00\x46\x4f\x4f\x42\x41\x52\x00\x00\x80\x3f' * 32)
100+
return ByteHandler(data)
101+
102+
@pytest.fixture
103+
def child_genie_class(self):
104+
@dataclasses.dataclass
105+
class ChildGenieClass(GenieClass):
106+
int_8_val: int
107+
int_16_val: int
108+
int_32_val: int
109+
str_val: str
110+
float_val: float
111+
112+
@classmethod
113+
def from_bytes(cls, data: 'ByteHandler'):
114+
return cls(
115+
int_8_val = data.read_int_8(),
116+
int_16_val = data.read_int_16(),
117+
int_32_val = data.read_int_32(),
118+
str_val = data.read_string(6),
119+
float_val = data.read_float(),
120+
)
121+
122+
return ChildGenieClass
123+
124+
def test_consume_range(self, byte_handler_8: ByteHandler):
125+
assert bytes(byte_handler_8.consume_range(10)) == b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09'
126+
127+
def test_read_string(self, byte_handler_8: ByteHandler):
128+
assert byte_handler_8.read_string(10) == '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09'
129+
130+
def test_read_int_8(self, byte_handler_8: ByteHandler):
131+
assert byte_handler_8.read_int_8() == 0
132+
133+
def test_read_int_8_array(self, byte_handler_8: ByteHandler):
134+
assert byte_handler_8.read_int_8_array(3) == [0, 1, 2]
135+
136+
def test_read_int_8_array_2(self, byte_handler_8: ByteHandler):
137+
assert byte_handler_8.read_int_8_array_2() == (0, 1)
138+
139+
def test_read_int_8_array_3(self, byte_handler_8: ByteHandler):
140+
assert byte_handler_8.read_int_8_array_3() == (0, 1, 2)
141+
142+
def test_read_int_8_array_5(self, byte_handler_8: ByteHandler):
143+
assert byte_handler_8.read_int_8_array_5() == (0, 1, 2, 3, 4)
144+
145+
def test_read_int_8_array_6(self, byte_handler_8: ByteHandler):
146+
assert byte_handler_8.read_int_8_array_6() == (0, 1, 2, 3, 4, 5)
147+
148+
def test_read_int_8_array_10(self, byte_handler_8: ByteHandler):
149+
assert byte_handler_8.read_int_8_array_10() == (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
150+
151+
def test_read_int_16(self, byte_handler_16: ByteHandler):
152+
assert byte_handler_16.read_int_16() == 0
153+
assert byte_handler_16.read_int_16(False) == 1
154+
155+
def test_read_int_16_array(self, byte_handler_16: ByteHandler):
156+
assert byte_handler_16.read_int_16_array(3) == [0, 1, 2]
157+
158+
def test_read_int_16_array_2(self, byte_handler_16: ByteHandler):
159+
assert byte_handler_16.read_int_16_array_2() == (0, 1)
160+
161+
def test_read_int_16_array_3(self, byte_handler_16: ByteHandler):
162+
assert byte_handler_16.read_int_16_array_3() == (0, 1, 2)
163+
164+
def test_read_int_16_array_4(self, byte_handler_16: ByteHandler):
165+
assert byte_handler_16.read_int_16_array_4() == (0, 1, 2, 3)
166+
167+
def test_read_int_16_array_6(self, byte_handler_16: ByteHandler):
168+
assert byte_handler_16.read_int_16_array_6() == (0, 1, 2, 3, 4, 5)
169+
170+
def test_read_int_32(self, byte_handler_32: ByteHandler):
171+
assert byte_handler_32.read_int_32() == 0
172+
assert byte_handler_32.read_int_32(False) == 1
173+
174+
def test_read_int_32_array(self, byte_handler_32: ByteHandler):
175+
assert byte_handler_32.read_int_32_array(3) == [0, 1, 2]
176+
177+
def test_read_int_32_array_10(self, byte_handler_32: ByteHandler):
178+
assert byte_handler_32.read_int_32_array_10() == (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
179+
180+
def test_read_float(self, byte_handler_float: ByteHandler):
181+
assert byte_handler_float.read_float() == 0.0
182+
183+
def test_read_float_array(self, byte_handler_float: ByteHandler):
184+
assert byte_handler_float.read_float_array(3) == [0.0, 1.0, 2.0]
185+
186+
def test_read_float_array_2(self, byte_handler_float: ByteHandler):
187+
assert byte_handler_float.read_float_array_2() == (0.0, 1.0)
188+
189+
def test_read_float_array_3(self, byte_handler_float: ByteHandler):
190+
assert byte_handler_float.read_float_array_3() == (0.0, 1.0, 2.0)
191+
192+
def test_read_class(self, byte_handler_class: ByteHandler, child_genie_class):
193+
read_class = byte_handler_class.read_class(child_genie_class)
194+
assert isinstance(read_class, child_genie_class)
195+
assert read_class.int_8_val == 128
196+
assert read_class.int_16_val == 1
197+
assert read_class.int_32_val == 2048
198+
assert read_class.str_val == 'FOOBAR'
199+
assert read_class.float_val == 1.0
200+
201+
def test_read_class_array(self, byte_handler_class: ByteHandler, child_genie_class):
202+
read_classes = byte_handler_class.read_class_array(child_genie_class, 3)
203+
assert len(read_classes) == 3
204+
205+
for read_class in read_classes:
206+
assert isinstance(read_class, child_genie_class)
207+
assert read_class.int_8_val == 128
208+
assert read_class.int_16_val == 1
209+
assert read_class.int_32_val == 2048
210+
assert read_class.str_val == 'FOOBAR'
211+
assert read_class.float_val == 1.0
212+
213+
214+
def test_read_class_array_3(self, byte_handler_class: ByteHandler, child_genie_class):
215+
read_classes = byte_handler_class.read_class_array_3(child_genie_class)
216+
assert len(read_classes) == 3
217+
218+
for read_class in read_classes:
219+
assert isinstance(read_class, child_genie_class)
220+
assert read_class.int_8_val == 128
221+
assert read_class.int_16_val == 1
222+
assert read_class.int_32_val == 2048
223+
assert read_class.str_val == 'FOOBAR'
224+
assert read_class.float_val == 1.0
225+
226+
def test_read_class_array_4(self, byte_handler_class: ByteHandler, child_genie_class):
227+
read_classes = byte_handler_class.read_class_array_4(child_genie_class)
228+
assert len(read_classes) == 4
229+
230+
for read_class in read_classes:
231+
assert isinstance(read_class, child_genie_class)
232+
assert read_class.int_8_val == 128
233+
assert read_class.int_16_val == 1
234+
assert read_class.int_32_val == 2048
235+
assert read_class.str_val == 'FOOBAR'
236+
assert read_class.float_val == 1.0

0 commit comments

Comments
 (0)