def test_bool_flags(self): for arg, expected in (('--nothing', True), ('--nothing=true', True), ('--nothing=false', False), ('--nonothing', False)): fv = _flagvalues.FlagValues() _defines.DEFINE_boolean('nothing', None, '', flag_values=fv) fv(('./program', arg)) self.assertIs(expected, fv.nothing) for arg in ('--nonothing=true', '--nonothing=false'): fv = _flagvalues.FlagValues() _defines.DEFINE_boolean('nothing', None, '', flag_values=fv) with self.assertRaises(ValueError): fv(('./program', arg))
def test_flags_into_string_deterministic(self): flag_values = _flagvalues.FlagValues() _defines.DEFINE_string('fa', 'x', '', flag_values=flag_values, module_name='mb') _defines.DEFINE_string('fb', 'x', '', flag_values=flag_values, module_name='mb') _defines.DEFINE_string('fc', 'x', '', flag_values=flag_values, module_name='ma') _defines.DEFINE_string('fd', 'x', '', flag_values=flag_values, module_name='ma') expected = ('--fc=x\n' '--fd=x\n' '--fa=x\n' '--fb=x\n') flags_by_module_items = sorted( flag_values.flags_by_module_dict().items(), reverse=True) for _, module_flags in flags_by_module_items: module_flags.sort(reverse=True) flag_values.__dict__['__flags_by_module'] = collections.OrderedDict( flags_by_module_items) actual = flag_values.flags_into_string() self.assertEqual(expected, actual)
def setUp(self): super(MarkFlagsAsMutualExclusiveTest, self).setUp() self.flag_values = _flagvalues.FlagValues() _defines.DEFINE_string('flag_one', None, 'flag one', flag_values=self.flag_values) _defines.DEFINE_string('flag_two', None, 'flag two', flag_values=self.flag_values) _defines.DEFINE_string('flag_three', None, 'flag three', flag_values=self.flag_values) _defines.DEFINE_integer('int_flag_one', None, 'int flag one', flag_values=self.flag_values) _defines.DEFINE_integer('int_flag_two', None, 'int flag two', flag_values=self.flag_values) _defines.DEFINE_multi_string('multi_flag_one', None, 'multi flag one', flag_values=self.flag_values) _defines.DEFINE_multi_string('multi_flag_two', None, 'multi flag two', flag_values=self.flag_values)
def setUp(self): self.fv = _flagvalues.FlagValues() _defines.DEFINE_string('long_name', 'default', 'help', flag_values=self.fv, short_name='s')
def test_re_raise_invalid(self): def setter(unused_name, unused_val): raise ValueError() new_flags = _flagvalues.FlagValues() new_flags._register_unknown_flag_setter(setter) with self.assertRaises(_exceptions.IllegalFlagValueError): new_flags.undefined_flag = 0
def test_re_raise_undefined(self): def setter(unused_name, unused_val): raise NameError() new_flags = _flagvalues.FlagValues() new_flags._register_unknown_flag_setter(setter) with self.assertRaises(_exceptions.UnrecognizedFlagError): new_flags.undefined_flag = 0
def test_allow_overwrite_false(self): fv = _flagvalues.FlagValues() _defines.DEFINE_string('default_none', None, 'help', allow_overwrite=False, flag_values=fv) _defines.DEFINE_string('default_foo', 'foo', 'help', allow_overwrite=False, flag_values=fv) fv.mark_as_parsed() self.assertEqual('foo', fv.default_foo) self.assertEqual(None, fv.default_none) fv(['', '--default_foo=notFoo', '--default_none=notNone']) self.assertEqual('notFoo', fv.default_foo) self.assertEqual('notNone', fv.default_none) fv.unparse_flags() self.assertEqual('foo', fv['default_foo'].value) self.assertEqual(None, fv['default_none'].value) fv(['', '--default_foo=alsoNotFoo', '--default_none=alsoNotNone']) self.assertEqual('alsoNotFoo', fv.default_foo) self.assertEqual('alsoNotNone', fv.default_none)
def setUp(self): super(FlagHolderTest, self).setUp() self.fv = _flagvalues.FlagValues() self.name_flag = _defines.DEFINE_string('name', 'default', 'help', flag_values=self.fv)
def test_invalid_argv(self): fv = _flagvalues.FlagValues() with self.assertRaises(TypeError): fv('./program') with self.assertRaises(TypeError): fv(b'./program') with self.assertRaises(TypeError): fv(u'./program')
def setUp(self): super(FlagsDashSyntaxTest, self).setUp() self.fv = _flagvalues.FlagValues() _defines.DEFINE_string('long_name', 'default', 'help', flag_values=self.fv, short_name='s')
def test_validate_all_flags(self): fv = _flagvalues.FlagValues() _defines.DEFINE_string('name', None, '', flag_values=fv) _validators.mark_flag_as_required('name', flag_values=fv) with self.assertRaises(_exceptions.IllegalFlagValueError): fv.validate_all_flags() fv.name = 'test' fv.validate_all_flags()
def test_unparsed_flags_access_raises_after_unparse_flags(self): fv = _flagvalues.FlagValues() _defines.DEFINE_string('a_str', 'default_value', 'help', flag_values=fv) fv.mark_as_parsed() self.assertEqual(fv.a_str, 'default_value') fv.unparse_flags() with self.assertRaises(_exceptions.UnparsedFlagAccessError): _ = fv.a_str
def _get_test_flag_values(self): """Get a _flagvalues.FlagValues() instance, set up for tests.""" flag_values = _flagvalues.FlagValues() _defines.DEFINE_string('strf', '', '', flag_values=flag_values) _defines.DEFINE_boolean('boolf', 0, '', flag_values=flag_values) return flag_values
def test_set_default(self): fv = _flagvalues.FlagValues() fv.mark_as_parsed() with self.assertRaises(_exceptions.UnrecognizedFlagError): fv.set_default('changelist', 1) _defines.DEFINE_integer('changelist', 0, 'help', flag_values=fv) self.assertEqual(0, fv.changelist) fv.set_default('changelist', 2) self.assertEqual(2, fv.changelist)
def test_boolean_flag_parser_gets_string_argument(self): for arg, expected in (('--nothing', 'true'), ('--nothing=true', 'true'), ('--nothing=false', 'false'), ('--nonothing', 'false')): fv = _flagvalues.FlagValues() _defines.DEFINE_boolean('nothing', None, '', flag_values=fv) with mock.patch.object(fv['nothing'].parser, 'parse') as mock_parse: fv(('./program', arg)) mock_parse.assert_called_once_with(expected)
def test_using_default_value_none(self): fv = _flagvalues.FlagValues() _defines.DEFINE_string('default_none', None, 'help', flag_values=fv) self.assertTrue(fv['default_none'].using_default_value) fv(['', '--default_none=notNone']) self.assertFalse(fv['default_none'].using_default_value) fv.unparse_flags() self.assertTrue(fv['default_none'].using_default_value) fv(['', '--default_none=alsoNotNone']) self.assertFalse(fv['default_none'].using_default_value) fv.unparse_flags() self.assertTrue(fv['default_none'].using_default_value)
def setUp(self): super(SetAttributesTest, self).setUp() self.new_flags = _flagvalues.FlagValues() _defines.DEFINE_boolean('defined_flag', None, '', flag_values=self.new_flags) _defines.DEFINE_boolean('another_defined_flag', None, '', flag_values=self.new_flags) self.setter_called = 0
def setUp(self): super(MultiFlagsValidatorTest, self).setUp() self.flag_values = _flagvalues.FlagValues() self.call_args = [] _defines.DEFINE_integer('foo', 1, 'Usual integer flag', flag_values=self.flag_values) _defines.DEFINE_integer('bar', 2, 'Usual integer flag', flag_values=self.flag_values)
def test_get_flags_for_module(self): fv = _flagvalues.FlagValues() _defines.DEFINE_string('foo', None, 'help', flag_values=fv) module_foo.define_flags(fv) flags = fv.get_flags_for_module('__main__') self.assertEqual({'foo'}, {flag.name for flag in flags}) flags = fv.get_flags_for_module(module_foo) self.assertEqual({'tmod_foo_bool', 'tmod_foo_int', 'tmod_foo_str'}, {flag.name for flag in flags})
def test_conflicting_flags(self): fv = _flagvalues.FlagValues() with self.assertRaises(_exceptions.FlagNameConflictsWithMethodError): _defines.DEFINE_boolean('is_gnu_getopt', False, 'help', flag_values=fv) _defines.DEFINE_boolean( 'is_gnu_getopt', False, 'help', flag_values=fv, allow_using_method_names=True) self.assertFalse(fv['is_gnu_getopt'].value) self.assertIsInstance(fv.is_gnu_getopt, types.MethodType)
def test_len(self): fv = _flagvalues.FlagValues() self.assertEqual(0, len(fv)) self.assertFalse(fv) _defines.DEFINE_boolean('boolean', False, 'help', flag_values=fv) self.assertEqual(1, len(fv)) self.assertTrue(fv) _defines.DEFINE_boolean( 'bool', False, 'help', short_name='b', flag_values=fv) self.assertEqual(3, len(fv)) self.assertTrue(fv)
def test_multi_string_default_none(self): fv = _flagvalues.FlagValues() _defines.DEFINE_multi_string('foo', None, 'help', flag_values=fv) fv.mark_as_parsed() self.assertEqual(None, fv.foo) fv(['', '--foo=aa']) self.assertEqual(['aa'], fv.foo) fv.unparse_flags() self.assertEqual(None, fv['foo'].value) fv(['', '--foo=bb', '--foo=cc']) self.assertEqual(['bb', 'cc'], fv.foo) fv.unparse_flags() self.assertEqual(None, fv['foo'].value)
def run_test(argv, defined_py_flags, expected_argv): fv = _flagvalues.FlagValues() fv.set_gnu_getopt(True) for f in defined_py_flags: if f.startswith('b'): _defines.DEFINE_boolean(f, False, 'help', flag_values=fv) else: _defines.DEFINE_string(f, 'default', 'help', flag_values=fv) output_argv = fv(argv, known_only=True) self.assertEqual(expected_argv, output_argv)
def test_multi_string_default_list(self): fv = _flagvalues.FlagValues() _defines.DEFINE_multi_string( 'foo', ['xx', 'yy', 'zz'], 'help', flag_values=fv) expected_default = ['xx', 'yy', 'zz'] fv.mark_as_parsed() self.assertEqual(expected_default, fv.foo) fv(['', '--foo=aa']) self.assertEqual(['aa'], fv.foo) fv.unparse_flags() self.assertEqual(expected_default, fv['foo'].value) fv(['', '--foo=bb', '--foo=cc']) self.assertEqual(['bb', 'cc'], fv.foo) fv.unparse_flags() self.assertEqual(expected_default, fv['foo'].value)
def test_copy(self): fv = _flagvalues.FlagValues() _defines.DEFINE_integer('answer', 0, 'help', flag_values=fv) fv(['', '--answer=1']) with self.assertRaisesRegexp( TypeError, 'FlagValues does not support shallow copies'): copy.copy(fv) fv2 = copy.deepcopy(fv) self.assertEqual(fv2.answer, 1) fv2.answer = 42 self.assertEqual(fv2.answer, 42) self.assertEqual(fv.answer, 1)
def test_flags_dir(self): flag_values = _flagvalues.FlagValues() flag_name1 = 'bool_flag' flag_name2 = 'string_flag' flag_name3 = 'float_flag' description = 'Description' _defines.DEFINE_boolean( flag_name1, None, description, flag_values=flag_values) _defines.DEFINE_string( flag_name2, None, description, flag_values=flag_values) self.assertEqual(sorted([flag_name1, flag_name2]), dir(flag_values)) _defines.DEFINE_float( flag_name3, None, description, flag_values=flag_values) self.assertEqual( sorted([flag_name1, flag_name2, flag_name3]), dir(flag_values))
def test_len(self): fv = _flagvalues.FlagValues() self.assertEmpty(fv) self.assertFalse(fv) _defines.DEFINE_boolean('boolean', False, 'help', flag_values=fv) self.assertLen(fv, 1) self.assertTrue(fv) _defines.DEFINE_boolean('bool', False, 'help', short_name='b', flag_values=fv) self.assertLen(fv, 3) self.assertTrue(fv)
def _define_flag_and_validators(self, first_validator, second_validator): local_flags = _flagvalues.FlagValues() _defines.DEFINE_integer('test_flag', 2, 'test flag', flag_values=local_flags) _validators.register_validator('test_flag', first_validator, message='', flag_values=local_flags) _validators.register_validator('test_flag', second_validator, message='', flag_values=local_flags) argv = ('./program', ) local_flags(argv)
def test_using_default_value_not_none(self): fv = _flagvalues.FlagValues() _defines.DEFINE_string('default_foo', 'foo', 'help', flag_values=fv) fv.mark_as_parsed() self.assertTrue(fv['default_foo'].using_default_value) fv(['', '--default_foo=foo']) self.assertFalse(fv['default_foo'].using_default_value) fv(['', '--default_foo=notFoo']) self.assertFalse(fv['default_foo'].using_default_value) fv.unparse_flags() self.assertTrue(fv['default_foo'].using_default_value) fv(['', '--default_foo=alsoNotFoo']) self.assertFalse(fv['default_foo'].using_default_value)
def setUp(self): super(MarkBoolFlagsAsMutualExclusiveTest, self).setUp() self.flag_values = _flagvalues.FlagValues() _defines.DEFINE_boolean('false_1', False, 'default false 1', flag_values=self.flag_values) _defines.DEFINE_boolean('false_2', False, 'default false 2', flag_values=self.flag_values) _defines.DEFINE_boolean('true_1', True, 'default true 1', flag_values=self.flag_values) _defines.DEFINE_integer('non_bool', None, 'non bool', flag_values=self.flag_values)