3
3
"""
4
4
# Standard
5
5
from unittest import mock
6
- import io
7
6
8
7
# Third Party
9
8
import pytest
10
9
10
+ from tests .conftest import ResettableStringIO
11
+
11
12
# Local
12
13
from scriptit import RefreshPrinter
13
14
14
15
15
16
def test_refresh_printer_multi_line ():
16
17
"""Test that adding multiple lines refreshes correctly"""
17
- stream = io . StringIO ()
18
+ stream = ResettableStringIO ()
18
19
printer = RefreshPrinter (write_stream = stream )
19
20
20
21
# Perform initial add and refresh
@@ -25,20 +26,21 @@ def test_refresh_printer_multi_line():
25
26
printed_lines = stream .getvalue ().split ("\n " )
26
27
assert len (printed_lines ) == 3 # Two plus final empty line
27
28
assert not any (line .startswith (RefreshPrinter .UP_LINE ) for line in printed_lines )
29
+ stream .reset ()
28
30
29
31
# Perform a second refresh and make sure clearing happens
30
32
printer .add ("Line three" )
31
33
printer .add ("Line four" )
32
34
printer .refresh ()
33
35
assert printer .refreshes == 2
34
36
printed_lines = stream .getvalue ().split ("\n " )
35
- assert len (printed_lines ) == 6 # First 2, clear , 2 new, final \n
36
- assert printed_lines [2 ].count (RefreshPrinter .UP_LINE ) == 3
37
+ assert len (printed_lines ) == 4 # Clear , 2 new, final \n
38
+ assert printed_lines [0 ].count (RefreshPrinter .UP_LINE ) == 3
37
39
38
40
39
41
def test_refresh_printer_newline_in_add ():
40
42
"""Make sure that a newline in add is handled as separate lines"""
41
- stream = io . StringIO ()
43
+ stream = ResettableStringIO ()
42
44
printer = RefreshPrinter (write_stream = stream )
43
45
44
46
# Perform initial add and refresh
@@ -57,7 +59,7 @@ def test_refresh_printer_wrap_lines(wrap):
57
59
term_width = 5
58
60
term_size_mock .columns = term_width
59
61
with mock .patch ("shutil.get_terminal_size" , return_value = term_size_mock ):
60
- stream = io . StringIO ()
62
+ stream = ResettableStringIO ()
61
63
printer = RefreshPrinter (write_stream = stream )
62
64
63
65
# Perform initial add and refresh
@@ -72,18 +74,19 @@ def test_clear_last_report_chars():
72
74
"""Make sure that shorter lines in an update don't retain characters from
73
75
previous reports
74
76
"""
75
- stream = io . StringIO ()
77
+ stream = ResettableStringIO ()
76
78
printer = RefreshPrinter (write_stream = stream )
77
79
78
80
# Perform initial add and refresh
79
81
printer .add ("Line one" )
80
82
printer .refresh ()
83
+ stream .reset ()
81
84
82
85
# Perform a second with a shorter line and make sure that the line does not
83
86
# have any of the previous report in it
84
87
printer .add ("two" )
85
88
printer .refresh ()
86
89
printed_lines = stream .getvalue ().split ("\n " )
87
- assert len (printed_lines ) == 4 # First 1, clear , 1 new, final \n
88
- assert printed_lines [1 ].count (RefreshPrinter .UP_LINE ) == 2
89
- assert printed_lines [2 ].strip () == "two"
90
+ assert len (printed_lines ) == 3 # Clear , 1 new, final \n
91
+ assert printed_lines [0 ].count (RefreshPrinter .UP_LINE ) == 2
92
+ assert printed_lines [1 ].strip () == "two"
0 commit comments