123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- #!/usr/bin/env python3
- """
- test/unit_tests_d/ut_lockable.py: unit test for the MMGen suite's Lockable class
- """
- from mmgen.common import *
- from mmgen.exception import *
- class unit_test(object):
- def run_test(self,name,ut):
- from mmgen.base_obj import AttrCtrl,Lockable
- from decimal import Decimal
- qmsg_r('Testing class AttrCtrl...')
- class MyAttrCtrl(AttrCtrl):
- foo = 'fooval'
- ac = MyAttrCtrl()
- ac.lock()
- ac.foo = 'new fooval'
- ac.foo = 'new fooval2'
- class MyAttrCtrlClsCheck(AttrCtrl):
- _use_class_attr = True
- foo = 'fooval'
- bar = None
- acc = MyAttrCtrlClsCheck()
- acc.lock()
- acc.foo = 'new_fooval'
- acc.foo = 'new_fooval2'
- acc.bar = 'bar val'
- acc.bar = 1 # class attribute bar is None, so can be set to any type
- qmsg('OK')
- qmsg_r('Testing class Lockable...')
- class MyLockable(Lockable): # class has no attrs, like UserOpts
- _autolock = False
- _set_ok = ('foo','baz','alpha','beta','gamma','delta','epsilon')
- _reset_ok = ('bar','baz')
- lc = MyLockable()
- lc.foo = None
- lc.bar = 'barval'
- lc.baz = 1
- lc.qux = 1
- # are these considered set?
- lc.alpha = 0 # yes
- lc.beta = False # yes
- lc.gamma = Decimal('0') # yes
- lc.delta = 0.0 # yes
- lc.epsilon = [] # no
- lc.lock()
- lc.foo = 'fooval2'
- lc.bar = 'barval2'
- lc.bar = 'barval3'
- lc.baz = 2
- lc.baz = 3
- lc.epsilon = [0]
- class MyLockableClsCheck(Lockable): # class has attrs, like GlobalContext
- _autolock = False
- _use_class_attr = True
- _set_ok = ('foo','baz')
- _reset_ok = ('bar','baz')
- foo = None
- bar = 1
- baz = 3.5
- qux = 'quxval'
- lcc = MyLockableClsCheck()
- lcc.lock()
- lcc.foo = 'fooval2' # class attribute foo is None, so can be set to any type
- lcc.bar = 2
- lcc.bar = 3 # bar is in reset list
- lcc.baz = 3.2
- lcc.baz = 3.1 # baz is in both lists
- qmsg('OK')
- qmsg_r('Testing class Lockable with autolock...')
- class MyLockableAutolock(Lockable):
- def __init__(self):
- self.foo = True
- lca = MyLockableAutolock()
- assert lca._autolock == True
- assert lca._lock == True
- assert lca.foo == True
- qmsg('OK')
- qmsg_r('Checking error handling...')
- vmsg('')
- def bad1(): ac.x = 1
- def bad2(): acc.foo = 1
- def bad3(): lc.foo = 'fooval3'
- def bad4(): lc.baz = 'str'
- def bad5(): lcc.bar = 'str'
- def bad6(): lc.qux = 2
- def bad7(): lcc.qux = 'quxval2'
- def bad8(): lcc.foo = 'fooval3'
- def bad9(): lc.x = 1
- def bad10(): lcc.x = 1
- def bad11(): lc.alpha = 0
- def bad12(): lc.beta = False
- def bad13(): lc.gamma = Decimal('0')
- def bad14(): lc.delta = float(0)
- def bad15(): lc.epsilon = [0]
- def bad16(): lca.foo = None
- ut.process_bad_data((
- ('attr (1)', 'AttributeError', 'has no attr', bad1 ),
- ('attr (2)', 'AttributeError', 'has no attr', bad9 ),
- ('attr (3)', 'AttributeError', 'has no attr', bad10 ),
- ('attr type (1)', 'AttributeError', 'type', bad2 ),
- ("attr type (2)", 'AttributeError', 'type', bad4 ),
- ("attr type (3)", 'AttributeError', 'type', bad5 ),
- ("attr (can't set)", 'AttributeError', 'read-only', bad6 ),
- ("attr (can't set)", 'AttributeError', 'read-only', bad7 ),
- ("attr (can't reset)", 'AttributeError', 'reset', bad3 ),
- ("attr (can't reset)", 'AttributeError', 'reset', bad8 ),
- ("attr (can't reset)", 'AttributeError', 'reset', bad11 ),
- ("attr (can't reset)", 'AttributeError', 'reset', bad12 ),
- ("attr (can't reset)", 'AttributeError', 'reset', bad13 ),
- ("attr (can't reset)", 'AttributeError', 'reset', bad14 ),
- ("attr (can't reset)", 'AttributeError', 'reset', bad15 ),
- ("attr (can't set)", 'AttributeError', 'read-only', bad16 ),
- ))
- qmsg('OK')
- return True
|