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 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_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_invalid_flag_name(self): with self.assertRaises(_exceptions.Error): _defines.DEFINE_boolean('test ', 0, '') with self.assertRaises(_exceptions.Error): _defines.DEFINE_boolean(' test', 0, '') with self.assertRaises(_exceptions.Error): _defines.DEFINE_boolean('te st', 0, '') with self.assertRaises(_exceptions.Error): _defines.DEFINE_boolean('', 0, '') with self.assertRaises(_exceptions.Error): _defines.DEFINE_boolean(1, 0, '')
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 _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_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 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)
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_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 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) _defines.DEFINE_boolean('flag_not_none', False, 'false default', flag_values=self.flag_values)