You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.0 KiB

  1. import unittest
  2. from gooey import GooeyParser
  3. class TestConstraints(unittest.TestCase):
  4. def test_listbox_constraints(self):
  5. """
  6. Listbox widgets must be provided a nargs option
  7. """
  8. # Trying to create a listbox widget without specifying nargs
  9. # throws an error
  10. with self.assertRaises(ValueError):
  11. parser = GooeyParser()
  12. parser.add_argument('one', choices=['one', 'two'], widget='Listbox')
  13. # Listbox with an invalid nargs value throws an error
  14. with self.assertRaises(ValueError):
  15. parser = GooeyParser()
  16. parser.add_argument(
  17. 'one', choices=['one', 'two'], widget='Listbox', nargs='?')
  18. # Listbox with an invalid nargs value throws an error
  19. with self.assertRaises(ValueError):
  20. parser = GooeyParser()
  21. parser.add_argument(
  22. 'one', choices=['one', 'two'], widget='Listbox', nargs=3)
  23. # valid nargs throw no errors
  24. for narg in ['*', '+']:
  25. parser = GooeyParser()
  26. parser.add_argument(
  27. 'one', choices=['one', 'two'], widget='Listbox', nargs=narg)
  28. def test_visibility_constraint(self):
  29. """
  30. When visible=False in Gooey config, the user MUST supply either
  31. a custom validator or a default value.
  32. """
  33. # added without issue
  34. parser = GooeyParser()
  35. parser.add_argument('one')
  36. # still fine
  37. parser = GooeyParser()
  38. parser.add_argument('one', gooey_options={'visible': True})
  39. # trying to hide an input without a default or custom validator
  40. # results in an error
  41. with self.assertRaises(ValueError):
  42. parser = GooeyParser()
  43. parser.add_argument('one', gooey_options={'visible': False})
  44. # explicit default=None; still error
  45. with self.assertRaises(ValueError):
  46. parser = GooeyParser()
  47. parser.add_argument(
  48. 'one',
  49. default=None,
  50. gooey_options={'visible': False})
  51. # default = empty string. Still error
  52. with self.assertRaises(ValueError):
  53. parser = GooeyParser()
  54. parser.add_argument(
  55. 'one',
  56. default='',
  57. gooey_options={'visible': False})
  58. # default = valid string. No Error
  59. parser = GooeyParser()
  60. parser.add_argument(
  61. 'one',
  62. default='Hello',
  63. gooey_options={'visible': False})
  64. # No default, but custom validator: Success
  65. parser = GooeyParser()
  66. parser.add_argument(
  67. 'one',
  68. gooey_options={
  69. 'visible': False,
  70. 'validator': {'test': 'true'}
  71. })
  72. # default AND validator, still fine
  73. parser = GooeyParser()
  74. parser.add_argument(
  75. 'one',
  76. default='Hai',
  77. gooey_options={
  78. 'visible': False,
  79. 'validator': {'test': 'true'}
  80. })