This commit is contained in:
wittmannzl103469 committed 2026-11-05 15:08:42 +01:00
1 parent de41a02749
commit 9af09cebd3
764 files changed
+54218 -78859

No files matched your search

@@ -1,14 +1,10 @@
import collections
import os
import time
import unittest
import os
import pygame
pygame.display.init()
is_wayland = pygame.display.get_driver() == "wayland"
pygame.display.quit()
EVENT_TYPES = (
# pygame.NOEVENT,
# pygame.ACTIVEEVENT,
@@ -86,10 +82,6 @@ NAMES_AND_EVENTS = (
class EventTypeTest(unittest.TestCase):
def test_Event_alias(self):
"""Check if pygame.Event is present and the correct type."""
self.assertIs(pygame.Event, pygame.event.Event)
def test_Event(self):
"""Ensure an Event object can be created."""
e = pygame.event.Event(pygame.USEREVENT, some_attr=1, other_attr="1")
@@ -98,7 +90,7 @@ class EventTypeTest(unittest.TestCase):
self.assertEqual(e.other_attr, "1")
# Event now uses tp_dictoffset and tp_members:
# https://github.com/pygame-community/pygame-ce/issues/77
# https://github.com/pygame/pygame/issues/62
self.assertEqual(e.type, pygame.USEREVENT)
self.assertIs(e.dict, e.__dict__)
@@ -151,60 +143,26 @@ class EventTypeTest(unittest.TestCase):
c = pygame.event.Event(EVENT_TYPES[1], a=1)
d = pygame.event.Event(EVENT_TYPES[0], a=2)
self.assertTrue(a == a)
self.assertFalse(a != a)
self.assertTrue(a == b)
self.assertFalse(a != b)
self.assertTrue(a != c)
self.assertFalse(a == c)
self.assertTrue(a != d)
self.assertFalse(a == d)
# Comparing event a
self.assertEqual(a, a) # Event is equal to itself
self.assertEqual(a, b) # Same type and attributes
self.assertNotEqual(a, c) # Different type but same attributes
self.assertNotEqual(a, d) # Same type but different attributes
def test_event_args(self):
"""Test event creation with args"""
NESTED_DICT = {"haha": True, "no": 92138023, "test": ("y", "e", "s")}
EXPECTED_ATTRIBUTES = {
"foo": "bar",
"ratio": 100,
"favourite_numbers": [3, 26, 42],
"nested_dict": NESTED_DICT,
"complex": 3 + 4j,
}
for event in (
pygame.event.Event(1, EXPECTED_ATTRIBUTES), # pass as dict
pygame.event.Event(1, **EXPECTED_ATTRIBUTES), # pass as kwargs
pygame.event.Event( # test passing both dict and kwargs
1,
{"nested_dict": NESTED_DICT, "complex": 3 + 4j},
foo="bar",
ratio=100,
favourite_numbers=[3, 26, 42],
),
):
self.assertDictEqual(event.dict, EXPECTED_ATTRIBUTES)
# Comparing event b
self.assertEqual(b, a) # Same type and attributes
self.assertNotEqual(b, c) # Different type but same attributes
self.assertNotEqual(b, d) # Same type but different attributes
for out_of_range_val in (-1, -10, pygame.NUMEVENTS, pygame.NUMEVENTS + 1):
self.assertRaises(ValueError, pygame.event.Event, out_of_range_val)
# Comparing event c
self.assertNotEqual(c, a) # Different type but same attributes
self.assertNotEqual(c, b) # Different type but same attributes
self.assertNotEqual(c, d) # Different type and different attributes
for incorrect_type in ("string", 4 + 3j, [1, 2, 3], {"a": "b"}):
self.assertRaises(TypeError, pygame.event.Event, incorrect_type)
def test_event_common_dict_changes(self):
d = {}
events = [pygame.Event(pygame.event.custom_type(), d) for _ in range(2)]
self.assertNotEqual(events[0], events[1])
d["hi"] = "hello"
events[0].hello = "hi"
events[1].dict["amongus"] = "sus"
self.assertEqual(d, {"hi": "hello", "hello": "hi", "amongus": "sus"})
for ev in events:
self.assertIs(d, ev.dict)
self.assertEqual(ev.dict, {"hi": "hello", "hello": "hi", "amongus": "sus"})
self.assertEqual(ev.hi, "hello")
self.assertEqual(ev.hello, "hi")
self.assertEqual(ev.amongus, "sus")
# Comparing event d
self.assertNotEqual(d, a) # Same type but different attributes
self.assertNotEqual(d, b) # Same type but different attributes
self.assertNotEqual(d, c) # Different type and different attributes
race_condition_notification = """
@@ -253,32 +211,16 @@ class EventModuleArgsTest(unittest.TestCase):
self.assertRaises(ValueError, pygame.event.clear, 0x0010FFFFF)
self.assertRaises(TypeError, pygame.event.get, ["a", "b", "c"])
def test_peek_no_arg(self):
pygame.event.clear()
# do a strict "is" check and not just a simple truthy-test to check that
# we have a boolean instance.
self.assertIs(pygame.event.peek(), False)
self.assertIs(pygame.event.peek(None), False)
# peek should return True if there's any event on the queue
pygame.event.post(pygame.event.Event(pygame.USEREVENT))
self.assertIs(pygame.event.peek(), True)
self.assertIs(pygame.event.peek(None), True)
def test_peek(self):
self.assertIsInstance(pygame.event.peek(), bool)
self.assertIsInstance(pygame.event.peek(None), bool)
self.assertIsInstance(pygame.event.peek(None, True), bool)
pygame.event.peek()
pygame.event.peek(None)
pygame.event.peek(None, True)
self.assertIsInstance(pygame.event.peek(pump=False), bool)
self.assertIsInstance(pygame.event.peek(pump=True), bool)
self.assertIsInstance(pygame.event.peek(eventtype=None), bool)
self.assertIsInstance(
pygame.event.peek(eventtype=[pygame.KEYUP, pygame.KEYDOWN]), bool
)
self.assertIsInstance(
pygame.event.peek(eventtype=pygame.USEREVENT, pump=False), bool
)
pygame.event.peek(pump=False)
pygame.event.peek(pump=True)
pygame.event.peek(eventtype=None)
pygame.event.peek(eventtype=[pygame.KEYUP, pygame.KEYDOWN])
pygame.event.peek(eventtype=pygame.USEREVENT, pump=False)
class Foo:
pass
@@ -492,19 +434,6 @@ class EventModuleTest(unittest.TestCase):
self.assertEqual(e.a, "a" * 1024)
self.assertEqual(e.test, list(range(100)))
def test_post_same_reference(self):
"""
Test that event.post posts an event with a common reference to a dict.
"""
for event in (
pygame.event.Event(pygame.event.custom_type(), attr=(1, 2, 3, 4)),
pygame.event.Event(pygame.event.custom_type()),
):
pygame.event.post(event)
e = pygame.event.poll()
self.assertEqual(e.type, event.type)
self.assertIs(e.dict, event.dict)
def test_post_blocked(self):
"""
Test blocked events are not posted. Also test whether post()
@@ -721,6 +650,13 @@ class EventModuleTest(unittest.TestCase):
pygame.event.event_name(event), unexpected_name, f"0x{event:X}"
)
def test_event_name__kwargs(self):
"""Ensure event_name() returns the correct event name when kwargs used."""
for expected_name, event in NAMES_AND_EVENTS:
self.assertEqual(
pygame.event.event_name(type=event), expected_name, f"0x{event:X}"
)
def test_peek(self):
"""Ensure queued events can be peeked at."""
event_types = [pygame.KEYDOWN, pygame.KEYUP, pygame.MOUSEMOTION]
@@ -839,11 +775,12 @@ class EventModuleTest(unittest.TestCase):
"""Ensure pump() functions properly."""
pygame.event.pump()
@unittest.skipIf(
os.environ.get("SDL_VIDEODRIVER") == pygame.NULL_VIDEODRIVER,
"requires the SDL_VIDEODRIVER to be a non-null value",
)
@unittest.skipIf(is_wayland, "broken on wayland")
# @unittest.skipIf(
# os.environ.get("SDL_VIDEODRIVER") == "dummy",
# 'requires the SDL_VIDEODRIVER to be a non "dummy" value',
# )
# Fails on SDL 2.0.18
@unittest.skip("flaky test, and broken on 2.0.18 windows")
def test_set_grab__and_get_symmetric(self):
"""Ensure event grabbing can be enabled and disabled.
@@ -908,11 +845,12 @@ class EventModuleTest(unittest.TestCase):
self.assertTrue(blocked)
@unittest.skipIf(
os.environ.get("SDL_VIDEODRIVER") == pygame.NULL_VIDEODRIVER,
"requires the SDL_VIDEODRIVER to be a non-null value",
)
@unittest.skipIf(is_wayland, "broken on wayland")
# @unittest.skipIf(
# os.environ.get("SDL_VIDEODRIVER") == "dummy",
# 'requires the SDL_VIDEODRIVER to be a non "dummy" value',
# )
# Fails on SDL 2.0.18
@unittest.skip("flaky test, and broken on 2.0.18 windows")
def test_get_grab(self):
"""Ensure get_grab() works as expected"""
surf = pygame.display.set_mode((10, 10))
@@ -921,6 +859,22 @@ class EventModuleTest(unittest.TestCase):
pygame.event.set_grab(i % 2)
self.assertEqual(pygame.event.get_grab(), i % 2)
@unittest.skipIf(
os.environ.get("SDL_VIDEODRIVER") == "dummy",
"requires the SDL_VIDEODRIVER to be a non dummy value",
)
@unittest.skipIf(pygame.get_sdl_version() < (2, 0, 16), "Needs at least SDL 2.0.16")
def test_set_keyboard_grab_and_get_keyboard_grab(self):
"""Ensure set_keyboard_grab() and get_keyboard_grab() work as expected"""
surf = pygame.display.set_mode((10, 10))
pygame.event.set_keyboard_grab(True)
self.assertTrue(pygame.event.get_keyboard_grab())
pygame.event.set_keyboard_grab(False)
self.assertFalse(pygame.event.get_keyboard_grab())
def test_poll(self):
"""Ensure poll() works as expected"""
pygame.event.clear()