diff options
author | Evan Martin <martine@danga.com> | 2011-07-27 16:59:22 (GMT) |
---|---|---|
committer | Evan Martin <martine@danga.com> | 2011-07-27 17:03:16 (GMT) |
commit | ba3f2a416cab345bf357abc979a77fe80d7058fe (patch) | |
tree | 5c3f7a0e33d29fc719e3795a139fdf4ad9d52699 /misc | |
parent | f60aee4330413b4d8ccf36b4d12bbe2f8fdd2913 (diff) | |
download | Ninja-ba3f2a416cab345bf357abc979a77fe80d7058fe.zip Ninja-ba3f2a416cab345bf357abc979a77fe80d7058fe.tar.gz Ninja-ba3f2a416cab345bf357abc979a77fe80d7058fe.tar.bz2 |
ninja.py: fix the new test and pass the test
From a patch from Elazar Leibovich <elazarl@gmail.com>.
Diffstat (limited to 'misc')
-rw-r--r-- | misc/ninja.py | 26 | ||||
-rwxr-xr-x | misc/ninja_test.py | 23 |
2 files changed, 36 insertions, 13 deletions
diff --git a/misc/ninja.py b/misc/ninja.py index 38b13c9..2c9d818 100644 --- a/misc/ninja.py +++ b/misc/ninja.py @@ -55,12 +55,28 @@ class Writer(object): return outputs def _line(self, text, indent=0): + """Write 'text' word-wrapped at self.width characters.""" + leading_space = ' ' * indent while len(text) > self.width: - space = text.rfind(' ', 0, self.width - 4) - assert space != -1 # TODO: handle if no space found. - self.output.write(text[0:space] + ' $\n') - text = ' ' * (indent+2) + text[space:].lstrip() - self.output.write(text + '\n') + # The text is too wide; wrap if possible. + + # Find the rightmost space that would obey our width constraint. + available_space = self.width - len(leading_space) - len(' $') + space = text.rfind(' ', 0, available_space) + if space < 0: + # No such space; just use the first space we can find. + space = text.find(' ', available_space) + if space < 0: + # Give up on breaking. + break + + self.output.write(leading_space + text[0:space] + ' $\n') + text = text[space+1:] + + # Subsequent lines are continuations, so indent them. + leading_space = ' ' * (indent+2) + + self.output.write(leading_space + text + '\n') def _as_list(self, input): if input is None: diff --git a/misc/ninja_test.py b/misc/ninja_test.py index 555f189..44c6230 100755 --- a/misc/ninja_test.py +++ b/misc/ninja_test.py @@ -20,26 +20,33 @@ from StringIO import StringIO import ninja LONGWORD = 'a' * 10 +INDENT = ' ' class TestLineWordWrap(unittest.TestCase): def setUp(self): self.out = StringIO() - self.n = ninja.Writer(self.out, width=5) + self.n = ninja.Writer(self.out, width=8) def test_single_long_word(self): # We shouldn't wrap a single long word. self.n._line(LONGWORD) - self.assertEqual(LONGWORD, self.out.getvalue()) + self.assertEqual(LONGWORD + '\n', self.out.getvalue()) def test_few_long_words(self): # We should wrap a line where the second word is overlong. self.n._line(' '.join(['x', LONGWORD, 'y'])) - self.assertEqual('x \n' + LONGWORD + '\ny', self.out.getvalue()) - - def test_few_long_words_space(self): - # We should also wrap indented lines. - self.n._line(' '.join(['x', LONGWORD, 'y']), indent=2) - self.assertEqual('x \n ' + LONGWORD + '\n y', self.out.getvalue()) + self.assertEqual(' $\n'.join(['x', + INDENT + LONGWORD, + INDENT + 'y']) + '\n', + self.out.getvalue()) + + def test_few_long_words_indented(self): + # Check wrapping in the presence of indenting. + self.n._line(' '.join(['x', LONGWORD, 'y']), indent=1) + self.assertEqual(' $\n'.join([' ' + 'x', + ' ' + INDENT + LONGWORD, + ' ' + INDENT + 'y']) + '\n', + self.out.getvalue()) if __name__ == '__main__': unittest.main() |