15
15
from pyamf import util
16
16
17
17
18
- class ElementTreeTestCase (unittest .TestCase ):
18
+ class _BaseTestCase (unittest .TestCase ):
19
19
"""
20
- Tests the type mappings .
20
+ :ivar mod: The concrete xml module that will be used for this test .
21
21
"""
22
22
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
24
43
25
44
def check_amf0 (self , bytes , xml ):
26
45
b = util .BufferedByteStream (bytes )
@@ -42,29 +61,23 @@ def check_amf3(self, bytes, xml):
42
61
self .assertEqual (l >> 1 , b .remaining ())
43
62
self .assertEqual (b .read (), xml )
44
63
64
+ def fromstring (self , xml ):
65
+ return self .etree .fromstring (xml )
45
66
46
- for mod in pyamf . xml . ETREE_MODULES :
47
- name = 'test_' + mod . replace ( '.' , '_' )
67
+ def tostring ( self , element ) :
68
+ return self . etree . tostring ( element )
48
69
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 ,))
60
70
61
- element = etree .fromstring (self .xml )
62
- xml = etree .tostring (element )
71
+ class ElementTreeTestCase (_BaseTestCase ):
72
+ """
73
+ Tests the type mappings.
74
+ """
63
75
64
- old = pyamf . set_default_etree ( etree )
76
+ xml = '<foo bar="baz" />'
65
77
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 )
68
81
69
82
bytes = pyamf .encode (element , encoding = pyamf .AMF0 ).getvalue ()
70
83
self .check_amf0 (bytes , xml )
@@ -78,6 +91,31 @@ def check_etree(self):
78
91
new_element = pyamf .decode (bytes , encoding = pyamf .AMF3 ).next ()
79
92
self .assertIdentical (type (element ), type (new_element ))
80
93
81
- check_etree .__name__ = name
82
94
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