-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathtest_console.py
More file actions
160 lines (123 loc) · 5.33 KB
/
test_console.py
File metadata and controls
160 lines (123 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""Tests for tcod.console."""
import pickle
from pathlib import Path
import numpy as np
import pytest
import tcod
# ruff: noqa: D103
@pytest.mark.filterwarnings("ignore:Directly access a consoles")
@pytest.mark.filterwarnings("ignore:This function may be deprecated in the fu")
def test_array_read_write() -> None:
console = tcod.console.Console(width=12, height=10)
FG = (255, 254, 253)
BG = (1, 2, 3)
CH = ord("&")
tcod.console_put_char_ex(console, 0, 0, CH, FG, BG)
assert console.ch[0, 0] == CH
assert tuple(console.fg[0, 0]) == FG
assert tuple(console.bg[0, 0]) == BG
tcod.console_put_char_ex(console, 1, 2, CH, FG, BG)
assert console.ch[2, 1] == CH
assert tuple(console.fg[2, 1]) == FG
assert tuple(console.bg[2, 1]) == BG
console.clear()
assert console.ch[1, 1] == ord(" ")
assert tuple(console.fg[1, 1]) == (255, 255, 255)
assert tuple(console.bg[1, 1]) == (0, 0, 0)
ch_slice = console.ch[1, :]
ch_slice[2] = CH
console.fg[1, ::2] = FG
console.bg[...] = BG
assert tcod.console_get_char(console, 2, 1) == CH
assert tuple(tcod.console_get_char_foreground(console, 2, 1)) == FG
assert tuple(tcod.console_get_char_background(console, 2, 1)) == BG
@pytest.mark.filterwarnings("ignore")
def test_console_defaults() -> None:
console = tcod.console.Console(width=12, height=10)
console.default_bg = [2, 3, 4] # type: ignore[assignment]
assert console.default_bg == (2, 3, 4)
console.default_fg = (4, 5, 6)
assert console.default_fg == (4, 5, 6)
console.default_bg_blend = tcod.BKGND_ADD
assert console.default_bg_blend == tcod.BKGND_ADD
console.default_alignment = tcod.RIGHT
assert console.default_alignment == tcod.RIGHT
@pytest.mark.filterwarnings("ignore:Parameter names have been moved around,")
@pytest.mark.filterwarnings("ignore:Pass the key color to Console.blit instea")
@pytest.mark.filterwarnings("ignore:.*default values have been deprecated")
def test_console_methods() -> None:
console = tcod.console.Console(width=12, height=10)
console.put_char(0, 0, ord("@"))
console.print_(0, 0, "Test")
console.print_rect(0, 0, 2, 8, "a b c d e f")
console.get_height_rect(0, 0, 2, 8, "a b c d e f")
console.rect(0, 0, 2, 2, True)
console.hline(0, 1, 10)
console.vline(1, 0, 10)
console.print_frame(0, 0, 8, 8, "Frame")
console.blit(0, 0, 0, 0, console, 0, 0) # type: ignore
console.blit(0, 0, 0, 0, console, 0, 0, key_color=(0, 0, 0)) # type: ignore
console.set_key_color((254, 0, 254))
def test_console_pickle() -> None:
console = tcod.console.Console(width=12, height=10)
console.ch[...] = ord(".")
console.fg[...] = (10, 20, 30)
console.bg[...] = (1, 2, 3)
console2 = pickle.loads(pickle.dumps(console))
assert (console.ch == console2.ch).all()
assert (console.fg == console2.fg).all()
assert (console.bg == console2.bg).all()
def test_console_pickle_fortran() -> None:
console = tcod.console.Console(2, 3, order="F")
console2 = pickle.loads(pickle.dumps(console))
assert console.ch.strides == console2.ch.strides
assert console.fg.strides == console2.fg.strides
assert console.bg.strides == console2.bg.strides
def test_console_repr() -> None:
from numpy import array # noqa: F401 # Used for eval
eval(repr(tcod.console.Console(10, 2)))
@pytest.mark.filterwarnings("ignore")
def test_console_str() -> None:
console = tcod.console.Console(10, 2)
console.ch[:] = ord(".")
console.print_(0, 0, "Test")
assert str(console) == ("<Test......\n" " ..........>")
def test_console_fortran_buffer() -> None:
tcod.console.Console(
width=1,
height=2,
order="F",
buffer=np.zeros((1, 2), order="F", dtype=tcod.console.Console.DTYPE),
)
def test_console_clear() -> None:
console = tcod.console.Console(1, 1)
assert console.fg[0, 0].tolist() == [255, 255, 255]
assert console.bg[0, 0].tolist() == [0, 0, 0]
console.clear(fg=(7, 8, 9), bg=(10, 11, 12))
assert console.fg[0, 0].tolist() == [7, 8, 9]
assert console.bg[0, 0].tolist() == [10, 11, 12]
def test_console_semigraphics() -> None:
console = tcod.console.Console(1, 1)
console.draw_semigraphics(
[[[255, 255, 255], [255, 255, 255]], [[255, 255, 255], [0, 0, 0]]],
)
def test_rexpaint(tmp_path: Path) -> None:
xp_path = tmp_path / "test.xp"
consoles = tcod.Console(80, 24, order="F"), tcod.Console(8, 8, order="F")
tcod.console.save_xp(xp_path, consoles, compress_level=0)
loaded = tcod.console.load_xp(xp_path, order="F")
assert len(consoles) == len(loaded)
assert loaded[0].rgba.flags["F_CONTIGUOUS"]
assert consoles[0].rgb.shape == loaded[0].rgb.shape
assert consoles[1].rgb.shape == loaded[1].rgb.shape
with pytest.raises(FileNotFoundError):
tcod.console.load_xp(tmp_path / "non_existant")
def test_draw_frame() -> None:
console = tcod.Console(3, 3, order="C")
with pytest.raises(TypeError):
console.draw_frame(0, 0, 3, 3, title="test", decoration="123456789")
with pytest.raises(TypeError):
console.draw_frame(0, 0, 3, 3, decoration="0123456789")
console.draw_frame(0, 0, 3, 3, decoration=(49, 50, 51, 52, 53, 54, 55, 56, 57))
assert console.ch.tolist() == [[49, 50, 51], [52, 53, 54], [55, 56, 57]]
console.draw_frame(0, 0, 3, 3, title="T")