Beispiel #1
0
 def test_case_insensitive_accepts_string_default(self):
     f = _flag.EnumClassFlag('fruit',
                             'oranGe',
                             'A sample enum flag.',
                             Fruit,
                             case_sensitive=False)
     self.assertEqual(Fruit.ORANGE, f.value)
Beispiel #2
0
 def test_case_sensitive_rejects_default_with_wrong_case(self):
     with self.assertRaises(_exceptions.IllegalFlagValueError):
         _flag.EnumClassFlag('fruit',
                             'oranGe',
                             'A sample enum flag.',
                             Fruit,
                             case_sensitive=True)
Beispiel #3
0
 def test_help_text_case_sensitive(self, helptext_input, helptext_output):
     f = _flag.EnumClassFlag('fruit',
                             None,
                             helptext_input,
                             Fruit,
                             case_sensitive=True)
     self.assertEqual(helptext_output, f.help)
Beispiel #4
0
def DEFINE_enum_class(  # pylint: disable=invalid-name,redefined-builtin
        name,
        default,
        enum_class,
        help,
        flag_values=_flagvalues.FLAGS,
        module_name=None,
        case_sensitive=False,
        **args):
    """Registers a flag whose value can be the name of enum members.

  Args:
    name: str, the flag name.
    default: Enum|str|None, the default value of the flag.
    enum_class: class, the Enum class with all the possible values for the flag.
    help: str, the help message.
    flag_values: FlagValues, the FlagValues instance with which the flag will be
      registered. This should almost never need to be overridden.
    module_name: str, the name of the Python module declaring this flag. If not
      provided, it will be computed using the stack trace of this call.
    case_sensitive: bool, whether to map strings to members of the enum_class
      without considering case.
    **args: dict, the extra keyword args that are passed to Flag __init__.

  Returns:
    a handle to defined flag.
  """
    return DEFINE_flag(
        _flag.EnumClassFlag(name,
                            default,
                            help,
                            enum_class,
                            case_sensitive=case_sensitive,
                            **args), flag_values, module_name)
Beispiel #5
0
def DEFINE_enum_class(name,
                      default,
                      enum_class,
                      help=None,
                      flag_values=_flagvalues.FLAGS,
                      module_name=None,
                      **args):
    """Registers a flag whose value can be the name of enum members.

    Args:
      name: str, the flag name.
      default: Enum|str|None, the default value of the flag.
      enum_class: class, the Enum class with all the possible values for the flag.
      help: str, the help message.
      flag_values: FlagValues, the FlagValues instance with which the flag will
          be registered. This should almost never need to be overridden.
      module_name: str, the name of the Python module declaring this flag.
          If not provided, it will be computed using the stack trace of this call.
      **args: dict, the extra keyword args that are passed to Flag __init__.
    """
    DEFINE_flag(_flag.EnumClassFlag(name, default, help, enum_class, **args),
                flag_values, module_name)
 def test_default_value_does_not_exist(self):
   with self.assertRaises(_exceptions.IllegalFlagValueError):
     _flag.EnumClassFlag('fruit', 'BANANA', 'help', Fruit)
 def test_accepts_string_default(self):
   f = _flag.EnumClassFlag('fruit', 'ORANGE', 'A sample enum flag.', Fruit)
   self.assertEqual(Fruit.ORANGE, f.value)
 def test_accepts_literal_default(self):
   f = _flag.EnumClassFlag('fruit', Fruit.APPLE, 'A sample enum flag.', Fruit)
   self.assertEqual(Fruit.APPLE, f.value)
 def test_requires_non_empty_enum_class(self):
   with self.assertRaises(ValueError):
     _flag.EnumClassFlag('empty', None, 'help', EmptyEnum)
 def test_requires_enum(self):
   with self.assertRaises(TypeError):
     _flag.EnumClassFlag('fruit', None, 'help', ['apple', 'orange'])
 def test_help_text(self, helptext_input, helptext_output):
   f = _flag.EnumClassFlag('fruit', None, helptext_input, Fruit)
   self.assertEqual(helptext_output, f.help)