ut_lockable.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/usr/bin/env python3
  2. """
  3. test/unit_tests_d/ut_lockable.py: unit test for the MMGen suite's Lockable class
  4. """
  5. from mmgen.common import *
  6. from mmgen.exception import *
  7. class unit_test(object):
  8. def run_test(self,name,ut):
  9. from mmgen.base_obj import AttrCtrl,Lockable
  10. from decimal import Decimal
  11. qmsg_r('Testing class AttrCtrl...')
  12. class MyAttrCtrl(AttrCtrl):
  13. foo = 'fooval'
  14. ac = MyAttrCtrl()
  15. ac.lock()
  16. ac.foo = 'new fooval'
  17. ac.foo = 'new fooval2'
  18. class MyAttrCtrlClsCheck(AttrCtrl):
  19. _use_class_attr = True
  20. foo = 'fooval'
  21. bar = None
  22. acc = MyAttrCtrlClsCheck()
  23. acc.lock()
  24. acc.foo = 'new_fooval'
  25. acc.foo = 'new_fooval2'
  26. acc.bar = 'bar val'
  27. acc.bar = 1 # class attribute bar is None, so can be set to any type
  28. qmsg('OK')
  29. qmsg_r('Testing class Lockable...')
  30. class MyLockable(Lockable): # class has no attrs, like UserOpts
  31. _autolock = False
  32. _set_ok = ('foo','baz','alpha','beta','gamma','delta','epsilon')
  33. _reset_ok = ('bar','baz')
  34. lc = MyLockable()
  35. lc.foo = None
  36. lc.bar = 'barval'
  37. lc.baz = 1
  38. lc.qux = 1
  39. # are these considered set?
  40. lc.alpha = 0 # yes
  41. lc.beta = False # yes
  42. lc.gamma = Decimal('0') # yes
  43. lc.delta = 0.0 # yes
  44. lc.epsilon = [] # no
  45. lc.lock()
  46. lc.foo = 'fooval2'
  47. lc.bar = 'barval2'
  48. lc.bar = 'barval3'
  49. lc.baz = 2
  50. lc.baz = 3
  51. lc.epsilon = [0]
  52. class MyLockableClsCheck(Lockable): # class has attrs, like GlobalContext
  53. _autolock = False
  54. _use_class_attr = True
  55. _set_ok = ('foo','baz')
  56. _reset_ok = ('bar','baz')
  57. foo = None
  58. bar = 1
  59. baz = 3.5
  60. qux = 'quxval'
  61. lcc = MyLockableClsCheck()
  62. lcc.lock()
  63. lcc.foo = 'fooval2' # class attribute foo is None, so can be set to any type
  64. lcc.bar = 2
  65. lcc.bar = 3 # bar is in reset list
  66. lcc.baz = 3.2
  67. lcc.baz = 3.1 # baz is in both lists
  68. qmsg('OK')
  69. qmsg_r('Testing class Lockable with autolock...')
  70. class MyLockableAutolock(Lockable):
  71. def __init__(self):
  72. self.foo = True
  73. lca = MyLockableAutolock()
  74. assert lca._autolock == True
  75. assert lca._lock == True
  76. assert lca.foo == True
  77. qmsg('OK')
  78. qmsg_r('Checking error handling...')
  79. vmsg('')
  80. def bad1(): ac.x = 1
  81. def bad2(): acc.foo = 1
  82. def bad3(): lc.foo = 'fooval3'
  83. def bad4(): lc.baz = 'str'
  84. def bad5(): lcc.bar = 'str'
  85. def bad6(): lc.qux = 2
  86. def bad7(): lcc.qux = 'quxval2'
  87. def bad8(): lcc.foo = 'fooval3'
  88. def bad9(): lc.x = 1
  89. def bad10(): lcc.x = 1
  90. def bad11(): lc.alpha = 0
  91. def bad12(): lc.beta = False
  92. def bad13(): lc.gamma = Decimal('0')
  93. def bad14(): lc.delta = float(0)
  94. def bad15(): lc.epsilon = [0]
  95. def bad16(): lca.foo = None
  96. ut.process_bad_data((
  97. ('attr (1)', 'AttributeError', 'has no attr', bad1 ),
  98. ('attr (2)', 'AttributeError', 'has no attr', bad9 ),
  99. ('attr (3)', 'AttributeError', 'has no attr', bad10 ),
  100. ('attr type (1)', 'AttributeError', 'type', bad2 ),
  101. ("attr type (2)", 'AttributeError', 'type', bad4 ),
  102. ("attr type (3)", 'AttributeError', 'type', bad5 ),
  103. ("attr (can't set)", 'AttributeError', 'read-only', bad6 ),
  104. ("attr (can't set)", 'AttributeError', 'read-only', bad7 ),
  105. ("attr (can't reset)", 'AttributeError', 'reset', bad3 ),
  106. ("attr (can't reset)", 'AttributeError', 'reset', bad8 ),
  107. ("attr (can't reset)", 'AttributeError', 'reset', bad11 ),
  108. ("attr (can't reset)", 'AttributeError', 'reset', bad12 ),
  109. ("attr (can't reset)", 'AttributeError', 'reset', bad13 ),
  110. ("attr (can't reset)", 'AttributeError', 'reset', bad14 ),
  111. ("attr (can't reset)", 'AttributeError', 'reset', bad15 ),
  112. ("attr (can't set)", 'AttributeError', 'read-only', bad16 ),
  113. ))
  114. qmsg('OK')
  115. return True