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.

74 lines
3.9 KiB

  1. import argparse
  2. import unittest
  3. import json
  4. from argparse_to_json import *
  5. class TestArgparseToJson(unittest.TestCase):
  6. def setUp(self):
  7. my_cool_parser = argparse.ArgumentParser(description='description')
  8. my_cool_parser.add_argument("filename", help='filename help msg') # positional
  9. my_cool_parser.add_argument("outfile", help="Name of the file where you'll save the output") # positional
  10. my_cool_parser.add_argument('-c', '--countdown', default=10, type=int, help='sets the time to count down from')
  11. my_cool_parser.add_argument("-s", "--showtime", action="store_true", help="display the countdown timer")
  12. my_cool_parser.add_argument("-d", "--delay", action="store_true", help="Delay execution for a bit")
  13. my_cool_parser.add_argument('--verbose', '-v', action='count')
  14. my_cool_parser.add_argument("-o", "--obfuscate", action="store_true", help="obfuscate the countdown timer!")
  15. my_cool_parser.add_argument('-r', '--recursive', choices=['yes', 'no'], help='Recurse into subfolders')
  16. my_cool_parser.add_argument("-w", "--writelog", default="No, NOT whatevs", help="write log to some file or something")
  17. my_cool_parser.add_argument("-e", "--expandAll", action="store_true", help="expand all processes")
  18. verbosity = my_cool_parser.add_mutually_exclusive_group()
  19. verbosity.add_argument('-t', '--verboze', dest='verboze', action="store_true", help="Show more details")
  20. verbosity.add_argument('-q', '--quiet', dest='quiet', action="store_true", help="Only output on error")
  21. self.parser = my_cool_parser
  22. self.mutually_exclusive_group = [
  23. mutex_action
  24. for group_actions in self.parser._mutually_exclusive_groups
  25. for mutex_action in group_actions._group_actions]
  26. self.base_actions = [action for action in self.parser._actions
  27. if action not in self.mutually_exclusive_group]
  28. def test_get_optionals_with_choices(self):
  29. target_arg = self.find_arg_by_option(self.base_actions, '--recursive')
  30. json_result = get_optionals_with_choices(self.base_actions)
  31. self._test_parser_to_json_mapping(target_arg, json_result[0], 'Dropdown')
  32. def test_get_optionals_without_choices(self):
  33. target_arg = self.find_arg_by_option(self.base_actions, '--showtime')
  34. json_result = get_optionals_without_choices(self.base_actions)
  35. self._test_parser_to_json_mapping(target_arg, json_result[0], 'TextField')
  36. def test_get_counter_style_optionals(self):
  37. target_arg = self.find_arg_by_option(self.base_actions, '--verbose')
  38. json_result = get_counter_style_optionals(self.base_actions)
  39. print json_result
  40. self._test_parser_to_json_mapping(target_arg, json_result[0], 'Dropdown')
  41. def test_get_mutually_exclusive_optionals(self):
  42. target_arg = self.find_arg_by_option(self.mutually_exclusive_group, '--verboze')
  43. json_result = get_mutually_exclusive_optionals(self.mutually_exclusive_group)[0]
  44. data = json_result['data'][0]
  45. self.assertEqual('RadioGroup', json_result['type'])
  46. self.assertEqual(target_arg.choices, data['choices'])
  47. self.assertEqual(target_arg.help, data['help'])
  48. self.assertEqual(target_arg.option_strings, data['commands'])
  49. self.assertEqual(target_arg.dest, data['display_name'])
  50. def _test_parser_to_json_mapping(self, target_arg, json_string, expected_type):
  51. self.assertEqual(expected_type, json_string['type'])
  52. self.assertEqual(target_arg.choices, json_string['data']['choices'])
  53. self.assertEqual(target_arg.help, json_string['data']['help'])
  54. self.assertEqual(target_arg.option_strings, json_string['data']['commands'])
  55. self.assertEqual(target_arg.dest, json_string['data']['display_name'])
  56. def find_arg_by_option(self, group, option_string):
  57. for arg in group:
  58. if option_string in arg.option_strings:
  59. return arg