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.

178 lines
6.5 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. import pytest
  2. from gooey.python_bindings.argparse_to_json import *
  3. @pytest.fixture
  4. def empty_parser():
  5. return argparse.ArgumentParser(description='description')
  6. @pytest.fixture
  7. def complete_parser():
  8. parser = argparse.ArgumentParser(description='description')
  9. parser.add_argument("req1", help='filename help msg') # positional
  10. parser.add_argument("req2", help="Name of the file where you'll save the output") # positional
  11. parser.add_argument('-r', dest="req3", default=10, type=int, help='sets the time to count down from', required=True)
  12. parser.add_argument('--req4', dest="req4", default=10, type=int, help='sets the time to count down from', required=True)
  13. parser.add_argument("-a", "--aa", action="store_true", help="aaa")
  14. parser.add_argument("-b", "--bb", action="store_true", help="bbb")
  15. parser.add_argument('-c', '--cc', action='count')
  16. parser.add_argument("-d", "--dd", action="store_true", help="ddd")
  17. parser.add_argument('-e', '--ee', choices=['yes', 'no'], help='eee')
  18. parser.add_argument("-f", "--ff", default="0000", help="fff")
  19. parser.add_argument("-g", "--gg", action="store_true", help="ggg")
  20. verbosity = parser.add_mutually_exclusive_group()
  21. verbosity.add_argument('-i', '--ii', action="store_true", help="iii")
  22. verbosity.add_argument('-j', '--jj', action="store_true", help="hhh")
  23. return parser
  24. @pytest.fixture
  25. def subparser():
  26. parser = argparse.ArgumentParser(description='qidev')
  27. parser.add_argument('--verbose', help='be verbose', dest='verbose', action='store_true', default=False)
  28. subs = parser.add_subparsers(help='commands', dest='command')
  29. config_parser = subs.add_parser('config', help='configure defaults for qidev')
  30. config_parser.add_argument('field', help='the field to configure', type=str)
  31. config_parser.add_argument('value', help='set field to value', type=str)
  32. # ########################################################
  33. connect_parser = subs.add_parser('connect', help='connect to a robot (ip/hostname)')
  34. connect_parser.add_argument('hostname', help='hostname or IP address of the robot', type=str)
  35. # ########################################################
  36. install_parser = subs.add_parser('install', help='package and install a project directory on a robot')
  37. install_parser.add_argument('path', help='path to the project directory (containing manifest.xml', type=str)
  38. install_parser.add_argument('--ip', nargs='*', type=str, dest='ip', help='specify hostname(es)/IP address(es)')
  39. return parser
  40. @pytest.fixture
  41. def exclusive_group():
  42. parser = argparse.ArgumentParser(description='description')
  43. verbosity = parser.add_mutually_exclusive_group()
  44. verbosity.add_argument('-i', dest="option1", action="store_true", help="iii")
  45. verbosity.add_argument('-j', dest="option2", action="store_true", help="hhh")
  46. mutually_exclusive_group = [mutex_action
  47. for group_actions in parser._mutually_exclusive_groups
  48. for mutex_action in group_actions._group_actions]
  49. return mutually_exclusive_group
  50. def test_parser_converts_to_correct_type(empty_parser, complete_parser, subparser):
  51. assert convert(subparser)['layout_type'] == 'column'
  52. assert convert(empty_parser)['layout_type'] == 'standard'
  53. assert convert(complete_parser)['layout_type'] == 'standard'
  54. def test_convert_std_parser(complete_parser):
  55. result = convert(complete_parser)
  56. assert result['layout_type'] == 'standard'
  57. assert result['widgets']
  58. assert isinstance(result['widgets'], list)
  59. entry = result['widgets'][0]
  60. assert 'type' in entry
  61. assert 'required' in entry
  62. assert 'data' in entry
  63. required = filter(lambda x: x['required'], result['widgets'])
  64. optional = filter(lambda x: not x['required'], result['widgets'])
  65. assert len(required) == 4
  66. assert len(optional) == 8
  67. def test_convert_sub_parser(subparser):
  68. result = convert(subparser)
  69. assert result['layout_type'] == 'column'
  70. assert result['widgets']
  71. assert isinstance(result['widgets'], dict)
  72. assert len(result['widgets']) == 3
  73. def test_has_required(empty_parser, complete_parser, subparser):
  74. assert has_required(complete_parser._actions)
  75. assert not has_required(empty_parser._actions)
  76. assert not has_required(subparser._actions)
  77. def test_has_subparsers(subparser, complete_parser):
  78. assert has_subparsers(subparser._actions)
  79. assert not has_subparsers(complete_parser._actions)
  80. def test_is_required(complete_parser):
  81. required = filter(is_required, complete_parser._actions)
  82. assert len(required) == 4
  83. for action in required:
  84. print action.dest.startswith('req')
  85. def test_is_optional(complete_parser):
  86. optional = filter(is_optional, complete_parser._actions)
  87. assert len(optional) == 10
  88. for action in optional:
  89. assert 'req' not in action.dest
  90. def test_is_choice(empty_parser):
  91. empty_parser.add_argument('--dropdown', choices=[1,2])
  92. assert is_choice(get_action(empty_parser, 'dropdown'))
  93. empty_parser.add_argument('--storetrue', action='store_true')
  94. assert not is_choice(get_action(empty_parser, 'storetrue'))
  95. # make sure positionals are caught as well (issue #85)
  96. empty_parser.add_argument('positional', choices=[1, 2])
  97. assert is_choice(get_action(empty_parser, 'positional'))
  98. def test_is_standard(empty_parser):
  99. empty_parser.add_argument('--count', action='count')
  100. assert not is_standard(get_action(empty_parser, 'count'))
  101. empty_parser.add_argument('--store', action='store')
  102. assert is_standard(get_action(empty_parser, 'store'))
  103. def test_is_counter(empty_parser):
  104. empty_parser.add_argument('--count', action='count')
  105. assert is_counter(get_action(empty_parser, 'count'))
  106. empty_parser.add_argument('--dropdown', choices=[1,2])
  107. assert not is_counter(get_action(empty_parser, 'dropdown'))
  108. def test_mutually(exclusive_group):
  109. target_arg = find_arg_by_option(exclusive_group, '-i')
  110. json_result = build_radio_group(exclusive_group)[0]
  111. data = json_result['data'][0]
  112. assert 'RadioGroup' == json_result['type']
  113. assert target_arg.choices == data['choices']
  114. assert target_arg.help == data['help']
  115. assert target_arg.option_strings == data['commands']
  116. assert target_arg.dest == data['display_name']
  117. def get_action(parser, dest):
  118. for action in parser._actions:
  119. if action.dest == dest:
  120. return action
  121. def find_arg_by_option(group, option_string):
  122. for arg in group:
  123. if option_string in arg.option_strings:
  124. return arg