* add a simple metaclass for testing automatic structure equivalence
using dict, called autoeq * fix: use autoeq to implement the structural equivalence test in xmlfiletests
This commit is contained in:
+13
-2
@@ -23,6 +23,17 @@ class autosuper(type):
|
|||||||
|
|
||||||
class autosuprop(autosuper, autoprop):
|
class autosuprop(autosuper, autoprop):
|
||||||
pass
|
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:
|
class Struct:
|
||||||
def __init__(self, **entries): self.__dict__.update(entries)
|
__metaclass__ = autoeq
|
||||||
|
|
||||||
|
def __init__(self, **entries):
|
||||||
|
self.__dict__.update(entries)
|
||||||
|
|||||||
+1
-1
@@ -143,7 +143,7 @@ class Writer(formatter.DumbWriter):
|
|||||||
self.atbreak = 0
|
self.atbreak = 0
|
||||||
|
|
||||||
|
|
||||||
class autoxml(oo.autosuper):
|
class autoxml(oo.autosuper, oo.autoprop, oo.autoeq):
|
||||||
"""High-level automatic XML transformation interface for xmlfile.
|
"""High-level automatic XML transformation interface for xmlfile.
|
||||||
The idea is to declare a class for each XML tag. Inside the
|
The idea is to declare a class for each XML tag. Inside the
|
||||||
class the tags and attributes nested in the tag are further
|
class the tags and attributes nested in the tag are further
|
||||||
|
|||||||
@@ -87,6 +87,11 @@ class AutoXmlTestCase(testcase.TestCase):
|
|||||||
a.write('/tmp/a2.xml')
|
a.write('/tmp/a2.xml')
|
||||||
a2 = self.A()
|
a2 = self.A()
|
||||||
a2.read('/tmp/a2.xml')
|
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)
|
suite = unittest.makeSuite(AutoXmlTestCase)
|
||||||
|
|||||||
Reference in New Issue
Block a user