Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Centralize shared behavior tests #913

Draft
wants to merge 15 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 22 additions & 10 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Tuple
from functools import lru_cache
from typing import Tuple, Union
from unittest import mock

from pytest import fixture
Expand Down Expand Up @@ -36,8 +37,12 @@ def _get_dummy_shader_program(*args, **kwargs):


# Color constants & fixtures for use with Shapes, UI elements, etc.
ORIGINAL_RGB_COLOR = 253, 254, 255
ORIGINAL_RGBA_COLOR = ORIGINAL_RGB_COLOR + (37,)

# Unusual, non-default colors test init's color keyword argument
ORIGINAL_RGB_COLOR = 252, 253, 254
ORIGINAL_RGBA_COLOR = 34, 35 ,36, 37

# Non-default, non-original colors test setters
NEW_RGB_COLOR = 1, 2, 3
NEW_RGBA_COLOR = 5, 6, 7, 59

Expand All @@ -57,18 +62,20 @@ def original_rgb_or_rgba_color(request):
return request.param


def expected_alpha_for_color(color: Tuple[int, ...]):
"""
Slow but readable color helper with validation.
@lru_cache
def expected_alpha_for_color(color: Union[Tuple[int, int, int, int], Tuple[int, int, int]]):
"""Get the expected alpha for a color.

This function offers the following benefits during tests:

This uses more readable logic than the main library and will raise
clear ValueErrors as part of validation.
1. More readable logic than the streamlined library code
2. Raises a clear ValueError when color's length is not 3 or 4

Args:
color: An RGB or RGBA color

Returns:

A 0-255 value for the color's expected alpha
"""
num_channels = len(color)

Expand All @@ -78,7 +85,7 @@ def expected_alpha_for_color(color: Tuple[int, ...]):
return 255

raise ValueError(
f"Expected color tuple with 3 or 4 elements, but got {color!r}.")
f"Expected color tuple with 3 or 4 values, but got {color!r}.")


@fixture
Expand All @@ -99,3 +106,8 @@ def new_rgba_color():
@fixture(params=[NEW_RGB_COLOR, NEW_RGBA_COLOR])
def new_rgb_or_rgba_color(request):
return request.param


@fixture
def new_rgb_or_rgba_expected_alpha(new_rgb_or_rgba_color) -> int:
return expected_alpha_for_color(new_rgb_or_rgba_color)
7 changes: 0 additions & 7 deletions tests/unit/shapes/conftest.py

This file was deleted.

6 changes: 6 additions & 0 deletions tests/unit/shapes/test_bordered_rectangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
from pyglet.shapes import BorderedRectangle


@pytest.fixture(autouse=True)
def monkeypatch_default_shape_shader(monkeypatch, get_dummy_shader_program):
"""Use a dummy shader when testing non-drawing functionality"""
monkeypatch.setattr('pyglet.shapes.get_default_shader', get_dummy_shader_program)


@pytest.fixture
def bordered_rectangle():
return BorderedRectangle(0, 0, 10, 50, color=(0, 0, 0, 31), border_color=(1, 1, 1))
Expand Down
85 changes: 0 additions & 85 deletions tests/unit/shapes/test_shapes.py

This file was deleted.

8 changes: 8 additions & 0 deletions tests/unit/shared_behavior/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""This module tests behavior common to objects which can be placed in a batch.

This includes:

- Color setters & getters
- Opacity setters & getters
- Batch setters & getters
"""