Skip to content

Commit bc423c4

Browse files
committed
Merge pull request #57 from hydralabs/xml-module-tests
Rewrite xml tests to better support third party libs
2 parents 9d68dfa + ac2d73c commit bc423c4

File tree

1 file changed

+61
-23
lines changed

1 file changed

+61
-23
lines changed

pyamf/tests/test_xml.py

Lines changed: 61 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,31 @@
1515
from pyamf import util
1616

1717

18-
class ElementTreeTestCase(unittest.TestCase):
18+
class _BaseTestCase(unittest.TestCase):
1919
"""
20-
Tests the type mappings.
20+
:ivar mod: The concrete xml module that will be used for this test.
2121
"""
2222

23-
xml = '<foo bar="baz" />'
23+
def setUp(self):
24+
try:
25+
self.etree = util.get_module(self.mod)
26+
except ImportError:
27+
self.skipTest('%r is not available' % (self.mod,))
28+
29+
previous_etree = pyamf.set_default_etree(self.etree)
30+
31+
if previous_etree:
32+
self.addCleanup(lambda: pyamf.set_default_etree(previous_etree))
33+
34+
@classmethod
35+
def cls_factory(cls, mod):
36+
mod_name = mod.replace('.', '_')
37+
38+
new_cls = type('%s_%s' % (cls.__name__, mod_name), (cls,), {})
39+
40+
new_cls.mod = mod
41+
42+
return new_cls
2443

2544
def check_amf0(self, bytes, xml):
2645
b = util.BufferedByteStream(bytes)
@@ -42,29 +61,23 @@ def check_amf3(self, bytes, xml):
4261
self.assertEqual(l >> 1, b.remaining())
4362
self.assertEqual(b.read(), xml)
4463

64+
def fromstring(self, xml):
65+
return self.etree.fromstring(xml)
4566

46-
for mod in pyamf.xml.ETREE_MODULES:
47-
name = 'test_' + mod.replace('.', '_')
67+
def tostring(self, element):
68+
return self.etree.tostring(element)
4869

49-
def check_etree(self):
50-
# holy hack batman
51-
import inspect
52-
53-
mod = inspect.stack()[1][0].f_locals['testMethod'].__name__[5:]
54-
mod = mod.replace('_', '.')
55-
56-
try:
57-
etree = util.get_module(mod)
58-
except ImportError:
59-
self.skipTest('%r is not available' % (mod,))
6070

61-
element = etree.fromstring(self.xml)
62-
xml = etree.tostring(element)
71+
class ElementTreeTestCase(_BaseTestCase):
72+
"""
73+
Tests the type mappings.
74+
"""
6375

64-
old = pyamf.set_default_etree(etree)
76+
xml = '<foo bar="baz" />'
6577

66-
if old:
67-
self.addCleanup(lambda x: pyamf.set_default_etree(x), old)
78+
def test_elementtree(self):
79+
element = self.fromstring(self.xml)
80+
xml = self.tostring(element)
6881

6982
bytes = pyamf.encode(element, encoding=pyamf.AMF0).getvalue()
7083
self.check_amf0(bytes, xml)
@@ -78,6 +91,31 @@ def check_etree(self):
7891
new_element = pyamf.decode(bytes, encoding=pyamf.AMF3).next()
7992
self.assertIdentical(type(element), type(new_element))
8093

81-
check_etree.__name__ = name
8294

83-
setattr(ElementTreeTestCase, name, check_etree)
95+
"""
96+
This chunk of code turns any subclass of _BaseTestCase in to a set of test
97+
cases that have a python xml lib module attached to them. This allows the test
98+
writer to write the test once and ensure that it works for all the supported
99+
xml modules.
100+
"""
101+
for name, value in globals().copy().iteritems():
102+
try:
103+
is_subclass = issubclass(value, _BaseTestCase)
104+
except TypeError:
105+
is_subclass = False
106+
107+
if not is_subclass:
108+
continue
109+
110+
if value is _BaseTestCase:
111+
continue
112+
113+
for mod in pyamf.xml.ETREE_MODULES:
114+
mod_test_class = value.cls_factory(mod)
115+
globals()[mod_test_class.__name__] = mod_test_class
116+
117+
del mod, mod_test_class
118+
119+
globals().pop(name)
120+
121+
del name, value, is_subclass

0 commit comments

Comments
 (0)