← Back to team overview

configglue team mailing list archive

[Merge] lp:~ricardokirkner/configglue/improve-equality-checks into lp:configglue

 

Ricardo Kirkner has proposed merging lp:~ricardokirkner/configglue/improve-equality-checks into lp:configglue.

Requested reviews:
  Configglue developers (configglue)

For more details, see:
https://code.launchpad.net/~ricardokirkner/configglue/improve-equality-checks/+merge/68977

Overview
========

This branch improves the equality checks on Option subclasses
-- 
https://code.launchpad.net/~ricardokirkner/configglue/improve-equality-checks/+merge/68977
Your team Configglue developers is requested to review the proposed merge of lp:~ricardokirkner/configglue/improve-equality-checks into lp:configglue.
=== modified file 'configglue/schema.py'
--- configglue/schema.py	2011-07-19 01:10:19 +0000
+++ configglue/schema.py	2011-07-23 21:21:31 +0000
@@ -295,12 +295,15 @@
             equal = (
                 type(self) == type(other) and
                 self.name == other.name and
+                self.short_name == other.short_name and
                 self.raw == other.raw and
                 self.fatal == other.fatal and
                 self.default == other.default and
-                self.help == other.help)
+                self.help == other.help and
+                self.action == other.action)
             if self.section is not None and other.section is not None:
-                # only test for section name to avoid recursion
+                # sections might be different while the options are the
+                # same; the section names though should be the same
                 equal &= self.section.name == other.section.name
             else:
                 equal &= (self.section is None and other.section is None)
@@ -413,9 +416,20 @@
             item = StringOption()
         self.item = item
         self.require_parser = item.require_parser
-        self.raw = item.raw
+        self.raw = raw or item.raw
         self.remove_duplicates = remove_duplicates
 
+    def __eq__(self, other):
+        equal = super(ListOption, self).__eq__(other)
+        if equal:
+            # we can be sure both objects are of the same type by now
+            equal &= (self.item == other.item and
+                self.require_parser == other.require_parser and
+                self.raw == other.raw and
+                self.remove_duplicates == other.remove_duplicates)
+
+        return equal
+
     def _get_default(self):
         return []
 
@@ -460,6 +474,14 @@
             default=default, fatal=fatal, help=help, action=action,
             short_name=short_name)
 
+    def __eq__(self, other):
+        equal = super(StringOption, self).__eq__(other)
+        if equal:
+            # we can be sure both objects are of the same type by now
+            equal &= self.null == other.null
+
+        return equal
+
     def _get_default(self):
         return '' if not self.null else None
 
@@ -503,6 +525,14 @@
             short_name=short_name)
         self.length = length
 
+    def __eq__(self, other):
+        equal = super(TupleOption, self).__eq__(other)
+        if equal:
+            # we can be sure both objects are of the same type by now
+            equal &= self.length == other.length
+
+        return equal
+
     def _get_default(self):
         return ()
 
@@ -560,6 +590,16 @@
             default=default, fatal=fatal, help=help, action=action,
             short_name=short_name)
 
+    def __eq__(self, other):
+        equal = super(DictOption, self).__eq__(other)
+        if equal:
+            # we can be sure both objects are of the same type by now
+            equal &= (self.spec == other.spec and
+                self.strict == other.strict and
+                self.item == other.item)
+
+        return equal
+
     def _get_default(self):
         default = {}
         for key, value in self.spec.items():

=== modified file 'configglue/tests/test_schema.py'
--- configglue/tests/test_schema.py	2011-07-19 01:10:19 +0000
+++ configglue/tests/test_schema.py	2011-07-23 21:21:31 +0000
@@ -368,6 +368,14 @@
         opt = self.cls(short_name='f')
         self.assertEqual(opt.short_name, 'f')
 
+    def test_equal(self):
+        """Test StringOption equality."""
+        option1 = StringOption()
+        option2 = StringOption(null=True)
+
+        self.assertEqual(option1, option1)
+        self.assertNotEqual(option1, option2)
+
 
 class TestIntOption(unittest.TestCase):
     cls = IntOption
@@ -570,6 +578,24 @@
         opt = self.cls(short_name='f')
         self.assertEqual(opt.short_name, 'f')
 
+    def test_equal(self):
+        """Test ListOption equality."""
+        option1 = ListOption()
+        option2 = ListOption(name='foo')
+        option3 = ListOption(item=StringOption())
+        option4 = ListOption(item=DictOption())
+        option5 = ListOption(raw=True)
+        option6 = ListOption(remove_duplicates=True)
+        option7 = ListOption(raw=False, item=StringOption(raw=True))
+
+        self.assertEqual(option1, option1)
+        self.assertNotEqual(option1, option2)
+        self.assertEqual(option1, option3)
+        self.assertNotEqual(option1, option4)
+        self.assertNotEqual(option1, option5)
+        self.assertNotEqual(option1, option6)
+        self.assertNotEqual(option1, option7)
+
 
 class TestTupleOption(unittest.TestCase):
     cls = TupleOption
@@ -638,6 +664,14 @@
         opt = self.cls(short_name='f')
         self.assertEqual(opt.short_name, 'f')
 
+    def test_equal(self):
+        """Test TupleOption equality."""
+        option1 = TupleOption()
+        option2 = TupleOption(length=2)
+
+        self.assertEqual(option1, option1)
+        self.assertNotEqual(option1, option2)
+
 
 class TestDictOption(unittest.TestCase):
     cls = DictOption
@@ -841,6 +875,20 @@
         opt = self.cls(short_name='f')
         self.assertEqual(opt.short_name, 'f')
 
+    def test_equal(self):
+        """Test DictOption equality."""
+        option1 = DictOption()
+        option2 = DictOption(spec={'foo': BoolOption()})
+        option3 = DictOption(strict=True)
+        option4 = DictOption(item=StringOption())
+        option5 = DictOption(item=IntOption())
+
+        self.assertEqual(option1, option1)
+        self.assertNotEqual(option1, option2)
+        self.assertNotEqual(option1, option3)
+        self.assertEqual(option1, option4)
+        self.assertNotEqual(option1, option5)
+
 
 class TestListOfDictOption(unittest.TestCase):
     def test_parse_lines_of_dict(self):


Follow ups