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.

119 lines
3.7 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 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_convert_std_parser(complete_parser):
  8. result = convert(complete_parser)
  9. assert result['layout_type'] == 'standard'
  10. assert result['widgets']
  11. assert isinstance(result['widgets'], list)
  12. entry = result['widgets'][0]
  13. assert 'type' in entry
  14. assert 'required' in entry
  15. assert 'data' in entry
  16. required = filter(lambda x: x['required'], result['widgets'])
  17. optional = filter(lambda x: not x['required'], result['widgets'])
  18. assert len(required) == 4
  19. assert len(optional) == 8
  20. def test_convert_sub_parser(subparser):
  21. result = convert(subparser)
  22. assert result['layout_type'] == 'column'
  23. assert result['widgets']
  24. assert isinstance(result['widgets'], dict)
  25. assert len(result['widgets']) == 3
  26. def test_has_required(empty_parser, complete_parser, subparser):
  27. assert has_required(complete_parser._actions)
  28. assert not has_required(empty_parser._actions)
  29. assert not has_required(subparser._actions)
  30. def test_has_subparsers(subparser, complete_parser):
  31. assert has_subparsers(subparser._actions)
  32. assert not has_subparsers(complete_parser._actions)
  33. def test_is_required(complete_parser):
  34. required = filter(is_required, complete_parser._actions)
  35. assert len(required) == 4
  36. for action in required:
  37. print action.dest.startswith('req')
  38. def test_is_optional(complete_parser):
  39. optional = filter(is_optional, complete_parser._actions)
  40. assert len(optional) == 10
  41. for action in optional:
  42. assert 'req' not in action.dest
  43. def test_is_choice(empty_parser):
  44. empty_parser.add_argument('--dropdown', choices=[1,2])
  45. assert is_choice(get_action(empty_parser, 'dropdown'))
  46. empty_parser.add_argument('--storetrue', action='store_true')
  47. assert not is_choice(get_action(empty_parser, 'storetrue'))
  48. # make sure positionals are caught as well (issue #85)
  49. empty_parser.add_argument('positional', choices=[1, 2])
  50. assert is_choice(get_action(empty_parser, 'positional'))
  51. def test_is_standard(empty_parser):
  52. empty_parser.add_argument('--count', action='count')
  53. assert not is_standard(get_action(empty_parser, 'count'))
  54. empty_parser.add_argument('--store', action='store')
  55. assert is_standard(get_action(empty_parser, 'store'))
  56. def test_is_counter(empty_parser):
  57. empty_parser.add_argument('--count', action='count')
  58. assert is_counter(get_action(empty_parser, 'count'))
  59. empty_parser.add_argument('--dropdown', choices=[1,2])
  60. assert not is_counter(get_action(empty_parser, 'dropdown'))
  61. def test_mutually(exclusive_group):
  62. target_arg = find_arg_by_option(exclusive_group, '-i')
  63. json_result = build_radio_group(exclusive_group)
  64. data = json_result['data'][0]
  65. assert 'RadioGroup' == json_result['type']
  66. assert target_arg.choices == data['choices']
  67. assert target_arg.help == data['help']
  68. assert target_arg.option_strings == data['commands']
  69. assert target_arg.dest == data['display_name']
  70. def test_empty_mutex_group():
  71. assert not build_radio_group(None)
  72. def test_as_json_invalid_widget():
  73. with pytest.raises(UnknownWidgetType):
  74. as_json(None, 'InvalidWidget', None)
  75. def get_action(parser, dest):
  76. for action in parser._actions:
  77. if action.dest == dest:
  78. return action
  79. def find_arg_by_option(group, option_string):
  80. for arg in group:
  81. if option_string in arg.option_strings:
  82. return arg