def test_short_name(self): registry = controldir.ControlDirFormatRegistry() opt = option.RegistryOption('format', help='', registry=registry) self.assertEqual(None, opt.short_name()) opt = option.RegistryOption('format', short_name='F', help='', registry=registry) self.assertEqual('F', opt.short_name())
def test_iter_switches(self): opt = option.Option('hello', help='fg') self.assertEqual(list(opt.iter_switches()), [('hello', None, None, 'fg')]) opt = option.Option('hello', help='fg', type=int) self.assertEqual(list(opt.iter_switches()), [('hello', None, 'ARG', 'fg')]) opt = option.Option('hello', help='fg', type=int, argname='gar') self.assertEqual(list(opt.iter_switches()), [('hello', None, 'GAR', 'fg')]) registry = controldir.ControlDirFormatRegistry() bzrdir.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help') bzrdir.register_metadir(registry, 'two', 'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1', 'two help', ) registry.set_default('one') opt = option.RegistryOption('format', 'format help', registry, value_switches=False) self.assertEqual(list(opt.iter_switches()), [('format', None, 'ARG', 'format help')]) opt = option.RegistryOption('format', 'format help', registry, value_switches=True) self.assertEqual(list(opt.iter_switches()), [('format', None, 'ARG', 'format help'), ('default', None, None, 'one help'), ('one', None, None, 'one help'), ('two', None, None, 'two help'), ])
def test_registry_conversion(self): registry = controldir.ControlDirFormatRegistry() bzrdir.register_metadir(registry, 'one', 'RepositoryFormat7', 'one help') bzrdir.register_metadir(registry, 'two', 'RepositoryFormatKnit1', 'two help') bzrdir.register_metadir(registry, 'hidden', 'RepositoryFormatKnit1', 'two help', hidden=True) registry.set_default('one') options = [option.RegistryOption('format', '', registry, str)] opts, args = self.parse(options, ['--format', 'one']) self.assertEqual({'format':'one'}, opts) opts, args = self.parse(options, ['--format', 'two']) self.assertEqual({'format':'two'}, opts) self.assertRaises(errors.BadOptionValue, self.parse, options, ['--format', 'three']) self.assertRaises(errors.BzrCommandError, self.parse, options, ['--two']) options = [option.RegistryOption('format', '', registry, str, value_switches=True)] opts, args = self.parse(options, ['--two']) self.assertEqual({'format':'two'}, opts) opts, args = self.parse(options, ['--two', '--one']) self.assertEqual({'format':'one'}, opts) opts, args = self.parse(options, ['--two', '--one', '--format', 'two']) self.assertEqual({'format':'two'}, opts) options = [option.RegistryOption('format', '', registry, str, enum_switch=False)] self.assertRaises(errors.BzrCommandError, self.parse, options, ['--format', 'two'])
def test_help(self): registry = bzrdir.BzrDirFormatRegistry() registry.register_metadir('one', 'RepositoryFormat7', 'one help') registry.register_metadir( 'two', 'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1', 'two help', ) registry.register_metadir('hidden', 'RepositoryFormat7', 'hidden help', hidden=True) registry.set_default('one') options = [ option.RegistryOption('format', 'format help', registry, str, value_switches=True, title='Formats') ] parser = option.get_optparser(dict((o.name, o) for o in options)) value = parser.format_option_help() self.assertContainsRe(value, 'format.*format help') self.assertContainsRe(value, 'one.*one help') self.assertContainsRe(value, 'Formats:\n *--format') self.assertNotContainsRe(value, 'hidden help')
def test_lazy_registry(self): options = [option.RegistryOption('format', '', lazy_registry=('bzrlib.controldir','format_registry'), converter=str)] opts, args = self.parse(options, ['--format', 'knit']) self.assertEqual({'format': 'knit'}, opts) self.assertRaises( errors.BadOptionValue, self.parse, options, ['--format', 'BAD'])
def test_registry_converter(self): options = [ option.RegistryOption('format', '', bzrdir.format_registry, bzrdir.format_registry.make_bzrdir) ] opts, args = self.parse(options, ['--format', 'knit']) self.assertIsInstance(opts.format.repository_format, knitrepo.RepositoryFormatKnit1)
def test_is_hidden(self): registry = controldir.ControlDirFormatRegistry() bzrdir.register_metadir(registry, 'hidden', 'HiddenFormat', 'hidden help text', hidden=True) bzrdir.register_metadir(registry, 'visible', 'VisibleFormat', 'visible help text', hidden=False) format = option.RegistryOption('format', '', registry, str) self.assertTrue(format.is_hidden('hidden')) self.assertFalse(format.is_hidden('visible'))
def test_short_value_switches(self): reg = registry.Registry() reg.register('short', 'ShortChoice') reg.register('long', 'LongChoice') ropt = option.RegistryOption('choice', '', reg, value_switches=True, short_value_switches={'short': 's'}) opts, args = parse([ropt], ['--short']) self.assertEqual('ShortChoice', opts.choice) opts, args = parse([ropt], ['-s']) self.assertEqual('ShortChoice', opts.choice)
def test_registry_option_value_switches_hidden(self): reg = registry.Registry() class Hider(object): hidden = True reg.register("new", 1, "Current.") reg.register("old", 0, "Legacy.", info=Hider()) opt = option.RegistryOption("protocol", "Talking.", reg, value_switches=True, enum_switch=False) pot = self.pot_from_option(opt) self.assertContainsString( pot, "\n" "# help of 'protocol' test\n" "msgid \"Talking.\"\n") self.assertContainsString( pot, "\n" "# help of 'protocol=new' test\n" "msgid \"Current.\"\n") self.assertNotContainsString(pot, "'protocol=old'")