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