/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_options.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-09-03 07:34:25 UTC
  • mfrom: (2779.1.1 ianc-integration)
  • Revision ID: pqm@pqm.ubuntu.com-20070903073425-ouk9qod51gqk18nn
(Ian Clatworthy) Verbosity levels and standard options

Show diffs side-by-side

added added

removed removed

Lines of Context:
93
93
        opts, args = self.parse(options, ['--no-hello', '--hello'])
94
94
        self.assertEqual(True, opts.hello)
95
95
        opts, args = self.parse(options, [])
96
 
        self.assertEqual(option.OptionParser.DEFAULT_VALUE, opts.hello)
 
96
        self.assertFalse(hasattr(opts, 'hello'))
97
97
        opts, args = self.parse(options, ['--hello', '--no-hello'])
98
98
        self.assertEqual(False, opts.hello)
99
99
        options = [option.Option('number', type=int)]
214
214
                          ('two', None, None, 'two help'),
215
215
                          ])
216
216
 
 
217
    def test_option_callback_bool(self):
 
218
        "Test booleans get True and False passed correctly to a callback."""
 
219
        cb_calls = []
 
220
        def cb(option, name, value, parser):
 
221
            cb_calls.append((option,name,value,parser))
 
222
        options = [option.Option('hello', custom_callback=cb)]
 
223
        opts, args = self.parse(options, ['--hello', '--no-hello'])
 
224
        self.assertEqual(2, len(cb_calls))
 
225
        opt,name,value,parser = cb_calls[0]
 
226
        self.assertEqual('hello', name)
 
227
        self.assertTrue(value)
 
228
        opt,name,value,parser = cb_calls[1]
 
229
        self.assertEqual('hello', name)
 
230
        self.assertFalse(value)
 
231
 
 
232
    def test_option_callback_str(self):
 
233
        """Test callbacks work for string options both long and short."""
 
234
        cb_calls = []
 
235
        def cb(option, name, value, parser):
 
236
            cb_calls.append((option,name,value,parser))
 
237
        options = [option.Option('hello', type=str, custom_callback=cb,
 
238
            short_name='h')]
 
239
        opts, args = self.parse(options, ['--hello', 'world', '-h', 'mars'])
 
240
        self.assertEqual(2, len(cb_calls))
 
241
        opt,name,value,parser = cb_calls[0]
 
242
        self.assertEqual('hello', name)
 
243
        self.assertEqual('world', value)
 
244
        opt,name,value,parser = cb_calls[1]
 
245
        self.assertEqual('hello', name)
 
246
        self.assertEqual('mars', value)
 
247
 
217
248
 
218
249
class TestListOptions(TestCase):
219
250
    """Tests for ListOption, used to specify lists on the command-line."""
250
281
            options, ['--hello=a', '--hello=b', '--hello=-', '--hello=c'])
251
282
        self.assertEqual(['c'], opts.hello)
252
283
 
 
284
    def test_option_callback_list(self):
 
285
        """Test callbacks work for list options."""
 
286
        cb_calls = []
 
287
        def cb(option, name, value, parser):
 
288
            # Note that the value is a reference so copy to keep it
 
289
            cb_calls.append((option,name,value[:],parser))
 
290
        options = [option.ListOption('hello', type=str, custom_callback=cb)]
 
291
        opts, args = self.parse(options, ['--hello=world', '--hello=mars',
 
292
            '--hello=-'])
 
293
        self.assertEqual(3, len(cb_calls))
 
294
        opt,name,value,parser = cb_calls[0]
 
295
        self.assertEqual('hello', name)
 
296
        self.assertEqual(['world'], value)
 
297
        opt,name,value,parser = cb_calls[1]
 
298
        self.assertEqual('hello', name)
 
299
        self.assertEqual(['world', 'mars'], value)
 
300
        opt,name,value,parser = cb_calls[2]
 
301
        self.assertEqual('hello', name)
 
302
        self.assertEqual([], value)
 
303
 
253
304
 
254
305
class TestOptionDefinitions(TestCase):
255
306
    """Tests for options in the Bazaar codebase."""
318
369
        format = option.RegistryOption('format', '', registry, str)
319
370
        self.assertTrue(format.is_hidden('hidden'))
320
371
        self.assertFalse(format.is_hidden('visible'))
 
372
 
 
373
    def test_option_custom_help(self):
 
374
        the_opt = option.Option.OPTIONS['help']
 
375
        orig_help = the_opt.help[:]
 
376
        my_opt = option.custom_help('help', 'suggest lottery numbers')
 
377
        # Confirm that my_opt has my help and the original is unchanged
 
378
        self.assertEqual('suggest lottery numbers', my_opt.help)
 
379
        self.assertEqual(orig_help, the_opt.help)
 
380
 
 
381
 
 
382
class TestVerboseQuietLinkage(TestCase):
 
383
 
 
384
    def check(self, parser, level, args):
 
385
        option._verbosity_level = 0
 
386
        opts, args = parser.parse_args(args)
 
387
        self.assertEqual(level, option._verbosity_level)
 
388
 
 
389
    def test_verbose_quiet_linkage(self):
 
390
        parser = option.get_optparser(option.Option.STD_OPTIONS)
 
391
        self.check(parser, 0, [])
 
392
        self.check(parser, 1, ['-v'])
 
393
        self.check(parser, 2, ['-v', '-v'])
 
394
        self.check(parser, -1, ['-q'])
 
395
        self.check(parser, -2, ['-qq'])
 
396
        self.check(parser, -1, ['-v', '-v', '-q'])
 
397
        self.check(parser, 2, ['-q', '-v', '-v'])
 
398
        self.check(parser, 0, ['--no-verbose'])
 
399
        self.check(parser, 0, ['-v', '-q', '--no-quiet'])