Skip to content

Commit

Permalink
display_test: Add more extensive display.update tests
Browse files Browse the repository at this point in the history
We have an interactive test, because there's no easy way to get
the screen pixels and see if the window only updates in the areas
given.

This is not perfect, because when running interactive the tests
run twice... once interactive, and once not. But it's still better
than repeating all the tests?
  • Loading branch information
illume committed Dec 26, 2021
1 parent b881c8a commit bc0eb39
Showing 1 changed file with 106 additions and 16 deletions.
122 changes: 106 additions & 16 deletions test/display_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,6 @@ def setUp(self):
def tearDown(self):
display.quit()

def test_update(self):
"""see if pygame.display.update takes rects with negative values.
"|Tags:display|"
"""
screen = pygame.display.set_mode((100, 100))
screen.fill((55, 55, 55))

r1 = pygame.Rect(0, 0, 100, 100)
pygame.display.update(r1)

r2 = pygame.Rect(-10, 0, 100, 100)
pygame.display.update(r2)

r3 = pygame.Rect(-10, 0, -100, -100)
pygame.display.update(r3)

def test_Info(self):
inf = pygame.display.Info()
self.assertNotEqual(inf.current_h, -1)
Expand Down Expand Up @@ -644,6 +628,112 @@ def test_toggle_fullscreen(self):
)


class DisplayUpdateTest(unittest.TestCase):
def question(self, qstr):
"""this is used in the interactive subclass."""

def setUp(self):
display.init()
self.screen = pygame.display.set_mode((500, 500))
self.screen.fill("black")
pygame.display.flip()
pygame.event.pump() # so mac updates

def tearDown(self):
display.quit()

def test_update_negative(self):
"""takes rects with negative values."""
self.screen.fill("green")

r1 = pygame.Rect(0, 0, 100, 100)
pygame.display.update(r1)

r2 = pygame.Rect(-10, 0, 100, 100)
pygame.display.update(r2)

r3 = pygame.Rect(-10, 0, -100, -100)
pygame.display.update(r3)

self.question("Is the screen green in (0, 0, 100, 100)?")

def test_update_sequence(self):
"""only updates the part of the display given by the rects."""
self.screen.fill("green")
rects = [
pygame.Rect(0, 0, 100, 100),
pygame.Rect(100, 0, 100, 100),
pygame.Rect(200, 0, 100, 100),
pygame.Rect(300, 300, 100, 100),
]
pygame.display.update(rects)
pygame.event.pump() # so mac updates

self.question(f"Is the screen green in {rects}?")

def test_update_none_skipped(self):
"""None is skipped inside sequences."""
self.screen.fill("green")
rects = (
None,
pygame.Rect(100, 0, 100, 100),
None,
pygame.Rect(200, 0, 100, 100),
pygame.Rect(300, 300, 100, 100),
)
pygame.display.update(rects)
pygame.event.pump() # so mac updates

self.question(f"Is the screen green in {rects}?")

def test_update_none(self):
"""does NOT update the display."""
self.screen.fill("green")
pygame.display.update(None)
pygame.event.pump() # so mac updates
self.question(f"Is the screen black and NOT green?")

def test_update_no_args(self):
"""does NOT update the display."""
self.screen.fill("green")
pygame.display.update()
pygame.event.pump() # so mac updates
self.question(f"Is the WHOLE screen green?")

def test_update_args(self):
"""updates the display using the args as a rect."""
self.screen.fill("green")
pygame.display.update(100, 100, 100, 100)
pygame.event.pump() # so mac updates
self.question("Is the screen green in (100, 100, 100, 100)?")

def test_update_incorrect_args(self):
"""raises a ValueError when inputs are wrong."""

with self.assertRaises(ValueError):
pygame.display.update(100, "asdf", 100, 100)

with self.assertRaises(ValueError):
pygame.display.update([100, "asdf", 100, 100])

def test_update_no_init(self):
"""raises a pygame.error."""

pygame.display.quit()
with self.assertRaises(pygame.error):
pygame.display.update()


class DisplayUpdateInteractiveTest(DisplayUpdateTest):
"""Because we want these tests to run as interactive and not interactive."""

__tags__ = ["interactive"]

def question(self, qstr):
"""since this is the interactive sublcass we ask a question."""
question(qstr)


class DisplayInteractiveTest(unittest.TestCase):

__tags__ = ["interactive"]
Expand Down

0 comments on commit bc0eb39

Please sign in to comment.