WMICraft/logic/__tests__/knights_queue_test.py

129 lines
3.8 KiB
Python
Raw Normal View History

2022-04-04 21:07:40 +02:00
import unittest
from logic.knights_queue import KnightsQueue
from models.knight import Knight
class KnightsQueueTest(unittest.TestCase):
2022-04-04 21:51:32 +02:00
def test_should_skip_dead_knights(self):
knight1 = Knight(None)
2022-04-28 14:13:59 +02:00
knight1.max_hp = 0
2022-04-04 21:51:32 +02:00
knight2 = Knight(None)
2022-04-28 14:13:59 +02:00
knight2.max_hp = 0
2022-04-04 21:51:32 +02:00
knight3 = Knight(None)
2022-04-28 14:13:59 +02:00
knight3.max_hp = 1
2022-04-04 21:51:32 +02:00
knight4 = Knight(None)
2022-04-28 14:13:59 +02:00
knight4.max_hp = 0
2022-04-04 21:51:32 +02:00
knight5 = Knight(None)
2022-04-28 14:13:59 +02:00
knight5.max_hp = 0
2022-04-04 21:51:32 +02:00
knight6 = Knight(None)
2022-04-28 14:13:59 +02:00
knight6.max_hp = 1
2022-04-04 21:51:32 +02:00
knights_queue = KnightsQueue([knight1, knight2, knight3], [knight4, knight5, knight6])
res1 = knights_queue.dequeue_knight()
res2 = knights_queue.dequeue_knight()
2022-04-28 14:13:59 +02:00
self.assertEqual(res1.max_hp, 1)
self.assertEqual(res2.max_hp, 1)
2022-04-04 21:51:32 +02:00
def test_should_return_first_alive_knight(self):
knight1 = Knight(None)
2022-04-28 14:13:59 +02:00
knight1.max_hp = 222
2022-04-04 21:51:32 +02:00
knight2 = Knight(None)
2022-04-28 14:13:59 +02:00
knight2.max_hp = -1
2022-04-04 21:51:32 +02:00
knight3 = Knight(None)
2022-04-28 14:13:59 +02:00
knight3.max_hp = 1
2022-04-04 21:51:32 +02:00
knights_queue = KnightsQueue([knight1, knight2], [knight3])
res1 = knights_queue.dequeue_knight()
res2 = knights_queue.dequeue_knight()
res3 = knights_queue.dequeue_knight()
res4 = knights_queue.dequeue_knight()
self.assertEqual(res1, res3)
self.assertEqual(res2, res4)
2022-04-04 21:27:47 +02:00
def test_should_raise_when_knight_died_and_whole_team_dead(self):
with self.assertRaises(Exception):
knight1 = Knight(None)
2022-04-28 14:13:59 +02:00
knight1.max_hp = 222
2022-04-04 21:27:47 +02:00
knight2 = Knight(None)
2022-04-28 14:13:59 +02:00
knight2.max_hp = 1
2022-04-04 21:27:47 +02:00
knights_queue = KnightsQueue([knight1], [knight2])
knights_queue.dequeue_knight()
knights_queue.dequeue_knight()
2022-04-28 14:13:59 +02:00
knight2.max_hp = -2
2022-04-04 21:27:47 +02:00
knights_queue.dequeue_knight()
2022-04-04 21:51:32 +02:00
knights_queue.dequeue_knight()
2022-04-04 21:27:47 +02:00
2022-04-04 21:23:32 +02:00
def test_should_make_valid_next_turn(self):
knight1 = Knight(None)
2022-04-28 14:13:59 +02:00
knight1.max_hp = 222
2022-04-04 21:23:32 +02:00
knight2 = Knight(None)
2022-04-28 14:13:59 +02:00
knight2.max_hp = 1
2022-04-04 21:23:32 +02:00
knights_queue = KnightsQueue([knight1], [knight2])
previous_turn = knights_queue.team_idx_turn
knights_queue.dequeue_knight()
current_turn = knights_queue.team_idx_turn
self.assertNotEqual(previous_turn, current_turn)
2022-04-04 21:07:40 +02:00
def test_should_raise_when_team_has_dead_knights(self):
with self.assertRaises(Exception):
knight1 = Knight(None)
2022-04-28 14:13:59 +02:00
knight1.max_hp = 0
2022-04-04 21:07:40 +02:00
knight2 = Knight(None)
2022-04-28 14:13:59 +02:00
knight2.max_hp = -1
2022-04-04 21:07:40 +02:00
knight3 = Knight(None)
2022-04-28 14:13:59 +02:00
knight3.max_hp = -2
2022-04-04 21:07:40 +02:00
knight4 = Knight(None)
2022-04-28 14:13:59 +02:00
knight4.max_hp = 20
2022-04-04 21:07:40 +02:00
knights_queue = KnightsQueue([knight1, knight2, knight3], [knight4])
knights_queue.dequeue_knight()
knights_queue.dequeue_knight()
def test_should_return_knight_from_any_team_and_add_to_queue_again(self):
knight1 = Knight(None)
2022-04-28 14:13:59 +02:00
knight1.max_hp = 10
2022-04-04 21:07:40 +02:00
knight2 = Knight(None)
2022-04-28 14:13:59 +02:00
knight2.max_hp = 20
2022-04-04 21:07:40 +02:00
knights_queue = KnightsQueue([knight1], [knight2])
result1 = knights_queue.dequeue_knight()
result2 = knights_queue.dequeue_knight()
result3 = knights_queue.dequeue_knight()
self.assertIsNotNone(result1)
self.assertIsNotNone(result2)
self.assertIsNotNone(result3)
2022-04-28 14:13:59 +02:00
self.assertTrue(result1.max_hp == result3.max_hp)
2022-04-04 21:07:40 +02:00
def test_should_raise_when_only_one_team_alive(self):
with self.assertRaises(Exception):
knight = Knight(None)
2022-04-28 14:13:59 +02:00
knight.max_hp = 21
2022-04-04 21:07:40 +02:00
knights_queue = KnightsQueue([knight], [])
knights_queue.dequeue_knight()
def test_should_raise_when_no_team_alive(self):
with self.assertRaises(Exception):
knights_queue = KnightsQueue([], [])
knights_queue.dequeue_knight()
if __name__ == '__main__':
unittest.main()