diff --git a/test/test_option.py b/test/test_option.py new file mode 100644 index 0000000..a9ce825 --- /dev/null +++ b/test/test_option.py @@ -0,0 +1,33 @@ +import autopath + +from tiramisu.option import BoolOption, IntOption + + +def test_option_comparison(): + dummy1 = BoolOption('dummy1', 'doc dummy') + dummy2 = BoolOption('dummy2', 'doc dummy') + dummy3 = BoolOption('dummy1', 'doc dummy') + assert dummy1 != dummy2 + assert dummy1 == dummy3 + + +def test_option_comparison_obj(): + dummy1 = BoolOption('dummy1', 'doc dummy') + dummy2 = IntOption('dummy1', 'doc dummy') + assert dummy1 != dummy2 + + +def test_option_comparison_advanced(): + dummy1 = BoolOption('dummy1', 'doc dummy') + dummy2 = BoolOption('dummy1', 'doc dummy') + dummy3 = BoolOption('dummy1', 'doc dummy', None) + dummy4 = BoolOption('dummy1', 'doc dummy', True) + dummy5 = BoolOption('dummy1', 'doc dummy', multi=True) + dummy6 = BoolOption('dummy1', 'doc dummy', properties=tuple()) + dummy7 = BoolOption('dummy1', 'doc dummy', properties=tuple('new',)) + assert dummy1 == dummy2 + assert dummy1 == dummy3 + assert dummy1 != dummy4 + assert dummy1 != dummy5 + assert dummy1 == dummy6 + assert dummy1 != dummy7 diff --git a/tiramisu/option.py b/tiramisu/option.py index 005bfa6..038cee8 100644 --- a/tiramisu/option.py +++ b/tiramisu/option.py @@ -53,7 +53,7 @@ def valid_name(name): class BaseInformation(object): - __slots__ = ('_informations') + __slots__ = ('_informations',) def optimpl_set_information(self, key, value): """updates the information's attribute @@ -172,6 +172,37 @@ class Option(BaseInformation): ' must be a tuple').format(type(properties), self._name)) self._properties = properties # 'hidden', 'disabled'... + def __eq__(self, other): + "Option comparison" + if not isinstance(other, Option): + return False + a = list(self.__slots__ + Option.__slots__ + BaseInformation.__slots__) + #a.remove('_default_multi') + a.remove('_master_slaves') + a.remove('_multitype') + for var in a: + try: + val1 = getattr(self, var) + not_in1 = False + except: + not_in1 = True + try: + val2 = getattr(other, var) + not_in2 = False + except: + not_in2 = True + if True in (not_in1, not_in2): + if not_in1 != not_in2: + return False + elif val1 != val2: + return False + return True + + def __ne__(self, other): + if not isinstance(other, Option): + return False + return not self == other + def optimpl_validate(self, value, context=None, validate=True): """ :param value: the option's value @@ -342,7 +373,7 @@ class ChoiceOption(Option): class BoolOption(Option): - __slots__ = ('_opt_type') + __slots__ = ('_opt_type',) _opt_type = 'bool' def _validate(self, value): @@ -350,7 +381,7 @@ class BoolOption(Option): class IntOption(Option): - __slots__ = ('_opt_type') + __slots__ = ('_opt_type',) _opt_type = 'int' def _validate(self, value): @@ -358,7 +389,7 @@ class IntOption(Option): class FloatOption(Option): - __slots__ = ('_opt_type') + __slots__ = ('_opt_type',) _opt_type = 'float' def _validate(self, value): @@ -366,7 +397,7 @@ class FloatOption(Option): class StrOption(Option): - __slots__ = ('_opt_type') + __slots__ = ('_opt_type',) _opt_type = 'string' def _validate(self, value): @@ -374,7 +405,7 @@ class StrOption(Option): class UnicodeOption(Option): - __slots__ = ('_opt_type') + __slots__ = ('_opt_type',) _opt_type = 'unicode' _empty = u'' @@ -428,7 +459,7 @@ class IPOption(Option): class NetworkOption(Option): - __slots__ = ('_opt_type') + __slots__ = ('_opt_type',) _opt_type = 'network' def _validate(self, value): @@ -440,7 +471,7 @@ class NetworkOption(Option): class NetmaskOption(Option): - __slots__ = ('_opt_type') + __slots__ = ('_opt_type',) _opt_type = 'netmask' def __init__(self, name, doc, default=None, default_multi=None,