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.

159 lines
5.3 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. def test_parser_converts_to_correct_type(empty_parser, complete_parser, subparser):
  4. assert convert(subparser)['layout_type'] == 'column'
  5. assert convert(empty_parser)['layout_type'] == 'standard'
  6. assert convert(complete_parser)['layout_type'] == 'standard'
  7. def test_parser_without_subparser_recieves_root_entry(complete_parser):
  8. '''
  9. Non-subparser setups should receive a default root key called 'primary'
  10. '''
  11. result = convert(complete_parser)
  12. assert 'primary' in result['widgets']
  13. def test_grouping_structure(complete_parser):
  14. '''
  15. The output of the 'widgets' branch is now wrapped in another
  16. layer to facilitate interop with the subparser structure
  17. old: widgets: []
  18. new: widgets: {'a': {}, 'b': {}, ..}
  19. '''
  20. result = convert(complete_parser)
  21. groupings = result['widgets']
  22. # should now be a dict rather than a list
  23. assert isinstance(groupings, dict)
  24. # make sure our expected root keys are there
  25. for name, group in groupings.iteritems():
  26. assert 'command' in group
  27. assert 'contents' in group
  28. # contents should be the old list of widget info
  29. assert isinstance(group['contents'], list)
  30. def test_subparser_uses_prog_value_if_available():
  31. parser = argparse.ArgumentParser(description='qidev')
  32. parser.add_argument('--verbose', help='be verbose', dest='verbose', action='store_true', default=False)
  33. subs = parser.add_subparsers(help='commands', dest='command')
  34. # NO prog definition for the sub parser
  35. subs.add_parser('config', help='configure defaults for qidev')
  36. # The stock parser name supplied above (e.g. config) is
  37. # now in the converted doc
  38. result = convert(parser)
  39. assert 'config' in result['widgets']
  40. # new subparser
  41. parser = argparse.ArgumentParser(description='qidev')
  42. parser.add_argument('--verbose', help='be verbose', dest='verbose', action='store_true', default=False)
  43. subs = parser.add_subparsers(help='commands', dest='command')
  44. # prog definition for the sub parser IS supplied
  45. subs.add_parser('config', prog="My Config", help='configure defaults for qidev')
  46. # Should've picked up the prog value
  47. result = convert(parser)
  48. assert 'My Config' in result['widgets']
  49. def test_convert_std_parser(complete_parser):
  50. result = convert(complete_parser)
  51. # grab the first entry from the dict
  52. entry = result['widgets']['primary']['contents'][0]
  53. print entry
  54. assert 'type' in entry
  55. assert 'required' in entry
  56. assert 'data' in entry
  57. def test_convert_sub_parser(subparser):
  58. result = convert(subparser)
  59. assert result['layout_type'] == 'column'
  60. assert result['widgets']
  61. assert isinstance(result['widgets'], dict)
  62. assert len(result['widgets']) == 3
  63. def test_has_required(empty_parser, complete_parser, subparser):
  64. assert has_required(complete_parser._actions)
  65. assert not has_required(empty_parser._actions)
  66. assert not has_required(subparser._actions)
  67. def test_has_subparsers(subparser, complete_parser):
  68. assert has_subparsers(subparser._actions)
  69. assert not has_subparsers(complete_parser._actions)
  70. def test_is_required(complete_parser):
  71. required = filter(is_required, complete_parser._actions)
  72. assert len(required) == 4
  73. for action in required:
  74. print action.dest.startswith('req')
  75. def test_is_optional(complete_parser):
  76. optional = filter(is_optional, complete_parser._actions)
  77. assert len(optional) == 10
  78. for action in optional:
  79. assert 'req' not in action.dest
  80. def test_is_choice(empty_parser):
  81. empty_parser.add_argument('--dropdown', choices=[1,2])
  82. assert is_choice(get_action(empty_parser, 'dropdown'))
  83. empty_parser.add_argument('--storetrue', action='store_true')
  84. assert not is_choice(get_action(empty_parser, 'storetrue'))
  85. # make sure positionals are caught as well (issue #85)
  86. empty_parser.add_argument('positional', choices=[1, 2])
  87. assert is_choice(get_action(empty_parser, 'positional'))
  88. def test_is_standard(empty_parser):
  89. empty_parser.add_argument('--count', action='count')
  90. assert not is_standard(get_action(empty_parser, 'count'))
  91. empty_parser.add_argument('--store', action='store')
  92. assert is_standard(get_action(empty_parser, 'store'))
  93. def test_is_counter(empty_parser):
  94. empty_parser.add_argument('--count', action='count')
  95. assert is_counter(get_action(empty_parser, 'count'))
  96. empty_parser.add_argument('--dropdown', choices=[1,2])
  97. assert not is_counter(get_action(empty_parser, 'dropdown'))
  98. def test_mutually(exclusive_group):
  99. target_arg = find_arg_by_option(exclusive_group, '-i')
  100. json_result = build_radio_group(exclusive_group)
  101. data = json_result['data'][0]
  102. assert 'RadioGroup' == json_result['type']
  103. assert target_arg.choices == data['choices']
  104. assert target_arg.help == data['help']
  105. assert target_arg.option_strings == data['commands']
  106. assert target_arg.dest == data['display_name']
  107. def test_empty_mutex_group():
  108. assert not build_radio_group(None)
  109. def test_as_json_invalid_widget():
  110. with pytest.raises(UnknownWidgetType):
  111. as_json(None, 'InvalidWidget', None)
  112. def get_action(parser, dest):
  113. for action in parser._actions:
  114. if action.dest == dest:
  115. return action
  116. def find_arg_by_option(group, option_string):
  117. for arg in group:
  118. if option_string in arg.option_strings:
  119. return arg