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.

85 lines
3.4 KiB

  1. import argparse
  2. import unittest
  3. from gooey import GooeyParser
  4. class TestParentInheritance(unittest.TestCase):
  5. def test_parent_arguments_exist_in_child(self):
  6. """
  7. Verifies that the parents parameter is honoured.
  8. """
  9. base_parser = GooeyParser(add_help=False)
  10. base_parser.add_argument("a_file", widget="FileChooser")
  11. parser = GooeyParser(parents=[base_parser])
  12. parser.add_argument("b_file", widget="DirChooser")
  13. found = 0
  14. for action in parser._actions:
  15. if action.dest == "a_file":
  16. found += 1
  17. elif action.dest == "b_file":
  18. found += 1
  19. self.assertEqual(2, found, "Did not find 2 expected arguments, found " + str(found))
  20. self.assertEqual(parser.widgets["a_file"], "FileChooser")
  21. self.assertEqual(parser.widgets["b_file"], "DirChooser")
  22. def test_parent_arguments_are_not_overridden(self):
  23. """
  24. Verifies that the same named argument in a parent and child parser is accepted, and only the child
  25. parser survives.
  26. """
  27. # Verify how vanilla argparse works
  28. base_parser = argparse.ArgumentParser(add_help=False)
  29. action1 = base_parser.add_argument("a_file", default="a")
  30. parser = argparse.ArgumentParser(parents=[base_parser])
  31. action2 = parser.add_argument("a_file", default="b")
  32. self._verify_duplicate_parameters(action1, action2, parser)
  33. # So a child can't override a parent - this isn't textbook inheritance
  34. # Run the same test on GooeyParser
  35. base_parser = GooeyParser(add_help=False)
  36. action1 = base_parser.add_argument("a_file", widget="FileChooser", default="a")
  37. parser = GooeyParser(parents=[base_parser])
  38. action2 = parser.add_argument("a_file", widget="DirChooser", default="b")
  39. self._verify_duplicate_parameters(action1, action2, parser)
  40. self.assertEqual(parser.widgets["a_file"], "FileChooser")
  41. def test_duplicates_on_same_parser_are_ignored(self):
  42. """
  43. Verify that adding duplicate named arguments works the same in argparse and Gooey.
  44. Assuming the behaviour of the "default" parameter is a good match for the "widget" parameter.
  45. """
  46. # Verify how vanilla argparse works
  47. parser = argparse.ArgumentParser()
  48. action1 = parser.add_argument("a_file", default="a")
  49. action2 = parser.add_argument("a_file", default="b")
  50. self._verify_duplicate_parameters(action1, action2, parser)
  51. # Run the same test on GooeyParser
  52. parser = GooeyParser()
  53. action1 = parser.add_argument("a_file", default="a", widget="FileChooser")
  54. action2 = parser.add_argument("a_file", default="b", widget="DirChooser")
  55. self._verify_duplicate_parameters(action1, action2, parser)
  56. self.assertEqual(parser.widgets["a_file"], "FileChooser")
  57. def _verify_duplicate_parameters(self, action1, action2, parser):
  58. """
  59. Verify two parameters named a_file exist and the default value is "a".
  60. """
  61. found = 0
  62. for action in parser._actions:
  63. if action.dest == "a_file":
  64. found += 1
  65. self.assertEqual(2, found, "Expected a both actions handling a_file but got " + str(found))
  66. self.assertEqual(parser.get_default("a_file"), "a")
  67. self.assertNotEqual(action1, action2)