support comparison of SimpleCnf() with non-SimpleCnf() objects
authorPhilipp Gesang <philipp.gesang@intra2net.com>
Mon, 5 Nov 2018 09:12:35 +0000 (10:12 +0100)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Mon, 5 Nov 2018 11:21:05 +0000 (12:21 +0100)
Since __eq__ operations are polymorphic, crashing on different
``types'' is not a good idea.

src/simple_cnf.py
test/test_simple_cnf.py

index 23b5ca4..ae7c264 100644 (file)
@@ -647,5 +647,8 @@ class SimpleCnf(object):
         """
         key_func = lambda cnf_var_entry: cnf_var_entry['number']
 
+        if isinstance (other_cnf, SimpleCnf) is False:
+            return False
+
         return sorted(self.__cnfvars, key=key_func) \
             == sorted(other_cnf.__cnfvars, key=key_func)   # pylint: disable=protected-access
index bdc9883..c0583d9 100755 (executable)
@@ -193,6 +193,10 @@ class SimpleCnfTest(unittest.TestCase):
         self.assertEqual(self.cnf['testvar'], self.cnf[61])
         self.assertEqual(self.cnf, self.cnf)
         self.assertNotEqual(self.cnf[56], self.cnf[57])
+        self.assertNotEqual(SimpleCnf ({"cnf": []}), None)
+        self.assertNotEqual(SimpleCnf ({"cnf": []}), 42)
+        self.assertNotEqual(SimpleCnf ({"cnf": []}), "got wood?")
+        self.assertEqual(SimpleCnf (), SimpleCnf ({"cnf": []}))
 
     def test_len(self):
         """ test method :py:meth:`SimpleCnf.__len__` """