1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
# Copyright 2013 Ben Longbons <b.r.longbons@gmail.com>
#
# This file is part of attoconf.
#
# attoconf is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# attoconf is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with attoconf. If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function, division, absolute_import
import unittest
from attoconf.help import Help, HelpSection, put_line_in_width
from cStringIO import StringIO
class TestHelpSection(unittest.TestCase):
def test_basic(self):
sec = HelpSection()
sec.add_text('foo', False)
sec.add_option('--foo', 'FOO', True)
sec.add_text('bar', True)
sec.add_option('--bar', 'BAR', False)
self.assertEqual(sec.headers, [(False, 'foo'), (True, 'bar')])
self.assertEqual(sec.options, [(True, '--foo', 'FOO'), (False, '--bar', 'BAR')])
out = StringIO()
sec.print(out, False, float('inf'))
self.assertEqual(out.getvalue(), '''foo
--bar BAR
''')
out = StringIO()
sec.print(out, True, float('inf'))
self.assertEqual(out.getvalue(), '''foo
bar
--foo FOO
--bar BAR
''')
def test_width(self):
out = StringIO()
put_line_in_width(out, 'foo bar baz', float('inf'), 0)
self.assertEqual(out.getvalue(), 'foo bar baz\n')
out = StringIO()
put_line_in_width(out, 'foo bar baz', 10, 0)
self.assertEqual(out.getvalue(), 'foo bar\nbaz\n')
out = StringIO()
put_line_in_width(out, 'foo bar baz', 10, 2)
self.assertEqual(out.getvalue(), 'foo bar\n baz\n')
out = StringIO()
put_line_in_width(out, ' foo bar baz', 10, 0)
self.assertEqual(out.getvalue(), ' foo bar\nbaz\n')
out = StringIO()
put_line_in_width(out, ' foo bar baz', 10, 0)
self.assertEqual(out.getvalue(), ' foo\nbar baz\n')
out = StringIO()
put_line_in_width(out, ' foo bar baz', 10, 3)
self.assertEqual(out.getvalue(), ' foo\n bar baz\n')
out = StringIO()
put_line_in_width(out, ' foo bar baz', 10, 4)
self.assertEqual(out.getvalue(), ' foo\n bar\n baz\n')
out = StringIO()
put_line_in_width(out, 'really-long-string', float('inf'), 0)
self.assertEqual(out.getvalue(), 'really-long-string\n')
out = StringIO()
put_line_in_width(out, ' really-long-string', float('inf'), 0)
self.assertEqual(out.getvalue(), ' really-long-string\n')
out = StringIO()
put_line_in_width(out, 'really-long-string', 10, 0)
self.assertEqual(out.getvalue(), 'really-long-string\n')
out = StringIO()
put_line_in_width(out, ' really-long-string', 10, 0)
self.assertEqual(out.getvalue(), ' really-long-string\n')
out = StringIO()
put_line_in_width(out, 'short really-long-string', float('inf'), 0)
self.assertEqual(out.getvalue(), 'short really-long-string\n')
out = StringIO()
put_line_in_width(out, ' short really-long-string', float('inf'), 0)
self.assertEqual(out.getvalue(), ' short really-long-string\n')
out = StringIO()
put_line_in_width(out, 'short really-long-string', 10, 0)
self.assertEqual(out.getvalue(), 'short\nreally-long-string\n')
out = StringIO()
put_line_in_width(out, ' short really-long-string', 10, 0)
self.assertEqual(out.getvalue(), ' short\nreally-long-string\n')
out = StringIO()
put_line_in_width(out, 'short really-long-string', 10, 1)
self.assertEqual(out.getvalue(), 'short\n really-long-string\n')
out = StringIO()
put_line_in_width(out, ' short really-long-string', 10, 1)
self.assertEqual(out.getvalue(), ' short\n really-long-string\n')
def test_print(self):
sec = HelpSection()
sec.add_option('--abcdefghijklmnopqrstuvwxyz',
'ABCDEFGHIJKLMNOPQRSTUVWXYZ', False)
out = StringIO()
sec.print(out, False, 80)
self.assertEqual(out.getvalue(), '''
--abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
'''[1:])
sec.add_option('--foo', 'FOO', False)
sec.add_option('--frob', 'FROB', False)
out = StringIO()
sec.print(out, False, 80)
self.assertEqual(out.getvalue(), '''
--abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
--foo FOO
--frob FROB
'''[1:])
out = StringIO()
sec.print(out, False, float('inf'))
self.assertEqual(out.getvalue(), '''
--abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ
--foo FOO
--frob FROB
'''[1:])
class TestHelp(unittest.TestCase):
def test_print(self):
help = Help()
help.add_option('--invisible', 'You can\'t see this', True)
help.add_text('General:', False)
help.add_option('--help', 'show this', False)
help.add_option('--version', 'show that', True)
help.add_text('Other stuff:', False)
help.add_text('with long header:', True)
help.add_option('--foo-lets-make-it-long-just-because', 'I\'m bored', False)
help.add_text('Visibility:', False)
help.add_option('--sneaky', 'This may be surprising. It also works as a demonstration of multi-line wrapping, just because.', True)
out = StringIO()
help.print(out, True, 80)
self.assertEqual(out.getvalue(), '''
--invisible You can't see this
General:
--help show this
--version show that
Other stuff:
with long header:
--foo-lets-make-it-long-just-because
I'm bored
Visibility:
--sneaky This may be surprising. It also works as a demonstration of
multi-line wrapping, just because.
'''[1:])
out = StringIO()
help.print(out, False, 80)
self.assertEqual(out.getvalue(), '''
General:
--help show this
Other stuff:
--foo-lets-make-it-long-just-because
I'm bored
'''[1:])
|