1
2
3
4
5
6
7
8
9 """Unit tests for PyMVPA args helpers"""
10
11 import unittest
12 from mvpa.misc.args import *
13
14 if __debug__:
15 from mvpa.base import debug
18
20 """Test if we are not missing basic parts"""
21 kwargs = {'a':1, 'slave_a':3, 'slave_z':4, 'slave_slave_z':5, 'c':3}
22
23 res = split_kwargs(kwargs, ['slave_'])
24 self.failUnless(res.has_key('slave_') and res.has_key(''))
25 self.failUnless(res['slave_'] == {'a':3, 'z':4, 'slave_z':5})
26 self.failUnless(res[''] == {'a':1, 'c':3})
27
28 res = split_kwargs(kwargs)
29 self.failUnless(res.keys() == [''])
30 self.failUnless(res[''] == kwargs)
31
32
34 """Test the group_kwargs decorator"""
35
36 selftop = self
37
38 class C1(object):
39
40 @group_kwargs(prefixes=['slave_'], assign=True)
41 def __init__(self, **kwargs):
42 selftop.failUnless(hasattr(self, '_slave_kwargs'))
43 self.method_passedempty()
44 self.method_passed(1, custom_p1=144, bugax=1)
45 self.method_filtered(1, custom_p1=123)
46
47 @group_kwargs(prefixes=['custom_'], passthrough=True)
48 def method_passedempty(self, **kwargs):
49
50 selftop.failUnless('custom_kwargs' in kwargs)
51
52 @group_kwargs(prefixes=['custom_', 'buga'], passthrough=True)
53 def method_passed(self, a, custom_kwargs, bugakwargs, **kwargs):
54
55 selftop.failUnless(custom_kwargs == {'p1':144})
56 selftop.failUnless(bugakwargs == {'x':1})
57 selftop.failUnless(not hasattr(self, '_custom_kwargs'))
58
59 @group_kwargs(prefixes=['custom_'])
60 def method_filtered(self, a, **kwargs):
61
62 selftop.failUnlessEqual(a, 1)
63 selftop.failUnless(not 'custom_kwargs' in kwargs)
64
65 def method(self):
66 return 123
67
68 @group_kwargs(prefixes=['xxx'])
69 def method_decorated(self):
70 return 124
71
72 c1 = C1(slave_p1=1, p1=2)
73 self.failUnless(c1.method() == 123)
74 self.failUnless(c1.method_decorated() == 124)
75
79
80
81 if __name__ == '__main__':
82 import runner
83