-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Compile module source from bytes to avoid encoding issues
- Loading branch information
Showing
6 changed files
with
192 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
WIDTH = HEIGHT = 300 | ||
|
||
|
||
def draw(): | ||
'''😂''' | ||
screen.fill('blue') | ||
screen.draw.text('foo', color='white', center=(150, 150)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,115 +1,117 @@ | ||
import unittest | ||
|
||
import pygame | ||
|
||
from pgzero.actor import calculate_anchor, Actor | ||
from pgzero.loaders import set_root | ||
|
||
|
||
TEST_MODULE = "pgzero.actor" | ||
TEST_DISP_W, TEST_DISP_H = (200, 100) | ||
|
||
|
||
pygame.init() | ||
pygame.display.set_mode((TEST_DISP_W, TEST_DISP_H)) | ||
|
||
|
||
class ModuleTest(unittest.TestCase): | ||
def test_calculate_anchor_with_float(self): | ||
self.assertEqual( | ||
calculate_anchor(1.23, "x", 12345), | ||
1.23 | ||
) | ||
|
||
def test_calculate_anchor_centre(self): | ||
self.assertEqual( | ||
calculate_anchor("center", "x", 100), | ||
50 | ||
) | ||
|
||
def test_calculate_anchor_bottom(self): | ||
self.assertEqual( | ||
calculate_anchor("bottom", "y", 100), | ||
100 | ||
) | ||
|
||
|
||
class ActorTest(unittest.TestCase): | ||
@classmethod | ||
def setUpClass(self): | ||
set_root(__file__) | ||
|
||
def test_sensible_init_defaults(self): | ||
a = Actor("alien") | ||
|
||
self.assertEqual(a.image, "alien") | ||
self.assertEqual(a.topleft, (0, 0)) | ||
|
||
def test_setting_absolute_initial_pos(self): | ||
a = Actor("alien", pos=(100, 200), anchor=("right", "bottom")) | ||
|
||
self.assertEqual( | ||
a.topleft, | ||
(100 - a.width, 200 - a.height), | ||
) | ||
|
||
def test_setting_relative_initial_pos_topleft(self): | ||
a = Actor("alien", topleft=(500, 500)) | ||
self.assertEqual(a.topleft, (500, 500)) | ||
|
||
def test_setting_relative_initial_pos_center(self): | ||
a = Actor("alien", center=(500, 500)) | ||
self.assertEqual(a.center, (500, 500)) | ||
|
||
def test_setting_relative_initial_pos_bottomright(self): | ||
a = Actor("alien", bottomright=(500, 500)) | ||
self.assertEqual(a.bottomright, (500, 500)) | ||
|
||
def test_setting_absolute_pos_and_relative_raises_typeerror(self): | ||
with self.assertRaises(TypeError): | ||
Actor("alien", pos=(0, 0), bottomright=(500, 500)) | ||
|
||
def test_setting_multiple_relative_pos_raises_typeerror(self): | ||
with self.assertRaises(TypeError): | ||
Actor("alien", topleft=(500, 500), bottomright=(600, 600)) | ||
|
||
def test_unexpected_kwargs(self): | ||
with self.assertRaises(TypeError) as cm: | ||
Actor("alien", toplift=(0, 0)) | ||
|
||
self.assertEqual( | ||
cm.exception.args[0], | ||
"Unexpected keyword argument 'toplift' (did you mean 'topleft'?)", | ||
) | ||
|
||
def test_set_pos_relative_to_anchor(self): | ||
a = Actor("alien", anchor=(10, 10)) | ||
a.pos = (100, 100) | ||
self.assertEqual(a.topleft, (90, 90)) | ||
|
||
def test_right_angle(self): | ||
a = Actor("alien") | ||
self.assertEqual(a.image, "alien") | ||
self.assertEqual(a.topleft, (0, 0)) | ||
self.assertEqual(a.pos, (33.0, 46.0)) | ||
self.assertEqual(a.width, 66) | ||
self.assertEqual(a.height, 92) | ||
a.angle += 90.0 | ||
self.assertEqual(a.angle, 90.0) | ||
self.assertEqual(a.topleft, (-13, 13)) | ||
self.assertEqual(a.pos, (33.0, 46.0)) | ||
self.assertEqual(a.width, 92) | ||
self.assertEqual(a.height, 66) | ||
|
||
def test_rotation(self): | ||
"""The pos of the actor must not drift with continued small rotation.""" | ||
a = Actor('alien', pos=(100.0, 100.0)) | ||
for _ in range(360): | ||
a.angle += 1.0 | ||
self.assertEqual(a.pos, (100.0, 100.0)) | ||
|
||
def test_dir_correct(self): | ||
"""Everything returned by dir should be indexable as an attribute.""" | ||
a = Actor("alien") | ||
for attribute in dir(a): | ||
a.__getattr__(attribute) | ||
import unittest | ||
|
||
import pygame | ||
|
||
from pgzero.actor import calculate_anchor, Actor | ||
from pgzero.loaders import set_root | ||
|
||
|
||
TEST_MODULE = "pgzero.actor" | ||
TEST_DISP_W, TEST_DISP_H = (200, 100) | ||
|
||
|
||
class ModuleTest(unittest.TestCase): | ||
def test_calculate_anchor_with_float(self): | ||
self.assertEqual( | ||
calculate_anchor(1.23, "x", 12345), | ||
1.23 | ||
) | ||
|
||
def test_calculate_anchor_centre(self): | ||
self.assertEqual( | ||
calculate_anchor("center", "x", 100), | ||
50 | ||
) | ||
|
||
def test_calculate_anchor_bottom(self): | ||
self.assertEqual( | ||
calculate_anchor("bottom", "y", 100), | ||
100 | ||
) | ||
|
||
|
||
class ActorTest(unittest.TestCase): | ||
@classmethod | ||
def setUpClass(self): | ||
pygame.init() | ||
pygame.display.set_mode((TEST_DISP_W, TEST_DISP_H)) | ||
set_root(__file__) | ||
|
||
@classmethod | ||
def tearDownClass(self): | ||
pygame.display.quit() | ||
|
||
def test_sensible_init_defaults(self): | ||
a = Actor("alien") | ||
|
||
self.assertEqual(a.image, "alien") | ||
self.assertEqual(a.topleft, (0, 0)) | ||
|
||
def test_setting_absolute_initial_pos(self): | ||
a = Actor("alien", pos=(100, 200), anchor=("right", "bottom")) | ||
|
||
self.assertEqual( | ||
a.topleft, | ||
(100 - a.width, 200 - a.height), | ||
) | ||
|
||
def test_setting_relative_initial_pos_topleft(self): | ||
a = Actor("alien", topleft=(500, 500)) | ||
self.assertEqual(a.topleft, (500, 500)) | ||
|
||
def test_setting_relative_initial_pos_center(self): | ||
a = Actor("alien", center=(500, 500)) | ||
self.assertEqual(a.center, (500, 500)) | ||
|
||
def test_setting_relative_initial_pos_bottomright(self): | ||
a = Actor("alien", bottomright=(500, 500)) | ||
self.assertEqual(a.bottomright, (500, 500)) | ||
|
||
def test_setting_absolute_pos_and_relative_raises_typeerror(self): | ||
with self.assertRaises(TypeError): | ||
Actor("alien", pos=(0, 0), bottomright=(500, 500)) | ||
|
||
def test_setting_multiple_relative_pos_raises_typeerror(self): | ||
with self.assertRaises(TypeError): | ||
Actor("alien", topleft=(500, 500), bottomright=(600, 600)) | ||
|
||
def test_unexpected_kwargs(self): | ||
with self.assertRaises(TypeError) as cm: | ||
Actor("alien", toplift=(0, 0)) | ||
|
||
self.assertEqual( | ||
cm.exception.args[0], | ||
"Unexpected keyword argument 'toplift' (did you mean 'topleft'?)", | ||
) | ||
|
||
def test_set_pos_relative_to_anchor(self): | ||
a = Actor("alien", anchor=(10, 10)) | ||
a.pos = (100, 100) | ||
self.assertEqual(a.topleft, (90, 90)) | ||
|
||
def test_right_angle(self): | ||
a = Actor("alien") | ||
self.assertEqual(a.image, "alien") | ||
self.assertEqual(a.topleft, (0, 0)) | ||
self.assertEqual(a.pos, (33.0, 46.0)) | ||
self.assertEqual(a.width, 66) | ||
self.assertEqual(a.height, 92) | ||
a.angle += 90.0 | ||
self.assertEqual(a.angle, 90.0) | ||
self.assertEqual(a.topleft, (-13, 13)) | ||
self.assertEqual(a.pos, (33.0, 46.0)) | ||
self.assertEqual(a.width, 92) | ||
self.assertEqual(a.height, 66) | ||
|
||
def test_rotation(self): | ||
"""The pos of the actor must not drift with continued small rotation.""" | ||
a = Actor('alien', pos=(100.0, 100.0)) | ||
for _ in range(360): | ||
a.angle += 1.0 | ||
self.assertEqual(a.pos, (100.0, 100.0)) | ||
|
||
def test_dir_correct(self): | ||
"""Everything returned by dir should be indexable as an attribute.""" | ||
a = Actor("alien") | ||
for attribute in dir(a): | ||
a.__getattr__(attribute) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
"""Tests for Pygame Zero's runner system. | ||
This module is also a Pygame Zero game so that we can run it with pgzero. | ||
""" | ||
import sys | ||
import unittest | ||
from pathlib import Path | ||
|
||
from pgzero.runner import load_and_run | ||
from pgzero import clock | ||
|
||
game_tests = Path(__file__).parent / 'game_tests' | ||
|
||
|
||
class RunnerTest(unittest.TestCase): | ||
"""Test that we can load and run the current file.""" | ||
|
||
def test_run(self): | ||
"""We can load and run a game saved as UTF-8.""" | ||
clock.schedule_unique(sys.exit, 0.05) | ||
with self.assertRaises(SystemExit): | ||
load_and_run(str(game_tests / 'utf8.py')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters