This commit is contained in:
wittmannzl103469 committed 2026-11-05 15:08:42 +01:00
1 parent de41a02749
commit 9af09cebd3
764 files changed
+54218 -78859

No files matched your search

@@ -1,14 +1,15 @@
from collections import OrderedDict
import copy
import platform
import random
import sys
import unittest
from collections import OrderedDict
import sys
import pygame
from pygame.locals import *
from pygame.math import Vector2
IS_PYPY = "PyPy" == platform.python_implementation()
@@ -24,7 +25,8 @@ def random_mask(size=(100, 100)):
def maskFromSurface(surface, threshold=127):
mask = pygame.Mask(surface.get_size())
if key := surface.get_colorkey():
key = surface.get_colorkey()
if key:
for y in range(surface.get_height()):
for x in range(surface.get_width()):
if surface.get_at((x + 0.1, y + 0.1)) != key:
@@ -197,7 +199,7 @@ class MaskTypeTest(unittest.TestCase):
"""Ensures masks are created correctly using the fill keyword
over a range of sizes.
Test masks of different sizes, including:
Tests masks of different sizes, including:
-masks 31 to 33 bits wide (32 bit boundaries)
-masks 63 to 65 bits wide (64 bit boundaries)
"""
@@ -707,7 +709,7 @@ class MaskTypeTest(unittest.TestCase):
def test_overlap__bit_boundaries(self):
"""Ensures overlap handles masks of different sizes correctly.
Test masks of different sizes, including:
Tests masks of different sizes, including:
-masks 31 to 33 bits wide (32 bit boundaries)
-masks 63 to 65 bits wide (64 bit boundaries)
"""
@@ -855,7 +857,7 @@ class MaskTypeTest(unittest.TestCase):
def test_overlap_area__bit_boundaries(self):
"""Ensures overlap_area handles masks of different sizes correctly.
Test masks of different sizes, including:
Tests masks of different sizes, including:
-masks 31 to 33 bits wide (32 bit boundaries)
-masks 63 to 65 bits wide (64 bit boundaries)
"""
@@ -1077,7 +1079,7 @@ class MaskTypeTest(unittest.TestCase):
def test_overlap_mask__bit_boundaries(self):
"""Ensures overlap_mask handles masks of different sizes correctly.
Test masks of different sizes, including:
Tests masks of different sizes, including:
-masks 31 to 33 bits wide (32 bit boundaries)
-masks 63 to 65 bits wide (64 bit boundaries)
"""
@@ -1169,7 +1171,7 @@ class MaskTypeTest(unittest.TestCase):
def test_fill__bit_boundaries(self):
"""Ensures masks of different sizes are filled correctly.
Test masks of different sizes, including:
Tests masks of different sizes, including:
-masks 31 to 33 bits wide (32 bit boundaries)
-masks 63 to 65 bits wide (64 bit boundaries)
"""
@@ -1198,7 +1200,7 @@ class MaskTypeTest(unittest.TestCase):
def test_clear__bit_boundaries(self):
"""Ensures masks of different sizes are cleared correctly.
Test masks of different sizes, including:
Tests masks of different sizes, including:
-masks 31 to 33 bits wide (32 bit boundaries)
-masks 63 to 65 bits wide (64 bit boundaries)
"""
@@ -1273,7 +1275,7 @@ class MaskTypeTest(unittest.TestCase):
def test_invert__bit_boundaries(self):
"""Ensures masks of different sizes are inverted correctly.
Test masks of different sizes, including:
Tests masks of different sizes, including:
-masks 31 to 33 bits wide (32 bit boundaries)
-masks 63 to 65 bits wide (64 bit boundaries)
"""
@@ -1467,7 +1469,7 @@ class MaskTypeTest(unittest.TestCase):
def test_draw__bit_boundaries(self):
"""Ensures draw handles masks of different sizes correctly.
Test masks of different sizes, including:
Tests masks of different sizes, including:
-masks 31 to 33 bits wide (32 bit boundaries)
-masks 63 to 65 bits wide (64 bit boundaries)
"""
@@ -1661,7 +1663,7 @@ class MaskTypeTest(unittest.TestCase):
def test_erase__bit_boundaries(self):
"""Ensures erase handles masks of different sizes correctly.
Test masks of different sizes, including:
Tests masks of different sizes, including:
-masks 31 to 33 bits wide (32 bit boundaries)
-masks 63 to 65 bits wide (64 bit boundaries)
"""
@@ -1740,7 +1742,7 @@ class MaskTypeTest(unittest.TestCase):
def test_count__bit_boundaries(self):
"""Ensures the set bits of different sized masks are counted correctly.
Test masks of different sizes, including:
Tests masks of different sizes, including:
-masks 31 to 33 bits wide (32 bit boundaries)
-masks 63 to 65 bits wide (64 bit boundaries)
"""
@@ -1919,9 +1921,17 @@ class MaskTypeTest(unittest.TestCase):
self.assertEqual(centroid, expected_centroid)
def todo_test_angle(self):
def test_angle(self):
"""Ensure a mask's orientation angle is correctly calculated."""
self.fail()
expected_angle = -45.0
expected_size = (100, 100)
surface = pygame.Surface(expected_size)
mask = pygame.mask.from_surface(surface)
angle = mask.angle() # Returns the orientation of the pixels
self.assertIsInstance(angle, float)
self.assertEqual(angle, expected_angle)
def test_angle__empty_mask(self):
"""Ensure an empty mask's angle is correctly calculated."""
@@ -2569,7 +2579,7 @@ class MaskTypeTest(unittest.TestCase):
@unittest.skipIf(IS_PYPY, "Segfaults on pypy")
def test_to_surface(self):
"""Ensures empty and full masks can be drawn onto surfaces."""
expected_ref_count = (2, 3)
expected_ref_count = 3
size = (33, 65)
surface = pygame.Surface(size, SRCALPHA, 32)
surface_color = pygame.Color("red")
@@ -2583,13 +2593,13 @@ class MaskTypeTest(unittest.TestCase):
self.assertIs(to_surface, surface)
if not IS_PYPY:
self.assertIn(sys.getrefcount(to_surface), expected_ref_count)
self.assertEqual(sys.getrefcount(to_surface), expected_ref_count)
self.assertEqual(to_surface.get_size(), size)
assertSurfaceFilled(self, to_surface, expected_color)
def test_to_surface__create_surface(self):
"""Ensures empty and full masks can be drawn onto a created surface."""
expected_ref_count = (1, 2)
expected_ref_count = 2
expected_flag = SRCALPHA
expected_depth = 32
size = (33, 65)
@@ -2606,7 +2616,7 @@ class MaskTypeTest(unittest.TestCase):
self.assertIsInstance(to_surface, pygame.Surface)
if not IS_PYPY:
self.assertIn(sys.getrefcount(to_surface), expected_ref_count)
self.assertEqual(sys.getrefcount(to_surface), expected_ref_count)
self.assertTrue(to_surface.get_flags() & expected_flag)
self.assertEqual(to_surface.get_bitsize(), expected_depth)
self.assertEqual(to_surface.get_size(), size)
@@ -2614,7 +2624,7 @@ class MaskTypeTest(unittest.TestCase):
def test_to_surface__surface_param(self):
"""Ensures to_surface accepts a surface arg/kwarg."""
expected_ref_count = (3, 4)
expected_ref_count = 4
expected_color = pygame.Color("white")
surface_color = pygame.Color("red")
size = (5, 3)
@@ -2632,13 +2642,13 @@ class MaskTypeTest(unittest.TestCase):
self.assertIs(to_surface, surface)
if not IS_PYPY:
self.assertIn(sys.getrefcount(to_surface), expected_ref_count)
self.assertEqual(sys.getrefcount(to_surface), expected_ref_count)
self.assertEqual(to_surface.get_size(), size)
assertSurfaceFilled(self, to_surface, expected_color)
def test_to_surface__setsurface_param(self):
"""Ensures to_surface accepts a setsurface arg/kwarg."""
expected_ref_count = (1, 2)
expected_ref_count = 2
expected_flag = SRCALPHA
expected_depth = 32
expected_color = pygame.Color("red")
@@ -2657,7 +2667,7 @@ class MaskTypeTest(unittest.TestCase):
self.assertIsInstance(to_surface, pygame.Surface)
if not IS_PYPY:
self.assertIn(sys.getrefcount(to_surface), expected_ref_count)
self.assertEqual(sys.getrefcount(to_surface), expected_ref_count)
self.assertTrue(to_surface.get_flags() & expected_flag)
self.assertEqual(to_surface.get_bitsize(), expected_depth)
self.assertEqual(to_surface.get_size(), size)
@@ -2665,7 +2675,7 @@ class MaskTypeTest(unittest.TestCase):
def test_to_surface__unsetsurface_param(self):
"""Ensures to_surface accepts a unsetsurface arg/kwarg."""
expected_ref_count = (1, 2)
expected_ref_count = 2
expected_flag = SRCALPHA
expected_depth = 32
expected_color = pygame.Color("red")
@@ -2683,7 +2693,7 @@ class MaskTypeTest(unittest.TestCase):
self.assertIsInstance(to_surface, pygame.Surface)
if not IS_PYPY:
self.assertIn(sys.getrefcount(to_surface), expected_ref_count)
self.assertEqual(sys.getrefcount(to_surface), expected_ref_count)
self.assertTrue(to_surface.get_flags() & expected_flag)
self.assertEqual(to_surface.get_bitsize(), expected_depth)
self.assertEqual(to_surface.get_size(), size)
@@ -2691,7 +2701,7 @@ class MaskTypeTest(unittest.TestCase):
def test_to_surface__setcolor_param(self):
"""Ensures to_surface accepts a setcolor arg/kwarg."""
expected_ref_count = (1, 2)
expected_ref_count = 2
expected_flag = SRCALPHA
expected_depth = 32
expected_color = pygame.Color("red")
@@ -2707,7 +2717,7 @@ class MaskTypeTest(unittest.TestCase):
self.assertIsInstance(to_surface, pygame.Surface)
if not IS_PYPY:
self.assertIn(sys.getrefcount(to_surface), expected_ref_count)
self.assertEqual(sys.getrefcount(to_surface), expected_ref_count)
self.assertTrue(to_surface.get_flags() & expected_flag)
self.assertEqual(to_surface.get_bitsize(), expected_depth)
self.assertEqual(to_surface.get_size(), size)
@@ -2727,8 +2737,8 @@ class MaskTypeTest(unittest.TestCase):
assertSurfaceFilled(self, to_surface, expected_color)
def test_to_surface__unsetcolor_param(self):
"""Ensures to_surface accepts an unsetcolor arg/kwarg."""
expected_ref_count = (1, 2)
"""Ensures to_surface accepts a unsetcolor arg/kwarg."""
expected_ref_count = 2
expected_flag = SRCALPHA
expected_depth = 32
expected_color = pygame.Color("red")
@@ -2746,7 +2756,7 @@ class MaskTypeTest(unittest.TestCase):
self.assertIsInstance(to_surface, pygame.Surface)
if not IS_PYPY:
self.assertIn(sys.getrefcount(to_surface), expected_ref_count)
self.assertEqual(sys.getrefcount(to_surface), expected_ref_count)
self.assertTrue(to_surface.get_flags() & expected_flag)
self.assertEqual(to_surface.get_bitsize(), expected_depth)
self.assertEqual(to_surface.get_size(), size)
@@ -2767,7 +2777,7 @@ class MaskTypeTest(unittest.TestCase):
def test_to_surface__dest_param(self):
"""Ensures to_surface accepts a dest arg/kwarg."""
expected_ref_count = (1, 2)
expected_ref_count = 2
expected_flag = SRCALPHA
expected_depth = 32
default_surface_color = (0, 0, 0, 0)
@@ -2791,7 +2801,7 @@ class MaskTypeTest(unittest.TestCase):
self.assertIsInstance(to_surface, pygame.Surface)
if not IS_PYPY:
self.assertIn(sys.getrefcount(to_surface), expected_ref_count)
self.assertEqual(sys.getrefcount(to_surface), expected_ref_count)
self.assertTrue(to_surface.get_flags() & expected_flag)
self.assertEqual(to_surface.get_bitsize(), expected_depth)
self.assertEqual(to_surface.get_size(), size)
@@ -2820,9 +2830,10 @@ class MaskTypeTest(unittest.TestCase):
assertSurfaceFilled(self, to_surface, expected_color, mask_rect)
assertSurfaceFilledIgnoreArea(self, to_surface, surface_color, mask_rect)
@unittest.expectedFailure
def test_to_surface__area_param(self):
"""Ensures to_surface accepts an area arg/kwarg."""
expected_ref_count = (1, 2)
expected_ref_count = 2
expected_flag = SRCALPHA
expected_depth = 32
default_surface_color = (0, 0, 0, 0)
@@ -2845,294 +2856,12 @@ class MaskTypeTest(unittest.TestCase):
self.assertIsInstance(to_surface, pygame.Surface)
if not IS_PYPY:
self.assertIn(sys.getrefcount(to_surface), expected_ref_count)
self.assertEqual(sys.getrefcount(to_surface), expected_ref_count)
self.assertTrue(to_surface.get_flags() & expected_flag)
self.assertEqual(to_surface.get_bitsize(), expected_depth)
self.assertEqual(to_surface.get_size(), size)
assertSurfaceFilled(self, to_surface, expected_color)
def test_to_surface__area_output(self):
dst = pygame.Surface((2, 2))
mask = pygame.mask.Mask((2, 2), fill=True)
white = (255, 255, 255, 255)
black = (0, 0, 0, 255)
# make sure the surface is black, this is to make sure the test surface and mask are different colors
self.assertEqual(dst.get_at((0, 0)), black)
# make sure the mask is set and the default color after conversion is white
self.assertEqual(mask.to_surface().get_at((0, 0)), white)
test_areas_plus_destinations_and_expected_pattern = [
[{"dest": (0, 0), "area": None}, [[white, white], [white, white]]],
[{"dest": (0, 0), "area": (0, 0, 1, 1)}, [[white, black], [black, black]]],
[{"dest": (0, 0), "area": (0, 0, 2, 2)}, [[white, white], [white, white]]],
[{"dest": (1, 0), "area": (0, 0, 1, 2)}, [[black, white], [black, white]]],
[{"dest": (0, 0), "area": (0, 0, 0, 0)}, [[black, black], [black, black]]],
[{"dest": (1, 0), "area": (-1, 0, 1, 1)}, [[black, black], [black, black]]],
[{"dest": (0, 0), "area": (-1, 0, 2, 1)}, [[white, black], [black, black]]],
[
{"dest": (1, 1), "area": (-2, -2, 1, 1)},
[[black, black], [black, black]],
],
[
{"dest": (0, 1), "area": (0, 0, 50, 50)},
[[black, black], [white, white]],
],
[
{"dest": (-1, 0), "area": (-1, 0, 2, 2)},
[[black, black], [black, black]],
],
[
{"dest": (0, -1), "area": (-1, 0, 3, 2)},
[[white, white], [black, black]],
],
[{"dest": (0, 0), "area": (2, 2, 2, 2)}, [[black, black], [black, black]]],
[
{"dest": (-5, -5), "area": (-5, -5, 6, 6)},
[[black, black], [black, black]],
],
[
{"dest": (-5, -5), "area": (-5, -5, 1, 1)},
[[black, black], [black, black]],
],
]
for kwargs, pattern in test_areas_plus_destinations_and_expected_pattern:
surface = dst.copy()
mask.to_surface(surface=surface, **kwargs)
for y, row in enumerate(pattern):
for x, value in enumerate(row):
self.assertEqual(surface.get_at((x, y)), value)
def create_surface_from_array(
self, size: tuple[int, int], array: list[list[pygame.Color]], *args, **kwargs
) -> pygame.Surface:
assert (len(array), len(array[0])) == size
surface = pygame.Surface(size, *args, **kwargs)
for y, row in enumerate(array):
for x, color in enumerate(row):
surface.set_at((x, y), color)
return surface
def assert_surface_matches_array(
self, surface: pygame.Surface, array: list[list[pygame.Color]]
) -> None:
assert (len(array), len(array[0])) == surface.get_size()
for y, row in enumerate(array):
for x, color in enumerate(row):
with self.subTest(x=x, y=y, color=color):
self.assertEqual(color, surface.get_at((x, y)))
def test_to_surface__area_with_setsurface_create_new(self):
c_1 = pygame.Color("red")
c_2 = pygame.Color("green")
size = (4, 4)
mask = pygame.Mask(size, fill=True)
set_surface = self.create_surface_from_array(
size=size,
array=[
[c_1, c_1, c_2, c_2],
[c_1, c_1, c_2, c_2],
[c_1, c_1, c_1, c_1],
[c_1, c_1, c_1, c_1],
],
flags=SRCALPHA,
)
area = pygame.Rect(0, 0, 2, 2)
expected_pixels = [
[c_1, c_1],
[c_1, c_1],
]
surface = mask.to_surface(area=area, setsurface=set_surface)
self.assert_surface_matches_array(surface, expected_pixels)
area = pygame.Rect(1, 1, 2, 2)
expected_pixels = [
[c_1, c_2],
[c_1, c_1],
]
surface = mask.to_surface(area=area, setsurface=set_surface)
self.assert_surface_matches_array(surface, expected_pixels)
area = pygame.Rect(2, 1, 2, 2)
expected_pixels = [
[c_2, c_2],
[c_1, c_1],
]
surface = mask.to_surface(area=area, setsurface=set_surface)
self.assert_surface_matches_array(surface, expected_pixels)
def test_to_surface__area_with_unsetsurface_create_new(self):
c_1 = pygame.Color("red")
c_2 = pygame.Color("green")
size = (4, 4)
mask = pygame.Mask(size, fill=False)
unset_surface = self.create_surface_from_array(
size=size,
array=[
[c_1, c_1, c_2, c_2],
[c_1, c_1, c_2, c_2],
[c_1, c_1, c_1, c_1],
[c_1, c_1, c_1, c_1],
],
flags=SRCALPHA,
)
area = pygame.Rect(0, 0, 2, 2)
expected_pixels = [
[c_1, c_1],
[c_1, c_1],
]
surface = mask.to_surface(area=area, unsetsurface=unset_surface)
self.assert_surface_matches_array(surface, expected_pixels)
area = pygame.Rect(1, 1, 2, 2)
expected_pixels = [
[c_1, c_2],
[c_1, c_1],
]
surface = mask.to_surface(area=area, unsetsurface=unset_surface)
self.assert_surface_matches_array(surface, expected_pixels)
area = pygame.Rect(2, 1, 2, 2)
expected_pixels = [
[c_2, c_2],
[c_1, c_1],
]
surface = mask.to_surface(area=area, unsetsurface=unset_surface)
self.assert_surface_matches_array(surface, expected_pixels)
def test_to_surface__area_with_setsurface_to_destination(self):
c_1 = pygame.Color("red")
c_2 = pygame.Color("green")
c_3 = pygame.Color("blue")
size = (4, 4)
mask = pygame.Mask(size, fill=True)
set_surface = self.create_surface_from_array(
size=size,
array=[
[c_1, c_1, c_2, c_2],
[c_1, c_1, c_2, c_2],
[c_1, c_1, c_1, c_1],
[c_1, c_1, c_1, c_1],
],
)
area = pygame.Rect(0, 0, 2, 2)
expected_pixels = [
[c_1, c_1, c_3, c_3],
[c_1, c_1, c_3, c_3],
[c_3, c_3, c_3, c_3],
[c_3, c_3, c_3, c_3],
]
surface = pygame.Surface(size)
surface.fill(c_3)
surface = mask.to_surface(surface=surface, area=area, setsurface=set_surface)
self.assert_surface_matches_array(surface, expected_pixels)
area = pygame.Rect(1, 1, 2, 2)
dest = (2, 0)
expected_pixels = [
[c_3, c_3, c_1, c_2],
[c_3, c_3, c_1, c_1],
[c_3, c_3, c_3, c_3],
[c_3, c_3, c_3, c_3],
]
surface = pygame.Surface(size)
surface.fill(c_3)
surface = mask.to_surface(
surface=surface, dest=dest, area=area, setsurface=set_surface
)
self.assert_surface_matches_array(surface, expected_pixels)
area = pygame.Rect(2, 1, 2, 2)
dest = (1, 1)
expected_pixels = [
[c_3, c_3, c_3, c_3],
[c_3, c_2, c_2, c_3],
[c_3, c_1, c_1, c_3],
[c_3, c_3, c_3, c_3],
]
surface = pygame.Surface(size)
surface.fill(c_3)
surface = mask.to_surface(
surface=surface, dest=dest, area=area, setsurface=set_surface
)
self.assert_surface_matches_array(surface, expected_pixels)
def test_to_surface__area_with_unsetsurface_to_destination(self):
c_1 = pygame.Color("red")
c_2 = pygame.Color("green")
c_3 = pygame.Color("blue")
size = (4, 4)
mask = pygame.Mask(size, fill=False)
unset_surface = self.create_surface_from_array(
size=size,
array=[
[c_1, c_1, c_2, c_2],
[c_1, c_1, c_2, c_2],
[c_1, c_1, c_1, c_1],
[c_1, c_1, c_1, c_1],
],
)
area = pygame.Rect(0, 0, 2, 2)
expected_pixels = [
[c_1, c_1, c_3, c_3],
[c_1, c_1, c_3, c_3],
[c_3, c_3, c_3, c_3],
[c_3, c_3, c_3, c_3],
]
surface = pygame.Surface(size)
surface.fill(c_3)
surface = mask.to_surface(
surface=surface, area=area, unsetsurface=unset_surface
)
self.assert_surface_matches_array(surface, expected_pixels)
area = pygame.Rect(1, 1, 2, 2)
dest = (2, 0)
expected_pixels = [
[c_3, c_3, c_1, c_2],
[c_3, c_3, c_1, c_1],
[c_3, c_3, c_3, c_3],
[c_3, c_3, c_3, c_3],
]
surface = pygame.Surface(size)
surface.fill(c_3)
surface = mask.to_surface(
surface=surface, dest=dest, area=area, unsetsurface=unset_surface
)
self.assert_surface_matches_array(surface, expected_pixels)
area = pygame.Rect(2, 1, 2, 2)
dest = (1, 1)
expected_pixels = [
[c_3, c_3, c_3, c_3],
[c_3, c_2, c_2, c_3],
[c_3, c_1, c_1, c_3],
[c_3, c_3, c_3, c_3],
]
surface = pygame.Surface(size)
surface.fill(c_3)
surface = mask.to_surface(
surface=surface, dest=dest, area=area, unsetsurface=unset_surface
)
self.assert_surface_matches_array(surface, expected_pixels)
def test_to_surface__area_default(self):
"""Ensures the default area is correct."""
expected_color = pygame.Color("white")
@@ -3492,6 +3221,7 @@ class MaskTypeTest(unittest.TestCase):
assertSurfaceFilled(self, to_surface, expected_color)
@unittest.expectedFailure
def test_to_surface__valid_area_formats(self):
"""Ensures to_surface handles valid area formats correctly."""
size = (3, 5)
@@ -3524,6 +3254,7 @@ class MaskTypeTest(unittest.TestCase):
assertSurfaceFilled(self, to_surface, expected_color, area_rect)
assertSurfaceFilledIgnoreArea(self, to_surface, surface_color, area_rect)
@unittest.expectedFailure
def test_to_surface__invalid_area_formats(self):
"""Ensures to_surface handles invalid area formats correctly."""
mask = pygame.mask.Mask((3, 5))
@@ -3539,9 +3270,10 @@ class MaskTypeTest(unittest.TestCase):
)
for area in invalid_areas:
with self.assertRaisesRegex(TypeError, "invalid rectstyle argument"):
mask.to_surface(area=area)
with self.assertRaisesRegex(TypeError, "invalid area argument"):
unused_to_surface = mask.to_surface(area=area)
@unittest.expectedFailure
def test_to_surface__negative_sized_area_rect(self):
"""Ensures to_surface correctly handles negative sized area rects."""
size = (3, 5)
@@ -3566,6 +3298,7 @@ class MaskTypeTest(unittest.TestCase):
assertSurfaceFilled(self, to_surface, expected_color, area)
assertSurfaceFilledIgnoreArea(self, to_surface, surface_color, area)
@unittest.expectedFailure
def test_to_surface__zero_sized_area_rect(self):
"""Ensures to_surface correctly handles zero sized area rects."""
size = (3, 5)
@@ -3591,10 +3324,10 @@ class MaskTypeTest(unittest.TestCase):
"""Ensures to_surface works with a default surface value
and combinations of other parameters.
This tests many parameter combinations with full and empty
This tests many different parameter combinations with full and empty
masks.
"""
expected_ref_count = (1, 2)
expected_ref_count = 2
expected_flag = SRCALPHA
expected_depth = 32
size = (5, 3)
@@ -3661,7 +3394,7 @@ class MaskTypeTest(unittest.TestCase):
self.assertIsInstance(to_surface, pygame.Surface)
if not IS_PYPY:
self.assertIn(
self.assertEqual(
sys.getrefcount(to_surface), expected_ref_count
)
self.assertTrue(to_surface.get_flags() & expected_flag)
@@ -3675,10 +3408,10 @@ class MaskTypeTest(unittest.TestCase):
"""Ensures to_surface works with a surface value
and combinations of other parameters.
This tests many parameter combinations with full and empty
This tests many different parameter combinations with full and empty
masks.
"""
expected_ref_count = (3, 4)
expected_ref_count = 4
expected_flag = SRCALPHA
expected_depth = 32
size = (5, 3)
@@ -3748,7 +3481,7 @@ class MaskTypeTest(unittest.TestCase):
self.assertIs(to_surface, surface)
if not IS_PYPY:
self.assertIn(
self.assertEqual(
sys.getrefcount(to_surface), expected_ref_count
)
self.assertTrue(to_surface.get_flags() & expected_flag)
@@ -3759,7 +3492,7 @@ class MaskTypeTest(unittest.TestCase):
assertSurfaceFilled(self, to_surface, expected_color)
def test_to_surface__set_and_unset_bits(self):
"""Ensures that to_surface works correctly with set/unset bits
"""Ensures that to_surface works correctly with with set/unset bits
when using the defaults for setcolor and unsetcolor.
"""
default_setcolor = pygame.Color("white")
@@ -3796,7 +3529,7 @@ class MaskTypeTest(unittest.TestCase):
to_surface.unlock()
def test_to_surface__set_and_unset_bits_with_setsurface_unsetsurface(self):
"""Ensures that to_surface works correctly with set/unset bits
"""Ensures that to_surface works correctly with with set/unset bits
when using setsurface and unsetsurface.
"""
width, height = size = (10, 20)
@@ -4778,12 +4511,12 @@ class MaskTypeTest(unittest.TestCase):
default_setcolor = pygame.Color("white")
default_unsetcolor = pygame.Color("black")
directions = [
[(s, 0) for s in range(-SIDE, SIDE + 1)], # left to right
[(0, s) for s in range(-SIDE, SIDE + 1)], # top to bottom
[(s, s) for s in range(-SIDE, SIDE + 1)], # topleft to bottomright diag
[(-s, s) for s in range(-SIDE, SIDE + 1)], # topright to bottomleft diag
]
directions = (
((s, 0) for s in range(-SIDE, SIDE + 1)), # left to right
((0, s) for s in range(-SIDE, SIDE + 1)), # top to bottom
((s, s) for s in range(-SIDE, SIDE + 1)), # topleft to bottomright diag
((-s, s) for s in range(-SIDE, SIDE + 1)), # topright to bottomleft diag
)
for fill in (True, False):
mask = pygame.mask.Mask((SIDE, SIDE), fill=fill)
@@ -4802,6 +4535,8 @@ class MaskTypeTest(unittest.TestCase):
self, to_surface, surface_color, overlap_rect
)
@unittest.expectedFailure
@unittest.skipIf(IS_PYPY, "Segfaults on pypy")
def test_to_surface__area_locations(self):
"""Ensures area rects can be different locations on/off the mask."""
SIDE = 7
@@ -4811,12 +4546,12 @@ class MaskTypeTest(unittest.TestCase):
default_setcolor = pygame.Color("white")
default_unsetcolor = pygame.Color("black")
directions = [
[(s, 0) for s in range(-SIDE, SIDE + 1)], # left to right
[(0, s) for s in range(-SIDE, SIDE + 1)], # top to bottom
[(s, s) for s in range(-SIDE, SIDE + 1)], # topleft to bottomright diag
[(-s, s) for s in range(-SIDE, SIDE + 1)], # topright to bottomleft diag
]
directions = (
((s, 0) for s in range(-SIDE, SIDE + 1)), # left to right
((0, s) for s in range(-SIDE, SIDE + 1)), # top to bottom
((s, s) for s in range(-SIDE, SIDE + 1)), # topleft to bottomright diag
((-s, s) for s in range(-SIDE, SIDE + 1)), # topright to bottomleft diag
)
for fill in (True, False):
mask = pygame.mask.Mask((SIDE, SIDE), fill=fill)
@@ -4838,6 +4573,7 @@ class MaskTypeTest(unittest.TestCase):
self, to_surface, surface_color, overlap_rect
)
@unittest.expectedFailure
def test_to_surface__dest_and_area_locations(self):
"""Ensures dest/area values can be different locations on/off the
surface/mask.
@@ -4851,16 +4587,16 @@ class MaskTypeTest(unittest.TestCase):
default_setcolor = pygame.Color("white")
default_unsetcolor = pygame.Color("black")
dest_directions = [
[(s, 0) for s in range(-SIDE, SIDE + 1)], # left to right
[(0, s) for s in range(-SIDE, SIDE + 1)], # top to bottom
[(s, s) for s in range(-SIDE, SIDE + 1)], # topleft to bottomright diag
[(-s, s) for s in range(-SIDE, SIDE + 1)], # topright to bottomleft diag
]
dest_directions = (
((s, 0) for s in range(-SIDE, SIDE + 1)), # left to right
((0, s) for s in range(-SIDE, SIDE + 1)), # top to bottom
((s, s) for s in range(-SIDE, SIDE + 1)), # topleft to bottomright diag
((-s, s) for s in range(-SIDE, SIDE + 1)), # topright to bottomleft diag
)
# Using only the topleft to bottomright diagonal to test the area (to
# reduce the number of loop iterations).
area_positions = dest_directions[2]
area_positions = list(dest_directions[2])
for fill in (True, False):
mask = pygame.mask.Mask((SIDE, SIDE), fill=fill)
@@ -4891,6 +4627,7 @@ class MaskTypeTest(unittest.TestCase):
self, to_surface, surface_color, dest_overlap_rect
)
@unittest.expectedFailure
def test_to_surface__area_sizes(self):
"""Ensures area rects can be different sizes."""
SIDE = 7
@@ -5350,6 +5087,8 @@ class MaskTypeTest(unittest.TestCase):
self, to_surface, surface_color, mask_rect
)
@unittest.expectedFailure
@unittest.skipIf(IS_PYPY, "Segfaults on pypy")
def test_to_surface__area_on_mask(self):
"""Ensures area values on the mask work correctly
when using the defaults for setcolor and unsetcolor.
@@ -5382,6 +5121,7 @@ class MaskTypeTest(unittest.TestCase):
self, to_surface, surface_color, overlap_rect
)
@unittest.expectedFailure
def test_to_surface__area_on_mask_with_setsurface_unsetsurface(self):
"""Ensures area values on the mask work correctly
when using setsurface and unsetsurface.
@@ -5438,6 +5178,8 @@ class MaskTypeTest(unittest.TestCase):
self, to_surface, surface_color, overlap_rect
)
@unittest.expectedFailure
@unittest.skipIf(IS_PYPY, "Segfaults on pypy")
def test_to_surface__area_off_mask(self):
"""Ensures area values off the mask work correctly
when using the defaults for setcolor and unsetcolor.
@@ -5473,6 +5215,8 @@ class MaskTypeTest(unittest.TestCase):
self, to_surface, surface_color, overlap_rect
)
@unittest.expectedFailure
@unittest.skipIf(IS_PYPY, "Segfaults on pypy")
def test_to_surface__area_off_mask_with_setsurface_unsetsurface(self):
"""Ensures area values off the mask work correctly
when using setsurface and unsetsurface.
@@ -5529,7 +5273,7 @@ class MaskTypeTest(unittest.TestCase):
def test_to_surface__surface_with_zero_size(self):
"""Ensures zero sized surfaces are handled correctly."""
expected_ref_count = (2, 3)
expected_ref_count = 3
size = (0, 0)
surface = pygame.Surface(size)
mask = pygame.mask.Mask((3, 4), fill=True)
@@ -5538,12 +5282,12 @@ class MaskTypeTest(unittest.TestCase):
self.assertIs(to_surface, surface)
if not IS_PYPY:
self.assertIn(sys.getrefcount(to_surface), expected_ref_count)
self.assertEqual(sys.getrefcount(to_surface), expected_ref_count)
self.assertEqual(to_surface.get_size(), size)
def test_to_surface__setsurface_with_zero_size(self):
"""Ensures zero sized setsurfaces are handled correctly."""
expected_ref_count = (1, 2)
expected_ref_count = 2
expected_flag = SRCALPHA
expected_depth = 32
expected_color = pygame.Color("white") # Default setcolor.
@@ -5555,7 +5299,7 @@ class MaskTypeTest(unittest.TestCase):
self.assertIsInstance(to_surface, pygame.Surface)
if not IS_PYPY:
self.assertIn(sys.getrefcount(to_surface), expected_ref_count)
self.assertEqual(sys.getrefcount(to_surface), expected_ref_count)
self.assertTrue(to_surface.get_flags() & expected_flag)
self.assertEqual(to_surface.get_bitsize(), expected_depth)
self.assertEqual(to_surface.get_size(), mask_size)
@@ -5563,7 +5307,7 @@ class MaskTypeTest(unittest.TestCase):
def test_to_surface__unsetsurface_with_zero_size(self):
"""Ensures zero sized unsetsurfaces are handled correctly."""
expected_ref_count = (1, 2)
expected_ref_count = 2
expected_flag = SRCALPHA
expected_depth = 32
expected_color = pygame.Color("black") # Default unsetcolor.
@@ -5575,7 +5319,7 @@ class MaskTypeTest(unittest.TestCase):
self.assertIsInstance(to_surface, pygame.Surface)
if not IS_PYPY:
self.assertIn(sys.getrefcount(to_surface), expected_ref_count)
self.assertEqual(sys.getrefcount(to_surface), expected_ref_count)
self.assertTrue(to_surface.get_flags() & expected_flag)
self.assertEqual(to_surface.get_bitsize(), expected_depth)
self.assertEqual(to_surface.get_size(), mask_size)
@@ -6329,7 +6073,7 @@ class MaskModuleTest(unittest.TestCase):
This test checks the masks created by the from_surface function using
16 and 32 bit surfaces. Each alpha value (0-255) is tested against
several threshold values.
several different threshold values.
Note: On 16 bit surface the requested alpha value can differ from what
is actually set. This test uses the value read from the surface.
"""
@@ -6348,7 +6092,7 @@ class MaskModuleTest(unittest.TestCase):
if depth < 32:
# On surfaces with depths < 32 the requested alpha can be
# different from what gets set. Use the value read from the
# different than what gets set. Use the value read from the
# surface.
alpha = surface.get_at((0, 0))[3]
@@ -6443,7 +6187,7 @@ class MaskModuleTest(unittest.TestCase):
# special handling.
for threshold in range(threshold_count):
# On surfaces with depths < 32 the requested alpha can be different
# from what gets set. Use the value read from the surface.
# than what gets set. Use the value read from the surface.
alpha = surface.get_at((threshold, 0))[3]
if alpha not in alpha_thresholds:
@@ -6512,7 +6256,7 @@ class MaskModuleTest(unittest.TestCase):
for colorkey in colorkeys:
surface.set_colorkey(colorkey)
# With some depths (i.e. 8 and 16) the actual colorkey can be
# different from what was requested via the set.
# different than what was requested via the set.
surface.fill(surface.get_colorkey())
mask = pygame.mask.from_surface(surface)
@@ -6590,19 +6334,21 @@ class MaskModuleTest(unittest.TestCase):
def test_from_threshold(self):
"""Does mask.from_threshold() work correctly?"""
bpp = [16, 24, 32]
a = [16, 24, 32]
for i in bpp:
for i in a:
surf = pygame.surface.Surface((70, 70), 0, i)
surf.fill((100, 50, 200), (20, 20, 20, 20))
mask = pygame.mask.from_threshold(
surf, (100, 50, 200, 255), (10, 10, 10, 255)
)
rects = mask.get_bounding_rects()
self.assertEqual(mask.count(), 400)
self.assertEqual(mask.get_bounding_rects(), [pygame.Rect((20, 20, 20, 20))])
for i in bpp:
for i in a:
surf = pygame.surface.Surface((70, 70), 0, i)
surf2 = pygame.surface.Surface((70, 70), 0, i)
surf.fill((100, 100, 100))
@@ -6619,10 +6365,6 @@ class MaskModuleTest(unittest.TestCase):
self.assertEqual(mask.count(), 100)
self.assertEqual(mask.get_bounding_rects(), [pygame.Rect((40, 40, 10, 10))])
for color in [(20, 20, 20), 10, "green", pygame.Color("blue")]:
surf = pygame.surface.Surface((10, 10))
pygame.mask.from_threshold(surf, color, color)
def test_zero_size_from_surface(self):
"""Ensures from_surface can create masks from zero sized surfaces."""
for size in ((100, 0), (0, 100), (0, 0)):