Skip to content

Commit 257ba78

Browse files
committed
test(steami_screen): Add mock tests and rename screen.py to device.py.
1 parent 52f0e9a commit 257ba78

3 files changed

Lines changed: 330 additions & 1 deletion

File tree

lib/steami_screen/steami_screen/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from steami_screen.colors import rgb_to_gray4, rgb_to_rgb8, rgb_to_rgb565
2-
from steami_screen.screen import (
2+
from steami_screen.device import (
33
BLACK,
44
BLUE,
55
DARK,

tests/scenarios/steami_screen.yaml

Lines changed: 329 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,329 @@
1+
driver: steami_screen
2+
driver_class: Screen
3+
4+
mock_init: |
5+
class FakeDisplay:
6+
def __init__(self):
7+
self.calls = []
8+
self.width = 128
9+
self.height = 128
10+
11+
def fill(self, color):
12+
self.calls.append(("fill", color))
13+
14+
def pixel(self, x, y, color):
15+
self.calls.append(("pixel", x, y, color))
16+
17+
def text(self, text, x, y, color):
18+
self.calls.append(("text", text, x, y, color))
19+
20+
def line(self, x1, y1, x2, y2, color):
21+
self.calls.append(("line", x1, y1, x2, y2, color))
22+
23+
def fill_rect(self, x, y, w, h, color):
24+
self.calls.append(("fill_rect", x, y, w, h, color))
25+
26+
def rect(self, x, y, w, h, color):
27+
self.calls.append(("rect", x, y, w, h, color))
28+
29+
def show(self):
30+
self.calls.append(("show",))
31+
32+
def clear_calls(self):
33+
self.calls = []
34+
35+
dev = Screen(FakeDisplay())
36+
37+
tests:
38+
# ----- Properties -----
39+
40+
- name: "Center is computed correctly"
41+
action: script
42+
script: |
43+
result = dev.center == (64, 64)
44+
expect_true: true
45+
mode: [mock]
46+
47+
- name: "Radius is computed correctly"
48+
action: script
49+
script: |
50+
result = dev.radius == 64
51+
expect_true: true
52+
mode: [mock]
53+
54+
- name: "Max chars matches screen width"
55+
action: script
56+
script: |
57+
result = dev.max_chars == 16
58+
expect_true: true
59+
mode: [mock]
60+
61+
# ----- Core methods -----
62+
63+
- name: "clear calls backend fill"
64+
action: script
65+
script: |
66+
d = dev._d
67+
d.clear_calls()
68+
dev.clear()
69+
result = len(d.calls) == 1 and d.calls[0][0] == "fill"
70+
expect_true: true
71+
mode: [mock]
72+
73+
- name: "show calls backend show"
74+
action: script
75+
script: |
76+
d = dev._d
77+
d.clear_calls()
78+
dev.show()
79+
result = len(d.calls) == 1 and d.calls[0][0] == "show"
80+
expect_true: true
81+
mode: [mock]
82+
83+
# ----- Drawing primitives -----
84+
85+
- name: "pixel calls backend pixel"
86+
action: script
87+
script: |
88+
d = dev._d
89+
d.clear_calls()
90+
dev.pixel(10, 20)
91+
result = d.calls == [("pixel", 10, 20, (255, 255, 255))]
92+
expect_true: true
93+
mode: [mock]
94+
95+
- name: "line calls backend line"
96+
action: script
97+
script: |
98+
d = dev._d
99+
d.clear_calls()
100+
dev.line(1, 2, 30, 40)
101+
result = d.calls == [("line", 1, 2, 30, 40, (255, 255, 255))]
102+
expect_true: true
103+
mode: [mock]
104+
105+
- name: "rect outline uses backend rect"
106+
action: script
107+
script: |
108+
d = dev._d
109+
d.clear_calls()
110+
dev.rect(5, 6, 20, 10)
111+
result = d.calls == [("rect", 5, 6, 20, 10, (255, 255, 255))]
112+
expect_true: true
113+
mode: [mock]
114+
115+
- name: "rect fill uses backend fill_rect"
116+
action: script
117+
script: |
118+
d = dev._d
119+
d.clear_calls()
120+
dev.rect(5, 6, 20, 10, fill=True)
121+
result = d.calls == [("fill_rect", 5, 6, 20, 10, (255, 255, 255))]
122+
expect_true: true
123+
mode: [mock]
124+
125+
# ----- Text -----
126+
127+
- name: "text at CENTER draws backend text"
128+
action: script
129+
script: |
130+
d = dev._d
131+
d.clear_calls()
132+
dev.text("Hi")
133+
result = len(d.calls) >= 1 and d.calls[0][0] == "text"
134+
expect_true: true
135+
mode: [mock]
136+
137+
- name: "text at explicit coordinates uses given position"
138+
action: script
139+
script: |
140+
d = dev._d
141+
d.clear_calls()
142+
dev.text("Hi", at=(12, 34))
143+
result = d.calls == [("text", "Hi", 12, 34, (255, 255, 255))]
144+
expect_true: true
145+
mode: [mock]
146+
147+
- name: "text scale 2 still renders text"
148+
action: script
149+
script: |
150+
d = dev._d
151+
d.clear_calls()
152+
dev.text("Hi", scale=2)
153+
text_calls = [c for c in d.calls if c[0] == "text"]
154+
result = len(text_calls) > 0
155+
expect_true: true
156+
mode: [mock]
157+
158+
# ----- Layout widgets -----
159+
160+
- name: "title draws text near north"
161+
action: script
162+
script: |
163+
d = dev._d
164+
d.clear_calls()
165+
dev.title("Hello")
166+
text_calls = [c for c in d.calls if c[0] == "text"]
167+
result = len(text_calls) == 1 and text_calls[0][1] == "Hello"
168+
expect_true: true
169+
mode: [mock]
170+
171+
- name: "subtitle draws text near south"
172+
action: script
173+
script: |
174+
d = dev._d
175+
d.clear_calls()
176+
dev.subtitle("Line1", "Line2")
177+
text_calls = [c for c in d.calls if c[0] == "text"]
178+
result = len(text_calls) == 2
179+
expect_true: true
180+
mode: [mock]
181+
182+
- name: "empty subtitle is no-op"
183+
action: script
184+
script: |
185+
d = dev._d
186+
d.clear_calls()
187+
dev.subtitle()
188+
result = len(d.calls) == 0
189+
expect_true: true
190+
mode: [mock]
191+
192+
- name: "value draws large text"
193+
action: script
194+
script: |
195+
d = dev._d
196+
d.clear_calls()
197+
dev.value(42)
198+
text_calls = [c for c in d.calls if c[0] == "text"]
199+
result = len(text_calls) > 0
200+
expect_true: true
201+
mode: [mock]
202+
203+
# ----- Widgets -----
204+
205+
- name: "bar draws rectangles"
206+
action: script
207+
script: |
208+
d = dev._d
209+
d.clear_calls()
210+
dev.bar(50)
211+
fill_calls = [c for c in d.calls if c[0] == "fill_rect"]
212+
result = len(fill_calls) == 2
213+
expect_true: true
214+
mode: [mock]
215+
216+
- name: "menu draws items"
217+
action: script
218+
script: |
219+
d = dev._d
220+
d.clear_calls()
221+
dev.menu(["A", "B", "C"], selected=1)
222+
text_calls = [c for c in d.calls if c[0] == "text"]
223+
result = len(text_calls) == 3
224+
expect_true: true
225+
mode: [mock]
226+
227+
- name: "face happy draws pixels"
228+
action: script
229+
script: |
230+
d = dev._d
231+
d.clear_calls()
232+
dev.face("happy")
233+
fill_calls = [c for c in d.calls if c[0] == "fill_rect"]
234+
result = len(fill_calls) > 0
235+
expect_true: true
236+
mode: [mock]
237+
238+
- name: "face unknown is no-op"
239+
action: script
240+
script: |
241+
d = dev._d
242+
d.clear_calls()
243+
dev.face("nonexistent")
244+
result = len(d.calls) == 0
245+
expect_true: true
246+
mode: [mock]
247+
248+
- name: "gauge renders without error"
249+
action: script
250+
script: |
251+
d = dev._d
252+
d.clear_calls()
253+
dev.gauge(50)
254+
result = len(d.calls) > 0
255+
expect_true: true
256+
mode: [mock]
257+
258+
- name: "gauge handles min equals max"
259+
action: script
260+
script: |
261+
d = dev._d
262+
d.clear_calls()
263+
dev.gauge(50, min_val=50, max_val=50)
264+
result = len(d.calls) > 0
265+
expect_true: true
266+
mode: [mock]
267+
268+
- name: "compass renders without error"
269+
action: script
270+
script: |
271+
d = dev._d
272+
d.clear_calls()
273+
dev.compass(90)
274+
result = len(d.calls) > 0
275+
expect_true: true
276+
mode: [mock]
277+
278+
- name: "watch renders without error"
279+
action: script
280+
script: |
281+
d = dev._d
282+
d.clear_calls()
283+
dev.watch(10, 30, 15)
284+
result = len(d.calls) > 0
285+
expect_true: true
286+
mode: [mock]
287+
288+
- name: "graph renders without error"
289+
action: script
290+
script: |
291+
d = dev._d
292+
d.clear_calls()
293+
dev.graph([10, 20, 30, 40])
294+
result = len(d.calls) > 0
295+
expect_true: true
296+
mode: [mock]
297+
298+
- name: "circle outline renders"
299+
action: script
300+
script: |
301+
d = dev._d
302+
d.clear_calls()
303+
dev.circle(64, 64, 20)
304+
pixel_calls = [c for c in d.calls if c[0] == "pixel"]
305+
result = len(pixel_calls) > 0
306+
expect_true: true
307+
mode: [mock]
308+
309+
- name: "circle fill renders"
310+
action: script
311+
script: |
312+
d = dev._d
313+
d.clear_calls()
314+
dev.circle(64, 64, 10, fill=True)
315+
line_calls = [c for c in d.calls if c[0] == "line"]
316+
result = len(line_calls) > 0
317+
expect_true: true
318+
mode: [mock]
319+
320+
- name: "invalid position falls back to center"
321+
action: script
322+
script: |
323+
d = dev._d
324+
d.clear_calls()
325+
dev.text("Test", at="INVALID")
326+
text_calls = [c for c in d.calls if c[0] == "text"]
327+
result = len(text_calls) == 1
328+
expect_true: true
329+
mode: [mock]

0 commit comments

Comments
 (0)