Skip to content

Commit

Permalink
Remove more misuse of SDL_GetError in tests (#271)
Browse files Browse the repository at this point in the history
* Remove improper SDL_GetError usage

* Improve SetTextInputRect test

* Make error clearing explicit for unsafe tests
  • Loading branch information
a-hurst authored Aug 21, 2023
1 parent ed28ea6 commit 949fe89
Show file tree
Hide file tree
Showing 15 changed files with 142 additions and 152 deletions.
5 changes: 2 additions & 3 deletions sdl2/test/clipboard_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
def window(with_sdl):
flag = sdl2.SDL_WINDOW_BORDERLESS
w = sdl2.SDL_CreateWindow(b"Test", 10, 40, 12, 13, flag)
if not isinstance(w.contents, sdl2.SDL_Window):
assert sdl2.SDL_GetError() == b""
assert isinstance(w.contents, sdl2.SDL_Window)
assert w, _check_error_msg()
assert isinstance(w.contents, sdl2.SDL_Window)
sdl2.SDL_ClearError()
yield w
sdl2.SDL_DestroyWindow(w)
Expand Down
45 changes: 22 additions & 23 deletions sdl2/test/joystick_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def joysticks(with_sdl):
count = sdl2.SDL_NumJoysticks()
for i in range(count):
stick = sdl2.SDL_JoystickOpen(i)
assert sdl2.SDL_GetError() == b""
assert stick, _check_error_msg()
assert isinstance(stick.contents, sdl2.SDL_Joystick)
devices.append(stick)
yield devices
Expand Down Expand Up @@ -121,47 +121,48 @@ def test_SDL_JoystickGetDeviceGUID(with_sdl):
def test_SDL_JoystickGetDeviceVendor(with_sdl):
count = sdl2.SDL_NumJoysticks()
for index in range(count):
sdl2.SDL_ClearError()
vid = sdl2.SDL_JoystickGetDeviceVendor(index)
assert SDL_GetError() == b""
if not is_virtual(index):
assert vid > 0
assert vid > 0, _check_error_msg()

def test_SDL_JoystickGetDeviceProduct(with_sdl):
count = sdl2.SDL_NumJoysticks()
for index in range(count):
sdl2.SDL_ClearError()
pid = sdl2.SDL_JoystickGetDeviceProduct(index)
assert SDL_GetError() == b""
if not is_virtual(index):
assert pid > 0
assert pid > 0, _check_error_msg()

def test_SDL_JoystickGetDeviceProductVersion(with_sdl):
count = sdl2.SDL_NumJoysticks()
for index in range(count):
sdl2.SDL_ClearError()
pver = sdl2.SDL_JoystickGetDeviceProductVersion(index)
assert SDL_GetError() == b""
assert pver >= 0
assert pver >= 0, _check_error_msg()

def test_SDL_JoystickGetDeviceType(with_sdl):
count = sdl2.SDL_NumJoysticks()
for index in range(count):
jtype = sdl2.SDL_JoystickGetDeviceType(index)
assert SDL_GetError() == b""
assert jtype in joystick_types
if is_virtual(index):
assert jtype == sdl2.SDL_JOYSTICK_TYPE_GAMECONTROLLER

@pytest.mark.skipif(sdl2.dll.version < 2006, reason="not available")
def test_SDL_JoystickGetDeviceInstanceID(joysticks):
for index in range(len(joysticks)):
sdl2.SDL_ClearError()
iid = sdl2.SDL_JoystickGetDeviceInstanceID(index)
assert SDL_GetError() == b""
stick = joysticks[index]
assert iid == sdl2.SDL_JoystickInstanceID(stick)
assert iid == sdl2.SDL_JoystickInstanceID(stick), _check_error_msg()

def test_SDL_JoystickOpenClose(with_sdl):
count = sdl2.SDL_NumJoysticks()
for index in range(count):
sdl2.SDL_ClearError()
stick = sdl2.SDL_JoystickOpen(index)
assert stick, _check_error_msg()
assert isinstance(stick.contents, sdl2.SDL_Joystick)
sdl2.SDL_JoystickClose(stick)

Expand Down Expand Up @@ -316,42 +317,40 @@ def test_SDL_JoystickGetGUID(joysticks):

def test_SDL_JoystickGetVendor(joysticks):
for stick in joysticks:
sdl2.SDL_ClearError()
vid = sdl2.SDL_JoystickGetVendor(stick)
assert SDL_GetError() == b""
if not is_virtual(stick):
assert vid > 0
assert vid > 0, _check_error_msg()

def test_SDL_JoystickGetProduct(joysticks):
for stick in joysticks:
sdl2.SDL_ClearError()
pid = sdl2.SDL_JoystickGetProduct(stick)
assert SDL_GetError() == b""
if not is_virtual(stick):
assert pid > 0
assert pid > 0, _check_error_msg()

def test_SDL_JoystickGetProductVersion(joysticks):
for stick in joysticks:
sdl2.SDL_ClearError()
pver = sdl2.SDL_JoystickGetProductVersion(stick)
assert SDL_GetError() == b""
assert pver >= 0
assert pver >= 0, _check_error_msg()

@pytest.mark.skipif(sdl2.dll.version < 2231, reason="not available")
def test_SDL_JoystickGetFirmwareVersion(joysticks):
for stick in joysticks:
sdl2.SDL_ClearError()
fw_ver = sdl2.SDL_JoystickGetFirmwareVersion(stick)
assert SDL_GetError() == b""
assert fw_ver >= 0
assert fw_ver >= 0, _check_error_msg()

@pytest.mark.skipif(sdl2.dll.version < 2014, reason="not available")
def test_SDL_JoystickGetSerial(joysticks):
for stick in joysticks:
serial = sdl2.SDL_JoystickGetSerial(stick)
assert SDL_GetError() == b""
assert serial == None or type(serial) in (str, bytes)

def test_SDL_JoystickGetType(joysticks):
for stick in joysticks:
jtype = sdl2.SDL_JoystickGetType(stick)
assert SDL_GetError() == b""
assert jtype in joystick_types
if is_virtual(stick):
assert jtype == sdl2.SDL_JOYSTICK_TYPE_GAMECONTROLLER
Expand Down Expand Up @@ -426,7 +425,9 @@ def test_SDL_JoystickNumButtons(joysticks):
assert buttons == 4

def test_SDL_JoystickUpdate(with_sdl):
# NOTE: Returns void, can't really test anything else
# TODO: This is not 100% safe, but in SDL2, JoystickUpdate returns void
# so we can't reliably detect error
sdl2.SDL_ClearError()
sdl2.SDL_JoystickUpdate()
assert SDL_GetError() == b""

Expand All @@ -450,7 +451,6 @@ def test_SDL_JoystickGetAxisInitialState(joysticks):
ret = sdl2.SDL_JoystickGetAxisInitialState(
stick, axis, byref(init_state)
)
assert SDL_GetError() == b""
assert -32768 <= init_state.value <= 32767
assert ret in [SDL_TRUE, SDL_FALSE]

Expand Down Expand Up @@ -544,5 +544,4 @@ def test_SDL_JoystickCurrentPowerLevel(joysticks):
]
for stick in joysticks:
pwr = sdl2.SDL_JoystickCurrentPowerLevel(stick)
assert SDL_GetError() == b""
assert pwr in levels
16 changes: 9 additions & 7 deletions sdl2/test/keyboard_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
import sdl2
from sdl2 import SDL_TRUE, SDL_FALSE, SDL_GetError
from sdl2 import rect, scancode, keycode, video
from .conftest import _check_error_msg

byteify = lambda x: x.encode("utf-8")

@pytest.fixture
def window(with_sdl):
flag = video.SDL_WINDOW_INPUT_FOCUS
w = video.SDL_CreateWindow(b"Test", 10, 40, 32, 24, flag)
if not isinstance(w.contents, sdl2.SDL_Window):
assert sdl2.SDL_GetError() == b""
assert isinstance(w.contents, sdl2.SDL_Window)
assert w, _check_error_msg()
assert isinstance(w.contents, sdl2.SDL_Window)
sdl2.SDL_ClearError()
yield w
video.SDL_DestroyWindow(w)
Expand Down Expand Up @@ -150,11 +150,9 @@ def test_SDL_GetKeyFromName(with_sdl):

def test_SDL_StartStopTextInput(with_sdl):
sdl2.SDL_StopTextInput()
assert SDL_GetError() == b""
assert sdl2.SDL_IsTextInputActive() == SDL_FALSE
assert sdl2.SDL_IsTextInputActive() == SDL_FALSE, _check_error_msg()
sdl2.SDL_StartTextInput()
assert SDL_GetError() == b""
assert sdl2.SDL_IsTextInputActive() == SDL_TRUE
assert sdl2.SDL_IsTextInputActive() == SDL_TRUE, _check_error_msg()

@pytest.mark.skipif(sdl2.dll.version < 2022, reason="not available")
def test_SDL_ClearComposition(with_sdl):
Expand All @@ -166,12 +164,16 @@ def test_SDL_IsTextInputShown(with_sdl):
assert ret in [SDL_TRUE, SDL_FALSE]

def test_SDL_SetTextInputRect(with_sdl):
# TODO: This is not 100% safe, but in SDL2, SetTextInputRect returns
# void, so we can't reliably detect error
sdl2.SDL_StartTextInput()
coords = [(0, 0, 0, 0), (-10, -70, 3, 6), (10, 10, 10, 10)]
for x, y, w, h in coords:
r = rect.SDL_Rect(x, y, w, h)
sdl2.SDL_ClearError()
sdl2.SDL_SetTextInputRect(r)
assert SDL_GetError() == b""
sdl2.SDL_StopTextInput()

def test_SDL_HasScreenKeyboardSupport(with_sdl):
ret = sdl2.SDL_HasScreenKeyboardSupport()
Expand Down
8 changes: 4 additions & 4 deletions sdl2/test/pixels_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from sdl2 import SDL_Init, SDL_Quit, SDL_INIT_EVERYTHING, SDL_TRUE
from sdl2 import SDL_Color
from sdl2.stdinc import Uint8, Uint16, Uint32
from .conftest import _check_error_msg

RGBA8888 = [0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF]
RGBX8888 = [0xFF000000, 0x00FF0000, 0x0000FF00, 0]
Expand Down Expand Up @@ -163,11 +164,11 @@ def test_SDL_PixelFormatEnumToMasks():
bpp = c_int(0)
r, g, b, a = Uint32(0), Uint32(0), Uint32(0), Uint32(0)
for fmt, expected in formats:
sdl2.SDL_ClearError()
ret = sdl2.SDL_PixelFormatEnumToMasks(
fmt, byref(bpp), byref(r), byref(g), byref(b), byref(a)
)
assert sdl2.SDL_GetError() == b""
assert ret == SDL_TRUE
assert ret == SDL_TRUE, _check_error_msg()
assert [x.value for x in (bpp, r, g, b, a)] == expected

def test_SDL_AllocFreeFormat():
Expand Down Expand Up @@ -232,8 +233,7 @@ def test_SDL_SetPixelFormatPalette():
pixfmt = sdl2.SDL_AllocFormat(sdl2.SDL_PIXELFORMAT_INDEX1MSB)
assert isinstance(pixfmt.contents, sdl2.SDL_PixelFormat)
ret = sdl2.SDL_SetPixelFormatPalette(pixfmt, palette)
assert sdl2.SDL_GetError() == b""
assert ret == 0
assert ret == 0, _check_error_msg()

fmt_palette = pixfmt.contents.palette.contents
assert fmt_palette.colors[1].r == 128
Expand Down
44 changes: 22 additions & 22 deletions sdl2/test/render_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ def _create_window(pos, size, flags=video.SDL_WINDOW_HIDDEN):
window = video.SDL_CreateWindow(
b"Test", pos[0], pos[1], size[0], size[1], video.SDL_WINDOW_HIDDEN
)
if not isinstance(window.contents, video.SDL_Window):
assert SDL_GetError() == b""
assert isinstance(window.contents, video.SDL_Window)
assert window, _check_error_msg()
assert isinstance(window.contents, video.SDL_Window)
sdl2.SDL_ClearError()
return window

Expand Down Expand Up @@ -59,18 +58,18 @@ def testsurf(with_sdl):
sf = surface.SDL_CreateRGBSurface(
0, 100, 100, 32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF
)
assert SDL_GetError() == b""
assert sf, _check_error_msg()
pixfmt = sf.contents.format.contents
fill = pixels.SDL_MapRGBA(pixfmt, 0, 0, 0, 255)
surface.SDL_FillRect(sf, None, fill)
assert SDL_GetError() == b""
ret = surface.SDL_FillRect(sf, None, fill)
assert ret == 0, _check_error_msg()
yield sf
surface.SDL_FreeSurface(sf)

@pytest.fixture
def sw_renderer(testsurf):
renderer = sdl2.SDL_CreateSoftwareRenderer(testsurf)
assert SDL_GetError() == b""
assert renderer, _check_error_msg()
assert isinstance(renderer.contents, sdl2.SDL_Renderer)
yield (renderer, testsurf)
sdl2.SDL_DestroyRenderer(renderer)
Expand All @@ -82,12 +81,11 @@ def with_renderer(with_sdl):
window = video.SDL_CreateWindow(
b"Test", 30, 30, 100, 100, video.SDL_WINDOW_HIDDEN
)
if not isinstance(window.contents, sdl2.SDL_Window):
assert sdl2.SDL_GetError() == b""
assert isinstance(window.contents, sdl2.SDL_Window)
assert window, _check_error_msg()
assert isinstance(window.contents, sdl2.SDL_Window)
sdl2.SDL_ClearError()
renderer = sdl2.SDL_CreateRenderer(window, -1, flags)
assert SDL_GetError() == b""
assert renderer, _check_error_msg()
yield (renderer, window)
sdl2.SDL_DestroyRenderer(renderer)
video.SDL_DestroyWindow(window)
Expand All @@ -98,7 +96,7 @@ def texture(with_renderer):
fmt = pixels.SDL_PIXELFORMAT_ARGB8888
access = sdl2.SDL_TEXTUREACCESS_STREAMING
tx = sdl2.SDL_CreateTexture(renderer, fmt, access, 16, 16)
assert SDL_GetError() == b""
assert tx, _check_error_msg()
assert isinstance(tx.contents, sdl2.SDL_Texture)
yield tx
sdl2.SDL_DestroyTexture(tx)
Expand Down Expand Up @@ -237,7 +235,7 @@ def test_SDL_CreateSoftwareRenderer(with_sdl):
0, 100, 100, 32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF
)
renderer = sdl2.SDL_CreateSoftwareRenderer(sf)
assert SDL_GetError() == b""
assert renderer, _check_error_msg()
assert isinstance(renderer.contents, sdl2.SDL_Renderer)
sdl2.SDL_DestroyRenderer(renderer)
surface.SDL_FreeSurface(sf)
Expand All @@ -250,8 +248,9 @@ def test_SDL_GetRenderer(supported_renderers):
renderer = sdl2.SDL_CreateRenderer(window, i, flags)
if (renderer and renderer.contents):
usable += 1
sdl2.SDL_ClearError()
ren = sdl2.SDL_GetRenderer(window)
assert SDL_GetError() == b""
assert ren, _check_error_msg()
assert isinstance(ren.contents, sdl2.SDL_Renderer)
sdl2.SDL_DestroyRenderer(renderer)
assert not sdl2.SDL_GetRenderer(window)
Expand All @@ -265,8 +264,9 @@ def test_SDL_RenderGetWindow(supported_renderers):
window = _create_window((30, 30), (100, 100))
renderer = sdl2.SDL_CreateRenderer(window, i, flags)
if (renderer and renderer.contents):
sdl2.SDL_ClearError()
win = sdl2.SDL_RenderGetWindow(renderer)
assert SDL_GetError() == b""
assert win, _check_error_msg()
assert isinstance(win.contents, sdl2.SDL_Window)
assert sdl2.SDL_GetWindowID(window) == sdl2.SDL_GetWindowID(win)
sdl2.SDL_DestroyRenderer(renderer)
Expand Down Expand Up @@ -335,8 +335,9 @@ def test_SDL_CreateDestroyTexture(with_renderer):
for fmt in formats:
for acc in access:
for w, h in sizes:
sdl2.SDL_ClearError()
tx = sdl2.SDL_CreateTexture(renderer, fmt, acc, w, h)
assert SDL_GetError() == b""
assert tx, _check_error_msg()
assert isinstance(tx.contents, sdl2.SDL_Texture)
sdl2.SDL_DestroyTexture(tx)
# Test SDL error on bad input
Expand All @@ -348,8 +349,7 @@ def test_SDL_CreateDestroyTexture(with_renderer):
def test_SDL_CreateTextureFromSurface(with_renderer, testsurf):
renderer, win = with_renderer
tx = sdl2.SDL_CreateTextureFromSurface(renderer, testsurf)
if sdl2.dll.version != 2008: # Weird non-fatal colorkey error on 2.0.8
assert SDL_GetError() == b""
assert tx, _check_error_msg()
assert isinstance(tx.contents, sdl2.SDL_Texture)
sdl2.SDL_DestroyTexture(tx)

Expand Down Expand Up @@ -454,8 +454,7 @@ def test_SDL_GetSetTextureUserData(texture):
assert ret == 0, _check_error_msg()
# Try retrieving the user data
dat_ptr = sdl2.SDL_GetTextureUserData(texture)
assert SDL_GetError() == b""
assert dat_ptr != None
assert dat_ptr, _check_error_msg()
dat_out = ctypes.cast(dat_ptr, ctypes.c_char_p)
assert dat_raw.value == dat_out.value

Expand Down Expand Up @@ -491,7 +490,6 @@ def test_SDL_RenderTargetSupported(supported_renderers):
usable += 1
assert isinstance(renderer.contents, sdl2.SDL_Renderer)
val = sdl2.SDL_RenderTargetSupported(renderer)
assert SDL_GetError() == b""
assert val in (SDL_TRUE, SDL_FALSE)
sdl2.SDL_DestroyRenderer(renderer)
video.SDL_DestroyWindow(window)
Expand Down Expand Up @@ -520,15 +518,17 @@ def test_SDL_GetSetRenderTarget(supported_renderers):
pixfmt = pixels.SDL_PIXELFORMAT_ARGB8888
for i in supports_targets:
window = _create_window((30, 30), (100, 100))
sdl2.SDL_ClearError()
renderer = sdl2.SDL_CreateRenderer(window, i, flags)
# Try setting a texture as the render target
tex = sdl2.SDL_CreateTexture(
renderer, pixfmt, sdl2.SDL_TEXTUREACCESS_TARGET, 10, 10
)
ret = sdl2.SDL_SetRenderTarget(renderer, tex)
assert ret == 0, _check_error_msg()
sdl2.SDL_ClearError()
tgt = sdl2.SDL_GetRenderTarget(renderer)
assert SDL_GetError() == b""
assert tgt, _check_error_msg()
assert isinstance(tgt.contents, sdl2.SDL_Texture)
# Try setting NULL as the render target (resets target to window)
ret = sdl2.SDL_SetRenderTarget(renderer, None)
Expand Down
Loading

0 comments on commit 949fe89

Please sign in to comment.