ut_lockable.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. _set_ok = ('foo','baz','alpha','beta','gamma','delta','epsilon')
  32. _reset_ok = ('bar','baz')
  33. lc = MyLockable()
  34. lc.foo = None
  35. lc.bar = 'barval'
  36. lc.baz = 1
  37. lc.qux = 1
  38. # are these considered set?
  39. lc.alpha = 0 # yes
  40. lc.beta = False # yes
  41. lc.gamma = Decimal('0') # yes
  42. lc.delta = 0.0 # yes
  43. lc.epsilon = [] # no
  44. lc.lock()
  45. lc.foo = 'fooval2'
  46. lc.bar = 'barval2'
  47. lc.bar = 'barval3'
  48. lc.baz = 2
  49. lc.baz = 3
  50. lc.epsilon = [0]
  51. class MyLockableClsCheck(Lockable): # class has attrs, like GlobalContext
  52. _use_class_attr = True
  53. _set_ok = ('foo','baz')
  54. _reset_ok = ('bar','baz')
  55. foo = None
  56. bar = 1
  57. baz = 3.5
  58. qux = 'quxval'
  59. lcc = MyLockableClsCheck()
  60. lcc.lock()
  61. lcc.foo = 'fooval2' # class attribute foo is None, so can be set to any type
  62. lcc.bar = 2
  63. lcc.bar = 3 # bar is in reset list
  64. lcc.baz = 3.2
  65. lcc.baz = 3.1 # baz is in both lists
  66. qmsg('OK')
  67. qmsg('Checking error handling:')
  68. def bad1(): ac.x = 1
  69. def bad2(): acc.foo = 1
  70. def bad3(): lc.foo = 'fooval3'
  71. def bad4(): lc.baz = 'str'
  72. def bad5(): lcc.bar = 'str'
  73. def bad6(): lc.qux = 2
  74. def bad7(): lcc.qux = 'quxval2'
  75. def bad8(): lcc.foo = 'fooval3'
  76. def bad9(): lc.x = 1
  77. def bad10(): lcc.x = 1
  78. def bad11(): lc.alpha = 0
  79. def bad12(): lc.beta = False
  80. def bad13(): lc.gamma = Decimal('0')
  81. def bad14(): lc.delta = float(0)
  82. def bad15(): lc.epsilon = [0]
  83. ut.process_bad_data((
  84. ('attr (1)', 'AttributeError', 'has no attr', bad1 ),
  85. ('attr (2)', 'AttributeError', 'has no attr', bad9 ),
  86. ('attr (3)', 'AttributeError', 'has no attr', bad10 ),
  87. ('attr type (1)', 'AttributeError', 'type', bad2 ),
  88. ("attr type (2)", 'AttributeError', 'type', bad4 ),
  89. ("attr type (3)", 'AttributeError', 'type', bad5 ),
  90. ("attr (can't set)", 'AttributeError', 'read-only', bad6 ),
  91. ("attr (can't set)", 'AttributeError', 'read-only', bad7 ),
  92. ("attr (can't reset)", 'AttributeError', 'reset', bad3 ),
  93. ("attr (can't reset)", 'AttributeError', 'reset', bad8 ),
  94. ("attr (can't reset)", 'AttributeError', 'reset', bad11 ),
  95. ("attr (can't reset)", 'AttributeError', 'reset', bad12 ),
  96. ("attr (can't reset)", 'AttributeError', 'reset', bad13 ),
  97. ("attr (can't reset)", 'AttributeError', 'reset', bad14 ),
  98. ("attr (can't reset)", 'AttributeError', 'reset', bad15 ),
  99. ))
  100. qmsg('OK')
  101. return True