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.

73 lines
4.0 KiB

  1. """
  2. Created on Jan 16, 2014
  3. @author: Chris
  4. """
  5. import unittest
  6. from argparse import _HelpAction
  7. from action_sorter import ActionSorter
  8. from gooey.gui import argparse_test_data
  9. class TestActionSorter(unittest.TestCase):
  10. def setUp(self):
  11. self._actions = argparse_test_data.parser._actions
  12. self.sorted_actions = ActionSorter(self._actions)
  13. # pain in the A...
  14. self.expected_positionals = [
  15. "_StoreAction(option_strings=[], dest='filename', nargs=None, const=None, default=None, type=None, choices=None, help='Name of the file you want to read', metavar=None)",
  16. """_StoreAction(option_strings=[], dest='outfile', nargs=None, const=None, default=None, type=None, choices=None, help="Name of the file where you'll save the output", metavar=None)"""
  17. ]
  18. self.expected_choices = [
  19. """_StoreAction(option_strings=['-T', '--tester'], dest='tester', nargs=None, const=None, default=None, type=None, choices=['yes', 'no'], help="Yo, what's up man? I'm a help message!", metavar=None)"""
  20. ]
  21. self.expected_optionals = [
  22. """_StoreAction(option_strings=['-m', '--moutfile'], dest='moutfile', nargs=None, const=None, default=None, type=None, choices=None, help='Redirects output to the file specified by you, the awesome user', metavar=None)""",
  23. """_StoreAction(option_strings=['-v', '--verbose'], dest='verbose', nargs=None, const=None, default=None, type=None, choices=None, help='Toggles verbosity off', metavar=None)""",
  24. """_StoreAction(option_strings=['-s', '--schimzammy'], dest='schimzammy', nargs=None, const=None, default=None, type=None, choices=None, help='Add in an optional shimzammy parameter', metavar=None)"""
  25. ]
  26. self.expected_counters = [
  27. """_CountAction(option_strings=['-e', '--repeat'], dest='repeat', nargs=0, const=None, default=None, type=None, choices=None, help='Set the number of times to repeat', metavar=None)"""
  28. ]
  29. self.expected_flags = [
  30. """_StoreConstAction(option_strings=['-c', '--constoption'], dest='constoption', nargs=0, const='myconstant', default=None, type=None, choices=None, help='Make sure the const action is correctly sorted', metavar=None)""",
  31. """_StoreTrueAction(option_strings=['-t', '--truify'], dest='truify', nargs=0, const=True, default=False, type=None, choices=None, help='Ensure the store_true actions are sorted', metavar=None)""",
  32. """_StoreFalseAction(option_strings=['-f', '--falsificle'], dest='falsificle', nargs=0, const=False, default=True, type=None, choices=None, help='Ensure the store_false actions are sorted', metavar=None)"""
  33. ]
  34. def test_positionals_returns_only_positional_actions(self):
  35. positionals = self.sorted_actions._positionals
  36. self.assertEqual(len(positionals), 2)
  37. self.assert_for_all_actions_in_list(positionals, self.expected_positionals)
  38. def test_help_action_not_in_optionals(self):
  39. _isinstance = lambda x: isinstance(x, _HelpAction)
  40. self.assertFalse(any(map(_isinstance, self.sorted_actions._optionals)))
  41. def test_choices_only_returns_choices(self):
  42. self.assert_for_all_actions_in_list(self.sorted_actions._choices,
  43. self.expected_choices)
  44. def test_optionals_only_returns_optionals(self):
  45. self.assert_for_all_actions_in_list(self.sorted_actions._optionals,
  46. self.expected_optionals)
  47. def test_counter_sort_only_returns_counters(self):
  48. self.assert_for_all_actions_in_list(self.sorted_actions._counters,
  49. self.expected_counters)
  50. def test_flag_sort_returns_only_flags(self):
  51. self.assert_for_all_actions_in_list(self.sorted_actions._flags,
  52. self.expected_flags)
  53. def assert_for_all_actions_in_list(self, actions, expected_actions):
  54. for index, action in enumerate(actions):
  55. self.assertEqual(str(action), expected_actions[index])
  56. if __name__ == "__main__":
  57. #import sys;sys.argv = ['', 'Test.testName']
  58. unittest.main()