~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_help.py

  • Committer: John Arbash Meinel
  • Date: 2005-09-15 21:35:53 UTC
  • mfrom: (907.1.57)
  • mto: (1393.2.1)
  • mto: This revision was merged to the branch mainline in revision 1396.
  • Revision ID: john@arbash-meinel.com-20050915213552-a6c83a5ef1e20897
(broken) Transport work is merged in. Tests do not pass yet.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2011 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
"""Unit tests for the bzrlib.help module."""
18
 
 
19
 
import textwrap
20
 
 
21
 
from bzrlib import (
22
 
    builtins,
23
 
    commands,
24
 
    errors,
25
 
    help,
26
 
    help_topics,
27
 
    i18n,
28
 
    plugin,
29
 
    tests,
30
 
    )
31
 
 
32
 
from bzrlib.tests.test_i18n import ZzzTranslations
33
 
import re
34
 
 
35
 
 
36
 
class TestCommandHelp(tests.TestCase):
37
 
    """Tests for help on commands."""
38
 
 
39
 
    def assertCmdHelp(self, expected, cmd):
40
 
        self.assertEqualDiff(textwrap.dedent(expected), cmd.get_help_text())
41
 
 
42
 
    def test_command_help_includes_see_also(self):
43
 
        class cmd_WithSeeAlso(commands.Command):
44
 
            __doc__ = """A sample command."""
45
 
            _see_also = ['foo', 'bar']
46
 
        self.assertCmdHelp('''\
47
 
            Purpose: A sample command.
48
 
            Usage:   bzr WithSeeAlso
49
 
            
50
 
            Options:
51
 
              --usage        Show usage message and options.
52
 
              -v, --verbose  Display more information.
53
 
              -q, --quiet    Only display errors and warnings.
54
 
              -h, --help     Show help message.
55
 
            
56
 
            See also: bar, foo
57
 
            ''',
58
 
                           cmd_WithSeeAlso())
59
 
 
60
 
    def test_get_help_text(self):
61
 
        """Commands have a get_help_text method which returns their help."""
62
 
        class cmd_Demo(commands.Command):
63
 
            __doc__ = """A sample command."""
64
 
        self.assertCmdHelp('''\
65
 
            Purpose: A sample command.
66
 
            Usage:   bzr Demo
67
 
            
68
 
            Options:
69
 
              --usage        Show usage message and options.
70
 
              -v, --verbose  Display more information.
71
 
              -q, --quiet    Only display errors and warnings.
72
 
              -h, --help     Show help message.
73
 
 
74
 
            ''',
75
 
                           cmd_Demo())
76
 
        cmd = cmd_Demo()
77
 
        helptext = cmd.get_help_text()
78
 
        self.assertStartsWith(helptext,
79
 
            'Purpose: A sample command.\n'
80
 
            'Usage:   bzr Demo')
81
 
        self.assertEndsWith(helptext,
82
 
            '  -h, --help     Show help message.\n\n')
83
 
 
84
 
    def test_command_with_additional_see_also(self):
85
 
        class cmd_WithSeeAlso(commands.Command):
86
 
            __doc__ = """A sample command."""
87
 
            _see_also = ['foo', 'bar']
88
 
        cmd = cmd_WithSeeAlso()
89
 
        helptext = cmd.get_help_text(['gam'])
90
 
        self.assertEndsWith(
91
 
            helptext,
92
 
            '  -v, --verbose  Display more information.\n'
93
 
            '  -q, --quiet    Only display errors and warnings.\n'
94
 
            '  -h, --help     Show help message.\n'
95
 
            '\n'
96
 
            'See also: bar, foo, gam\n')
97
 
 
98
 
    def test_command_only_additional_see_also(self):
99
 
        class cmd_WithSeeAlso(commands.Command):
100
 
            __doc__ = """A sample command."""
101
 
        cmd = cmd_WithSeeAlso()
102
 
        helptext = cmd.get_help_text(['gam'])
103
 
        self.assertEndsWith(
104
 
            helptext,
105
 
            '  -v, --verbose  Display more information.\n'
106
 
            '  -q, --quiet    Only display errors and warnings.\n'
107
 
            '  -h, --help     Show help message.\n'
108
 
            '\n'
109
 
            'See also: gam\n')
110
 
 
111
 
    def test_get_help_topic(self):
112
 
        """The help topic for a Command is its name()."""
113
 
        class cmd_foo_bar(commands.Command):
114
 
            __doc__ = """A sample command."""
115
 
        cmd = cmd_foo_bar()
116
 
        self.assertEqual(cmd.name(), cmd.get_help_topic())
117
 
 
118
 
    def test_formatted_help_text(self):
119
 
        """Help text should be plain text by default."""
120
 
        class cmd_Demo(commands.Command):
121
 
            __doc__ = """A sample command.
122
 
 
123
 
            :Examples:
124
 
                Example 1::
125
 
 
126
 
                    cmd arg1
127
 
 
128
 
                Example 2::
129
 
 
130
 
                    cmd arg2
131
 
 
132
 
                A code block follows.
133
 
 
134
 
                ::
135
 
 
136
 
                    bzr Demo something
137
 
            """
138
 
        cmd = cmd_Demo()
139
 
        helptext = cmd.get_help_text()
140
 
        self.assertEquals(
141
 
            helptext,
142
 
            'Purpose: A sample command.\n'
143
 
            'Usage:   bzr Demo\n'
144
 
            '\n'
145
 
            'Options:\n'
146
 
            '  --usage        Show usage message and options.\n'
147
 
            '  -v, --verbose  Display more information.\n'
148
 
            '  -q, --quiet    Only display errors and warnings.\n'
149
 
            '  -h, --help     Show help message.\n'
150
 
            '\n'
151
 
            'Examples:\n'
152
 
            '    Example 1:\n'
153
 
            '\n'
154
 
            '        cmd arg1\n'
155
 
            '\n'
156
 
            '    Example 2:\n'
157
 
            '\n'
158
 
            '        cmd arg2\n'
159
 
            '\n'
160
 
            '    A code block follows.\n'
161
 
            '\n'
162
 
            '        bzr Demo something\n'
163
 
            '\n')
164
 
        helptext = cmd.get_help_text(plain=False)
165
 
        self.assertEquals(helptext,
166
 
            ':Purpose: A sample command.\n'
167
 
            ':Usage:   bzr Demo\n'
168
 
            '\n'
169
 
            ':Options:\n'
170
 
            '  --usage        Show usage message and options.\n'
171
 
            '  -v, --verbose  Display more information.\n'
172
 
            '  -q, --quiet    Only display errors and warnings.\n'
173
 
            '  -h, --help     Show help message.\n'
174
 
            '\n'
175
 
            ':Examples:\n'
176
 
            '    Example 1::\n'
177
 
            '\n'
178
 
            '        cmd arg1\n'
179
 
            '\n'
180
 
            '    Example 2::\n'
181
 
            '\n'
182
 
            '        cmd arg2\n'
183
 
            '\n'
184
 
            '    A code block follows.\n'
185
 
            '\n'
186
 
            '    ::\n'
187
 
            '\n'
188
 
            '        bzr Demo something\n'
189
 
            '\n')
190
 
 
191
 
    def test_concise_help_text(self):
192
 
        """Concise help text excludes the descriptive sections."""
193
 
        class cmd_Demo(commands.Command):
194
 
            __doc__ = """A sample command.
195
 
 
196
 
            Blah blah blah.
197
 
 
198
 
            :Examples:
199
 
                Example 1::
200
 
 
201
 
                    cmd arg1
202
 
            """
203
 
        cmd = cmd_Demo()
204
 
        helptext = cmd.get_help_text()
205
 
        self.assertEqualDiff(
206
 
            helptext,
207
 
            'Purpose: A sample command.\n'
208
 
            'Usage:   bzr Demo\n'
209
 
            '\n'
210
 
            'Options:\n'
211
 
            '  --usage        Show usage message and options.\n'
212
 
            '  -v, --verbose  Display more information.\n'
213
 
            '  -q, --quiet    Only display errors and warnings.\n'
214
 
            '  -h, --help     Show help message.\n'
215
 
            '\n'
216
 
            'Description:\n'
217
 
            '  Blah blah blah.\n'
218
 
            '\n'
219
 
            'Examples:\n'
220
 
            '    Example 1:\n'
221
 
            '\n'
222
 
            '        cmd arg1\n'
223
 
            '\n')
224
 
        helptext = cmd.get_help_text(verbose=False)
225
 
        self.assertEquals(helptext,
226
 
            'Purpose: A sample command.\n'
227
 
            'Usage:   bzr Demo\n'
228
 
            '\n'
229
 
            'Options:\n'
230
 
            '  --usage        Show usage message and options.\n'
231
 
            '  -v, --verbose  Display more information.\n'
232
 
            '  -q, --quiet    Only display errors and warnings.\n'
233
 
            '  -h, --help     Show help message.\n'
234
 
            '\n'
235
 
            'See bzr help Demo for more details and examples.\n'
236
 
            '\n')
237
 
 
238
 
    def test_help_custom_section_ordering(self):
239
 
        """Custom descriptive sections should remain in the order given."""
240
 
        class cmd_Demo(commands.Command):
241
 
            __doc__ = """A sample command.
242
 
 
243
 
            Blah blah blah.
244
 
 
245
 
            :Formats:
246
 
              Interesting stuff about formats.
247
 
 
248
 
            :Examples:
249
 
              Example 1::
250
 
 
251
 
                cmd arg1
252
 
 
253
 
            :Tips:
254
 
              Clever things to keep in mind.
255
 
            """
256
 
        cmd = cmd_Demo()
257
 
        helptext = cmd.get_help_text()
258
 
        self.assertEqualDiff(
259
 
            helptext,
260
 
            'Purpose: A sample command.\n'
261
 
            'Usage:   bzr Demo\n'
262
 
            '\n'
263
 
            'Options:\n'
264
 
            '  --usage        Show usage message and options.\n'
265
 
            '  -v, --verbose  Display more information.\n'
266
 
            '  -q, --quiet    Only display errors and warnings.\n'
267
 
            '  -h, --help     Show help message.\n'
268
 
            '\n'
269
 
            'Description:\n'
270
 
            '  Blah blah blah.\n'
271
 
            '\n'
272
 
            'Formats:\n'
273
 
            '  Interesting stuff about formats.\n'
274
 
            '\n'
275
 
            'Examples:\n'
276
 
            '  Example 1:\n'
277
 
            '\n'
278
 
            '    cmd arg1\n'
279
 
            '\n'
280
 
            'Tips:\n'
281
 
            '  Clever things to keep in mind.\n'
282
 
            '\n')
283
 
 
284
 
    def test_help_text_custom_usage(self):
285
 
        """Help text may contain a custom usage section."""
286
 
        class cmd_Demo(commands.Command):
287
 
            __doc__ = """A sample command.
288
 
 
289
 
            :Usage:
290
 
                cmd Demo [opts] args
291
 
 
292
 
                cmd Demo -h
293
 
 
294
 
            Blah blah blah.
295
 
            """
296
 
        cmd = cmd_Demo()
297
 
        helptext = cmd.get_help_text()
298
 
        self.assertEquals(helptext,
299
 
            'Purpose: A sample command.\n'
300
 
            'Usage:\n'
301
 
            '    cmd Demo [opts] args\n'
302
 
            '\n'
303
 
            '    cmd Demo -h\n'
304
 
            '\n'
305
 
            '\n'
306
 
            'Options:\n'
307
 
            '  --usage        Show usage message and options.\n'
308
 
            '  -v, --verbose  Display more information.\n'
309
 
            '  -q, --quiet    Only display errors and warnings.\n'
310
 
            '  -h, --help     Show help message.\n'
311
 
            '\n'
312
 
            'Description:\n'
313
 
            '  Blah blah blah.\n\n')
314
 
 
315
 
 
316
 
class ZzzTranslationsForDoc(ZzzTranslations):
317
 
 
318
 
    _section_pat = re.compile(':\w+:\\n\\s+')
319
 
    _indent_pat = re.compile('\\s+')
320
 
 
321
 
    def zzz(self, s):
322
 
        m = self._section_pat.match(s)
323
 
        if m is None:
324
 
            m = self._indent_pat.match(s)
325
 
        if m:
326
 
            return u'%szz{{%s}}' % (m.group(0), s[m.end():])
327
 
        return u'zz{{%s}}' % s
328
 
 
329
 
 
330
 
class TestCommandHelpI18n(tests.TestCase):
331
 
    """Tests for help on translated commands."""
332
 
 
333
 
    def setUp(self):
334
 
        super(TestCommandHelpI18n, self).setUp()
335
 
        self.overrideAttr(i18n, '_translations', ZzzTranslationsForDoc())
336
 
 
337
 
    def assertCmdHelp(self, expected, cmd):
338
 
        self.assertEqualDiff(textwrap.dedent(expected), cmd.get_help_text())
339
 
 
340
 
    def test_command_help_includes_see_also(self):
341
 
        class cmd_WithSeeAlso(commands.Command):
342
 
            __doc__ = """A sample command."""
343
 
            _see_also = ['foo', 'bar']
344
 
        self.assertCmdHelp('''\
345
 
            zz{{:Purpose: zz{{A sample command.}}
346
 
            }}zz{{:Usage:   bzr WithSeeAlso
347
 
            }}
348
 
            zz{{:Options:
349
 
              --usage        Show usage message and options.
350
 
              -v, --verbose  Display more information.
351
 
              -q, --quiet    Only display errors and warnings.
352
 
              -h, --help     Show help message.
353
 
            }}
354
 
            zz{{:See also: bar, foo}}
355
 
            ''',
356
 
                           cmd_WithSeeAlso())
357
 
 
358
 
    def test_get_help_text(self):
359
 
        """Commands have a get_help_text method which returns their help."""
360
 
        class cmd_Demo(commands.Command):
361
 
            __doc__ = """A sample command."""
362
 
        self.assertCmdHelp('''\
363
 
            zz{{:Purpose: zz{{A sample command.}}
364
 
            }}zz{{:Usage:   bzr Demo
365
 
            }}
366
 
            zz{{:Options:
367
 
              --usage        Show usage message and options.
368
 
              -v, --verbose  Display more information.
369
 
              -q, --quiet    Only display errors and warnings.
370
 
              -h, --help     Show help message.
371
 
            }}
372
 
            ''',
373
 
                           cmd_Demo())
374
 
 
375
 
    def test_command_with_additional_see_also(self):
376
 
        class cmd_WithSeeAlso(commands.Command):
377
 
            __doc__ = """A sample command."""
378
 
            _see_also = ['foo', 'bar']
379
 
        cmd = cmd_WithSeeAlso()
380
 
        helptext = cmd.get_help_text(['gam'])
381
 
        self.assertEndsWith(
382
 
            helptext,
383
 
            '  -v, --verbose  Display more information.\n'
384
 
            '  -q, --quiet    Only display errors and warnings.\n'
385
 
            '  -h, --help     Show help message.\n'
386
 
            '}}\n'
387
 
            'zz{{:See also: bar, foo, gam}}\n')
388
 
 
389
 
    def test_command_only_additional_see_also(self):
390
 
        class cmd_WithSeeAlso(commands.Command):
391
 
            __doc__ = """A sample command."""
392
 
        cmd = cmd_WithSeeAlso()
393
 
        helptext = cmd.get_help_text(['gam'])
394
 
        self.assertEndsWith(
395
 
            helptext,
396
 
            'zz{{:Options:\n'
397
 
            '  --usage        Show usage message and options.\n'
398
 
            '  -v, --verbose  Display more information.\n'
399
 
            '  -q, --quiet    Only display errors and warnings.\n'
400
 
            '  -h, --help     Show help message.\n'
401
 
            '}}\n'
402
 
            'zz{{:See also: gam}}\n')
403
 
 
404
 
 
405
 
    def test_help_custom_section_ordering(self):
406
 
        """Custom descriptive sections should remain in the order given."""
407
 
        # The help formatter expect the class name to start with 'cmd_'
408
 
        class cmd_Demo(commands.Command):
409
 
            __doc__ = """A sample command.
410
 
 
411
 
            Blah blah blah.
412
 
 
413
 
            :Formats:
414
 
              Interesting stuff about formats.
415
 
 
416
 
            :Examples:
417
 
              Example 1::
418
 
 
419
 
                cmd arg1
420
 
 
421
 
            :Tips:
422
 
              Clever things to keep in mind.
423
 
            """
424
 
        self.assertCmdHelp('''\
425
 
            zz{{:Purpose: zz{{A sample command.}}
426
 
            }}zz{{:Usage:   bzr Demo
427
 
            }}
428
 
            zz{{:Options:
429
 
              --usage        Show usage message and options.
430
 
              -v, --verbose  Display more information.
431
 
              -q, --quiet    Only display errors and warnings.
432
 
              -h, --help     Show help message.
433
 
            }}
434
 
            Description:
435
 
              zz{{zz{{Blah blah blah.}}
436
 
            
437
 
            }}:Formats:
438
 
              zz{{Interesting stuff about formats.}}
439
 
            
440
 
            Examples:
441
 
              zz{{Example 1::}}
442
 
            
443
 
                zz{{cmd arg1}}
444
 
            
445
 
            Tips:
446
 
              zz{{Clever things to keep in mind.}}
447
 
 
448
 
            ''',
449
 
                           cmd_Demo())
450
 
 
451
 
    def test_help_text_custom_usage(self):
452
 
        """Help text may contain a custom usage section."""
453
 
        class cmd_Demo(commands.Command):
454
 
            __doc__ = """A sample command.
455
 
 
456
 
            :Usage:
457
 
                cmd Demo [opts] args
458
 
 
459
 
                cmd Demo -h
460
 
 
461
 
            Blah blah blah.
462
 
            """
463
 
        self.assertCmdHelp('''\
464
 
            zz{{:Purpose: zz{{A sample command.}}
465
 
            }}zz{{:Usage:
466
 
                zz{{cmd Demo [opts] args}}
467
 
            
468
 
                zz{{cmd Demo -h}}
469
 
 
470
 
            }}
471
 
            zz{{:Options:
472
 
              --usage        Show usage message and options.
473
 
              -v, --verbose  Display more information.
474
 
              -q, --quiet    Only display errors and warnings.
475
 
              -h, --help     Show help message.
476
 
            }}
477
 
            Description:
478
 
              zz{{zz{{Blah blah blah.}}
479
 
 
480
 
            }}
481
 
            ''',
482
 
                           cmd_Demo())
483
 
 
484
 
 
485
 
class TestHelp(tests.TestCase):
486
 
 
487
 
    def setUp(self):
488
 
        tests.TestCase.setUp(self)
489
 
        commands.install_bzr_command_hooks()
490
 
 
491
 
 
492
 
class TestRegisteredTopic(TestHelp):
493
 
    """Tests for the RegisteredTopic class."""
494
 
 
495
 
    def test_contruct(self):
496
 
        """Construction takes the help topic name for the registered item."""
497
 
        # validate our test
498
 
        self.assertTrue('basic' in help_topics.topic_registry)
499
 
        topic = help_topics.RegisteredTopic('basic')
500
 
        self.assertEqual('basic', topic.topic)
501
 
 
502
 
    def test_get_help_text(self):
503
 
        """RegisteredTopic returns the get_detail results for get_help_text."""
504
 
        topic = help_topics.RegisteredTopic('commands')
505
 
        self.assertEqual(help_topics.topic_registry.get_detail('commands'),
506
 
                         topic.get_help_text())
507
 
 
508
 
    def test_get_help_text_with_additional_see_also(self):
509
 
        topic = help_topics.RegisteredTopic('commands')
510
 
        self.assertEndsWith(
511
 
            topic.get_help_text(['foo', 'bar']),
512
 
            '\n'
513
 
            'See also: bar, foo\n')
514
 
 
515
 
    def test_get_help_text_loaded_from_file(self):
516
 
        # Pick a known topic stored in an external file
517
 
        topic = help_topics.RegisteredTopic('authentication')
518
 
        self.assertStartsWith(topic.get_help_text(),
519
 
            'Authentication Settings\n'
520
 
            '=======================\n'
521
 
            '\n')
522
 
 
523
 
    def test_get_help_topic(self):
524
 
        """The help topic for RegisteredTopic is its topic from construction."""
525
 
        topic = help_topics.RegisteredTopic('foobar')
526
 
        self.assertEqual('foobar', topic.get_help_topic())
527
 
        topic = help_topics.RegisteredTopic('baz')
528
 
        self.assertEqual('baz', topic.get_help_topic())
529
 
 
530
 
 
531
 
class TestTopicIndex(TestHelp):
532
 
    """Tests for the HelpTopicIndex class."""
533
 
 
534
 
    def test_default_constructable(self):
535
 
        index = help_topics.HelpTopicIndex()
536
 
 
537
 
    def test_get_topics_None(self):
538
 
        """Searching for None returns the basic help topic."""
539
 
        index = help_topics.HelpTopicIndex()
540
 
        topics = index.get_topics(None)
541
 
        self.assertEqual(1, len(topics))
542
 
        self.assertIsInstance(topics[0], help_topics.RegisteredTopic)
543
 
        self.assertEqual('basic', topics[0].topic)
544
 
 
545
 
    def test_get_topics_topics(self):
546
 
        """Searching for a string returns the matching string."""
547
 
        index = help_topics.HelpTopicIndex()
548
 
        topics = index.get_topics('topics')
549
 
        self.assertEqual(1, len(topics))
550
 
        self.assertIsInstance(topics[0], help_topics.RegisteredTopic)
551
 
        self.assertEqual('topics', topics[0].topic)
552
 
 
553
 
    def test_get_topics_no_topic(self):
554
 
        """Searching for something not registered returns []."""
555
 
        index = help_topics.HelpTopicIndex()
556
 
        self.assertEqual([], index.get_topics('nothing by this name'))
557
 
 
558
 
    def test_prefix(self):
559
 
        """TopicIndex has a prefix of ''."""
560
 
        index = help_topics.HelpTopicIndex()
561
 
        self.assertEqual('', index.prefix)
562
 
 
563
 
 
564
 
class TestCommandIndex(TestHelp):
565
 
    """Tests for the HelpCommandIndex class."""
566
 
 
567
 
    def test_default_constructable(self):
568
 
        index = commands.HelpCommandIndex()
569
 
 
570
 
    def test_get_topics_None(self):
571
 
        """Searching for None returns an empty list."""
572
 
        index = commands.HelpCommandIndex()
573
 
        self.assertEqual([], index.get_topics(None))
574
 
 
575
 
    def test_get_topics_rocks(self):
576
 
        """Searching for 'rocks' returns the cmd_rocks command instance."""
577
 
        index = commands.HelpCommandIndex()
578
 
        topics = index.get_topics('rocks')
579
 
        self.assertEqual(1, len(topics))
580
 
        self.assertIsInstance(topics[0], builtins.cmd_rocks)
581
 
 
582
 
    def test_get_topics_no_topic(self):
583
 
        """Searching for something that is not a command returns []."""
584
 
        index = commands.HelpCommandIndex()
585
 
        self.assertEqual([], index.get_topics('nothing by this name'))
586
 
 
587
 
    def test_prefix(self):
588
 
        """CommandIndex has a prefix of 'commands/'."""
589
 
        index = commands.HelpCommandIndex()
590
 
        self.assertEqual('commands/', index.prefix)
591
 
 
592
 
    def test_get_topic_with_prefix(self):
593
 
        """Searching for commands/rocks returns the rocks command object."""
594
 
        index = commands.HelpCommandIndex()
595
 
        topics = index.get_topics('commands/rocks')
596
 
        self.assertEqual(1, len(topics))
597
 
        self.assertIsInstance(topics[0], builtins.cmd_rocks)
598
 
 
599
 
 
600
 
class TestHelpIndices(tests.TestCase):
601
 
    """Tests for the HelpIndices class."""
602
 
 
603
 
    def test_default_search_path(self):
604
 
        """The default search path should include internal indexs."""
605
 
        indices = help.HelpIndices()
606
 
        self.assertEqual(3, len(indices.search_path))
607
 
        # help topics should be searched in first.
608
 
        self.assertIsInstance(indices.search_path[0],
609
 
            help_topics.HelpTopicIndex)
610
 
        # with commands being search second.
611
 
        self.assertIsInstance(indices.search_path[1],
612
 
            commands.HelpCommandIndex)
613
 
        # and plugins are a third index.
614
 
        self.assertIsInstance(indices.search_path[2],
615
 
            plugin.PluginsHelpIndex)
616
 
 
617
 
    def test_search_for_unknown_topic_raises(self):
618
 
        """Searching for an unknown topic should raise NoHelpTopic."""
619
 
        indices = help.HelpIndices()
620
 
        indices.search_path = []
621
 
        error = self.assertRaises(errors.NoHelpTopic, indices.search, 'foo')
622
 
        self.assertEqual('foo', error.topic)
623
 
 
624
 
    def test_search_calls_get_topic(self):
625
 
        """Searching should call get_topics in all indexes in order."""
626
 
        calls = []
627
 
        class RecordingIndex(object):
628
 
            def __init__(self, name):
629
 
                self.prefix = name
630
 
            def get_topics(self, topic):
631
 
                calls.append(('get_topics', self.prefix, topic))
632
 
                return ['something']
633
 
        index = help.HelpIndices()
634
 
        index.search_path = [RecordingIndex('1'), RecordingIndex('2')]
635
 
        # try with None
636
 
        index.search(None)
637
 
        self.assertEqual([
638
 
            ('get_topics', '1', None),
639
 
            ('get_topics', '2', None),
640
 
            ],
641
 
            calls)
642
 
        # and with a string
643
 
        del calls[:]
644
 
        index.search('bar')
645
 
        self.assertEqual([
646
 
            ('get_topics', '1', 'bar'),
647
 
            ('get_topics', '2', 'bar'),
648
 
            ],
649
 
            calls)
650
 
 
651
 
    def test_search_returns_index_and_results(self):
652
 
        """Searching should return help topics with their index"""
653
 
        class CannedIndex(object):
654
 
            def __init__(self, prefix, search_result):
655
 
                self.prefix = prefix
656
 
                self.result = search_result
657
 
            def get_topics(self, topic):
658
 
                return self.result
659
 
        index = help.HelpIndices()
660
 
        index_one = CannedIndex('1', ['a'])
661
 
        index_two = CannedIndex('2', ['b', 'c'])
662
 
        index.search_path = [index_one, index_two]
663
 
        self.assertEqual([(index_one, 'a'), (index_two, 'b'), (index_two, 'c')],
664
 
            index.search(None))
665
 
 
666
 
    def test_search_checks_for_duplicate_prefixes(self):
667
 
        """Its an error when there are multiple indices with the same prefix."""
668
 
        indices = help.HelpIndices()
669
 
        indices.search_path = [help_topics.HelpTopicIndex(),
670
 
            help_topics.HelpTopicIndex()]
671
 
        self.assertRaises(errors.DuplicateHelpPrefix, indices.search, None)