~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_options.py

Merge Robert and indirectly trunk

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)]
134
134
        self.assertRaises(errors.BzrCommandError, self.parse, options,
135
135
                          ['--format', 'two'])
136
136
 
 
137
    def test_override(self):
 
138
        options = [option.Option('hello', type=str),
 
139
                   option.Option('hi', type=str, param_name='hello')]
 
140
        opts, args = self.parse(options, ['--hello', 'a', '--hello', 'b'])
 
141
        self.assertEqual('b', opts.hello)
 
142
        opts, args = self.parse(options, ['--hello', 'b', '--hello', 'a'])
 
143
        self.assertEqual('a', opts.hello)
 
144
        opts, args = self.parse(options, ['--hello', 'a', '--hi', 'b'])
 
145
        self.assertEqual('b', opts.hello)
 
146
        opts, args = self.parse(options, ['--hi', 'b', '--hello', 'a'])
 
147
        self.assertEqual('a', opts.hello)
 
148
 
137
149
    def test_registry_converter(self):
138
150
        options = [option.RegistryOption('format', '',
139
151
                   bzrdir.format_registry, bzrdir.format_registry.make_bzrdir)]
202
214
                          ('two', None, None, 'two help'),
203
215
                          ])
204
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
 
205
248
 
206
249
class TestListOptions(TestCase):
207
250
    """Tests for ListOption, used to specify lists on the command-line."""
238
281
            options, ['--hello=a', '--hello=b', '--hello=-', '--hello=c'])
239
282
        self.assertEqual(['c'], opts.hello)
240
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
 
241
304
 
242
305
class TestOptionDefinitions(TestCase):
243
306
    """Tests for options in the Bazaar codebase."""
296
359
        if msgs:
297
360
            self.fail("The following options don't match the style guide:\n"
298
361
                    + '\n'.join(msgs))
 
362
 
 
363
    def test_is_hidden(self):
 
364
        registry = bzrdir.BzrDirFormatRegistry()
 
365
        registry.register_metadir('hidden', 'HiddenFormat',
 
366
            'hidden help text', hidden=True)
 
367
        registry.register_metadir('visible', 'VisibleFormat',
 
368
            'visible help text', hidden=False)
 
369
        format = option.RegistryOption('format', '', registry, str)
 
370
        self.assertTrue(format.is_hidden('hidden'))
 
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'])