Skip to content

Commit

Permalink
fix bug in dedent
Browse files Browse the repository at this point in the history
  • Loading branch information
Ken Kundert authored and Ken Kundert committed Dec 11, 2024
1 parent 465b65d commit 0e889d6
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 99 deletions.
12 changes: 6 additions & 6 deletions inform/inform.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,10 +818,10 @@ def dedent(text, strip_nl=None, *, bolm=None, wrap=False):
strip_nl = None:
strip_nl is used to strip off a single leading or trailing newline.
strip_nl may be None, 'l', 't', or 'b' representing neither,
leading, trailing, or both. True may also be passed, which is equivalent to 'b'.
Can also use 's' (start) as synonym for 'l' and 'e' (end) as
synonym for 't'.
strip_nl may be None, 'l', 't', or 'b' representing neither, leading,
trailing, or both. True may also be passed, which is equivalent to 'b'.
Can also use 's' (start) as synonym for 'l' and 'e' (end) or 'r' (right)
as synonym for 't'.
wrap (bool or int):
If true the string is wrapped using a width of 70. If an integer value
Expand Down Expand Up @@ -879,11 +879,11 @@ def dedent(text, strip_nl=None, *, bolm=None, wrap=False):
dedented = '\n' + ' '*len(bolm) + dedented[l:]

# remove leading newline if desired
if strip_nl in ['s', 'b', 'l', 'True'] and dedented[0] == '\n':
if strip_nl in ['s', 'b', 'l', True] and dedented[:1] == '\n':
dedented = dedented[1:]

# remove trailing newline if desired
if strip_nl in ['e', 'b', 't', 'True'] and dedented[-1] == '\n':
if strip_nl in ['e', 'b', 't', 'r', True] and dedented[-1:] == '\n':
dedented = dedented[:-1]

# wrap text to desired width
Expand Down
91 changes: 0 additions & 91 deletions tests/test_inform.py
Original file line number Diff line number Diff line change
Expand Up @@ -861,97 +861,6 @@ def test_stripy():
{expected}
''').strip().format(expected=expected)

def test_tree():
with messenger() as (msg, stdout, stderr, logfile):
data = data = dict(
str = 'text',
mlstr = dedent("""
line 1
line 2
""").strip(),
alist = [1, 2, 3],
adict = {1: 'one', 2: 'two', 3: 'three', 'four': None}
)
result = tree(data, squeeze=True)
expected = dedent('''
str: text
mlstr: line 1
line 2
alist
├── 1
├── 2
└── 3
adict
├── 1: one
├── 2: two
├── 3: three
└── four
''').strip()
assert result == expected

result = tree(data)
expected = dedent('''
str
└── text
mlstr
└── line 1
line 2
alist
├── 1
├── 2
└── 3
adict
├── 1
│   └── one
├── 2
│   └── two
├── 3
│   └── three
└── four
''').strip()
assert result == expected

result = tree(dict(root=data), squeeze=True)
expected = dedent('''
root
├── str: text
├── mlstr: line 1
│    line 2
├── alist
│   ├── 1
│   ├── 2
│   └── 3
└── adict
    ├── 1: one
    ├── 2: two
    ├── 3: three
    └── four
''').strip()
assert result == expected

result = tree(dict(root=data))
expected = dedent('''
root
├── str
│   └── text
├── mlstr
│   └── line 1
│    line 2
├── alist
│   ├── 1
│   ├── 2
│   └── 3
└── adict
    ├── 1
    │   └── one
    ├── 2
    │   └── two
    ├── 3
    │   └── three
    └── four
''').strip()
assert result == expected

def test_capsys_out(capsys):
output('hello world')
cap = capsys.readouterr()
Expand Down
111 changes: 109 additions & 2 deletions tests/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
cull, dedent, display, done, error, fatal, fmt, full_stop, indent, Inform,
is_collection, is_iterable, is_mapping, is_str, join, get_prog_name,
get_informer, narrate, os_error, output, plural, render, terminate,
title_case, truth, warn, ddd, ppp, sss, vvv, ProgressBar, parse_range,
title_case, tree, truth, warn, ddd, ppp, sss, vvv, ProgressBar, parse_range,
format_range
)
from textwrap import dedent as tw_dedent
Expand Down Expand Up @@ -1810,9 +1810,26 @@ def test_oblong():
assert render_bar(0.66, 25) == '████████████████▌'


@parametrize('given', ['a', '\na', 'a\n', '\na\n', '\na\nb\n'])
@parametrize('given', ['', 'a', '\na', 'a\n', '\na\n', '\na\nb\n'])
def test_dedent_compatibility(given):
assert dedent(given) == tw_dedent(given)
assert dedent(given, strip_nl='b') == tw_dedent(given).strip()
assert dedent(given, strip_nl='l') == tw_dedent(given).lstrip()
assert dedent(given, strip_nl='t') == tw_dedent(given).rstrip()
assert dedent(given, strip_nl='s') == tw_dedent(given).lstrip()
assert dedent(given, strip_nl='e') == tw_dedent(given).rstrip()
assert dedent(given, strip_nl='r') == tw_dedent(given).rstrip()
assert dedent(given, strip_nl=True) == tw_dedent(given).strip()
assert dedent(given, strip_nl=False) == tw_dedent(given)

given = '''
◊ Diaspar
Lys
'''
assert dedent(given, bolm='◊', strip_nl='b') == " Diaspar\n Lys"

given = '\nDiaspar\nLys\n'
assert dedent(given, strip_nl='b', wrap=True) == "Diaspar Lys"


def test_render_tuples():
Expand All @@ -1821,6 +1838,96 @@ def test_render_tuples():
assert render((0,1)) == '(0, 1)'
assert render((0,1,2)) == '(0, 1, 2)'

def test_tree():
data = data = dict(
str = 'text',
mlstr = dedent("""
line 1
line 2
""").strip(),
alist = [1, 2, 3],
adict = {1: 'one', 2: 'two', 3: 'three', 'four': None}
)
result = tree(data, squeeze=True)
expected = dedent('''
str: text
mlstr: line 1
line 2
alist
├── 1
├── 2
└── 3
adict
├── 1: one
├── 2: two
├── 3: three
└── four
''').strip()
assert result == expected

result = tree(data)
expected = dedent('''
str
└── text
mlstr
└── line 1
line 2
alist
├── 1
├── 2
└── 3
adict
├── 1
│   └── one
├── 2
│   └── two
├── 3
│   └── three
└── four
''').strip()
assert result == expected

result = tree(dict(root=data), squeeze=True)
expected = dedent('''
root
├── str: text
├── mlstr: line 1
│    line 2
├── alist
│   ├── 1
│   ├── 2
│   └── 3
└── adict
    ├── 1: one
    ├── 2: two
    ├── 3: three
    └── four
''').strip()
assert result == expected

result = tree(dict(root=data))
expected = dedent('''
root
├── str
│   └── text
├── mlstr
│   └── line 1
│    line 2
├── alist
│   ├── 1
│   ├── 2
│   └── 3
└── adict
    ├── 1
    │   └── one
    ├── 2
    │   └── two
    ├── 3
    │   └── three
    └── four
''').strip()
assert result == expected


if __name__ == '__main__':
# As a debugging aid allow the tests to be run on their own, outside pytest.
Expand Down

0 comments on commit 0e889d6

Please sign in to comment.