Package pytilities :: Package test :: Package delegation :: Module delegation
[hide private]
[frames] | no frames]

Source Code for Module pytilities.test.delegation.delegation

  1  # Copyright (C) 2010 Tim Diels <limyreth@users.sourceforge.net> 
  2  #  
  3  # This file is part of pytilities. 
  4  #  
  5  # pytilities is free software: you can redistribute it and/or modify 
  6  # it under the terms of the GNU General Public License as published by 
  7  # the Free Software Foundation, either version 3 of the License, or 
  8  # (at your option) any later version. 
  9  #  
 10  # pytilities is distributed in the hope that it will be useful, 
 11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 13  # GNU General Public License for more details. 
 14  #  
 15  # You should have received a copy of the GNU General Public License 
 16  # along with pytilities.  If not, see <http://www.gnu.org/licenses/>. 
 17  # 
 18   
 19  __docformat__ = 'reStructuredText' 
 20   
 21  import unittest 
 22   
 23  from pytilities.delegation import delegator_factory, delegated 
 24  from pytilities import AttributeCollectionBase 
25 26 @delegator_factory() 27 -class A(object):
28 - def __init__(self):
29 self.reset()
30
31 - def reset(self):
32 self._f = self._xw = None 33 self._g = self._xr = False
34 35 @delegated("public")
36 - def f(self, a, b, c): # func with args
37 self._f = (a, b , c)
38 39 @delegated()
40 - def g(self): # func withot args
41 self._g = True 42 43 @delegated(modifiers='w') # only delegate write 44 @property
45 - def x(self):
46 self._xr = True
47 48 @x.setter
49 - def x(self, value):
50 self._xw = value
51 52 # default extends/includes public 53 p = A._delegator_factory.get_profile('default') 54 p |= A._delegator_factory.get_profile('public')
55 56 # some beasty that delegates only public 57 -class Public(AttributeCollectionBase):
58 - def __init__(self, a):
61
62 # some beasty that delegates the default 63 -class Default(AttributeCollectionBase):
64 - def __init__(self, a):
67 68 @property
69 - def x(self):
70 return 666
71
72 73 -class MainTestCase(unittest.TestCase):
74 - def test_public(self):
75 a = A() 76 p = Public(a) 77 78 # public 79 p.f(5, None, 3) 80 self.assertEquals(a._f, (5, None, 3)) 81 a.reset() 82 83 self.assertRaises(AttributeError, lambda: p.g) 84 self.assertRaises(AttributeError, lambda: p.x) 85 86 # default 87 d = Default(a) 88 d.f(5, None, 3) 89 self.assertEquals(a._f, (5, None, 3)) 90 a.reset() 91 92 d.g() 93 self.assert_(a._g) 94 a.reset() 95 96 d.x = 2 97 self.assertEquals(a._xw, 2) 98 a.reset() 99 100 self.assertEquals(d.x, 666)
101