Skip to content

Commit de59245

Browse files
committed
Move assert out of prod code
The assert that checks the width of each char is removed from screen.display and put it into the tests. This ensures that our test suite maintains the same quality and at the same time we make screen.display ~x1.7 faster.
1 parent b3b7db4 commit de59245

File tree

6 files changed

+118
-5
lines changed

6 files changed

+118
-5
lines changed

pyte/screens.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,6 @@ def display(self):
256256
is_wide_char = False
257257
continue
258258
char = cell.data
259-
assert sum(map(wcwidth, char[1:])) == 0
260259
is_wide_char = wcwidth(char[0]) == 2
261260
display_line.append(char)
262261

tests/helpers/asserts.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from wcwidth import wcwidth
2+
def consistency_asserts(screen):
3+
# Ensure that all the cells in the buffer, if they have
4+
# a data of 2 or more code points, they all sum up 0 width
5+
# In other words, the width of the cell is determinated by the
6+
# width of the first code point.
7+
for y in range(screen.lines):
8+
for x in range(screen.columns):
9+
char = screen.buffer[y][x].data
10+
assert sum(map(wcwidth, char[1:])) == 0
11+
12+

tests/test_history.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import os
1+
import os, sys
22

33
import pyte
44
from pyte import control as ctrl, modes as mo
55

6+
sys.path.append(os.path.join(os.path.dirname(__file__), "helpers"))
7+
from asserts import consistency_asserts
68

79
def chars(history_lines, columns):
810
return ["".join(history_lines[y][x].data for x in range(columns))
@@ -96,6 +98,7 @@ def test_prev_page():
9698
"39 ",
9799
" "
98100
]
101+
consistency_asserts(screen)
99102

100103
assert chars(screen.history.top, screen.columns)[-4:] == [
101104
"33 ",
@@ -114,6 +117,7 @@ def test_prev_page():
114117
"37 ",
115118
"38 "
116119
]
120+
consistency_asserts(screen)
117121

118122
assert chars(screen.history.top, screen.columns)[-4:] == [
119123
"31 ",
@@ -138,6 +142,7 @@ def test_prev_page():
138142
"35 ",
139143
"36 ",
140144
]
145+
consistency_asserts(screen)
141146

142147
assert len(screen.history.bottom) == 4
143148
assert chars(screen.history.bottom, screen.columns) == [
@@ -165,6 +170,7 @@ def test_prev_page():
165170
"49 ",
166171
" "
167172
]
173+
consistency_asserts(screen)
168174

169175
screen.prev_page()
170176
assert screen.history.position == 47
@@ -175,6 +181,7 @@ def test_prev_page():
175181
"46 ",
176182
"47 "
177183
]
184+
consistency_asserts(screen)
178185

179186
assert len(screen.history.bottom) == 3
180187
assert chars(screen.history.bottom, screen.columns) == [
@@ -200,6 +207,7 @@ def test_prev_page():
200207
"39 ",
201208
" "
202209
]
210+
consistency_asserts(screen)
203211

204212
screen.prev_page()
205213
assert screen.history.position == 37
@@ -209,6 +217,7 @@ def test_prev_page():
209217
"36 ",
210218
"37 "
211219
]
220+
consistency_asserts(screen)
212221

213222
assert len(screen.history.bottom) == 3
214223
assert chars(screen.history.bottom, screen.columns) == [
@@ -235,6 +244,7 @@ def test_prev_page():
235244
"49 ",
236245
" "
237246
]
247+
consistency_asserts(screen)
238248

239249
screen.cursor_to_line(screen.lines // 2)
240250

@@ -250,6 +260,7 @@ def test_prev_page():
250260
"4 ",
251261
"5 "
252262
]
263+
consistency_asserts(screen)
253264

254265
while screen.history.position < screen.history.size:
255266
screen.next_page()
@@ -262,6 +273,7 @@ def test_prev_page():
262273
"49 ",
263274
" "
264275
]
276+
consistency_asserts(screen)
265277

266278
# e) same with cursor near the middle of the screen.
267279
screen = pyte.HistoryScreen(5, 5, history=50)
@@ -282,6 +294,7 @@ def test_prev_page():
282294
"49 ",
283295
" "
284296
]
297+
consistency_asserts(screen)
285298

286299
screen.cursor_to_line(screen.lines // 2 - 2)
287300

@@ -297,6 +310,7 @@ def test_prev_page():
297310
"4 ",
298311
"5 "
299312
]
313+
consistency_asserts(screen)
300314

301315
while screen.history.position < screen.history.size:
302316
screen.next_page()
@@ -310,6 +324,7 @@ def test_prev_page():
310324
"49 ",
311325
" "
312326
]
327+
consistency_asserts(screen)
313328

314329

315330
def test_next_page():
@@ -332,6 +347,7 @@ def test_next_page():
332347
"24 ",
333348
" "
334349
]
350+
consistency_asserts(screen)
335351

336352
# a) page up -- page down.
337353
screen.prev_page()
@@ -346,6 +362,7 @@ def test_next_page():
346362
"24 ",
347363
" "
348364
]
365+
consistency_asserts(screen)
349366

350367
# b) double page up -- page down.
351368
screen.prev_page()
@@ -366,6 +383,7 @@ def test_next_page():
366383
"21 ",
367384
"22 "
368385
]
386+
consistency_asserts(screen)
369387

370388
# c) double page up -- double page down
371389
screen.prev_page()
@@ -381,6 +399,7 @@ def test_next_page():
381399
"21 ",
382400
"22 "
383401
]
402+
consistency_asserts(screen)
384403

385404

386405
def test_ensure_width(monkeypatch):
@@ -402,6 +421,7 @@ def test_ensure_width(monkeypatch):
402421
"0024 ",
403422
" "
404423
]
424+
consistency_asserts(screen)
405425

406426
# Shrinking the screen should truncate the displayed lines following lines.
407427
screen.resize(5, 3)
@@ -416,6 +436,7 @@ def test_ensure_width(monkeypatch):
416436
"002", # 21
417437
"002" # 22
418438
]
439+
consistency_asserts(screen)
419440

420441

421442
def test_not_enough_lines():
@@ -436,6 +457,7 @@ def test_not_enough_lines():
436457
"4 ",
437458
" "
438459
]
460+
consistency_asserts(screen)
439461

440462
screen.prev_page()
441463
assert not screen.history.top
@@ -448,6 +470,7 @@ def test_not_enough_lines():
448470
"3 ",
449471
"4 ",
450472
]
473+
consistency_asserts(screen)
451474

452475
screen.next_page()
453476
assert screen.history.top
@@ -459,6 +482,7 @@ def test_not_enough_lines():
459482
"4 ",
460483
" "
461484
]
485+
consistency_asserts(screen)
462486

463487

464488
def test_draw(monkeypatch):
@@ -479,6 +503,7 @@ def test_draw(monkeypatch):
479503
"24 ",
480504
" "
481505
]
506+
consistency_asserts(screen)
482507

483508
# a) doing a pageup and then a draw -- expecting the screen
484509
# to scroll to the bottom before drawing anything.
@@ -494,6 +519,7 @@ def test_draw(monkeypatch):
494519
"24 ",
495520
"x "
496521
]
522+
consistency_asserts(screen)
497523

498524

499525
def test_cursor_is_hidden(monkeypatch):

tests/test_input_output.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import json
2-
import os.path
2+
import os.path, sys
33

44
import pytest
55

@@ -8,6 +8,8 @@
88

99
captured_dir = os.path.join(os.path.dirname(__file__), "captured")
1010

11+
sys.path.append(os.path.join(os.path.dirname(__file__), "helpers"))
12+
from asserts import consistency_asserts
1113

1214
@pytest.mark.parametrize("name", [
1315
"cat-gpl3", "find-etc", "htop", "ls", "mc", "top", "vi"
@@ -23,3 +25,4 @@ def test_input_output(name):
2325
stream = pyte.ByteStream(screen)
2426
stream.feed(input)
2527
assert screen.display == output
28+
consistency_asserts(screen)

0 commit comments

Comments
 (0)