From 68e9450ef0451ef058cebbff3325458e8b5fd3d3 Mon Sep 17 00:00:00 2001 From: James G Sack Date: Sat, 5 Apr 2008 04:34:46 +0000 Subject: [PATCH] src/GrampsDbUtils/_WriteGedcom.py :#2008 fix bug in breakout() src/GrampsDbUtils/test/gedwrite_breakup_test.py : unittest for breakout svn: r10470 --- src/GrampsDbUtils/_WriteGedcom.py | 12 +++- .../test/gedwrite_breakup_test.py | 57 +++++++++++++++++++ 2 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 src/GrampsDbUtils/test/gedwrite_breakup_test.py diff --git a/src/GrampsDbUtils/_WriteGedcom.py b/src/GrampsDbUtils/_WriteGedcom.py index 7cd01e3e5..b36f30f74 100644 --- a/src/GrampsDbUtils/_WriteGedcom.py +++ b/src/GrampsDbUtils/_WriteGedcom.py @@ -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: diff --git a/src/GrampsDbUtils/test/gedwrite_breakup_test.py b/src/GrampsDbUtils/test/gedwrite_breakup_test.py new file mode 100644 index 000000000..ade11704b --- /dev/null +++ b/src/GrampsDbUtils/test/gedwrite_breakup_test.py @@ -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===