status: reduce public method count
[git-cola.git] / test / textwrap_test.py
blobec02da49535191fbadd12f11d455d424d0dadd4c
1 #!/usr/bin/env python
3 from __future__ import absolute_import, division, unicode_literals
5 import unittest
6 from cola import textwrap
9 class WordWrapTestCase(unittest.TestCase):
10 def setUp(self):
11 self.tabwidth = 8
12 self.limit = None
14 def wrap(self, text, break_on_hyphens=True):
15 return textwrap.word_wrap(text, self.tabwidth, self.limit,
16 break_on_hyphens=break_on_hyphens)
18 def test_word_wrap(self):
19 self.limit = 16
20 text = """
21 12345678901 3 56 8 01 3 5 7
23 1 3 5"""
24 expect = """
25 12345678901 3 56
26 8 01 3 5 7
28 1 3 5"""
29 self.assertEqual(expect, self.wrap(text))
31 def test_word_wrap_dashes(self):
32 self.limit = 4
33 text = '123-5'
34 expect = '123-5'
35 self.assertEqual(expect, self.wrap(text))
37 def test_word_wrap_leading_spaces(self):
38 self.limit = 4
39 expect = '1234\n5'
41 self.assertEqual(expect, self.wrap('1234 5'))
42 self.assertEqual(expect, self.wrap('1234 5'))
43 self.assertEqual(expect, self.wrap('1234 5'))
44 self.assertEqual(expect, self.wrap('1234 5'))
45 self.assertEqual(expect, self.wrap('1234 5'))
47 expect = '123\n4'
48 self.assertEqual(expect, self.wrap('123 4'))
49 self.assertEqual(expect, self.wrap('123 4'))
50 self.assertEqual(expect, self.wrap('123 4'))
51 self.assertEqual(expect, self.wrap('123 4'))
52 self.assertEqual(expect, self.wrap('123 4'))
54 def test_word_wrap_double_dashes(self):
55 self.limit = 4
56 text = '12--5'
57 expect = '12--\n5'
58 self.assertEqual(expect, self.wrap(text, break_on_hyphens=True))
60 expect = '12--5'
61 self.assertEqual(expect, self.wrap(text, break_on_hyphens=False))
63 def test_word_wrap_many_lines(self):
64 self.limit = 2
65 text = """
69 bb cc dd"""
70 expect = """
76 dd"""
77 self.assertEqual(expect, self.wrap(text))
79 def test_word_python_code(self):
80 self.limit = 78
81 text = """
82 if True:
83 print "hello world"
84 else:
85 print "hello world"
87 """
88 self.assertEqual(text, self.wrap(text))
90 def test_word_wrap_spaces(self):
91 self.limit = 2
92 text = ' ' * 6
93 self.assertEqual('', self.wrap(text))
95 def test_word_wrap_special_tag(self):
96 self.limit = 2
97 text = """
98 This test is so meta, even this sentence
100 Cheered-on-by: Avoids word-wrap
101 C.f. This also avoids word-wrap
102 References: This also avoids word-wrap
103 See-also: This also avoids word-wrap
104 Related-to: This also avoids word-wrap
105 Link: This also avoids word-wrap
108 expect = """
109 This
110 test
113 meta,
114 even
115 this
116 sentence
118 Cheered-on-by: Avoids word-wrap
119 C.f. This also avoids word-wrap
120 References: This also avoids word-wrap
121 See-also: This also avoids word-wrap
122 Related-to: This also avoids word-wrap
123 Link: This also avoids word-wrap
126 self.assertEqual(self.wrap(text), expect)
128 def test_word_wrap_space_at_start_of_wrap(self):
129 inputs = """0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 """
130 expect = """0 1 2 3 4 5 6 7 8 9\n0 1 2 3 4 5 6 7 8"""
131 self.limit = 20
132 actual = self.wrap(inputs)
133 self.assertEqual(expect, actual)
135 def test_word_wrap_keeps_tabs_at_start(self):
136 inputs = """\tfirst line\n\n\tsecond line"""
137 expect = """\tfirst line\n\n\tsecond line"""
138 self.limit = 20
139 actual = self.wrap(inputs)
140 self.assertEqual(expect, actual)
142 def test_word_wrap_keeps_twospace_indents(self):
143 inputs = """first line\n\n* branch:\n line1\n line2\n"""
144 expect = """first line\n\n* branch:\n line1\n line2\n"""
145 self.limit = 20
146 actual = self.wrap(inputs)
147 self.assertEqual(expect, actual)
149 def test_word_wrap_ranges(self):
150 text = 'a bb ccc dddd\neeeee'
151 expect = 'a\nbb\nccc\ndddd\neeeee'
152 actual = textwrap.word_wrap(text, 8, 2)
153 self.assertEqual(expect, actual)
155 expect = 'a bb\nccc\ndddd\neeeee'
156 actual = textwrap.word_wrap(text, 8, 4)
157 self.assertEqual(expect, actual)
159 text = 'a bb ccc dddd\n\teeeee'
160 expect = 'a bb\nccc\ndddd\n\t\neeeee'
161 actual = textwrap.word_wrap(text, 8, 4)
162 self.assertEqual(expect, actual)
165 if __name__ == '__main__':
166 unittest.main()