From ee39385cdf7e0290c9e6034a75142426b27880c7 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Mon, 5 Nov 2018 10:12:35 +0100 Subject: [PATCH] support comparison of SimpleCnf() with non-SimpleCnf() objects Since __eq__ operations are polymorphic, crashing on different ``types'' is not a good idea. --- src/simple_cnf.py | 3 +++ test/test_simple_cnf.py | 4 ++++ 2 files changed, 7 insertions(+), 0 deletions(-) diff --git a/src/simple_cnf.py b/src/simple_cnf.py index 23b5ca4..ae7c264 100644 --- a/src/simple_cnf.py +++ b/src/simple_cnf.py @@ -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 diff --git a/test/test_simple_cnf.py b/test/test_simple_cnf.py index bdc9883..c0583d9 100755 --- a/test/test_simple_cnf.py +++ b/test/test_simple_cnf.py @@ -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__` """ -- 1.7.1