ut_lockable.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. qmsg_r('Testing class AttrCtrl...')
  11. class MyAttrCtrl(AttrCtrl):
  12. foo = 'fooval'
  13. ac = MyAttrCtrl()
  14. ac.lock()
  15. ac.foo = 'new fooval'
  16. ac.foo = 'new fooval2'
  17. class MyAttrCtrlClsCheck(AttrCtrl):
  18. _use_class_attr = True
  19. foo = 'fooval'
  20. bar = None
  21. acc = MyAttrCtrlClsCheck()
  22. acc.lock()
  23. acc.foo = 'new_fooval'
  24. acc.foo = 'new_fooval2'
  25. acc.bar = 'bar val'
  26. acc.bar = 1 # class attribute bar is None, so can be set to any type
  27. qmsg('OK')
  28. qmsg_r('Testing class Lockable...')
  29. class MyLockable(Lockable): # class has no attrs, like UserOpts
  30. _set_ok = ('foo','baz')
  31. _reset_ok = ('bar','baz')
  32. lc = MyLockable()
  33. lc.foo = None
  34. lc.bar = 'barval'
  35. lc.baz = 1
  36. lc.qux = 1
  37. lc.lock()
  38. lc.foo = 'fooval2'
  39. lc.bar = 'barval2'
  40. lc.bar = 'barval3'
  41. lc.baz = 2
  42. lc.baz = 3
  43. class MyLockableClsCheck(Lockable): # class has attrs, like GlobalContext
  44. _use_class_attr = True
  45. _set_ok = ('foo','baz')
  46. _reset_ok = ('bar','baz')
  47. foo = None
  48. bar = 1
  49. baz = 3.5
  50. qux = 'quxval'
  51. lcc = MyLockableClsCheck()
  52. lcc.lock()
  53. lcc.foo = 'fooval2' # class attribute foo is None, so can be set to any type
  54. lcc.bar = 2
  55. lcc.bar = 3 # bar is in reset list
  56. lcc.baz = 3.2
  57. lcc.baz = 3.1 # baz is in both lists
  58. qmsg('OK')
  59. qmsg('Checking error handling:')
  60. def bad1(): ac.x = 1
  61. def bad2(): acc.foo = 1
  62. def bad3(): lc.foo = 'fooval3'
  63. def bad4(): lc.baz = 'str'
  64. def bad5(): lcc.bar = 'str'
  65. def bad6(): lc.qux = 2
  66. def bad7(): lcc.qux = 'quxval2'
  67. def bad8(): lcc.foo = 'fooval3'
  68. def bad9(): lc.x = 1
  69. def bad10(): lcc.x = 1
  70. ut.process_bad_data((
  71. ('attr (1)', 'AttributeError', 'has no attr', bad1 ),
  72. ('attr (2)', 'AttributeError', 'has no attr', bad9 ),
  73. ('attr (3)', 'AttributeError', 'has no attr', bad10 ),
  74. ('attr type (1)', 'AttributeError', 'type', bad2 ),
  75. ("attr type (2)", 'AttributeError', 'type', bad4 ),
  76. ("attr type (3)", 'AttributeError', 'type', bad5 ),
  77. ("attr (can't set)", 'AttributeError', 'read-only', bad6 ),
  78. ("attr (can't set)", 'AttributeError', 'read-only', bad7 ),
  79. ("attr (can't reset)", 'AttributeError', 'reset', bad3 ),
  80. ("attr (can't reset)", 'AttributeError', 'reset', bad8 ),
  81. ))
  82. qmsg('OK')
  83. return True