ut_lockable.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. _autolock = False
  14. foo = 'fooval'
  15. ac = MyAttrCtrl()
  16. ac.lock()
  17. ac.foo = 'new fooval'
  18. ac.foo = 'new fooval2'
  19. class MyAttrCtrlAutolock(AttrCtrl): pass
  20. aca = MyAttrCtrlAutolock()
  21. class MyAttrCtrlClsCheck(AttrCtrl):
  22. _autolock = False
  23. _use_class_attr = True
  24. foo = 'fooval'
  25. bar = None
  26. acc = MyAttrCtrlClsCheck()
  27. acc.lock()
  28. acc.foo = 'new_fooval'
  29. acc.foo = 'new_fooval2'
  30. acc.bar = 'bar val'
  31. acc.bar = 1 # class attribute bar is None, so can be set to any type
  32. qmsg('OK')
  33. qmsg_r('Testing class Lockable...')
  34. class MyLockable(Lockable): # class has no attrs, like UserOpts
  35. _autolock = False
  36. _set_ok = ('foo','baz','alpha','beta','gamma','delta','epsilon')
  37. _reset_ok = ('bar','baz')
  38. lc = MyLockable()
  39. lc.foo = None
  40. lc.bar = 'barval'
  41. lc.baz = 1
  42. lc.qux = 1
  43. # are these considered set?
  44. lc.alpha = 0 # yes
  45. lc.beta = False # yes
  46. lc.gamma = Decimal('0') # yes
  47. lc.delta = 0.0 # yes
  48. lc.epsilon = [] # no
  49. lc.lock()
  50. lc.foo = 'fooval2'
  51. lc.bar = 'barval2'
  52. lc.bar = 'barval3'
  53. lc.baz = 2
  54. lc.baz = 3
  55. lc.epsilon = [0]
  56. class MyLockableClsCheck(Lockable): # class has attrs, like GlobalContext
  57. _autolock = False
  58. _use_class_attr = True
  59. _set_ok = ('foo','baz')
  60. _reset_ok = ('bar','baz')
  61. foo = None
  62. bar = 1
  63. baz = 3.5
  64. qux = 'quxval'
  65. lcc = MyLockableClsCheck()
  66. lcc.lock()
  67. lcc.foo = 'fooval2' # class attribute foo is None, so can be set to any type
  68. lcc.bar = 2
  69. lcc.bar = 3 # bar is in reset list
  70. lcc.baz = 3.2
  71. lcc.baz = 3.1 # baz is in both lists
  72. qmsg('OK')
  73. qmsg_r('Testing class Lockable with autolock...')
  74. class MyLockableAutolock(Lockable):
  75. def __init__(self):
  76. self.foo = True
  77. lca = MyLockableAutolock()
  78. assert lca._autolock == True
  79. assert lca._lock == True
  80. assert lca.foo == True
  81. class MyLockableBad(Lockable):
  82. _set_ok = ('foo','bar')
  83. foo = 1
  84. qmsg('OK')
  85. qmsg_r('Checking error handling...')
  86. vmsg('')
  87. def bad1(): ac.x = 1
  88. def bad2(): acc.foo = 1
  89. def bad3(): lc.foo = 'fooval3'
  90. def bad4(): lc.baz = 'str'
  91. def bad5(): lcc.bar = 'str'
  92. def bad6(): lc.qux = 2
  93. def bad7(): lcc.qux = 'quxval2'
  94. def bad8(): lcc.foo = 'fooval3'
  95. def bad9(): lc.x = 1
  96. def bad10(): lcc.x = 1
  97. def bad11(): lc.alpha = 0
  98. def bad12(): lc.beta = False
  99. def bad13(): lc.gamma = Decimal('0')
  100. def bad14(): lc.delta = float(0)
  101. def bad15(): lc.epsilon = [0]
  102. def bad16(): lca.foo = None
  103. def bad17(): lb = MyLockableBad()
  104. def bad18(): aca.lock()
  105. ut.process_bad_data((
  106. ('attr (1)', 'AttributeError', 'has no attr', bad1 ),
  107. ('attr (2)', 'AttributeError', 'has no attr', bad9 ),
  108. ('attr (3)', 'AttributeError', 'has no attr', bad10 ),
  109. ('attr type (1)', 'AttributeError', 'type', bad2 ),
  110. ("attr type (2)", 'AttributeError', 'type', bad4 ),
  111. ("attr type (3)", 'AttributeError', 'type', bad5 ),
  112. ("attr (can't set)", 'AttributeError', 'read-only', bad6 ),
  113. ("attr (can't set)", 'AttributeError', 'read-only', bad7 ),
  114. ("attr (can't reset)", 'AttributeError', 'reset', bad3 ),
  115. ("attr (can't reset)", 'AttributeError', 'reset', bad8 ),
  116. ("attr (can't reset)", 'AttributeError', 'reset', bad11 ),
  117. ("attr (can't reset)", 'AttributeError', 'reset', bad12 ),
  118. ("attr (can't reset)", 'AttributeError', 'reset', bad13 ),
  119. ("attr (can't reset)", 'AttributeError', 'reset', bad14 ),
  120. ("attr (can't reset)", 'AttributeError', 'reset', bad15 ),
  121. ("attr (can't set)", 'AttributeError', 'read-only', bad16 ),
  122. ("attr (bad _set_ok)", 'AssertionError', 'not found in',bad17 ),
  123. ("call to lock()", 'AssertionError', 'only once', bad18 ),
  124. ))
  125. qmsg('OK')
  126. return True