1
# Copyright (C) 2007 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Unit tests for the bzrlib.help module."""
19
from cStringIO import StringIO
32
class TestCommandHelp(tests.TestCase):
33
"""Tests for help on commands."""
35
def test_command_help_includes_see_also(self):
36
class cmd_WithSeeAlso(commands.Command):
37
"""A sample command."""
38
_see_also = ['foo', 'bar']
39
cmd = cmd_WithSeeAlso()
40
helptext = cmd.get_help_text()
43
' -v, --verbose Display more information.\n'
44
' -q, --quiet Only display errors and warnings.\n'
45
' -h, --help Show help message.\n'
47
'See also: bar, foo\n')
49
def test_get_help_text(self):
50
"""Commands have a get_help_text method which returns their help."""
51
class cmd_Demo(commands.Command):
52
"""A sample command."""
54
helptext = cmd.get_help_text()
55
self.assertStartsWith(helptext,
56
'Purpose: A sample command.\n'
58
self.assertEndsWith(helptext,
59
' -h, --help Show help message.\n\n')
61
def test_command_with_additional_see_also(self):
62
class cmd_WithSeeAlso(commands.Command):
63
"""A sample command."""
64
_see_also = ['foo', 'bar']
65
cmd = cmd_WithSeeAlso()
66
helptext = cmd.get_help_text(['gam'])
69
' -v, --verbose Display more information.\n'
70
' -q, --quiet Only display errors and warnings.\n'
71
' -h, --help Show help message.\n'
73
'See also: bar, foo, gam\n')
75
def test_command_only_additional_see_also(self):
76
class cmd_WithSeeAlso(commands.Command):
77
"""A sample command."""
78
cmd = cmd_WithSeeAlso()
79
helptext = cmd.get_help_text(['gam'])
82
' -v, --verbose Display more information.\n'
83
' -q, --quiet Only display errors and warnings.\n'
84
' -h, --help Show help message.\n'
88
def test_get_help_topic(self):
89
"""The help topic for a Command is its name()."""
90
class cmd_foo_bar(commands.Command):
91
"""A sample command."""
93
self.assertEqual(cmd.name(), cmd.get_help_topic())
95
def test_formatted_help_text(self):
96
"""Help text should be plain text by default."""
97
class cmd_Demo(commands.Command):
110
helptext = cmd.get_help_text()
113
'Purpose: A sample command.\n'
117
' -v, --verbose Display more information.\n'
118
' -q, --quiet Only display errors and warnings.\n'
119
' -h, --help Show help message.\n'
130
helptext = cmd.get_help_text(plain=False)
131
self.assertEquals(helptext,
132
':Purpose: A sample command.\n'
136
' -v, --verbose Display more information.\n'
137
' -q, --quiet Only display errors and warnings.\n'
138
' -h, --help Show help message.\n'
150
def test_help_text_custom_usage(self):
151
"""Help text may contain a custom usage section."""
152
class cmd_Demo(commands.Command):
163
helptext = cmd.get_help_text()
164
self.assertEquals(helptext,
165
'Purpose: A sample command.\n'
167
' cmd Demo [opts] args\n'
173
' -v, --verbose Display more information.\n'
174
' -q, --quiet Only display errors and warnings.\n'
175
' -h, --help Show help message.\n'
178
' Blah blah blah.\n\n')
181
class TestRegisteredTopic(tests.TestCase):
182
"""Tests for the RegisteredTopic class."""
184
def test_contruct(self):
185
"""Construction takes the help topic name for the registered item."""
187
self.assertTrue('basic' in help_topics.topic_registry)
188
topic = help_topics.RegisteredTopic('basic')
189
self.assertEqual('basic', topic.topic)
191
def test_get_help_text(self):
192
"""A RegisteredTopic returns the get_detail results for get_help_text."""
193
topic = help_topics.RegisteredTopic('commands')
194
self.assertEqual(help_topics.topic_registry.get_detail('commands'),
195
topic.get_help_text())
197
def test_get_help_text_with_additional_see_also(self):
198
topic = help_topics.RegisteredTopic('commands')
200
topic.get_help_text(['foo', 'bar']),
202
'See also: bar, foo\n')
204
def test_get_help_topic(self):
205
"""The help topic for a RegisteredTopic is its topic from construction."""
206
topic = help_topics.RegisteredTopic('foobar')
207
self.assertEqual('foobar', topic.get_help_topic())
208
topic = help_topics.RegisteredTopic('baz')
209
self.assertEqual('baz', topic.get_help_topic())
212
class TestTopicIndex(tests.TestCase):
213
"""Tests for the HelpTopicIndex class."""
215
def test_default_constructable(self):
216
index = help_topics.HelpTopicIndex()
218
def test_get_topics_None(self):
219
"""Searching for None returns the basic help topic."""
220
index = help_topics.HelpTopicIndex()
221
topics = index.get_topics(None)
222
self.assertEqual(1, len(topics))
223
self.assertIsInstance(topics[0], help_topics.RegisteredTopic)
224
self.assertEqual('basic', topics[0].topic)
226
def test_get_topics_topics(self):
227
"""Searching for a string returns the matching string."""
228
index = help_topics.HelpTopicIndex()
229
topics = index.get_topics('topics')
230
self.assertEqual(1, len(topics))
231
self.assertIsInstance(topics[0], help_topics.RegisteredTopic)
232
self.assertEqual('topics', topics[0].topic)
234
def test_get_topics_no_topic(self):
235
"""Searching for something not registered returns []."""
236
index = help_topics.HelpTopicIndex()
237
self.assertEqual([], index.get_topics('nothing by this name'))
239
def test_prefix(self):
240
"""TopicIndex has a prefix of ''."""
241
index = help_topics.HelpTopicIndex()
242
self.assertEqual('', index.prefix)
245
class TestCommandIndex(tests.TestCase):
246
"""Tests for the HelpCommandIndex class."""
248
def test_default_constructable(self):
249
index = commands.HelpCommandIndex()
251
def test_get_topics_None(self):
252
"""Searching for None returns an empty list."""
253
index = commands.HelpCommandIndex()
254
self.assertEqual([], index.get_topics(None))
256
def test_get_topics_rocks(self):
257
"""Searching for 'rocks' returns the cmd_rocks command instance."""
258
index = commands.HelpCommandIndex()
259
topics = index.get_topics('rocks')
260
self.assertEqual(1, len(topics))
261
self.assertIsInstance(topics[0], builtins.cmd_rocks)
263
def test_get_topics_no_topic(self):
264
"""Searching for something that is not a command returns []."""
265
index = commands.HelpCommandIndex()
266
self.assertEqual([], index.get_topics('nothing by this name'))
268
def test_prefix(self):
269
"""CommandIndex has a prefix of 'commands/'."""
270
index = commands.HelpCommandIndex()
271
self.assertEqual('commands/', index.prefix)
273
def test_get_topic_with_prefix(self):
274
"""Searching for commands/rocks returns the rocks command object."""
275
index = commands.HelpCommandIndex()
276
topics = index.get_topics('commands/rocks')
277
self.assertEqual(1, len(topics))
278
self.assertIsInstance(topics[0], builtins.cmd_rocks)
281
class TestHelpIndices(tests.TestCase):
282
"""Tests for the HelpIndices class."""
284
def test_default_search_path(self):
285
"""The default search path should include internal indexs."""
286
indices = help.HelpIndices()
287
self.assertEqual(3, len(indices.search_path))
288
# help topics should be searched in first.
289
self.assertIsInstance(indices.search_path[0],
290
help_topics.HelpTopicIndex)
291
# with commands being search second.
292
self.assertIsInstance(indices.search_path[1],
293
commands.HelpCommandIndex)
294
# and plugins are a third index.
295
self.assertIsInstance(indices.search_path[2],
296
plugin.PluginsHelpIndex)
298
def test_search_for_unknown_topic_raises(self):
299
"""Searching for an unknown topic should raise NoHelpTopic."""
300
indices = help.HelpIndices()
301
indices.search_path = []
302
error = self.assertRaises(errors.NoHelpTopic, indices.search, 'foo')
303
self.assertEqual('foo', error.topic)
305
def test_search_calls_get_topic(self):
306
"""Searching should call get_topics in all indexes in order."""
308
class RecordingIndex(object):
309
def __init__(self, name):
311
def get_topics(self, topic):
312
calls.append(('get_topics', self.prefix, topic))
314
index = help.HelpIndices()
315
index.search_path = [RecordingIndex('1'), RecordingIndex('2')]
319
('get_topics', '1', None),
320
('get_topics', '2', None),
327
('get_topics', '1', 'bar'),
328
('get_topics', '2', 'bar'),
332
def test_search_returns_index_and_results(self):
333
"""Searching should return help topics with their index"""
334
class CannedIndex(object):
335
def __init__(self, prefix, search_result):
337
self.result = search_result
338
def get_topics(self, topic):
340
index = help.HelpIndices()
341
index_one = CannedIndex('1', ['a'])
342
index_two = CannedIndex('2', ['b', 'c'])
343
index.search_path = [index_one, index_two]
344
self.assertEqual([(index_one, 'a'), (index_two, 'b'), (index_two, 'c')],
347
def test_search_checks_for_duplicate_prefixes(self):
348
"""Its an error when there are multiple indices with the same prefix."""
349
indices = help.HelpIndices()
350
indices.search_path = [help_topics.HelpTopicIndex(),
351
help_topics.HelpTopicIndex()]
352
self.assertRaises(errors.DuplicateHelpPrefix, indices.search, None)