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.assertFalse(hasattr(opts, 'hello'))
96
self.assertEqual(option.OptionParser.DEFAULT_VALUE, 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'])
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)
149
137
def test_registry_converter(self):
150
138
options = [option.RegistryOption('format', '',
151
139
bzrdir.format_registry, bzrdir.format_registry.make_bzrdir)]
163
151
be_long='go long', value_switches=True)
164
152
self.assertEqual(['my-option', 'be-long', 'short'],
165
153
[x[0] for x in my_option.iter_switches()])
166
self.assertEqual('test option', my_option.help)
168
155
def test_help(self):
169
156
registry = bzrdir.BzrDirFormatRegistry()
214
201
('two', None, None, 'two help'),
217
def test_option_callback_bool(self):
218
"Test booleans get True and False passed correctly to a callback."""
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)
232
def test_option_callback_str(self):
233
"""Test callbacks work for string options both long and short."""
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,
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)
249
205
class TestListOptions(TestCase):
250
206
"""Tests for ListOption, used to specify lists on the command-line."""
281
237
options, ['--hello=a', '--hello=b', '--hello=-', '--hello=c'])
282
238
self.assertEqual(['c'], opts.hello)
284
def test_option_callback_list(self):
285
"""Test callbacks work for list options."""
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',
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)
305
241
class TestOptionDefinitions(TestCase):
306
242
"""Tests for options in the Bazaar codebase."""
360
296
self.fail("The following options don't match the style guide:\n"
361
297
+ '\n'.join(msgs))
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'))
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)
382
class TestVerboseQuietLinkage(TestCase):
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)
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'])