Skip to content

Commit

Permalink
test: improve testmarkdown.py, test pygame.IS_CE
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
KennedyRichard committed Oct 30, 2023
1 parent f8ef960 commit 1408fdc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
12 changes: 7 additions & 5 deletions nodezator/graphman/validation/testmarkdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
9 changes: 5 additions & 4 deletions nodezator/ourstdlibs/collections/fldict/testmarkdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]

Expand Down
12 changes: 10 additions & 2 deletions nodezator/rectsman/doctests/introduction.test.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 11 additions & 5 deletions nodezator/rectsman/doctests/testmarkdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
from doctest import DocFileSuite


### third-party import
### third-party imports

import pygame

from pygame import Rect


Expand All @@ -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'
]

Expand All @@ -42,6 +47,7 @@ def load_tests(loader, tests, ignore):
DocFileSuite(
*test_md_files,
globs={
'pygame': pygame,
'Rect': Rect,
'RectsManager': RectsManager,
'Simple': Simple,
Expand Down

0 comments on commit 1408fdc

Please sign in to comment.