/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_help.py

  • Committer: Robert Collins
  • Date: 2007-04-20 03:54:06 UTC
  • mto: This revision was merged to the branch mainline in revision 2441.
  • Revision ID: robertc@robertcollins.net-20070420035406-e68xf089otkpo7xx
Teach Command.get_help_text to show additional help cross references when supplied.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007 Canonical Ltd
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
16
 
 
17
"""Unit tests for the bzrlib.help module."""
 
18
 
 
19
from cStringIO import StringIO
 
20
 
 
21
from bzrlib import (
 
22
    builtins,
 
23
    commands,
 
24
    errors,
 
25
    help,
 
26
    help_topics,
 
27
    tests,
 
28
    )
 
29
 
 
30
 
 
31
class TestCommandHelp(tests.TestCase):
 
32
    """Tests for help on commands."""
 
33
 
 
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()
 
40
        self.assertEndsWith(
 
41
            helptext,
 
42
            '  -h, --help  show help message\n'
 
43
            '\n'
 
44
            'See also: bar, foo\n')
 
45
 
 
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."""
 
50
        cmd = cmd_Demo()
 
51
        helptext = cmd.get_help_text()
 
52
        self.assertStartsWith(helptext, 'usage:bzr Demo')
 
53
        self.assertEndsWith(helptext, 'show help message\n')
 
54
 
 
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'])
 
61
        self.assertEndsWith(
 
62
            helptext,
 
63
            '  -h, --help  show help message\n'
 
64
            '\n'
 
65
            'See also: bar, foo, gam\n')
 
66
 
 
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'])
 
72
        self.assertEndsWith(
 
73
            helptext,
 
74
            '  -h, --help  show help message\n'
 
75
            '\n'
 
76
            'See also: gam\n')
 
77
    
 
78
 
 
79
class TestRegisteredTopic(tests.TestCase):
 
80
    """Tests for the RegisteredTopic class."""
 
81
 
 
82
    def test_contruct(self):
 
83
        """Construction takes the help topic name for the registered item."""
 
84
        # validate our test 
 
85
        self.assertTrue('basic' in help_topics.topic_registry)
 
86
        topic = help_topics.RegisteredTopic('basic')
 
87
        self.assertEqual('basic', topic.topic)
 
88
 
 
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())
 
94
 
 
95
 
 
96
class TestTopicIndex(tests.TestCase):
 
97
    """Tests for the HelpTopicIndex class."""
 
98
 
 
99
    def test_default_constructable(self):
 
100
        index = help_topics.HelpTopicIndex()
 
101
 
 
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)
 
109
 
 
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)
 
117
 
 
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'))
 
122
 
 
123
    def test_prefix(self):
 
124
        """TopicIndex has a prefix of ''."""
 
125
        index = help_topics.HelpTopicIndex()
 
126
        self.assertEqual('', index.prefix)
 
127
 
 
128
 
 
129
class TestCommandIndex(tests.TestCase):
 
130
    """Tests for the HelpCommandIndex class."""
 
131
 
 
132
    def test_default_constructable(self):
 
133
        index = commands.HelpCommandIndex()
 
134
 
 
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))
 
139
 
 
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)
 
146
 
 
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'))
 
151
 
 
152
    def test_prefix(self):
 
153
        """CommandIndex has a prefix of 'commands/'."""
 
154
        index = commands.HelpCommandIndex()
 
155
        self.assertEqual('commands/', index.prefix)
 
156
 
 
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)
 
163
 
 
164
 
 
165
class TestHelpIndices(tests.TestCase):
 
166
    """Tests for the HelpIndices class."""
 
167
 
 
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)
 
178
 
 
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)
 
185
 
 
186
    def test_search_calls_get_topic(self):
 
187
        """Searching should call get_topics in all indexes in order."""
 
188
        calls = []
 
189
        class RecordingIndex(object):
 
190
            def __init__(self, name):
 
191
                self.prefix = name
 
192
            def get_topics(self, topic):
 
193
                calls.append(('get_topics', self.prefix, topic))
 
194
                return ['something']
 
195
        index = help.HelpIndices()
 
196
        index.search_path = [RecordingIndex('1'), RecordingIndex('2')]
 
197
        # try with None
 
198
        index.search(None)
 
199
        self.assertEqual([
 
200
            ('get_topics', '1', None),
 
201
            ('get_topics', '2', None),
 
202
            ],
 
203
            calls)
 
204
        # and with a string
 
205
        del calls[:]
 
206
        index.search('bar')
 
207
        self.assertEqual([
 
208
            ('get_topics', '1', 'bar'),
 
209
            ('get_topics', '2', 'bar'),
 
210
            ],
 
211
            calls)
 
212
 
 
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):
 
217
                self.prefix = prefix
 
218
                self.result = search_result
 
219
            def get_topics(self, topic):
 
220
                return self.result
 
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')],
 
226
            index.search(None))
 
227
 
 
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)