| 1 |
import unittest |
| 2 |
from unittest.mock import Mock |
| 3 |
from pgzero.game import PGZeroGame |
| 4 |
from pgzero.constants import mouse |
| 5 |
|
| 6 |
|
| 7 |
class Event: |
| 8 |
"""Mock event.""" |
| 9 |
def __init__(self, **kwargs): |
| 10 |
self.__dict__.update(kwargs) |
| 11 |
|
| 12 |
|
| 13 |
class EventDispatchTest(unittest.TestCase): |
| 14 |
def setUp(self): |
| 15 |
self.game = PGZeroGame(Mock()) |
| 16 |
|
| 17 |
def test_dispatch_handler(self): |
| 18 |
"""The handler dispatch converts a button value to an enum.""" |
| 19 |
presses = [] |
| 20 |
h = self.game.prepare_handler(lambda button: presses.append(button)) |
| 21 |
h(Event(button=3)) |
| 22 |
self.assertEqual(presses, [mouse.RIGHT]) |
| 23 |
|
| 24 |
def test_invalid_enum_value(self): |
| 25 |
"""Invalid enum values are suppressed (the handler is not called). |
| 26 |
|
| 27 |
This case exists because Pygame can emit event codes that do not |
| 28 |
correspond to any of its defined constants, in the case of unusual |
| 29 |
mice or unusual keyboard combinations. Because these are edge cases |
| 30 |
we simply drop the event. |
| 31 |
|
| 32 |
""" |
| 33 |
presses = [] |
| 34 |
h = self.game.prepare_handler(lambda button: presses.append(True)) |
| 35 |
h(Event(button=7)) |
| 36 |
self.assertEqual(presses, []) |
| 37 |
|
| 38 |
|
| 39 |
if __name__ == '__main__': |
| 40 |
unittest.main() |