Skip to content

Commit

Permalink
Migrate tests to modern pytest style (#215)
Browse files Browse the repository at this point in the history
* Misc. cleanup

* Add basic conftest module

* Migrate simpler modules to new pytest style

* Clean up init, error, and power module tests

* Clean up pixels module tests

* Fix RGB detection bug in ext.Color

* Major cleanup of ext.Color tests

* Migrate ext.surface to pytest

* Fix spritesystem test failures on real macOS

* Fix conftest mistake

* Clean up spritesystem tests

* Move cleanup fixture to conftest

* Initial round of video module pytest cleanup

* Fix video mode test with dummy driver

* Fix ext.color on 2.7 and SDL init test for CI

* Whoops

* Try fixing color module and init tests again

* Fix pixel test bug with SDL 2.0.5

* Initial cleanup/improvements for SDL_image tests

* Fix video tests on smaller screens

* Clear some non-critical ext errors

* Ignore non-critical errors for image tests

* Additional video test cleanup

* Try fixing initial video driver detection

* xfail problem test on PyPy

* More video test overhaul

* Migrate joystick tests to pytest

* Fix new video tests with dummy driver

* Improve handling of broken joystick subsystem

* Fix tests with real X360 gamepad on Windows

* Fix an error with a None-or-string test

* Additional test fixes

* More fixes and cleanup

* Initial cleanup of ext.Renderer tests

* Mark unreliable framerate test as xfail

* Clean and expland gamecontroller tests

* Finish video test rewrites

* Mark hidapi init as xfail for now

* Migrate more modules to pytest

* Migrate timer and syswm tests to pytest

* Migrate keyboard tests to pytest

* Finish migrating SDL_image to pytest

* xfail some timer tests for CI

* Clean up and migrate render tests

* Migrate sdl2.ext common tests to pytest

* Avoid test failure on SDL 2.0.8

* Migrate more ext modules to pytest

* Migrate the ext.draw and ext.ebs tests to pytest

* Migrate more ext modules to pytest

* Make annoying audiovisual tests optional

* Migrate ext.window tests to pytest

* Migrate ext.events and ext.renderer to pytest

* Implement clipboard tests

* Fix some tests on real hardware

* Minor cleanup of the events tests

* Fix more tests for local use

* Migrate TTF tests to pytest

* Migrate audio tests to pytest

* Migrate empty mouse tests to pytest

* Try fixing local tests

* Relax audio format tests
  • Loading branch information
a-hurst authored Jan 26, 2022
1 parent d4210dc commit 7ebf0a3
Show file tree
Hide file tree
Showing 55 changed files with 6,424 additions and 7,153 deletions.
92 changes: 55 additions & 37 deletions sdl2/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
"AUDIO_FORMATS"
]


# Constants, enums, and macros

SDL_AudioFormat = Uint16

SDL_AUDIO_MASK_BITSIZE = 0xFF
Expand Down Expand Up @@ -82,7 +85,6 @@
AUDIO_F32MSB = 0x9120
AUDIO_F32 = AUDIO_F32LSB


# All of the audio formats should be in this set which is provided as a
# convenience to the end user for purposes of iteration and validation.
# (is the provided audio format in the supported set?)
Expand All @@ -98,10 +100,13 @@
AUDIO_F32LSB: "AUDIO_F32LSB",
AUDIO_F32MSB: "AUDIO_F32MSB",
}
AUDIO_FORMATS = set([AUDIO_U8, AUDIO_S8, AUDIO_U16LSB, AUDIO_S16LSB,
AUDIO_U16MSB, AUDIO_S16MSB, AUDIO_U16, AUDIO_S16,
AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_S32, AUDIO_F32LSB,
AUDIO_F32MSB, AUDIO_F32])
AUDIO_FORMATS = set([
AUDIO_U8, AUDIO_S8,
AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_U16,
AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_S16,
AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_S32,
AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_F32
])

if SDL_BYTEORDER == SDL_LIL_ENDIAN:
AUDIO_U16SYS = AUDIO_U16LSB
Expand All @@ -123,21 +128,37 @@
SDL_AUDIO_ALLOW_CHANNELS_CHANGE | SDL_AUDIO_ALLOW_SAMPLES_CHANGE
)

SDL_AudioDeviceID = Uint32

SDL_AudioStatus = c_int
SDL_AUDIO_STOPPED = 0
SDL_AUDIO_PLAYING = 1
SDL_AUDIO_PAUSED = 2

SDL_MIX_MAXVOLUME = 128
SDL_AUDIOCVT_MAX_FILTERS = 9


# Structure, opaque type, and callback definitions

SDL_AudioCallback = CFUNCTYPE(None, c_void_p, POINTER(Uint8), c_int)

class SDL_AudioSpec(Structure):
_fields_ = [("freq", c_int),
("format", SDL_AudioFormat),
("channels", Uint8),
("silence", Uint8),
("samples", Uint16),
("padding", Uint16),
("size", Uint32),
("callback", SDL_AudioCallback),
("userdata", c_void_p)
]
def __init__(self, freq, aformat, channels, samples,
callback=SDL_AudioCallback(), userdata=c_void_p(0)):
_fields_ = [
("freq", c_int),
("format", SDL_AudioFormat),
("channels", Uint8),
("silence", Uint8),
("samples", Uint16),
("padding", Uint16),
("size", Uint32),
("callback", SDL_AudioCallback),
("userdata", c_void_p),
]
def __init__(
self, freq, aformat, channels, samples, callback=SDL_AudioCallback(),
userdata=c_void_p(0)
):
super(SDL_AudioSpec, self).__init__()
self.freq = freq
self.format = aformat
Expand All @@ -146,43 +167,41 @@ def __init__(self, freq, aformat, channels, samples,
self.callback = callback
self.userdata = userdata

SDL_AUDIOCVT_MAX_FILTERS = 9

class SDL_AudioCVT(Structure):
pass

SDL_AudioFilter = CFUNCTYPE(POINTER(SDL_AudioCVT), SDL_AudioFormat)
SDL_AudioCVT._fields_ = [("needed", c_int),
("src_format", SDL_AudioFormat),
("dst_format", SDL_AudioFormat),
("rate_incr", c_double),
("buf", POINTER(Uint8)),
("len", c_int),
("len_cvt", c_int),
("len_mult", c_int),
("len_ratio", c_double),
("filters", (SDL_AudioFilter * (SDL_AUDIOCVT_MAX_FILTERS+1))),
("filter_index", c_int)
]

SDL_AudioCVT._fields_ = [
("needed", c_int),
("src_format", SDL_AudioFormat),
("dst_format", SDL_AudioFormat),
("rate_incr", c_double),
("buf", POINTER(Uint8)),
("len", c_int),
("len_cvt", c_int),
("len_mult", c_int),
("len_ratio", c_double),
("filters", (SDL_AudioFilter * (SDL_AUDIOCVT_MAX_FILTERS+1))),
("filter_index", c_int),
]

class SDL_AudioStream(c_void_p):
pass


# Raw ctypes function definitions

SDL_GetNumAudioDrivers = _bind("SDL_GetNumAudioDrivers", None, c_int)
SDL_GetAudioDriver = _bind("SDL_GetAudioDriver", [c_int], c_char_p)
SDL_AudioInit = _bind("SDL_AudioInit", [c_char_p], c_int)
SDL_AudioQuit = _bind("SDL_AudioQuit")
SDL_GetCurrentAudioDriver = _bind("SDL_GetCurrentAudioDriver", None, c_char_p)
SDL_OpenAudio = _bind("SDL_OpenAudio", [POINTER(SDL_AudioSpec), POINTER(SDL_AudioSpec)], c_int)
SDL_AudioDeviceID = Uint32
SDL_GetNumAudioDevices = _bind("SDL_GetNumAudioDevices", [c_int], c_int)
SDL_GetAudioDeviceName = _bind("SDL_GetAudioDeviceName", [c_int, c_int], c_char_p)
SDL_GetAudioDeviceSpec = _bind("SDL_GetAudioDeviceSpec", [c_int, c_int, POINTER(SDL_AudioSpec)], c_int, added='2.0.16')
SDL_OpenAudioDevice = _bind("SDL_OpenAudioDevice", [c_char_p, c_int, POINTER(SDL_AudioSpec), POINTER(SDL_AudioSpec), c_int], SDL_AudioDeviceID)
SDL_AUDIO_STOPPED = 0
SDL_AUDIO_PLAYING = 1
SDL_AUDIO_PAUSED = 2
SDL_AudioStatus = c_int
SDL_GetAudioStatus = _bind("SDL_GetAudioStatus", None, SDL_AudioStatus)
SDL_GetAudioDeviceStatus = _bind("SDL_GetAudioDeviceStatus", [SDL_AudioDeviceID], SDL_AudioStatus)
SDL_PauseAudio = _bind("SDL_PauseAudio", [c_int])
Expand All @@ -192,7 +211,6 @@ class SDL_AudioStream(c_void_p):
SDL_FreeWAV = _bind("SDL_FreeWAV", [POINTER(Uint8)])
SDL_BuildAudioCVT = _bind("SDL_BuildAudioCVT", [POINTER(SDL_AudioCVT), SDL_AudioFormat, Uint8, c_int, SDL_AudioFormat, Uint8, c_int], c_int)
SDL_ConvertAudio = _bind("SDL_ConvertAudio", [POINTER(SDL_AudioCVT)], c_int)
SDL_MIX_MAXVOLUME = 128
SDL_MixAudio = _bind("SDL_MixAudio", [POINTER(Uint8), POINTER(Uint8), Uint32, c_int])
SDL_MixAudioFormat = _bind("SDL_MixAudioFormat", [POINTER(Uint8), POINTER(Uint8), SDL_AudioFormat, Uint32, c_int])
SDL_LockAudio = _bind("SDL_LockAudio")
Expand Down
6 changes: 0 additions & 6 deletions sdl2/dll.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,6 @@ def _findlib(libnames, path=None):
else:
patterns = ["lib{0}.so"]

# On Apple Silicon Macs, search the non-standard Homebrew library path if no other
# path explicitly set
arm_brewpath = "/opt/Homebrew/lib"
if not path and platform == "darwin" and os.path.exists(arm_brewpath):
path = arm_brewpath

# Adding the potential 'd' suffix that is present on the library
# when built in debug configuration
searchfor = libnames + [libname + 'd' for libname in libnames]
Expand Down
15 changes: 5 additions & 10 deletions sdl2/ext/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,17 +454,12 @@ def is_rgb_color(v):
False.
"""
try:
if hasattr(v, "r") and hasattr(v, "g") and hasattr(v, "b"):
if 0 <= int(v.r) <= 255 and 0 <= int(v.g) <= 255 and \
0 <= v.b <= 255:
return True

if len(v) >= 3:
if 0 <= int(v[0]) <= 255 and 0 <= int(v[1]) <= 255 and \
0 < int(v[2]) < 255:
return True
if hasattr(v, "r") and hasattr(v, "g") and hasattr(v, "b"):
v = [v.r, v.g, v.b]
if not isiterable(v) or len(v) < 3:
return False
try:
return all([0 <= int(x) <= 255 for x in v[:3]])
except (TypeError, ValueError):
return False

Expand Down
1 change: 1 addition & 0 deletions sdl2/ext/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ def load_img(path, as_argb=True):
img_surf = surfcopy
if not img_surf:
raise_sdl_err("converting '{0}' to ARGB format".format(fname))
error.SDL_ClearError() # Clear any non-critical errors during loading

return img_surf.contents

Expand Down
1 change: 1 addition & 0 deletions sdl2/ext/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ def __init__(self, target, backend=-1, logical_size=None,
raise TypeError("unsupported target type")
if not _renderer:
raise_sdl_err("creating the SDL renderer")
error.SDL_ClearError() # Clear any errors from renderer selection
self._renderer_ref = [_renderer]

self.rendertarget = target
Expand Down
8 changes: 4 additions & 4 deletions sdl2/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ class SDL_Texture(c_void_p):
SDL_SetTextureUserData = _bind("SDL_SetTextureUserData", [POINTER(SDL_Texture), c_void_p], c_int, added='2.0.18')
SDL_GetTextureUserData = _bind("SDL_GetTextureUserData", [POINTER(SDL_Texture)], c_void_p, added='2.0.18')
SDL_UpdateTexture = _bind("SDL_UpdateTexture", [POINTER(SDL_Texture), POINTER(SDL_Rect), c_void_p, c_int], c_int)
SDL_UpdateYUVTexture = _bind("SDL_UpdateYUVTexture", [POINTER(SDL_Texture), POINTER(SDL_Rect), POINTER(Uint8), c_int, POINTER(Uint8), c_int, POINTER(Uint8), c_int], c_int)
SDL_UpdateNVTexture = _bind("SDL_UpdateNVTexture", [POINTER(SDL_Texture), POINTER(SDL_Rect), POINTER(Uint8), c_int, POINTER(Uint8), c_int], c_int, added='2.0.16')
SDL_LockTexture = _bind("SDL_LockTexture", [POINTER(SDL_Texture), POINTER(SDL_Rect), POINTER(c_void_p), POINTER(c_int)], c_int)
SDL_LockTextureToSurface = _bind("SDL_LockTextureToSurface", [POINTER(SDL_Texture), POINTER(SDL_Rect), POINTER(POINTER(SDL_Surface))], c_int, added='2.0.12')
SDL_UnlockTexture = _bind("SDL_UnlockTexture", [POINTER(SDL_Texture)])
Expand All @@ -195,6 +197,8 @@ class SDL_Texture(c_void_p):
SDL_GetRenderTarget = _bind("SDL_GetRenderTarget", [POINTER(SDL_Renderer)], POINTER(SDL_Texture))
SDL_RenderSetLogicalSize = _bind("SDL_RenderSetLogicalSize", [POINTER(SDL_Renderer), c_int, c_int], c_int)
SDL_RenderGetLogicalSize = _bind("SDL_RenderGetLogicalSize", [POINTER(SDL_Renderer), POINTER(c_int), POINTER(c_int)])
SDL_RenderSetIntegerScale = _bind("SDL_RenderSetIntegerScale", [POINTER(SDL_Renderer), SDL_bool], c_int, added='2.0.5')
SDL_RenderGetIntegerScale = _bind("SDL_RenderGetIntegerScale", [POINTER(SDL_Renderer)], SDL_bool, added='2.0.5')
SDL_RenderSetViewport = _bind("SDL_RenderSetViewport", [POINTER(SDL_Renderer), POINTER(SDL_Rect)], c_int)
SDL_RenderGetViewport = _bind("SDL_RenderGetViewport", [POINTER(SDL_Renderer), POINTER(SDL_Rect)])
SDL_RenderGetClipRect = _bind("SDL_RenderGetClipRect", [POINTER(SDL_Renderer), POINTER(SDL_Rect)])
Expand All @@ -204,8 +208,6 @@ class SDL_Texture(c_void_p):
SDL_RenderGetScale = _bind("SDL_RenderGetScale", [POINTER(SDL_Renderer), POINTER(c_float), POINTER(c_float)])
SDL_RenderWindowToLogical = _bind("SDL_RenderWindowToLogical", [POINTER(SDL_Renderer), c_int, c_int, POINTER(c_float), POINTER(c_float)], added='2.0.18')
SDL_RenderLogicalToWindow = _bind("SDL_RenderLogicalToWindow", [POINTER(SDL_Renderer), c_float, c_float, POINTER(c_int), POINTER(c_int)], added='2.0.18')
SDL_RenderGetIntegerScale = _bind("SDL_RenderGetIntegerScale", [POINTER(SDL_Renderer)], SDL_bool, added='2.0.5')
SDL_RenderSetIntegerScale = _bind("SDL_RenderSetIntegerScale", [POINTER(SDL_Renderer), SDL_bool], c_int, added='2.0.5')
SDL_SetRenderDrawColor = _bind("SDL_SetRenderDrawColor", [POINTER(SDL_Renderer), Uint8, Uint8, Uint8, Uint8], c_int)
SDL_GetRenderDrawColor = _bind("SDL_GetRenderDrawColor", [POINTER(SDL_Renderer), POINTER(Uint8), POINTER(Uint8), POINTER(Uint8), POINTER(Uint8)], c_int)
SDL_SetRenderDrawBlendMode = _bind("SDL_SetRenderDrawBlendMode", [POINTER(SDL_Renderer), SDL_BlendMode], c_int)
Expand Down Expand Up @@ -240,8 +242,6 @@ class SDL_Texture(c_void_p):
SDL_RenderFlush = _bind("SDL_RenderFlush", [POINTER(SDL_Renderer)], c_int, added='2.0.10')
SDL_GL_BindTexture = _bind("SDL_GL_BindTexture", [POINTER(SDL_Texture), POINTER(c_float), POINTER(c_float)], c_int)
SDL_GL_UnbindTexture = _bind("SDL_GL_UnbindTexture", [POINTER(SDL_Texture)], c_int)
SDL_UpdateYUVTexture = _bind("SDL_UpdateYUVTexture", [POINTER(SDL_Texture), POINTER(SDL_Rect), POINTER(Uint8), c_int, POINTER(Uint8), c_int, POINTER(Uint8), c_int], c_int)
SDL_UpdateNVTexture = _bind("SDL_UpdateNVTexture", [POINTER(SDL_Texture), POINTER(SDL_Rect), POINTER(Uint8), c_int, POINTER(Uint8), c_int], c_int, added='2.0.16')
SDL_RenderGetMetalLayer = _bind("SDL_RenderGetMetalLayer", [POINTER(SDL_Renderer)], c_void_p, added='2.0.8')
SDL_RenderGetMetalCommandEncoder = _bind("SDL_RenderGetMetalCommandEncoder", [POINTER(SDL_Renderer)], c_void_p, added='2.0.8')
SDL_RenderSetVSync = _bind("SDL_RenderSetVSync", [POINTER(SDL_Renderer), c_int], c_int, added='2.0.18')
Loading

0 comments on commit 7ebf0a3

Please sign in to comment.