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