src/GrampsDbUtils/_WriteGedcom.py :#2008 fix bug in breakout()

src/GrampsDbUtils/test/gedwrite_breakup_test.py : unittest for breakout



svn: r10470
This commit is contained in:
James G Sack 2008-04-05 04:34:46 +00:00
parent 2d663ce2ee
commit 68e9450ef0
2 changed files with 66 additions and 3 deletions

View File

@ -252,11 +252,17 @@ def breakup(txt, limit):
maximum length specified, while breaking words in the middle of a word
to avoid issues with spaces.
"""
if limit < 1:
raise ValueError("breakup: unexpected limit: %r" % limit)
data = []
while limit < len(txt)+1:
idx = limit-1
while txt[idx-1].isspace() or txt[idx].isspace() :
while len(txt) > limit:
# look for non-space pair to break between
idx = limit
while idx>0 and (txt[idx-1].isspace() or txt[idx].isspace()):
idx -= 1
if idx == 0:
#no words to break on, just break at limit anyway
idx = limit
data.append(txt[:idx])
txt = txt[idx:]
if len(txt) > 0:

View File

@ -0,0 +1,57 @@
"""gedwrite_breakup_test tests the breakup function in GedcomWrite"""
import unittest as U
from test.test_util import msg, path_append_parent
path_append_parent();
import _WriteGedcom as WG
class Test1(U.TestCase):
def test1a_common(s):
#txt, limit, [split-results]
L=4
dat = (
##0123456
("a", L, ["a",]),
("abc", L, ["abc",]),
("abcd", L, ["abcd",]),
("abcde", L, ["abcd","e"]),
("1234567", L, ["1234","567"]),
("12345678", L, ["1234","5678"]),
("123456789", L, ["1234","5678", "9"]),
)
for (t,l,r) in dat:
g = WG.breakup(t,l)
s.assertEquals(g,r, msg(g,r, "breakup(%r,%d) results" % (t,l)))
def test1b_spaces(s):
#txt, limit, [split-results]
L=4
dat = (
##0123456
("a b ", L, ["a b ",]),
(" a b", L, [" a b",]),
("asdf g", L, ["asd", "f g"]),
(" z", L, [" ", "z"]),
(" z", L, [" ", " z"]),
(" A", 2, [" ", " ", " ", "A"]),
)
for (t,l,r) in dat:
g = WG.breakup(t,l)
s.assertEquals(g,r, msg(g,r, "breakup(%r,%d) results" % (t,l)))
def test1c_unusual(s):
#just documenting behavior for unlikely input
#txt, limit, [split-results]
dat = (
##0123456
("", 4, []),
("xyz", 1, ["x", "y", "z"]),
)
for (t,l,r) in dat:
g = WG.breakup(t,l)
s.assertEquals(g,r, msg(g,r, "breakup(%r,%d) results" % (t,l)))
s.assertRaises(ValueError, WG.breakup, "xy",0)
if __name__ == "__main__":
U.main()
#===eof===