From 1408fdca79d4e98f79adda492cd32e8ca848c220 Mon Sep 17 00:00:00 2001 From: Kennedy Richard Date: Mon, 30 Oct 2023 11:42:40 -0300 Subject: [PATCH] test: improve testmarkdown.py, test pygame.IS_CE Improved doctests finding (testmarkdown.py files across the package) by defining the current directory outside the function that loads the tests. Also added a test that checks whether the pygame module used is indeed pygame-ce, by checking the existence of the pygame.IS_CE attribute. --- nodezator/graphman/validation/testmarkdown.py | 12 +++++++----- .../collections/fldict/testmarkdown.py | 9 +++++---- nodezator/rectsman/doctests/introduction.test.md | 12 ++++++++++-- nodezator/rectsman/doctests/testmarkdown.py | 16 +++++++++++----- 4 files changed, 33 insertions(+), 16 deletions(-) diff --git a/nodezator/graphman/validation/testmarkdown.py b/nodezator/graphman/validation/testmarkdown.py index 96236ef8..778fef76 100644 --- a/nodezator/graphman/validation/testmarkdown.py +++ b/nodezator/graphman/validation/testmarkdown.py @@ -12,24 +12,26 @@ from .main import check_return_annotation_mini_lang + +### this directory +THIS_DIR = Path(__file__).parent + ### function responsible for including the doctests from the .test.md ### files among the tests run when executing "python3 -m unittest" from ### the top level directory of the repository def load_tests(loader, tests, ignore): - topleveldir = Path(__file__).parent - test_md_files = [ - str(item.relative_to(topleveldir)) - for item in topleveldir.iterdir() + str(item.relative_to(THIS_DIR)) + for item in THIS_DIR.iterdir() if ''.join(item.suffixes) == '.test.md' ] if test_md_files: TEST_DATA = literal_eval( - (topleveldir / 'test_data.pyl').read_text(encoding='utf-8') + (THIS_DIR / 'test_data.pyl').read_text(encoding='utf-8') ) tests.addTests( diff --git a/nodezator/ourstdlibs/collections/fldict/testmarkdown.py b/nodezator/ourstdlibs/collections/fldict/testmarkdown.py index b6cade3a..2ee86c74 100644 --- a/nodezator/ourstdlibs/collections/fldict/testmarkdown.py +++ b/nodezator/ourstdlibs/collections/fldict/testmarkdown.py @@ -18,17 +18,18 @@ +### this directory +THIS_DIR = Path(__file__).parent + ### function responsible for including the doctests from the .test.md ### files among the tests run when executing "python3 -m unittest" from ### the top level directory of the repository def load_tests(loader, tests, ignore): - topleveldir = Path(__file__).parent - test_md_files = [ - str(item.relative_to(topleveldir)) - for item in topleveldir.iterdir() + str(item.relative_to(THIS_DIR)) + for item in THIS_DIR.iterdir() if ''.join(item.suffixes) == '.test.md' ] diff --git a/nodezator/rectsman/doctests/introduction.test.md b/nodezator/rectsman/doctests/introduction.test.md index 80bf2343..4126d3f6 100644 --- a/nodezator/rectsman/doctests/introduction.test.md +++ b/nodezator/rectsman/doctests/introduction.test.md @@ -7,7 +7,7 @@ This chapter is the first in a series containing documentation and tests for the ## Main goal and alternative usage -The rectsman package provide two important objects used to make any instance containing multiple rects to behave as if it had a single rect. +The rectsman package provides two important objects used to make any instance containing multiple rects to behave as if it had a single rect. The first object is a property called `rect_property`. It has a getter and setter implementation and was created to be injected in custom classes' in their "rect" class attribute so that it acts as the "rect" property of the class' instances. @@ -58,7 +58,7 @@ The RectsManager was created with the purpose of moving and aligning groups of r However, once we realized how handy such class is and that it could be used in a lot of use cases, even gameplay not related to UIs, we decided to make its API fully compatible with the pygame.Rect API shown in the online docs, thereby implementing size transformation as well. Therefore, code like shown below is now possible, too: ```python -# again, since this is just a demosntration, we +# again, since this is just a demonstration, we # skip the tests >>> obj.rect.width = 50 # doctest: +SKIP >>> obj.rect.width += 50 # doctest: +SKIP @@ -161,6 +161,14 @@ As you can see above, we pass the `get_all_rects` method from the instance of Di Since the RectsManager class is meant to emulate the behaviour of pygame.Rect, the representation of the RectsManager class is the same adopted by pygame.Rect. +Before we proceed, it is important to check whether we are using pygame-ce, which is the community edition fork of regular pygame. The representation of regular rects and RectsManager instances match the representation as presented in pygame-ce, so using regular pygame will cause the tests to fail. If the installed pygame is pygame-ce, the module will have a `IS_CE` attribute set to 1, like so: + +```python +>>> bool(getattr(pygame, 'IS_CE', False)) +True + +``` + We'll use the ListGroup class defined early and the `SimpleObject` class defined below to demonstrate the RectsManager representation ```python diff --git a/nodezator/rectsman/doctests/testmarkdown.py b/nodezator/rectsman/doctests/testmarkdown.py index 00b99357..ed1c5bfd 100644 --- a/nodezator/rectsman/doctests/testmarkdown.py +++ b/nodezator/rectsman/doctests/testmarkdown.py @@ -6,7 +6,10 @@ from doctest import DocFileSuite -### third-party import +### third-party imports + +import pygame + from pygame import Rect @@ -22,17 +25,19 @@ ) + +### this directory +THIS_DIR = Path(__file__).parent + ### function responsible for including the doctests from the .test.md ### files among the tests run when executing "python3 -m unittest" from ### the top level directory of the repository def load_tests(loader, tests, ignore): - topleveldir = Path(__file__).parent - test_md_files = [ - str(item.relative_to(topleveldir)) - for item in topleveldir.iterdir() + str(item.relative_to(THIS_DIR)) + for item in THIS_DIR.iterdir() if ''.join(item.suffixes) == '.test.md' ] @@ -42,6 +47,7 @@ def load_tests(loader, tests, ignore): DocFileSuite( *test_md_files, globs={ + 'pygame': pygame, 'Rect': Rect, 'RectsManager': RectsManager, 'Simple': Simple,