""" test_codewrap - code wrapping and filling tests @copyright: (c) 2006 by Nir Soffer @license: GNU GPL, see COPYING for details. """ import unittest import codewrap class code_wrapping(unittest.TestCase): def setUp(self): self.wrapper = codewrap.CodeWrapper(width=72) def test_fill_short_text(self): test = 'text line without newline' result = self.wrapper.fill(test) self.assertEqual(result, test) def test_fill_short_text_preserving_trailing_newline(self): test = 'text line without newline\n' result = self.wrapper.fill(test) self.assertEqual(result, test) def test_fill_text(self): test = """\ This long line is too long and should be wraped. This long line is too long and should be wraped. """ expected = """\ This long line is too long and should be wraped. This long line is too long and should be wraped. """ result = self.wrapper.fill(test) self.assertEqual(result, expected) def test_fill_indented_text(self): test = """\ This long line is too long and should be wraped. This long line is too long and should be wraped. """ expected = """\ This long line is too long and should be wraped. This long line is too long and should be wraped. """ result = self.wrapper.fill(test) self.assertEqual(result, expected) def test_fill_comment(self): test = """\ # This long comment should be wrapped to a comment using multiple lines, not comments with some text bellow, like most dumb editors are doing. """ expected = """\ # This long comment should be wrapped to a comment using multiple lines, # not comments with some text bellow, like most dumb editors are doing. """ result = self.wrapper.fill(test) self.assertEqual(result, expected) def test_fill_indented_comment(self): test = """\ # This long comment should be wrapped to a comment using multiple lines, not comments with some text bellow, like most dumb editors are doing. """ expected = """\ # This long comment should be wrapped to a comment using multiple # lines, not comments with some text bellow, like most dumb editors # are doing. """ result = self.wrapper.fill(test) self.assertEqual(result, expected) def test_fill_comment_with_multiple_comments_characters(self): test = """\ #### This long comment should be wrapped to a comment using multiple lines, not comments with some text bellow, like most dumb editors are doing. """ expected = """\ #### This long comment should be wrapped to a comment using multiple #### lines, not comments with some text bellow, like most dumb #### editors are doing. """ result = self.wrapper.fill(test) self.assertEqual(result, expected) def test_fill_comment_with_leading_whitespace(self): test = """\ # This long comment should be wrapped to a comment using multiple lines, not comments with some text bellow, like most dumb editors are doing. """ expected = """\ # This long comment should be wrapped to a comment using multiple # lines, not comments with some text bellow, like most dumb # editors are doing. """ result = self.wrapper.fill(test) self.assertEqual(result, expected) def test_fill_comment_ignoring_inline_comments_characters(self): """ Only comments characters right after indent are used when refilling. """ test = """\ # This long comment should be wrapped, but this # and this ## should # not be treated as comment. """ expected = """\ # This long comment should be wrapped, but this # and # this ## should not be treated as comment. """ self.wrapper.width = 60 result = self.wrapper.fill(test) self.assertEqual(result, expected) def test_c_style_comment(self): test = """\ // This long comment should be wrapped to a comment using multiple lines, not comments with some text bellow, like most dumb editors are doing. """ # hard wrpped text using 60 chars width expected = """\ // This long comment should be wrapped to a comment // using multiple lines, not comments with some text // bellow, like most dumb editors are doing. """ self.wrapper.width = 60 result = self.wrapper.fill(test) self.assertEqual(result, expected) def test_lisp_style_comment(self): test = """\ ;; This long comment should be wrapped to a comment using multiple lines, not comments with some text bellow, like most dumb editors are doing. """ # hard wrpped text using 60 chars width expected = """\ ;; This long comment should be wrapped to a comment ;; using multiple lines, not comments with some text ;; bellow, like most dumb editors are doing. """ self.wrapper.width = 60 result = self.wrapper.fill(test) self.assertEqual(result, expected) def test_refill_wrapped_comment(self): """ When handling hard wraped comment, when each line starts with a comment character, the text sould first be converted to one line comment, then filled again with new fill width value. """ # hard wrpped text using 78 chars width test = """\ # This long comment should be wrapped to a comment using multiple lines, # not comments with some text bellow, like most dumb editors are doing. """ # hard wrpped text using 60 chars width expected = """\ # This long comment should be wrapped to a comment using # multiple lines, not comments with some text bellow, # like most dumb editors are doing. """ self.wrapper.width = 60 result = self.wrapper.fill(test) self.assertEqual(result, expected) def test_fill_multiple_paragraphs(self): test = """\ # This long comment should be wrapped to a comment using multiple lines, not comments with some text bellow, like most dumb editors are doing. # This long comment should be wrapped to a comment using multiple lines, not comments with some text bellow, like most dumb editors are doing. """ expected = """\ # This long comment should be wrapped to a comment using multiple # lines, not comments with some text bellow, like most dumb editors # are doing. # This long comment should be wrapped to a comment using multiple # lines, not comments with some text bellow, like most dumb editors # are doing. """ result = self.wrapper.fillParagraphs(test) self.assertEqual(result, expected) def test_fill_paragraphs_preserving_paragraph_separators(self): test = """\ # This long comment should be wrapped to a comment using multiple lines, not comments with some text bellow, like most dumb editors are doing. # This long comment should be wrapped to a comment using multiple lines, not comments with some text bellow, like most dumb editors are doing. """ expected = """\ # This long comment should be wrapped to a comment using multiple # lines, not comments with some text bellow, like most dumb editors # are doing. # This long comment should be wrapped to a comment using multiple # lines, not comments with some text bellow, like most dumb editors # are doing. """ result = self.wrapper.fillParagraphs(test) self.assertEqual(result, expected) def test_dewrap(self): test = 'line\nline' expected = 'line line' self.assertEqual(self.wrapper.dewrap(test), expected) def test_dewrap_preserving_trailing_newline(self): test = 'line\nline\n' expected = 'line line\n' self.assertEqual(self.wrapper.dewrap(test), expected) def test_dewrap_paragraphs(self): test = 'line\nline\n\nline\nline\n' expected = 'line line\n\nline line\n' self.assertEqual(self.wrapper.dewrapParagraphs(test), expected) def test_dewrap_paragraphs_preserving_whitespace(self): test = 'line\nline\n \n\nline\nline\n' expected = 'line line\n \n\nline line\n' self.assertEqual(self.wrapper.dewrapParagraphs(test), expected) class convenience_functions(unittest.TestCase): """ Minimal tests as the same is teste above Maybe the keywords should be tested also. """ text = """\ This long line is too long and should be wraped. This long line is too long and should be wraped. This long line is too long and should be wraped. This long line is too long and should be wraped. """ wrapped = """\ This long line is too long and should be wraped. This long line is too long and should be wraped. This long line is too long and should be wraped. This long line is too long and should be wraped. """ def test_fill_paragraphs(self): self.assertEqual(codewrap.fillParagraphs(self.text), self.wrapped) def test_dewrap_paragraphs(self): self.assertEqual(codewrap.dewrapParagraphs(self.wrapped), self.text) if __name__ == '__main__': unittest.main()