added test
This commit is contained in:
parent
b136ddbf62
commit
b7933faed6
6 changed files with 283 additions and 5 deletions
12
source/_test/core.py
Normal file
12
source/_test/core.py
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
|
||||||
|
class TestCore(unittest.TestCase):
|
||||||
|
# TODO
|
||||||
|
|
||||||
|
def test_something(self):
|
||||||
|
self.assertEqual(True, False) # add assertion here
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
53
source/_test/network.py
Normal file
53
source/_test/network.py
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
import socket
|
||||||
|
import string
|
||||||
|
import unittest
|
||||||
|
import random
|
||||||
|
from threading import Thread
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from source.network.packet import PacketChat
|
||||||
|
|
||||||
|
|
||||||
|
class TestNetwork(unittest.TestCase):
|
||||||
|
PORT: int = 54231
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
# Prépare deux sockets pour simuler les packets
|
||||||
|
self.co_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
self.so_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
|
||||||
|
self.so_server.bind(("", self.PORT))
|
||||||
|
self.so_server.listen()
|
||||||
|
self.co_server: Optional[socket.socket] = None
|
||||||
|
|
||||||
|
def connect_server(): self.co_server, _ = self.so_server.accept()
|
||||||
|
def connect_client(): self.co_client.connect(("127.0.0.1", self.PORT))
|
||||||
|
|
||||||
|
thread_server = Thread(target=connect_server)
|
||||||
|
thread_client = Thread(target=connect_client)
|
||||||
|
thread_server.start()
|
||||||
|
thread_client.start()
|
||||||
|
thread_server.join()
|
||||||
|
thread_client.join()
|
||||||
|
|
||||||
|
def __del__(self):
|
||||||
|
self.co_client.close()
|
||||||
|
self.so_server.close()
|
||||||
|
|
||||||
|
def test_packet_chat(self):
|
||||||
|
for _ in range(100):
|
||||||
|
message = "".join(random.choice(string.printable) for _ in range(random.randint(1, 100)))
|
||||||
|
|
||||||
|
PacketChat(message).send_data_connection(self.co_server)
|
||||||
|
packet_chat = PacketChat.from_connection(self.co_client)
|
||||||
|
|
||||||
|
self.assertEqual(message, packet_chat.message)
|
||||||
|
|
||||||
|
# TODO: autre type de packets
|
||||||
|
# TODO: type de packet générique
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
76
source/_test/position.py
Normal file
76
source/_test/position.py
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from source.gui.position import *
|
||||||
|
from source.gui.scene.abc import Scene
|
||||||
|
from source.gui.widget.abc import BoxWidget
|
||||||
|
from source.gui.window import Window
|
||||||
|
|
||||||
|
|
||||||
|
class TestWindow(Window): # NOQA
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super().__init__(width=1920, height=1080, visible=False, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
class TestScene(Scene):
|
||||||
|
def __init__(self, window: "Window", **kwargs):
|
||||||
|
super().__init__(window=window, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
class TestBoxWidget(BoxWidget):
|
||||||
|
def __init__(self, scene: "Scene"):
|
||||||
|
super().__init__(x=100, y=200, width=150, height=175, scene=scene)
|
||||||
|
|
||||||
|
|
||||||
|
window = TestWindow()
|
||||||
|
scene = TestScene(window)
|
||||||
|
widget = TestBoxWidget(scene)
|
||||||
|
|
||||||
|
|
||||||
|
class TestPosition(unittest.TestCase):
|
||||||
|
def test_unit_px(self):
|
||||||
|
for value in range(1, 500):
|
||||||
|
self.assertEqual((value*px)(widget), value)
|
||||||
|
|
||||||
|
def test_unit_vw(self):
|
||||||
|
for value in range(1, 200):
|
||||||
|
self.assertEqual((value*vw)(widget), int(window.width * (value / 100)))
|
||||||
|
|
||||||
|
def test_unit_vh(self):
|
||||||
|
for value in range(1, 200):
|
||||||
|
self.assertEqual((value*vh)(widget), int(window.height * (value / 100)))
|
||||||
|
|
||||||
|
def test_unit_ww(self):
|
||||||
|
for value in range(1, 200):
|
||||||
|
self.assertEqual((value*ww)(widget), int(widget.width * (value / 100)))
|
||||||
|
|
||||||
|
def test_unit_wh(self):
|
||||||
|
for value in range(1, 200):
|
||||||
|
self.assertEqual((value * wh)(widget), int(widget.height * (value / 100)))
|
||||||
|
|
||||||
|
def test_unit_add(self):
|
||||||
|
for value_px in range(1, 100):
|
||||||
|
for value_vw in range(1, 100):
|
||||||
|
self.assertEqual(
|
||||||
|
(value_px*px + value_vw*vw)(widget),
|
||||||
|
value_px + int(window.width * (value_vw / 100))
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_unit_sub(self):
|
||||||
|
for value_px in range(1, 100):
|
||||||
|
for value_vw in range(1, 100):
|
||||||
|
self.assertEqual(
|
||||||
|
(value_px * px - value_vw * vw)(widget),
|
||||||
|
value_px - int(window.width * (value_vw / 100))
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_unit_rsub(self):
|
||||||
|
for value_px in range(1, 100):
|
||||||
|
for value_vw in range(1, 100):
|
||||||
|
self.assertEqual(
|
||||||
|
(value_vw * vw - value_px * px)(widget),
|
||||||
|
int(window.width * (value_vw / 100)) - value_px
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
135
source/_test/utils.py
Normal file
135
source/_test/utils.py
Normal file
|
@ -0,0 +1,135 @@
|
||||||
|
import unittest
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
from source.utils import dict_filter, dict_filter_prefix, dict_add_prefix, copy_array_offset
|
||||||
|
|
||||||
|
|
||||||
|
class TestDict(unittest.TestCase):
|
||||||
|
def test_dict_filter(self):
|
||||||
|
self.assertEqual(
|
||||||
|
dict_filter(
|
||||||
|
lambda key, value: key.startswith("valeur"),
|
||||||
|
{"valeur1": 1, "valeur2": 2, "clé1": None}
|
||||||
|
),
|
||||||
|
{"valeur1": 1, "valeur2": 2}
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
dict_filter(
|
||||||
|
lambda key, value: key.startswith("test"),
|
||||||
|
{"test_AB": 1, "TEST_AB": 2.5, "début": 3}
|
||||||
|
),
|
||||||
|
{"test_AB": 1}
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
dict_filter(
|
||||||
|
lambda key, value: key.startswith("test"),
|
||||||
|
{"input_AB": 1, "TEST_AB": 2.5, "début": 3}
|
||||||
|
),
|
||||||
|
{}
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_dict_filter_prefix(self):
|
||||||
|
self.assertEqual(
|
||||||
|
dict_filter_prefix(
|
||||||
|
"valeur",
|
||||||
|
{"valeur1": 1, "valeur2": 2, "clé1": None}
|
||||||
|
),
|
||||||
|
{"1": 1, "2": 2}
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
dict_filter_prefix(
|
||||||
|
"test_",
|
||||||
|
{"test_AB": 1, "TEST_AB": 2.5, "début": 3}
|
||||||
|
),
|
||||||
|
{"AB": 1}
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
dict_filter_prefix(
|
||||||
|
"test",
|
||||||
|
{"input_AB": 1, "TEST_AB": 2.5, "début": 3}
|
||||||
|
),
|
||||||
|
{}
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_dict_add_prefix(self):
|
||||||
|
self.assertEqual(
|
||||||
|
dict_add_prefix(
|
||||||
|
"valeur",
|
||||||
|
{"1": 1, "2": 2, "A": None}
|
||||||
|
),
|
||||||
|
{"valeur1": 1, "valeur2": 2, "valeurA": None}
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
dict_add_prefix(
|
||||||
|
"test_",
|
||||||
|
{"AB": 1, "2": 2.5, "fin": 3}
|
||||||
|
),
|
||||||
|
{"test_AB": 1, "test_2": 2.5, "test_fin": 3}
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
dict_add_prefix(
|
||||||
|
"test",
|
||||||
|
{"A": 1, "B": 2.5, "E": 3}
|
||||||
|
),
|
||||||
|
{"testA": 1, "testB": 2.5, "testE": 3}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestMatrice(unittest.TestCase):
|
||||||
|
def test_copy_array_offset(self):
|
||||||
|
src = np.array([
|
||||||
|
[1, 2, 3, 4],
|
||||||
|
[5, 6, 7, 8]
|
||||||
|
])
|
||||||
|
dst = np.array([
|
||||||
|
[2, 5, 1, 3, 5, 0],
|
||||||
|
[2, 3, 4, 1, 4, 5],
|
||||||
|
[0, 1, 4, 0, 3, 9],
|
||||||
|
[4, 0, 9, 5, 2, 8]
|
||||||
|
])
|
||||||
|
|
||||||
|
copy_dst = dst.copy()
|
||||||
|
copy_array_offset(src=src, dst=copy_dst, offset=(1, 2))
|
||||||
|
|
||||||
|
self.assertTrue(
|
||||||
|
(copy_dst == np.array([
|
||||||
|
[2, 5, 1, 3, 5, 0],
|
||||||
|
[2, 3, 4, 1, 4, 5],
|
||||||
|
[0, 1, 2, 3, 4, 9],
|
||||||
|
[4, 5, 6, 7, 8, 8]
|
||||||
|
])).all()
|
||||||
|
)
|
||||||
|
|
||||||
|
copy_dst = dst.copy()
|
||||||
|
copy_array_offset(src=src, dst=copy_dst, offset=(2, 2))
|
||||||
|
|
||||||
|
self.assertTrue(
|
||||||
|
(copy_dst == np.array([
|
||||||
|
[2, 5, 1, 3, 5, 0],
|
||||||
|
[2, 3, 4, 1, 4, 5],
|
||||||
|
[0, 1, 1, 2, 3, 4],
|
||||||
|
[4, 0, 5, 6, 7, 8]
|
||||||
|
])).all()
|
||||||
|
)
|
||||||
|
|
||||||
|
copy_dst = dst.copy()
|
||||||
|
copy_array_offset(src=src, dst=copy_dst, offset=(1, 0))
|
||||||
|
|
||||||
|
self.assertTrue(
|
||||||
|
(copy_dst == np.array([
|
||||||
|
[2, 1, 2, 3, 4, 0],
|
||||||
|
[2, 5, 6, 7, 8, 5],
|
||||||
|
[0, 1, 4, 0, 3, 9],
|
||||||
|
[4, 0, 9, 5, 2, 8]
|
||||||
|
])).all()
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
|
@ -1,8 +1,6 @@
|
||||||
import builtins
|
|
||||||
import socket
|
import socket
|
||||||
from typing import Type, Callable
|
from typing import Type, Callable, TYPE_CHECKING
|
||||||
|
|
||||||
from source.gui.scene import Game
|
|
||||||
from source.network.packet.abc import Packet
|
from source.network.packet.abc import Packet
|
||||||
from source.network import packet
|
from source.network import packet
|
||||||
|
|
||||||
|
@ -10,10 +8,14 @@ from source.utils import StoppableThread
|
||||||
from source.utils.thread import in_pyglet_context
|
from source.utils.thread import in_pyglet_context
|
||||||
|
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from source.gui.scene import Game
|
||||||
|
|
||||||
|
|
||||||
def game_network(
|
def game_network(
|
||||||
thread: "StoppableThread",
|
thread: "StoppableThread",
|
||||||
connection: socket.socket,
|
connection: socket.socket,
|
||||||
game_scene: Game,
|
game_scene: "Game",
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Run the networking to make the game work and react with the other player
|
Run the networking to make the game work and react with the other player
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
from .matrice import copy_array_offset
|
from .matrice import copy_array_offset
|
||||||
from .bbox import in_bbox
|
from .bbox import in_bbox
|
||||||
from .dict import dict_filter, dict_filter_prefix
|
from .dict import dict_filter, dict_filter_prefix, dict_add_prefix
|
||||||
from .thread import StoppableThread
|
from .thread import StoppableThread
|
||||||
from .path import path_ctime_str
|
from .path import path_ctime_str
|
||||||
|
|
Loading…
Reference in a new issue