Skip to content

Commit fc27a46

Browse files
committed
expand trepan.lib.pp test
1 parent e8ca5b2 commit fc27a46

2 files changed

Lines changed: 76 additions & 11 deletions

File tree

test/unit/lib/test_lib_pp.py

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,76 @@
11
"""Unit test for trepan.lib.pp"""
22

3-
from test.unit.cmdhelper import msg, msgs, reset_output
3+
from typing import List
44

5-
from trepan.lib.pp import pprint_simple_array
5+
from trepan.lib.pp import pp, pprint_simple_array
6+
7+
errmsgs: List[str] = []
8+
9+
10+
def errmsg(msg_str: str):
11+
"""
12+
error message collection routine for unit testing.
13+
"""
14+
global errmsgs
15+
errmsgs.append(msg_str)
16+
return
17+
18+
19+
msgs: List[str] = []
20+
21+
22+
def msg(msg_str: str):
23+
"""
24+
message collection routine for unit testing.
25+
"""
26+
global msgs
27+
msgs.append(msg_str)
28+
return
29+
30+
31+
def reset_output():
32+
"""
33+
Reset error messages and messages in advance of testing.
34+
"""
35+
global errmsgs
36+
errmsgs = []
37+
global msgs
38+
msgs = []
639

740

841
def msg_nocr(msg_str: str):
942
global msgs
10-
msgs = [msg_str]
43+
if len(msgs) > 0:
44+
msgs[-1] += msg_str
45+
else:
46+
msgs = [msg_str]
47+
return
48+
49+
50+
def test_lib_pp():
51+
reset_output()
52+
pp([i for i in range(8)], 50, msg_nocr, msg)
53+
assert ["[0, 1, 2, 3, 4, 5, 6, 7]\n\n"] == msgs
54+
55+
reset_output()
56+
x = [i for i in range(5)]
57+
pp(x, 50, msg_nocr, msg, "x =")
58+
assert ["x = [0, 1, 2, 3, 4]"] == msgs
59+
60+
reset_output()
61+
x = [i for i in range(10)]
62+
pp(x, 20, msg_nocr, msg, "x = ")
63+
assert ["x = [0, 1, 2, 3, 4, 5,\n 6, 7, 8, 9]\n\n"] == msgs
1164
return
1265

1366

1467
def test_lib_pprint_simple_array():
1568
reset_output()
69+
70+
# We can't pprint these types, so return False.
71+
for val in (5, {}, None, True):
72+
assert pprint_simple_array(5, 53, msg_nocr, msg) is False
73+
1674
pprint_simple_array(list(range(50)), 53, msg_nocr, msg)
1775
assert [
1876
"[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,\n"

trepan/lib/pp.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717

1818
import pprint
19+
from typing import Callable
20+
1921
from columnize import columnize
2022

2123
# Maximum length of strings
@@ -44,18 +46,21 @@ def truncate_length(obj, length=MAX_PP_COUNT):
4446

4547
class SafePP(pprint.PrettyPrinter):
4648
def _format(self, obj, *args, **kwargs):
47-
if isinstance(obj, str):
48-
if len(obj) > MAX_PP_STRLEN:
49-
obj = obj[:MAX_PP_STRLEN] + "..."
49+
try:
50+
if isinstance(obj, str):
51+
if len(obj) > MAX_PP_STRLEN:
52+
obj = obj[:MAX_PP_STRLEN] + "..."
53+
pass
5054
pass
55+
elif hasattr(obj, "__len__") and len(obj) > MAX_PP_COUNT:
56+
obj = truncate_length(obj)
57+
except Exception:
5158
pass
52-
elif hasattr(obj, "__len__") and len(obj) > MAX_PP_COUNT:
53-
obj = truncate_length(obj)
5459

5560
return pprint.PrettyPrinter._format(self, obj, *args, **kwargs)
5661

5762

58-
def pp(val, display_width, msg_nocr, msg, prefix=None):
63+
def pp(val, display_width, msg_nocr: Callable, msg: Callable, prefix=None):
5964
if prefix is not None:
6065
val_len = len(repr(val))
6166
if val_len + len(prefix) < display_width - 1:
@@ -77,7 +82,9 @@ def pp(val, display_width, msg_nocr, msg, prefix=None):
7782

7883
# Actually... code like this should go in pformat.
7984
# Possibly some will go into columnize.
80-
def pprint_simple_array(val, displaywidth, msg_nocr, msg, lineprefix=""):
85+
def pprint_simple_array(
86+
val, displaywidth, msg_nocr: Callable, msg: Callable, lineprefix=""
87+
) -> bool:
8188
"""Try to pretty print a simple case where a list is not nested.
8289
Return True if we can do it and False if not."""
8390

@@ -116,7 +123,7 @@ def msg_nocr(m):
116123
def msg(m):
117124
print(m)
118125

119-
pprint_simple_array(range(50), 50, msg_nocr, msg)
126+
assert pprint_simple_array(range(50), 50, msg_nocr, msg) is False
120127
pp([i for i in range(10)], 50, msg_nocr, msg)
121128
pp(locals(), 50, msg_nocr, msg)
122129
x = [i for i in range(10)]

0 commit comments

Comments
 (0)