diff options
author | Jason Gerard DeRose <jderose@redhat.com> | 2009-01-14 09:56:10 -0700 |
---|---|---|
committer | Jason Gerard DeRose <jderose@redhat.com> | 2009-01-14 09:56:10 -0700 |
commit | 05514292dcf2448f0340e09cd0da88f815c43c5b (patch) | |
tree | 1b7d1956b544f5e128ad0671c57b9470639df240 | |
parent | 8cc38e681f9caca838540511664337f964302f56 (diff) | |
download | freeipa-05514292dcf2448f0340e09cd0da88f815c43c5b.tar.gz freeipa-05514292dcf2448f0340e09cd0da88f815c43c5b.tar.xz freeipa-05514292dcf2448f0340e09cd0da88f815c43c5b.zip |
New Param: Flag now fill-in default=False and also forces default to be a bool
-rw-r--r-- | ipalib/parameter.py | 27 | ||||
-rw-r--r-- | tests/test_ipalib/test_parameter.py | 20 |
2 files changed, 43 insertions, 4 deletions
diff --git a/ipalib/parameter.py b/ipalib/parameter.py index 31bd9db2b..0617c84da 100644 --- a/ipalib/parameter.py +++ b/ipalib/parameter.py @@ -638,11 +638,34 @@ class Flag(Bool): """ A boolean parameter that always gets filled in with a default value. - This `Bool` subclass forces ``autofill=True`` in `Flag.__init__()`. + This `Bool` subclass forces ``autofill=True`` in `Flag.__init__()`. If no + default is provided, it also fills in a default value of ``False``. + Lastly, unlike the `Bool` class, the default must be either ``True`` or + ``False`` and cannot be ``None``. + + For example: + + >>> Flag('my_flag') + Flag('my_flag', autofill=True, default=False) + >>> Flag('my_flag', default=True) # Do this for default of True + Flag('my_flag', autofill=True, default=True) + + Also note that creating a `Flag` instance with ``autofill=False`` will have + no effect. For example: + + >>> Flag('my_flag', autofill=False) # autofill will still be True + Flag('my_flag', autofill=True, default=False) """ def __init__(self, name, *rules, **kw): kw['autofill'] = True + if 'default' not in kw: + kw['default'] = False + if type(kw['default']) is not bool: + default = kw['default'] + raise TypeError( + TYPE_ERROR % ('default', bool, default, type(default)) + ) super(Flag, self).__init__(name, *rules, **kw) @@ -751,7 +774,7 @@ class Bytes(Param): class Str(Bytes): """ - A parameter for Unicode text (stored in the``unicode`` type). + A parameter for Unicode text (stored in the ``unicode`` type). This class is named *Str* instead of *Unicode* so it's aligned with the Python v3 ``(str, unicode) => (bytes, str)`` clean-up. See: diff --git a/tests/test_ipalib/test_parameter.py b/tests/test_ipalib/test_parameter.py index 4a6fee55e..d98888ab8 100644 --- a/tests/test_ipalib/test_parameter.py +++ b/tests/test_ipalib/test_parameter.py @@ -554,22 +554,38 @@ class test_Flag(ClassChecker): """ Test the `ipalib.parameter.Flag.__init__` method. """ + # Test with no kwargs: o = self.cls('my_flag') assert o.type is bool assert isinstance(o, parameter.Bool) assert o.autofill is True + assert o.default is False - # Test with autofill=False - o = self.cls('my_flag', autofill=False) + # Test that TypeError is raise if default is not a bool: + e = raises(TypeError, self.cls, 'my_flag', default=None) + assert str(e) == TYPE_ERROR % ('default', bool, None, NoneType) + + # Test with autofill=False, default=True + o = self.cls('my_flag', autofill=False, default=True) assert o.autofill is True + assert o.default is True # Test when cloning: orig = self.cls('my_flag') for clone in [orig.clone(), orig.clone(autofill=False)]: assert clone.autofill is True + assert clone.default is False assert clone is not orig assert type(clone) is self.cls + # Test when cloning with default=True/False + orig = self.cls('my_flag') + assert orig.clone().default is False + assert orig.clone(default=True).default is True + orig = self.cls('my_flag', default=True) + assert orig.clone().default is True + assert orig.clone(default=False).default is False + class test_Bytes(ClassChecker): """ |