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
31
class TestCommandHelp(tests.TestCase):
32
"""Tests for help on commands."""
34
def test_command_help_includes_see_also(self):
35
class cmd_WithSeeAlso(commands.Command):
36
"""A sample command."""
37
_see_also = ['foo', 'bar']
38
cmd = cmd_WithSeeAlso()
39
helptext = cmd.get_help_text()
42
' -h, --help show help message\n'
44
'See also: bar, foo\n')
46
def test_get_help_text(self):
47
"""Commands have a get_help_text method which returns their help."""
48
class cmd_Demo(commands.Command):
49
"""A sample command."""
51
helptext = cmd.get_help_text()
52
self.assertStartsWith(helptext, 'usage:bzr Demo')
53
self.assertEndsWith(helptext, 'show help message\n')
55
def test_command_with_additional_see_also(self):
56
class cmd_WithSeeAlso(commands.Command):
57
"""A sample command."""
58
_see_also = ['foo', 'bar']
59
cmd = cmd_WithSeeAlso()
60
helptext = cmd.get_help_text(['gam'])
63
' -h, --help show help message\n'
65
'See also: bar, foo, gam\n')
67
def test_command_only_additional_see_also(self):
68
class cmd_WithSeeAlso(commands.Command):
69
"""A sample command."""
70
cmd = cmd_WithSeeAlso()
71
helptext = cmd.get_help_text(['gam'])
74
' -h, --help show help message\n'
79
class TestRegisteredTopic(tests.TestCase):
80
"""Tests for the RegisteredTopic class."""
82
def test_contruct(self):
83
"""Construction takes the help topic name for the registered item."""
85
self.assertTrue('basic' in help_topics.topic_registry)
86
topic = help_topics.RegisteredTopic('basic')
87
self.assertEqual('basic', topic.topic)
89
def test_get_help_text(self):
90
"""A RegisteredTopic returns the get_detail results for get_help_text."""
91
topic = help_topics.RegisteredTopic('commands')
92
self.assertEqual(help_topics.topic_registry.get_detail('commands'),
93
topic.get_help_text())
96
class TestTopicIndex(tests.TestCase):
97
"""Tests for the HelpTopicIndex class."""
99
def test_default_constructable(self):
100
index = help_topics.HelpTopicIndex()
102
def test_get_topics_None(self):
103
"""Searching for None returns the basic help topic."""
104
index = help_topics.HelpTopicIndex()
105
topics = index.get_topics(None)
106
self.assertEqual(1, len(topics))
107
self.assertIsInstance(topics[0], help_topics.RegisteredTopic)
108
self.assertEqual('basic', topics[0].topic)
110
def test_get_topics_topics(self):
111
"""Searching for a string returns the matching string."""
112
index = help_topics.HelpTopicIndex()
113
topics = index.get_topics('topics')
114
self.assertEqual(1, len(topics))
115
self.assertIsInstance(topics[0], help_topics.RegisteredTopic)
116
self.assertEqual('topics', topics[0].topic)
118
def test_get_topics_no_topic(self):
119
"""Searching for something not registered returns []."""
120
index = help_topics.HelpTopicIndex()
121
self.assertEqual([], index.get_topics('nothing by this name'))
123
def test_prefix(self):
124
"""TopicIndex has a prefix of ''."""
125
index = help_topics.HelpTopicIndex()
126
self.assertEqual('', index.prefix)
129
class TestCommandIndex(tests.TestCase):
130
"""Tests for the HelpCommandIndex class."""
132
def test_default_constructable(self):
133
index = commands.HelpCommandIndex()
135
def test_get_topics_None(self):
136
"""Searching for None returns an empty list."""
137
index = commands.HelpCommandIndex()
138
self.assertEqual([], index.get_topics(None))
140
def test_get_topics_rocks(self):
141
"""Searching for 'rocks' returns the cmd_rocks command instance."""
142
index = commands.HelpCommandIndex()
143
topics = index.get_topics('rocks')
144
self.assertEqual(1, len(topics))
145
self.assertIsInstance(topics[0], builtins.cmd_rocks)
147
def test_get_topics_no_topic(self):
148
"""Searching for something that is not a command returns []."""
149
index = commands.HelpCommandIndex()
150
self.assertEqual([], index.get_topics('nothing by this name'))
152
def test_prefix(self):
153
"""CommandIndex has a prefix of 'commands/'."""
154
index = commands.HelpCommandIndex()
155
self.assertEqual('commands/', index.prefix)
157
def test_get_topic_with_prefix(self):
158
"""Searching for commands/rocks returns the rocks command object."""
159
index = commands.HelpCommandIndex()
160
topics = index.get_topics('commands/rocks')
161
self.assertEqual(1, len(topics))
162
self.assertIsInstance(topics[0], builtins.cmd_rocks)
165
class TestHelpIndices(tests.TestCase):
166
"""Tests for the HelpIndices class."""
168
def test_default_search_path(self):
169
"""The default search path should include internal indexs."""
170
indices = help.HelpIndices()
171
self.assertEqual(2, len(indices.search_path))
172
# help topics should be searched in first.
173
self.assertIsInstance(indices.search_path[0],
174
help_topics.HelpTopicIndex)
175
# with commands being search second.
176
self.assertIsInstance(indices.search_path[1],
177
commands.HelpCommandIndex)
179
def test_search_for_unknown_topic_raises(self):
180
"""Searching for an unknown topic should raise NoHelpTopic."""
181
indices = help.HelpIndices()
182
indices.search_path = []
183
error = self.assertRaises(errors.NoHelpTopic, indices.search, 'foo')
184
self.assertEqual('foo', error.topic)
186
def test_search_calls_get_topic(self):
187
"""Searching should call get_topics in all indexes in order."""
189
class RecordingIndex(object):
190
def __init__(self, name):
192
def get_topics(self, topic):
193
calls.append(('get_topics', self.prefix, topic))
195
index = help.HelpIndices()
196
index.search_path = [RecordingIndex('1'), RecordingIndex('2')]
200
('get_topics', '1', None),
201
('get_topics', '2', None),
208
('get_topics', '1', 'bar'),
209
('get_topics', '2', 'bar'),
213
def test_search_returns_index_and_results(self):
214
"""Searching should return help topics with their index"""
215
class CannedIndex(object):
216
def __init__(self, prefix, search_result):
218
self.result = search_result
219
def get_topics(self, topic):
221
index = help.HelpIndices()
222
index_one = CannedIndex('1', ['a'])
223
index_two = CannedIndex('2', ['b', 'c'])
224
index.search_path = [index_one, index_two]
225
self.assertEqual([(index_one, 'a'), (index_two, 'b'), (index_two, 'c')],
228
def test_search_checks_for_duplicate_prefixes(self):
229
"""Its an error when there are multiple indices with the same prefix."""
230
indices = help.HelpIndices()
231
indices.search_path = [help_topics.HelpTopicIndex(),
232
help_topics.HelpTopicIndex()]
233
self.assertRaises(errors.DuplicateHelpPrefix, indices.search, None)