bro
This commit is contained in:
1 parent
de41a02749
commit
9af09cebd3
764 files changed
+54218
-78859
No files matched your search
@@ -1,15 +1,14 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import io
|
||||
import sys
|
||||
import os
|
||||
import unittest
|
||||
import pathlib
|
||||
import platform
|
||||
import sys
|
||||
import unittest
|
||||
from tempfile import TemporaryDirectory
|
||||
|
||||
import pygame
|
||||
from pygame import font as pygame_font # So font can be replaced with ftfont
|
||||
|
||||
|
||||
FONTDIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "fixtures", "fonts")
|
||||
|
||||
|
||||
@@ -118,10 +117,7 @@ class FontModuleTest(unittest.TestCase):
|
||||
for font in fonts:
|
||||
path = pygame_font.match_font(font)
|
||||
self.assertFalse(path is None)
|
||||
self.assertTrue(
|
||||
os.path.isabs(path) and os.path.isfile(path),
|
||||
msg=f"path '{path}': exists: {os.path.exists(path)} is abs path:{os.path.isabs(path)} and is file:{os.path.isfile(path)} is link: {os.path.islink(path)} is mount: {os.path.ismount(path)} is dir: {os.path.isdir(path)}",
|
||||
)
|
||||
self.assertTrue(os.path.isabs(path) and os.path.isfile(path))
|
||||
|
||||
def test_match_font_name(self):
|
||||
"""That match_font accepts names of various types"""
|
||||
@@ -181,7 +177,7 @@ class FontModuleTest(unittest.TestCase):
|
||||
any(pygame_font.match_font(font, italic=True) for font in fonts)
|
||||
)
|
||||
|
||||
def test_issue_553(self):
|
||||
def test_issue_742(self):
|
||||
"""that the font background does not crash."""
|
||||
surf = pygame.Surface((320, 240))
|
||||
font = pygame_font.Font(None, 24)
|
||||
@@ -190,7 +186,7 @@ class FontModuleTest(unittest.TestCase):
|
||||
image.set_alpha(255)
|
||||
surf.blit(image, (0, 0))
|
||||
|
||||
# not pygame-ce issue 553, but be sure to test that background color is
|
||||
# not issue 742, but be sure to test that background color is
|
||||
# correctly issued on this mode
|
||||
self.assertEqual(surf.get_at((0, 0)), pygame.Color(0, 0, 0))
|
||||
|
||||
@@ -248,34 +244,9 @@ class FontTest(unittest.TestCase):
|
||||
self.assertEqual(tuple(screen.get_at((0, 0)))[:3], (255, 255, 255))
|
||||
self.assertEqual(tuple(screen.get_at(font_rect.topleft))[:3], (255, 255, 255))
|
||||
|
||||
# ftfont and font render with different arguments
|
||||
if pygame_font.__name__ == "pygame.font":
|
||||
font_surface = f.render(
|
||||
text=" bar",
|
||||
antialias=True,
|
||||
color=(0, 0, 0),
|
||||
bgcolor=(255, 255, 255),
|
||||
wraplength=0,
|
||||
)
|
||||
else:
|
||||
font_surface = f.render(
|
||||
text=" bar",
|
||||
antialias=True,
|
||||
color=(0, 0, 0),
|
||||
bgcolor=(255, 255, 255),
|
||||
)
|
||||
screen.fill((10, 10, 10))
|
||||
font_rect = font_surface.get_rect()
|
||||
font_rect.topleft = rect.topleft
|
||||
self.assertTrue(font_surface)
|
||||
screen.blit(font_surface, font_rect, font_rect)
|
||||
pygame.display.update()
|
||||
self.assertEqual(tuple(screen.get_at((0, 0)))[:3], (255, 255, 255))
|
||||
self.assertEqual(tuple(screen.get_at(font_rect.topleft))[:3], (255, 255, 255))
|
||||
|
||||
# If we don't have a real display, don't do this test.
|
||||
# Transparent background doesn't seem to work without a read video card.
|
||||
if os.environ.get("SDL_VIDEODRIVER") != pygame.NULL_VIDEODRIVER:
|
||||
if os.environ.get("SDL_VIDEODRIVER") != "dummy":
|
||||
screen.fill((10, 10, 10))
|
||||
font_surface = f.render(" bar", True, (0, 0, 0), None)
|
||||
font_rect = font_surface.get_rect()
|
||||
@@ -296,42 +267,6 @@ class FontTest(unittest.TestCase):
|
||||
self.assertEqual(tuple(screen.get_at((0, 0)))[:3], (10, 10, 10))
|
||||
self.assertEqual(tuple(screen.get_at(font_rect.topleft))[:3], (10, 10, 10))
|
||||
|
||||
def test_render_multiline(self):
|
||||
if pygame_font.__name__ == "pygame.ftfont":
|
||||
return
|
||||
|
||||
f = pygame_font.Font(None, 20)
|
||||
one_line = f.render("hello", True, "black", "white", 200)
|
||||
two_lines = f.render("hello\nworld", True, "black", "white", 200)
|
||||
self.assertGreater(two_lines.get_height(), one_line.get_height())
|
||||
|
||||
one_line = f.render("hello", True, "black", None, 200)
|
||||
two_lines = f.render("hello\nworld", True, "black", None, 200)
|
||||
self.assertGreater(two_lines.get_height(), one_line.get_height())
|
||||
|
||||
one_line = f.render("hello", False, "black", None, 200)
|
||||
two_lines = f.render("hello\nworld", False, "black", None, 200)
|
||||
self.assertGreater(two_lines.get_height(), one_line.get_height())
|
||||
|
||||
@unittest.skipIf(
|
||||
pygame.font.get_sdl_ttf_version() < (2, 22, 0), "bug fixed in SDL_ttf 2.22.0"
|
||||
)
|
||||
def test_render_multiple_newlines(self):
|
||||
if pygame_font.__name__ == "pygame.ftfont":
|
||||
return
|
||||
|
||||
f = pygame_font.Font(None, 20)
|
||||
one_newline = f.render("\n", True, "black", "white")
|
||||
two_newlines = f.render("\n\n", True, "black", "white")
|
||||
three_newlines = f.render("\n\n\n", True, "black", "white")
|
||||
self.assertTrue(
|
||||
one_newline.get_height()
|
||||
< two_newlines.get_height()
|
||||
< three_newlines.get_height()
|
||||
)
|
||||
self.assertEqual(one_newline.get_height() * 2, two_newlines.get_height())
|
||||
self.assertEqual(one_newline.get_height() * 3, three_newlines.get_height())
|
||||
|
||||
|
||||
@unittest.skipIf(IS_PYPY, "pypy skip known failure") # TODO
|
||||
class FontTypeTest(unittest.TestCase):
|
||||
@@ -344,10 +279,6 @@ class FontTypeTest(unittest.TestCase):
|
||||
def test_default_parameters(self):
|
||||
f = pygame_font.Font()
|
||||
|
||||
def test_font_alias(self):
|
||||
"""Check if pygame.Font is present and the correct type."""
|
||||
self.assertIs(pygame.Font, pygame.font.Font)
|
||||
|
||||
def test_get_ascent(self):
|
||||
# Checking ascent would need a custom test font to do properly.
|
||||
f = pygame_font.Font(None, 20)
|
||||
@@ -371,7 +302,7 @@ class FontTypeTest(unittest.TestCase):
|
||||
self.assertTrue(isinstance(height, int))
|
||||
self.assertTrue(height > 0)
|
||||
s = f.render("X", False, (255, 255, 255))
|
||||
self.assertAlmostEqual(s.get_height(), height, delta=3)
|
||||
self.assertTrue(s.get_size()[1] == height)
|
||||
|
||||
def test_get_linesize(self):
|
||||
# Checking linesize would need a custom test font to do properly.
|
||||
@@ -381,35 +312,6 @@ class FontTypeTest(unittest.TestCase):
|
||||
self.assertTrue(isinstance(linesize, int))
|
||||
self.assertTrue(linesize > 0)
|
||||
|
||||
@unittest.skipIf(
|
||||
pygame.font.get_sdl_ttf_version() < (2, 24, 0),
|
||||
"supported in SDL_ttf 2.24.0 onwards",
|
||||
)
|
||||
def test_set_linesize(self):
|
||||
if pygame_font.__name__ == "pygame.ftfont":
|
||||
return # not a pygame.ftfont thing
|
||||
|
||||
f = pygame_font.Font(None, 20)
|
||||
linesize = f.get_linesize()
|
||||
|
||||
# check increasing linesize
|
||||
f.set_linesize(linesize + 1)
|
||||
self.assertEqual(f.get_linesize(), linesize + 1)
|
||||
|
||||
# check random linesize
|
||||
expected_linesizes = [30, 1, 22, 34, 5, 10, 0]
|
||||
for expected_size in expected_linesizes:
|
||||
f.set_linesize(expected_size)
|
||||
self.assertEqual(f.get_linesize(), expected_size)
|
||||
|
||||
# check invalid linesize
|
||||
with self.assertRaises(ValueError):
|
||||
f.set_linesize(-1)
|
||||
with self.assertRaises(OverflowError):
|
||||
f.set_linesize(2**100)
|
||||
with self.assertRaises(TypeError):
|
||||
f.set_linesize(12.0)
|
||||
|
||||
def test_metrics(self):
|
||||
# Ensure bytes decoding works correctly. Can only compare results
|
||||
# with unicode for now.
|
||||
@@ -422,7 +324,7 @@ class FontTypeTest(unittest.TestCase):
|
||||
self.assertIsNotNone(um[0])
|
||||
self.assertEqual(um, bm)
|
||||
|
||||
u = "\u212a"
|
||||
u = "\u212A"
|
||||
b = u.encode("UTF-16")[2:] # Keep byte order consistent. [2:] skips BOM
|
||||
bm = f.metrics(b)
|
||||
|
||||
@@ -441,10 +343,7 @@ class FontTypeTest(unittest.TestCase):
|
||||
bm = f.metrics(u)
|
||||
|
||||
self.assertEqual(len(bm), 1)
|
||||
if pygame_font.__name__ != "pygame.ftfont":
|
||||
self.assertIsNotNone(bm[0])
|
||||
else:
|
||||
self.assertIsNone(bm[0])
|
||||
self.assertIsNone(bm[0])
|
||||
|
||||
return # unfinished
|
||||
# The documentation is useless here. How large a list?
|
||||
@@ -492,7 +391,7 @@ class FontTypeTest(unittest.TestCase):
|
||||
su = f.render(".", False, [0, 0, 0], [255, 255, 255])
|
||||
sb = f.render(b".", False, [0, 0, 0], [255, 255, 255])
|
||||
self.assertTrue(equal_images(su, sb))
|
||||
u = "\u212a"
|
||||
u = "\u212A"
|
||||
b = u.encode("UTF-16")[2:] # Keep byte order consistent. [2:] skips BOM
|
||||
sb = f.render(b, False, [0, 0, 0], [255, 255, 255])
|
||||
try: # FIXME why do we do this try/except ?
|
||||
@@ -512,7 +411,7 @@ class FontTypeTest(unittest.TestCase):
|
||||
# If the font module is SDL_ttf < 2.0.15 based, then it only supports UCS-2
|
||||
# it will raise an exception for an out-of-range UCS-4 code point.
|
||||
if hasattr(pygame_font, "UCS4"):
|
||||
ucs_2 = "\uffee"
|
||||
ucs_2 = "\uFFEE"
|
||||
s = f.render(ucs_2, False, [0, 0, 0], [255, 255, 255])
|
||||
ucs_4 = "\U00010000"
|
||||
s = f.render(ucs_4, False, [0, 0, 0], [255, 255, 255])
|
||||
@@ -542,12 +441,13 @@ class FontTypeTest(unittest.TestCase):
|
||||
self.assertFalse(f.get_underline())
|
||||
|
||||
def test_set_strikethrough(self):
|
||||
f = pygame_font.Font(None, 20)
|
||||
self.assertFalse(f.get_strikethrough())
|
||||
f.set_strikethrough(True)
|
||||
self.assertTrue(f.get_strikethrough())
|
||||
f.set_strikethrough(False)
|
||||
self.assertFalse(f.get_strikethrough())
|
||||
if pygame_font.__name__ != "pygame.ftfont":
|
||||
f = pygame_font.Font(None, 20)
|
||||
self.assertFalse(f.get_strikethrough())
|
||||
f.set_strikethrough(True)
|
||||
self.assertTrue(f.get_strikethrough())
|
||||
f.set_strikethrough(False)
|
||||
self.assertFalse(f.get_strikethrough())
|
||||
|
||||
def test_bold_attr(self):
|
||||
f = pygame_font.Font(None, 20)
|
||||
@@ -574,31 +474,13 @@ class FontTypeTest(unittest.TestCase):
|
||||
self.assertFalse(f.underline)
|
||||
|
||||
def test_set_strikethrough_property(self):
|
||||
f = pygame_font.Font(None, 20)
|
||||
self.assertFalse(f.strikethrough)
|
||||
f.strikethrough = True
|
||||
self.assertTrue(f.strikethrough)
|
||||
f.strikethrough = False
|
||||
self.assertFalse(f.strikethrough)
|
||||
|
||||
def test_set_align_property(self):
|
||||
if pygame_font.__name__ == "pygame.ftfont":
|
||||
return
|
||||
|
||||
f = pygame_font.Font(None, 20)
|
||||
|
||||
if pygame.font.get_sdl_ttf_version() < (2, 20, 0):
|
||||
with self.assertRaises(pygame.error):
|
||||
f.align = pygame.FONT_CENTER
|
||||
return
|
||||
|
||||
self.assertEqual(f.align, pygame.FONT_LEFT)
|
||||
f.align = pygame.FONT_CENTER
|
||||
self.assertEqual(f.align, pygame.FONT_CENTER)
|
||||
f.align = pygame.FONT_RIGHT
|
||||
self.assertEqual(f.align, pygame.FONT_RIGHT)
|
||||
f.align = pygame.FONT_LEFT
|
||||
self.assertEqual(f.align, pygame.FONT_LEFT)
|
||||
if pygame_font.__name__ != "pygame.ftfont":
|
||||
f = pygame_font.Font(None, 20)
|
||||
self.assertFalse(f.strikethrough)
|
||||
f.strikethrough = True
|
||||
self.assertTrue(f.strikethrough)
|
||||
f.strikethrough = False
|
||||
self.assertFalse(f.strikethrough)
|
||||
|
||||
def test_size(self):
|
||||
f = pygame_font.Font(None, 20)
|
||||
@@ -610,116 +492,16 @@ class FontTypeTest(unittest.TestCase):
|
||||
|
||||
self.assertIsInstance(w, int)
|
||||
self.assertIsInstance(h, int)
|
||||
|
||||
# test width and height match, with a tolerance for 1 pixel errors
|
||||
self.assertTrue(w - 1 <= s.get_width() <= w + 1)
|
||||
self.assertTrue(h - 1 <= s.get_height() <= h + 1)
|
||||
self.assertEqual(s.get_size(), size)
|
||||
self.assertEqual(f.size(btext), size)
|
||||
|
||||
text = "\u212a"
|
||||
text = "\u212A"
|
||||
btext = text.encode("UTF-16")[2:] # Keep the byte order consistent.
|
||||
bsize = f.size(btext)
|
||||
size = f.size(text)
|
||||
|
||||
self.assertNotEqual(size, bsize)
|
||||
|
||||
def test_point_size_property(self):
|
||||
if pygame_font.__name__ == "pygame.ftfont":
|
||||
return # not a pygame.ftfont feature
|
||||
|
||||
pygame_font.init()
|
||||
font_path = os.path.join(
|
||||
os.path.split(pygame.__file__)[0], pygame_font.get_default_font()
|
||||
)
|
||||
f = pygame_font.Font(pathlib.Path(font_path), 25)
|
||||
self.assertEqual(25, f.point_size)
|
||||
f.point_size = 10
|
||||
self.assertEqual(10, f.point_size)
|
||||
f.point_size += 23
|
||||
self.assertEqual(33, f.point_size)
|
||||
f.point_size -= 2
|
||||
self.assertEqual(31, f.point_size)
|
||||
|
||||
def test_neg():
|
||||
f.point_size = -500
|
||||
|
||||
def test_incorrect_type():
|
||||
f.point_size = "15"
|
||||
|
||||
self.assertRaises(ValueError, test_neg)
|
||||
self.assertRaises(TypeError, test_incorrect_type)
|
||||
|
||||
def test_point_size_method(self):
|
||||
if pygame_font.__name__ == "pygame.ftfont":
|
||||
return # not a pygame.ftfont feature
|
||||
|
||||
pygame_font.init()
|
||||
font_path = os.path.join(
|
||||
os.path.split(pygame.__file__)[0], pygame_font.get_default_font()
|
||||
)
|
||||
f = pygame_font.Font(pathlib.Path(font_path), 25)
|
||||
|
||||
self.assertEqual(25, f.get_point_size())
|
||||
f = pygame_font.Font(None, 25)
|
||||
f.set_point_size(10)
|
||||
self.assertEqual(10, f.get_point_size())
|
||||
self.assertRaises(ValueError, f.set_point_size, -500)
|
||||
self.assertRaises(TypeError, f.set_point_size, "15")
|
||||
|
||||
def test_outline_property(self):
|
||||
if pygame_font.__name__ == "pygame.ftfont":
|
||||
return # not a pygame.ftfont feature
|
||||
|
||||
pygame_font.init()
|
||||
font_path = os.path.join(
|
||||
os.path.split(pygame.__file__)[0], pygame_font.get_default_font()
|
||||
)
|
||||
f = pygame_font.Font(pathlib.Path(font_path), 25)
|
||||
|
||||
# Default outline should be an integer >= 0 (typically 0)
|
||||
self.assertIsInstance(f.outline, int)
|
||||
self.assertGreaterEqual(f.outline, 0)
|
||||
|
||||
orig = f.outline
|
||||
f.outline = orig + 1
|
||||
self.assertEqual(orig + 1, f.outline)
|
||||
f.outline += 2
|
||||
self.assertEqual(orig + 3, f.outline)
|
||||
f.outline -= 1
|
||||
self.assertEqual(orig + 2, f.outline)
|
||||
|
||||
def test_neg():
|
||||
f.outline = -1
|
||||
|
||||
def test_incorrect_type():
|
||||
f.outline = "2"
|
||||
|
||||
self.assertRaises(ValueError, test_neg)
|
||||
self.assertRaises(TypeError, test_incorrect_type)
|
||||
|
||||
def test_font_name(self):
|
||||
f = pygame_font.Font(None, 20)
|
||||
self.assertEqual(f.name, "FreeSans")
|
||||
|
||||
def test_set_name():
|
||||
f.name = "Say my name."
|
||||
|
||||
self.assertRaises(AttributeError, test_set_name)
|
||||
|
||||
def test_font_style_name(self):
|
||||
f = pygame_font.Font(None, 20)
|
||||
self.assertNotEqual(f.style_name, "")
|
||||
self.assertNotEqual(f.style_name, None)
|
||||
self.assertIsInstance(f.style_name, str)
|
||||
|
||||
def test_font_style_name_is_readonly(self):
|
||||
f = pygame_font.Font(None, 20)
|
||||
|
||||
def _set_style_name():
|
||||
f.style_name = "Say my other name."
|
||||
|
||||
self.assertRaises(AttributeError, _set_style_name)
|
||||
|
||||
def test_font_file_not_found(self):
|
||||
# A per BUG reported by Bo Jangeborg on pygame-user mailing list,
|
||||
# http://www.mail-archive.com/pygame-users@seul.org/msg11675.html
|
||||
@@ -774,12 +556,6 @@ class FontTypeTest(unittest.TestCase):
|
||||
with open(font_path, "rb") as f:
|
||||
font = pygame_font.Font(f)
|
||||
|
||||
def test_load_from_invalid_sized_file_obj(self):
|
||||
pygame.font.init()
|
||||
f = io.StringIO()
|
||||
with self.assertRaises(ValueError):
|
||||
font = pygame.font.Font(f)
|
||||
|
||||
def test_load_default_font_filename(self):
|
||||
# In font_init, a special case is when the filename argument is
|
||||
# identical to the default font file name.
|
||||
@@ -793,12 +569,19 @@ class FontTypeTest(unittest.TestCase):
|
||||
def _load_unicode(self, path):
|
||||
import shutil
|
||||
|
||||
with TemporaryDirectory() as tmpdir:
|
||||
fdir = str(FONTDIR)
|
||||
temp = os.path.join(tmpdir, path)
|
||||
pgfont = os.path.join(fdir, "test_sans.ttf")
|
||||
shutil.copy(pgfont, temp)
|
||||
fdir = str(FONTDIR)
|
||||
temp = os.path.join(fdir, path)
|
||||
pgfont = os.path.join(fdir, "test_sans.ttf")
|
||||
shutil.copy(pgfont, temp)
|
||||
try:
|
||||
with open(temp, "rb") as f:
|
||||
pass
|
||||
except FileNotFoundError:
|
||||
raise unittest.SkipTest("the path cannot be opened")
|
||||
try:
|
||||
pygame_font.Font(temp, 20)
|
||||
finally:
|
||||
os.remove(temp)
|
||||
|
||||
def test_load_from_file_unicode_0(self):
|
||||
"""ASCII string as a unicode object"""
|
||||
@@ -820,7 +603,7 @@ class FontTypeTest(unittest.TestCase):
|
||||
bfont_path = font_path.encode(filesystem_encoding, filesystem_errors)
|
||||
f = pygame_font.Font(bfont_path, 20)
|
||||
|
||||
def test_issue_1587(self):
|
||||
def test_issue_3144(self):
|
||||
fpath = os.path.join(FONTDIR, "PlayfairDisplaySemibold.ttf")
|
||||
|
||||
# issue in SDL_ttf 2.0.18 DLL on Windows
|
||||
@@ -851,224 +634,9 @@ class FontTypeTest(unittest.TestCase):
|
||||
else:
|
||||
self.assertRaises(pygame.error, font.set_script, "Deva")
|
||||
|
||||
def test_font_set_direction(self):
|
||||
if pygame_font.__name__ == "pygame.ftfont":
|
||||
return # not a pygame.ftfont thing
|
||||
|
||||
font = pygame_font.Font(None, 16)
|
||||
|
||||
ttf_version = pygame.font.get_sdl_ttf_version()
|
||||
if ttf_version >= (2, 20, 0):
|
||||
self.assertRaises(TypeError, pygame.font.Font.set_direction)
|
||||
self.assertRaises(TypeError, pygame.font.Font.set_direction, font)
|
||||
self.assertRaises(TypeError, pygame.font.Font.set_direction, "font")
|
||||
self.assertRaises(TypeError, font.set_direction, [1, 1])
|
||||
self.assertRaises(TypeError, font.set_direction, "string1", "string2")
|
||||
|
||||
self.assertRaises(ValueError, font.set_direction, -1)
|
||||
self.assertRaises(ValueError, font.set_direction, 4)
|
||||
|
||||
font.set_direction(pygame.DIRECTION_RTL)
|
||||
|
||||
else:
|
||||
self.assertRaises(pygame.error, font.set_direction, pygame.DIRECTION_RTL)
|
||||
|
||||
def test_font_method_should_raise_exception_after_quit(self):
|
||||
# arrange
|
||||
if pygame_font.__name__ == "pygame.ftfont":
|
||||
surf = pygame.Surface((1, 1))
|
||||
methods = [
|
||||
("get_sized_height", ()),
|
||||
("get_sized_ascender", ()),
|
||||
("get_sized_glyph_height", ()),
|
||||
("get_rect", ("any text",)),
|
||||
("get_metrics", ("any text",)),
|
||||
("get_sizes", ()),
|
||||
("render", ("any text", True, "white")),
|
||||
(
|
||||
"render_to",
|
||||
(
|
||||
surf,
|
||||
(10, 11),
|
||||
"text",
|
||||
),
|
||||
), # surf
|
||||
("render_raw", ("any text",)),
|
||||
(
|
||||
"render_raw_to",
|
||||
(
|
||||
surf.get_view("2"),
|
||||
None,
|
||||
),
|
||||
),
|
||||
("get_ascent", ()),
|
||||
("get_bold", ()),
|
||||
("get_descent", ()),
|
||||
("get_height", ()),
|
||||
("get_italic", ()),
|
||||
("get_linesize", ()),
|
||||
("get_sized_descender", ()),
|
||||
("get_underline", ()),
|
||||
("metrics", ("any text",)),
|
||||
("set_bold", (True,)),
|
||||
("set_italic", (True,)),
|
||||
("set_underline", (True,)),
|
||||
("size", ("any Text",)),
|
||||
]
|
||||
skip_methods = {"get_strikethrough", "set_strikethrough"}
|
||||
else:
|
||||
methods = [
|
||||
("get_height", ()),
|
||||
("get_descent", ()),
|
||||
("get_ascent", ()),
|
||||
("get_linesize", ()),
|
||||
("get_bold", ()),
|
||||
("set_bold", (True,)),
|
||||
("get_italic", ()),
|
||||
("set_italic", (True,)),
|
||||
("get_underline", ()),
|
||||
("set_underline", (True,)),
|
||||
("get_strikethrough", ()),
|
||||
("set_strikethrough", (True,)),
|
||||
("metrics", ("any text",)),
|
||||
("render", ("hello", True, "white")),
|
||||
("size", ("any text",)),
|
||||
("set_script", ("is it other text",)),
|
||||
("set_direction", ("is it text",)),
|
||||
("get_point_size", ()),
|
||||
("set_point_size", (34,)),
|
||||
]
|
||||
skip_methods = set()
|
||||
version = pygame.font.get_sdl_ttf_version()
|
||||
|
||||
if version >= (2, 24, 0):
|
||||
methods.append(("set_linesize", (2,)))
|
||||
else:
|
||||
skip_methods.add("set_linesize")
|
||||
|
||||
if version < (2, 20, 0):
|
||||
skip_methods.add("align")
|
||||
|
||||
font = pygame_font.Font(None, 10)
|
||||
actual_names = []
|
||||
for n in sorted(dir(font)):
|
||||
if n in skip_methods:
|
||||
continue
|
||||
if n.startswith("_"):
|
||||
continue
|
||||
if not callable(getattr(font, n)):
|
||||
continue
|
||||
actual_names.append(n)
|
||||
expected_names = [n for n, a in sorted(methods)]
|
||||
self.assertEqual(
|
||||
len(expected_names),
|
||||
len(actual_names),
|
||||
msg=f"ensure all methods are checked \n{expected_names} \n!= \n{actual_names}",
|
||||
)
|
||||
|
||||
for name, args in methods:
|
||||
# import sys
|
||||
# print("###", name,file=sys.__stdout__,flush=True)
|
||||
with self.subTest(name=name):
|
||||
# arrange
|
||||
font = pygame_font.Font(None, 16)
|
||||
pygame_font.quit()
|
||||
pygame_font.init()
|
||||
|
||||
# act / assert
|
||||
self.assertRaises(pygame.error, getattr(font, name), *args)
|
||||
|
||||
def test_font_property_should_raise_exception_after_quit(self):
|
||||
if pygame_font.__name__ == "pygame.ftfont":
|
||||
properties = [
|
||||
("style", 10.5),
|
||||
("height", None),
|
||||
("ascender", None),
|
||||
("descender", None),
|
||||
("name", None),
|
||||
("style_name", None),
|
||||
("path", None),
|
||||
("scalable", None),
|
||||
("fixed_width", None),
|
||||
("fixed_sizes", None),
|
||||
("antialiased", 45),
|
||||
("kerning", 46),
|
||||
("vertical", 47),
|
||||
("pad", 48),
|
||||
("oblique", 1),
|
||||
("strong", 1),
|
||||
("underline", 1),
|
||||
("wide", 1),
|
||||
("strength", 49),
|
||||
("underline_adjustment", 50),
|
||||
("ucs4", 1),
|
||||
("use_bitmap_strikes", 1),
|
||||
("rotation", 33),
|
||||
("fgcolor", (1, 1, 1)),
|
||||
("bgcolor", (2, 2, 2)),
|
||||
("origin", None),
|
||||
("bold", True),
|
||||
("italic", True),
|
||||
("resolution", None),
|
||||
]
|
||||
skip_properties = {"strikethrough"}
|
||||
else:
|
||||
properties = [
|
||||
("name", None),
|
||||
("style_name", None),
|
||||
("bold", True),
|
||||
("italic", True),
|
||||
("underline", True),
|
||||
("strikethrough", True),
|
||||
("outline", 1),
|
||||
("point_size", 1),
|
||||
]
|
||||
skip_properties = set()
|
||||
version = pygame.font.get_sdl_ttf_version()
|
||||
if version >= (2, 20, 0):
|
||||
properties.append(("align", 1))
|
||||
else:
|
||||
skip_properties.add("align")
|
||||
|
||||
font = pygame_font.Font(None, 10)
|
||||
actual_names = []
|
||||
|
||||
for n in sorted(dir(font)):
|
||||
if n in skip_properties:
|
||||
continue
|
||||
if n.startswith("_"):
|
||||
continue
|
||||
if callable(getattr(font, n)):
|
||||
continue
|
||||
actual_names.append(n)
|
||||
|
||||
expected_names = [n for n, a in sorted(properties)]
|
||||
self.assertEqual(
|
||||
len(expected_names),
|
||||
len(actual_names),
|
||||
msg=f"ensure all properties are checked \n{expected_names} \n!= \n{actual_names}",
|
||||
)
|
||||
|
||||
for name, arg in properties:
|
||||
# import sys
|
||||
# print(">>>", name,file=sys.__stdout__,flush=True)
|
||||
with self.subTest(name=name + " get"):
|
||||
# arrange
|
||||
font = pygame_font.Font(None, 16)
|
||||
pygame_font.quit()
|
||||
pygame_font.init()
|
||||
|
||||
# act / assert
|
||||
self.assertRaises(pygame.error, getattr, font, name) # read
|
||||
|
||||
if arg is not None:
|
||||
with self.subTest(name=name + " write"):
|
||||
# act / assert
|
||||
self.assertRaises(pygame.error, setattr, font, name, arg) # write
|
||||
|
||||
|
||||
@unittest.skipIf(IS_PYPY, "pypy skip known failure") # TODO
|
||||
class VisualTestsInteractive(unittest.TestCase):
|
||||
class VisualTests(unittest.TestCase):
|
||||
__tags__ = ["interactive"]
|
||||
|
||||
screen = None
|
||||
@@ -1094,7 +662,6 @@ class VisualTestsInteractive(unittest.TestCase):
|
||||
underline=False,
|
||||
strikethrough=False,
|
||||
antialiase=False,
|
||||
outline=0,
|
||||
):
|
||||
if self.aborted:
|
||||
return False
|
||||
@@ -1105,7 +672,7 @@ class VisualTestsInteractive(unittest.TestCase):
|
||||
screen = self.screen
|
||||
screen.fill((255, 255, 255))
|
||||
pygame.display.flip()
|
||||
if not (bold or italic or underline or strikethrough or antialiase or outline):
|
||||
if not (bold or italic or underline or strikethrough or antialiase):
|
||||
text = "normal"
|
||||
else:
|
||||
modes = []
|
||||
@@ -1119,22 +686,20 @@ class VisualTestsInteractive(unittest.TestCase):
|
||||
modes.append("strikethrough")
|
||||
if antialiase:
|
||||
modes.append("antialiased")
|
||||
if outline:
|
||||
modes.append("outlined")
|
||||
text = f"{'-'.join(modes)} (y/n):"
|
||||
f.set_bold(bold)
|
||||
f.set_italic(italic)
|
||||
f.set_underline(underline)
|
||||
f.set_strikethrough(strikethrough)
|
||||
f.outline = outline
|
||||
if pygame_font.__name__ != "pygame.ftfont":
|
||||
f.set_strikethrough(strikethrough)
|
||||
s = f.render(text, antialiase, (0, 0, 0))
|
||||
screen.blit(s, (offset, y))
|
||||
y += s.get_size()[1] + spacing
|
||||
f.set_bold(False)
|
||||
f.set_italic(False)
|
||||
f.set_underline(False)
|
||||
f.set_strikethrough(False)
|
||||
f.outline = 0
|
||||
if pygame_font.__name__ != "pygame.ftfont":
|
||||
f.set_strikethrough(False)
|
||||
s = f.render("(some comparison text)", False, (0, 0, 0))
|
||||
screen.blit(s, (offset, y))
|
||||
pygame.display.flip()
|
||||
@@ -1162,7 +727,8 @@ class VisualTestsInteractive(unittest.TestCase):
|
||||
self.assertTrue(self.query(underline=True))
|
||||
|
||||
def test_strikethrough(self):
|
||||
self.assertTrue(self.query(strikethrough=True))
|
||||
if pygame_font.__name__ != "pygame.ftfont":
|
||||
self.assertTrue(self.query(strikethrough=True))
|
||||
|
||||
def test_antialiase(self):
|
||||
self.assertTrue(self.query(antialiase=True))
|
||||
@@ -1174,10 +740,8 @@ class VisualTestsInteractive(unittest.TestCase):
|
||||
self.assertTrue(self.query(italic=True, underline=True))
|
||||
|
||||
def test_bold_strikethrough(self):
|
||||
self.assertTrue(self.query(bold=True, strikethrough=True))
|
||||
|
||||
def test_outline(self):
|
||||
self.assertTrue(self.query(outline=1))
|
||||
if pygame_font.__name__ != "pygame.ftfont":
|
||||
self.assertTrue(self.query(bold=True, strikethrough=True))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in new issue
Block a user