Skip to content

Commit 8442bf7

Browse files
committed
convert unit test test_misc.py to pytest
1 parent 3f3018c commit 8442bf7

5 files changed

Lines changed: 65 additions & 50 deletions

File tree

setup.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,6 @@ per-file-ignores =
7676
enabled = true
7777
live_mode = true
7878
exclude = ["trepan/version.py", "tests/*"]
79+
80+
[mypy]
81+
exclude = ["trepan/version.py", "tests/*"]

test/unit/test-misc.py

Lines changed: 0 additions & 36 deletions
This file was deleted.

test/unit/test_misc.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""Unit test for trepan.misc"""
2+
3+
from trepan.misc import bool2YN, option_set, wrapped_lines
4+
5+
6+
def test_bool2YN():
7+
for arg, expect in ((True, "Y"), (False, "N"), (None, "N")):
8+
assert bool2YN(arg) == expect
9+
10+
11+
def test_option_set():
12+
"""Test trepan.misc.option_set()"""
13+
TEST_OPTS = {"a": True, "b": 5, "c": None}
14+
15+
def get_option(key):
16+
return option_set(opts, key, TEST_OPTS)
17+
18+
opts = {"d": 6, "a": False}
19+
for opt, expect in [("a", False), ("b", 5), ("c", None), ("d", 6)]:
20+
assert expect == get_option(opt)
21+
opts = None
22+
for opt, expect in [("a", True), ("b", 5), ("c", None), ("d", None)]:
23+
assert expect == get_option(opt)
24+
pass
25+
26+
27+
def test_wrapped_msg():
28+
"""Test trepan.misc.wrapped_lines()"""
29+
assert "hi there" == wrapped_lines("hi", "there", 80)
30+
assert "hi\n\tthere" == wrapped_lines("hi", "there", 5)

trepan/lib/stack.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,15 @@ def format_stack_entry(
154154
else:
155155
is_module = False
156156
try:
157-
parms = inspect.formatargvalues(args, varargs, varkw, local_vars)
157+
params = inspect.formatargvalues(args, varargs, varkw, local_vars)
158158
except Exception:
159159
pass
160160
else:
161161
maxargstrsize = dbg_obj.settings["maxargstrsize"]
162-
if len(parms) >= maxargstrsize:
163-
parms = f"{parms[0:maxargstrsize]}...)"
162+
if len(params) >= maxargstrsize:
163+
params = f"{params[0:maxargstrsize]}...)"
164164
pass
165-
s += parms
165+
s += params
166166
pass
167167

168168
# Note: ddd can't handle wrapped stack entries (yet).

trepan/misc.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
2-
# Copyright (C) 2008-2010, 2013-2015, 2020-2021 Rocky Bernstein <rocky@gnu.org>
2+
# Copyright (C) 2008-2010, 2013-2015, 2020-2021, 2024
3+
# Rocky Bernstein <rocky@gnu.org>
34
#
45
# This program is free software: you can redistribute it and/or modify
56
# it under the terms of the GNU General Public License as published by
@@ -14,25 +15,43 @@
1415
# You should have received a copy of the GNU General Public License
1516
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1617

18+
import os
19+
from glob import glob
20+
from typing import Any
21+
1722

18-
def option_set(options, value, default_options):
23+
def option_set(options: dict, value, default_options) -> Any:
24+
"""
25+
If ``value`` is found in ``options``, return that, otherwise return the value
26+
from ``defaulit_options``.
27+
"""
28+
# Yes, there is probably some fancy dictionary merge operation, that will do this,
29+
# as a one-shot but this code is simple and clear.
1930
if not options or value not in options:
2031
return default_options.get(value)
2132
else:
2233
return options.get(value)
2334
return None # Not reached
2435

2536

26-
def bool2YN(b) -> bool:
37+
def bool2YN(b: bool) -> str:
38+
"""
39+
Turn a bool into the string "Y" or "N".
40+
"""
2741
return "Y" if b else "N"
2842

2943

30-
def wrapped_lines(msg_part1, msg_part2, width):
44+
def wrapped_lines(msg_part1: str, msg_part2: str, width: int) -> str:
45+
"""
46+
if ``msg_part1`` concatenated with ``msg_part2`` is larger than
47+
``width`` then concatenate the strings with a new line and tab inserted
48+
between the strings. Otherwise, just concatenate with a space between the
49+
two strings.
50+
"""
3151
if len(msg_part1) + len(msg_part2) + 1 > width:
3252
return msg_part1 + "\n\t" + msg_part2
3353
else:
3454
return msg_part1 + " " + msg_part2
35-
return # Not reached
3655

3756

3857
def pretty_modfunc_name(s) -> str:
@@ -44,10 +63,6 @@ def pretty_modfunc_name(s) -> str:
4463
return str(s) + "()"
4564

4665

47-
import os
48-
from glob import glob
49-
50-
5166
def pyfiles(callername, level=2):
5267
"All python files caller's dir without the path and trailing .py"
5368
d = os.path.dirname(callername)
@@ -61,7 +76,10 @@ def pyfiles(callername, level=2):
6176
# Demo it
6277
if __name__ == "__main__":
6378
TEST_OPTS = {"a": True, "b": 5, "c": None}
64-
get_option = lambda key: option_set(opts, key, TEST_OPTS)
79+
80+
def get_option(key):
81+
return option_set(opts, key, TEST_OPTS)
82+
6583
opts = {"d": 6, "a": False}
6684
for opt in ["a", "b", "c", "d"]:
6785
print(opt, get_option(opt))

0 commit comments

Comments
 (0)