ut_flags.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/env python3
  2. """
  3. test.modtest_d.ut_flags: unit test for the MMGen suite's ClassFlags class
  4. """
  5. from mmgen.flags import ClassOpts, ClassFlags
  6. from ..include.common import qmsg, qmsg_r, vmsg
  7. class unit_test:
  8. def run_test(self, name, ut):
  9. class MyClassOpts(ClassOpts):
  10. reserved_attrs = ('foo',)
  11. class cls1:
  12. avail_opts = ()
  13. avail_flags = ()
  14. def __init__(self, opts=None, flags=None):
  15. self.opt = ClassOpts(self, opts)
  16. self.flag = ClassFlags(self, flags)
  17. class cls2(cls1):
  18. avail_opts = ('foo', 'bar')
  19. avail_flags = ('baz',)
  20. class cls3(cls1):
  21. avail_opts = ('_foo',)
  22. class cls4(cls1):
  23. avail_opts = ('foo',)
  24. def __init__(self, opts=None, flags=None):
  25. self.opt = MyClassOpts(self, opts)
  26. def test_flags():
  27. def gen():
  28. for n, cls in enumerate((
  29. cls1(),
  30. cls2(),
  31. cls2(opts=['bar']),
  32. cls2(flags=['baz']),
  33. )):
  34. vmsg(f'Cfg {n+1}:')
  35. for k in ('opt', 'flag'):
  36. vmsg(f' {k}s: {getattr(cls, k)}')
  37. yield cls
  38. return list(gen())
  39. def test_flags_err(d):
  40. def bad1(): d[1].flag.foo = False
  41. def bad2(): d[1].opt.baz = False
  42. def bad3(): cls3()
  43. def bad4(): cls4()
  44. def bad5(): cls1(opts='foo')
  45. def bad6(): cls2(opts=['qux'])
  46. def bad7(): d[1].flag.baz = False
  47. def bad8(): d[3].flag.baz = True
  48. def bad9(): d[1].flag.baz = 'x'
  49. ut.process_bad_data((
  50. ('flag (1)', 'ClassFlagsError', 'unrecognized flag', bad1),
  51. ('opt (1)', 'ClassFlagsError', 'unrecognized opt', bad2),
  52. ('avail_opts (1)', 'ClassFlagsError', 'underscore', bad3),
  53. ('avail_opts (2)', 'ClassFlagsError', 'reserved name', bad4),
  54. ('class invocation (1)', 'AssertionError', 'list or tuple', bad5),
  55. ('class invocation (2)', 'ClassFlagsError', 'unrecognized opt', bad6),
  56. ('flag (2)', 'ClassFlagsError', 'not set', bad7),
  57. ('flag (3)', 'ClassFlagsError', 'already set', bad8),
  58. ('flag (4)', 'AssertionError', 'not boolean', bad9),
  59. ))
  60. qmsg_r('Testing flags and opts...')
  61. vmsg('')
  62. classes = test_flags()
  63. qmsg('OK')
  64. qmsg_r('Testing error handling for flags and opts...')
  65. vmsg('')
  66. test_flags_err(classes)
  67. qmsg('OK')
  68. return True