import os import unittest import pygame from pathlib import Path from sliceitoff.field import Field from sliceitoff.field.field import FieldSprite, SliceSprite from sliceitoff.display import Scaling class TestFieldSprite(unittest.TestCase): def test_can_create(self): fs = FieldSprite((0,0,320_000,220_000)) self.assertNotEqual(None, fs) class TestSliceSprite(unittest.TestCase): def test_can_create(self): ss = SliceSprite((0,0,20_000,20_000)) self.assertNotEqual(None, ss) def test_updating_and_selfkill_works(self): ss = SliceSprite((0,0,320_000,220_000)) g = pygame.sprite.Group() g.add(ss) self.assertNotEqual(None, ss) for _ in range(100): ss.update(dt=100) self.assertFalse(ss.alive()) class TestField(unittest.TestCase): def setUp(self): class MockStats: def __init__(self): self.field_count = 0 self.percent = 0 self.stats = MockStats() def test_can_create(self): field = Field() self.assertNotEqual(None, field) def test_calculate_current_area(self): field = Field(stats = self.stats) field.update_stats() self.assertAlmostEqual(100, self.stats.percent, delta = 0.1) def test_slicing_do_nothing_out_of_area(self): field = Field(stats = self.stats) self.assertEqual(None, field.slice((-10,-10), True, 0)) def test_slicing_does_something_on_the_area(self): field = Field(stats = self.stats) self.assertNotEqual(None, field.slice((10,10), True, 5)) def test_slicing_makes_areas(self): field = Field(stats = self.stats) field.slice((100_000,100_000), False, 8_000) self.assertNotEqual(2, field.active_rects()) def test_killing_empty_areas_works(self): field = Field(stats = self.stats) field.kill_if_not_colliding([]) self.assertNotEqual(0, field.active_rects()) def test_killing_empty_areas_not_killing_areas_in_use(self): field = Field(stats = self.stats) ss = SliceSprite((20_000,20_000,20_000,20_000)) field.kill_if_not_colliding([ss]) self.assertNotEqual(1, field.active_rects()) def test_update_not_crashing_after_slicing(self): field = Field(stats = self.stats) field.slice((100_000,100_000), False, 16_000) for _ in range(1000): field.update(dt=10)