Skip to content

Commit

Permalink
Fix bug in wraplines
Browse files Browse the repository at this point in the history
A long string without spaces would cause infinite recursion because
the space at the beginning would always be found.

Now the function is tested thoroughly.
  • Loading branch information
Zaharid committed Sep 19, 2017
1 parent 76c5623 commit 6798d2d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/reportengine/helputils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,20 @@ def _indent():

def wraplines(txt):
for line in txt.splitlines(keepends=True):
line = "%s%s" % (next(indent), line)
ni = next(indent)
line = f"{ni}{line}"
if len(line) >= width:
break_space = line[:width].rfind(' ')
#Break by the last space before the with limit
break_space = line.rfind(' ', len(ni), width)
if break_space == -1:
yield line
else:
yield line[:break_space] + '\n'
#+1 removes the marching space
yield from wraplines(line[break_space+1:])
#If not possible, break by the first space afer the limit
break_space = line.find(' ', width)
if break_space == -1:
yield line
continue
yield line[:break_space] + '\n'
#+1 removes the marching space
yield from wraplines(line[break_space+1:])
else:
yield line
return list(wraplines(txt))
Expand Down
16 changes: 16 additions & 0 deletions src/reportengine/tests/test_helputils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import re

from hypothesis import given
from hypothesis.strategies import text

from reportengine import helputils

@given(text(min_size=1, max_size=1000))
def test_sane_wrap(txt):
ind = ' '
w = 70
res = helputils.sane_fill(txt, initial_indent=ind, width=w)
assert res.startswith(ind)
assert re.sub(r'\s', '', txt) == re.sub(r'\s', '', res)
for line in res.splitlines():
assert line.rfind(' ') < w

0 comments on commit 6798d2d

Please sign in to comment.