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.

60 lines
2.1 KiB

2 years ago
2 years ago
2 years ago
  1. import unittest
  2. from collections import namedtuple
  3. from tests.harness import instrumentGooey
  4. from gooey import GooeyParser
  5. from gooey.tests import *
  6. Case = namedtuple('Case', 'inputs initialExpected expectedAfterClearing')
  7. class TestTextField(unittest.TestCase):
  8. def makeParser(self, **kwargs):
  9. parser = GooeyParser(description='description')
  10. parser.add_argument('--widget', widget="TextField", **kwargs)
  11. return parser
  12. def testPlaceholder(self):
  13. cases = [
  14. [{}, ''],
  15. [{'placeholder': 'Hello'}, 'Hello']
  16. ]
  17. for options, expected in cases:
  18. parser = self.makeParser(gooey_options=options)
  19. with instrumentGooey(parser) as (app, frame, gapp):
  20. # because of how poorly designed the Gooey widgets are
  21. # we have to reach down 3 levels in order to find the
  22. # actual WX object we need to test.
  23. widget = gapp.getActiveConfig().reifiedWidgets[0].widget
  24. self.assertEqual(widget.widget.GetHint(), expected)
  25. def testDefaultAndInitialValue(self):
  26. cases = [
  27. # initial_value takes precedence when both are present
  28. Case(
  29. {'default': 'default_val', 'gooey_options': {'initial_value': 'some val'}},
  30. 'some val',
  31. None),
  32. # when no default is present
  33. # Case({'gooey_options': {'initial_value': 'some val'}},
  34. # 'some val',
  35. # ''),
  36. # [{'default': 'default', 'gooey_options': {}},
  37. # 'default'],
  38. # [{'default': 'default'},
  39. # 'default'],
  40. ]
  41. for case in cases:
  42. parser = self.makeParser(**case.inputs)
  43. with instrumentGooey(parser) as (app, frame, gapp):
  44. widget = gapp.getActiveConfig().reifiedWidgets[0]
  45. self.assertEqual(widget.getValue()['rawValue'], case.initialExpected)
  46. widget.setValue('')
  47. print(widget.getValue())
  48. self.assertEqual(widget.getValue()['cmd'], case.expectedAfterClearing)
  49. if __name__ == '__main__':
  50. unittest.main()