From ddb39ad642a9294a7bc9eb833e64169e3b2e877a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eray=20=C3=96zkural?= Date: Wed, 2 Nov 2005 17:09:15 +0000 Subject: [PATCH] * add a simple metaclass for testing automatic structure equivalence using dict, called autoeq * fix: use autoeq to implement the structural equivalence test in xmlfiletests --- pisi/oo.py | 15 +++++++++++++-- pisi/xmlfile.py | 2 +- tests/xmlfiletests.py | 7 ++++++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/pisi/oo.py b/pisi/oo.py index ca6949fd..10e4689d 100644 --- a/pisi/oo.py +++ b/pisi/oo.py @@ -23,6 +23,17 @@ class autosuper(type): class autosuprop(autosuper, autoprop): pass - + +class autoeq(type): + "useful for structures" + def __init__(cls, name, bases, dict): + super(autoeq, cls).__init__(name, bases, dict) + def equal(self, other): + return self.__dict__ == other.__dict__ + cls.__eq__ = equal + class Struct: - def __init__(self, **entries): self.__dict__.update(entries) + __metaclass__ = autoeq + + def __init__(self, **entries): + self.__dict__.update(entries) diff --git a/pisi/xmlfile.py b/pisi/xmlfile.py index 322ef779..1a6e79f0 100644 --- a/pisi/xmlfile.py +++ b/pisi/xmlfile.py @@ -143,7 +143,7 @@ class Writer(formatter.DumbWriter): self.atbreak = 0 -class autoxml(oo.autosuper): +class autoxml(oo.autosuper, oo.autoprop, oo.autoeq): """High-level automatic XML transformation interface for xmlfile. The idea is to declare a class for each XML tag. Inside the class the tags and attributes nested in the tag are further diff --git a/tests/xmlfiletests.py b/tests/xmlfiletests.py index d8dba404..16ade4c4 100644 --- a/tests/xmlfiletests.py +++ b/tests/xmlfiletests.py @@ -87,6 +87,11 @@ class AutoXmlTestCase(testcase.TestCase): a.write('/tmp/a2.xml') a2 = self.A() a2.read('/tmp/a2.xml') - self.assertEqual(a, a2) + self.assertEqual(a.name, a2.name) + self.assertEqual(a.email, a2.email) + self.assertEqual(a.description, a2.description) + self.assertEqual(a.comment, a2.comment) + self.assertEqual(a.href, a2.href) + self.assertEqual(a.otherInfo, a2.otherInfo) suite = unittest.makeSuite(AutoXmlTestCase)