1+ """Tests for the libtcodpy API."""
2+ from pathlib import Path
13from typing import Any , Callable , Iterator , List , Optional , Tuple , Union
24
3- import numpy
45import numpy as np
56import pytest
67from numpy .typing import NDArray
78
89import tcod
910import tcod as libtcodpy
1011
12+ # ruff: noqa: D103
13+
1114pytestmark = [
1215 pytest .mark .filterwarnings ("ignore::DeprecationWarning" ),
1316 pytest .mark .filterwarnings ("ignore::PendingDeprecationWarning" ),
@@ -29,7 +32,7 @@ def test_credits(console: tcod.console.Console) -> None:
2932 libtcodpy .console_credits_reset ()
3033
3134
32- def assert_char (
35+ def assert_char ( # noqa: PLR0913
3336 console : tcod .console .Console ,
3437 x : int ,
3538 y : int ,
@@ -143,20 +146,20 @@ def test_console_blit(console: tcod.console.Console, offscreen: tcod.console.Con
143146
144147
145148@pytest .mark .filterwarnings ("ignore" )
146- def test_console_asc_read_write (console : tcod .console .Console , offscreen : tcod .console .Console , tmpdir : Any ) -> None :
149+ def test_console_asc_read_write (console : tcod .console .Console , offscreen : tcod .console .Console , tmp_path : Path ) -> None :
147150 libtcodpy .console_print (console , 0 , 0 , "test" )
148151
149- asc_file = tmpdir . join ( "test.asc" ). strpath
152+ asc_file = tmp_path / "test.asc"
150153 assert libtcodpy .console_save_asc (console , asc_file )
151154 assert libtcodpy .console_load_asc (offscreen , asc_file )
152155 assertConsolesEqual (console , offscreen )
153156
154157
155158@pytest .mark .filterwarnings ("ignore" )
156- def test_console_apf_read_write (console : tcod .console .Console , offscreen : tcod .console .Console , tmpdir : Any ) -> None :
159+ def test_console_apf_read_write (console : tcod .console .Console , offscreen : tcod .console .Console , tmp_path : Path ) -> None :
157160 libtcodpy .console_print (console , 0 , 0 , "test" )
158161
159- apf_file = tmpdir . join ( "test.apf" ). strpath
162+ apf_file = tmp_path / "test.apf"
160163 assert libtcodpy .console_save_apf (console , apf_file )
161164 assert libtcodpy .console_load_apf (offscreen , apf_file )
162165 assertConsolesEqual (console , offscreen )
@@ -176,14 +179,14 @@ def test_console_rexpaint_load_test_file(console: tcod.console.Console) -> None:
176179@pytest .mark .filterwarnings ("ignore" )
177180def test_console_rexpaint_save_load (
178181 console : tcod .console .Console ,
179- tmpdir : Any ,
182+ tmp_path : Path ,
180183 ch : int ,
181184 fg : Tuple [int , int , int ],
182185 bg : Tuple [int , int , int ],
183186) -> None :
184187 libtcodpy .console_print (console , 0 , 0 , "test" )
185188 libtcodpy .console_put_char_ex (console , 1 , 1 , ch , fg , bg )
186- xp_file = tmpdir . join ( "test.xp" ). strpath
189+ xp_file = tmp_path / "test.xp"
187190 assert libtcodpy .console_save_xp (console , xp_file , 1 )
188191 xp_console = libtcodpy .console_from_xp (xp_file )
189192 assert xp_console
@@ -193,12 +196,12 @@ def test_console_rexpaint_save_load(
193196
194197
195198@pytest .mark .filterwarnings ("ignore" )
196- def test_console_rexpaint_list_save_load (console : tcod .console .Console , tmpdir : Any ) -> None :
199+ def test_console_rexpaint_list_save_load (console : tcod .console .Console , tmp_path : Path ) -> None :
197200 con1 = libtcodpy .console_new (8 , 2 )
198201 con2 = libtcodpy .console_new (8 , 2 )
199202 libtcodpy .console_print (con1 , 0 , 0 , "hello" )
200203 libtcodpy .console_print (con2 , 0 , 0 , "world" )
201- xp_file = tmpdir . join ( "test.xp" ). strpath
204+ xp_file = tmp_path / "test.xp"
202205 assert libtcodpy .console_list_save_xp ([con1 , con2 ], xp_file , 1 )
203206 loaded_consoles = libtcodpy .console_list_load_xp (xp_file )
204207 assert loaded_consoles
@@ -252,7 +255,7 @@ def test_console_fill(console: tcod.console.Console) -> None:
252255def test_console_fill_numpy (console : tcod .console .Console ) -> None :
253256 width = libtcodpy .console_get_width (console )
254257 height = libtcodpy .console_get_height (console )
255- fill : NDArray [np .intc ] = numpy .zeros ((height , width ), dtype = np .intc )
258+ fill : NDArray [np .intc ] = np .zeros ((height , width ), dtype = np .intc )
256259 for y in range (height ):
257260 fill [y , :] = y % 256
258261
@@ -261,9 +264,9 @@ def test_console_fill_numpy(console: tcod.console.Console) -> None:
261264 libtcodpy .console_fill_char (console , fill ) # type: ignore
262265
263266 # verify fill
264- bg : NDArray [np .intc ] = numpy .zeros ((height , width ), dtype = numpy .intc )
265- fg : NDArray [np .intc ] = numpy .zeros ((height , width ), dtype = numpy .intc )
266- ch : NDArray [np .intc ] = numpy .zeros ((height , width ), dtype = numpy .intc )
267+ bg : NDArray [np .intc ] = np .zeros ((height , width ), dtype = np .intc )
268+ fg : NDArray [np .intc ] = np .zeros ((height , width ), dtype = np .intc )
269+ ch : NDArray [np .intc ] = np .zeros ((height , width ), dtype = np .intc )
267270 for y in range (height ):
268271 for x in range (width ):
269272 bg [y , x ] = libtcodpy .console_get_char_background (console , x , y )[0 ]
@@ -291,7 +294,7 @@ def test_console_buffer(console: tcod.console.Console) -> None:
291294@pytest .mark .filterwarnings ("ignore:Console array attributes perform better" )
292295def test_console_buffer_error (console : tcod .console .Console ) -> None :
293296 buffer = libtcodpy .ConsoleBuffer (0 , 0 )
294- with pytest .raises (ValueError ):
297+ with pytest .raises (ValueError , match = r".*Destination console has an incorrect size." ):
295298 buffer .blit (console )
296299
297300
@@ -322,8 +325,8 @@ def test_sys_time(console: tcod.console.Console) -> None:
322325
323326
324327@pytest .mark .filterwarnings ("ignore" )
325- def test_sys_screenshot (console : tcod .console .Console , tmpdir : Any ) -> None :
326- libtcodpy .sys_save_screenshot (tmpdir . join ( "test.png" ). strpath )
328+ def test_sys_screenshot (console : tcod .console .Console , tmp_path : Path ) -> None :
329+ libtcodpy .sys_save_screenshot (tmp_path / "test.png" )
327330
328331
329332@pytest .mark .filterwarnings ("ignore" )
@@ -333,7 +336,7 @@ def test_sys_custom_render(console: tcod.console.Console) -> None:
333336
334337 escape = []
335338
336- def sdl_callback (sdl_surface : Any ) -> None :
339+ def sdl_callback (sdl_surface : object ) -> None :
337340 escape .append (True )
338341
339342 libtcodpy .sys_register_SDL_renderer (sdl_callback )
@@ -342,7 +345,7 @@ def sdl_callback(sdl_surface: Any) -> None:
342345
343346
344347@pytest .mark .filterwarnings ("ignore" )
345- def test_image (console : tcod .console .Console , tmpdir : Any ) -> None :
348+ def test_image (console : tcod .console .Console , tmp_path : Path ) -> None :
346349 img = libtcodpy .image_new (16 , 16 )
347350 libtcodpy .image_clear (img , (0 , 0 , 0 ))
348351 libtcodpy .image_invert (img )
@@ -360,7 +363,7 @@ def test_image(console: tcod.console.Console, tmpdir: Any) -> None:
360363 libtcodpy .image_blit (img , console , 0 , 0 , libtcodpy .BKGND_SET , 1 , 1 , 0 )
361364 libtcodpy .image_blit_rect (img , console , 0 , 0 , 16 , 16 , libtcodpy .BKGND_SET )
362365 libtcodpy .image_blit_2x (img , console , 0 , 0 )
363- libtcodpy .image_save (img , tmpdir . join ( "test.png" ). strpath )
366+ libtcodpy .image_save (img , tmp_path / "test.png" )
364367 libtcodpy .image_delete (img )
365368
366369 img = libtcodpy .image_from_console (console )
@@ -385,14 +388,12 @@ def test_clipboard(console: tcod.console.Console, sample: str) -> None:
385388# arguments to test with and the results expected from these arguments
386389LINE_ARGS = (- 5 , 0 , 5 , 10 )
387390EXCLUSIVE_RESULTS = [(- 4 , 1 ), (- 3 , 2 ), (- 2 , 3 ), (- 1 , 4 ), (0 , 5 ), (1 , 6 ), (2 , 7 ), (3 , 8 ), (4 , 9 ), (5 , 10 )]
388- INCLUSIVE_RESULTS = [(- 5 , 0 )] + EXCLUSIVE_RESULTS
391+ INCLUSIVE_RESULTS = [(- 5 , 0 ), * EXCLUSIVE_RESULTS ]
389392
390393
391394@pytest .mark .filterwarnings ("ignore" )
392395def test_line_step () -> None :
393- """
394- libtcodpy.line_init and libtcodpy.line_step
395- """
396+ """libtcodpy.line_init and libtcodpy.line_step."""
396397 libtcodpy .line_init (* LINE_ARGS )
397398 for expected_xy in EXCLUSIVE_RESULTS :
398399 assert libtcodpy .line_step () == expected_xy
@@ -401,9 +402,7 @@ def test_line_step() -> None:
401402
402403@pytest .mark .filterwarnings ("ignore" )
403404def test_line () -> None :
404- """
405- tests normal use, lazy evaluation, and error propagation
406- """
405+ """Tests normal use, lazy evaluation, and error propagation."""
407406 # test normal results
408407 test_result : List [Tuple [int , int ]] = []
409408
@@ -427,17 +426,13 @@ def return_false(x: int, y: int) -> bool:
427426
428427@pytest .mark .filterwarnings ("ignore" )
429428def test_line_iter () -> None :
430- """
431- libtcodpy.line_iter
432- """
429+ """libtcodpy.line_iter."""
433430 assert list (libtcodpy .line_iter (* LINE_ARGS )) == INCLUSIVE_RESULTS
434431
435432
436433@pytest .mark .filterwarnings ("ignore" )
437434def test_bsp () -> None :
438- """
439- commented out statements work in libtcod-cffi
440- """
435+ """Commented out statements work in libtcod-cffi."""
441436 bsp = libtcodpy .bsp_new_with_size (0 , 0 , 64 , 64 )
442437 repr (bsp ) # test __repr__ on leaf
443438 libtcodpy .bsp_resize (bsp , 0 , 0 , 32 , 32 )
@@ -479,7 +474,7 @@ def test_bsp() -> None:
479474 libtcodpy .bsp_split_recursive (bsp , None , 4 , 2 , 2 , 1.0 , 1.0 )
480475
481476 # cover bsp_traverse
482- def traverse (node : tcod .bsp .BSP , user_data : Any ) -> None :
477+ def traverse (node : tcod .bsp .BSP , user_data : object ) -> None :
483478 return None
484479
485480 libtcodpy .bsp_traverse_pre_order (bsp , traverse )
@@ -498,9 +493,10 @@ def traverse(node: tcod.bsp.BSP, user_data: Any) -> None:
498493
499494@pytest .mark .filterwarnings ("ignore" )
500495def test_map () -> None :
501- map = libtcodpy .map_new (16 , 16 )
502- assert libtcodpy .map_get_width (map ) == 16
503- assert libtcodpy .map_get_height (map ) == 16
496+ WIDTH , HEIGHT = 13 , 17
497+ map = libtcodpy .map_new (WIDTH , HEIGHT )
498+ assert libtcodpy .map_get_width (map ) == WIDTH
499+ assert libtcodpy .map_get_height (map ) == HEIGHT
504500 libtcodpy .map_copy (map , map )
505501 libtcodpy .map_clear (map )
506502 libtcodpy .map_set_properties (map , 0 , 0 , True , True )
0 commit comments