def test_fail_fast_single_flag_and_skip_remaining_validators(self): def raise_unexpected_error(x): del x raise _exceptions.ValidationError('Should not be raised.') _defines.DEFINE_float('flag_1', None, 'flag 1', flag_values=self.flag_values) _defines.DEFINE_float('flag_2', 4.2, 'flag 2', flag_values=self.flag_values) _validators.mark_flag_as_required('flag_1', flag_values=self.flag_values) _validators.register_validator('flag_1', raise_unexpected_error, flag_values=self.flag_values) _validators.register_multi_flags_validator( ['flag_2', 'flag_1'], raise_unexpected_error, flag_values=self.flag_values) argv = ('./program', '') expected = ( 'flag --flag_1=None: Flag --flag_1 must have a value other than None.' ) with self.assertRaises(_exceptions.IllegalFlagValueError) as cm: self.flag_values(argv) self.assertEqual(expected, str(cm.exception))
def test_catch_multiple_flags_as_none_at_program_start(self): _defines.DEFINE_float('float_flag_1', None, 'string flag 1', flag_values=self.flag_values) _defines.DEFINE_float('float_flag_2', None, 'string flag 2', flag_values=self.flag_values) _validators.mark_flags_as_required(['float_flag_1', 'float_flag_2'], flag_values=self.flag_values) argv = ('./program', '') expected = ( 'flag --float_flag_1=None: Flag --float_flag_1 must have a value ' 'other than None.\n' 'flag --float_flag_2=None: Flag --float_flag_2 must have a value ' 'other than None.') with self.assertRaises(_exceptions.IllegalFlagValueError) as cm: self.flag_values(argv) self.assertEqual(expected, str(cm.exception))
def test_fail_fast_multi_flag_and_skip_remaining_validators(self): def raise_expected_error(x): del x raise _exceptions.ValidationError('Expected error.') def raise_unexpected_error(x): del x raise _exceptions.ValidationError('Got unexpected error.') _defines.DEFINE_float('flag_1', 5.1, 'flag 1', flag_values=self.flag_values) _defines.DEFINE_float('flag_2', 10.0, 'flag 2', flag_values=self.flag_values) _validators.register_multi_flags_validator( ['flag_1', 'flag_2'], raise_expected_error, flag_values=self.flag_values) _validators.register_multi_flags_validator( ['flag_2', 'flag_1'], raise_unexpected_error, flag_values=self.flag_values) _validators.register_validator('flag_1', raise_unexpected_error, flag_values=self.flag_values) _validators.register_validator('flag_2', raise_unexpected_error, flag_values=self.flag_values) argv = ('./program', '') expected = ('flags flag_1=5.1, flag_2=10.0: Expected error.') with self.assertRaises(_exceptions.IllegalFlagValueError) as cm: self.flag_values(argv) self.assertEqual(expected, str(cm.exception))
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))