Browse Source

Updated tests

pull/90/merge
chriskiehl 9 years ago
parent
commit
9796d50fa9
1 changed files with 103 additions and 72 deletions
  1. 175
      gooey/tests/argparse_to_json_unittest.py

175
gooey/tests/argparse_to_json_unittest.py

@ -1,74 +1,105 @@
import argparse
import unittest
import json
from argparse_to_json import *
class TestArgparseToJson(unittest.TestCase):
def setUp(self):
my_cool_parser = argparse.ArgumentParser(description='description')
my_cool_parser.add_argument("filename", help='filename help msg') # positional
my_cool_parser.add_argument("outfile", help="Name of the file where you'll save the output") # positional
my_cool_parser.add_argument('-c', '--countdown', default=10, type=int, help='sets the time to count down from')
my_cool_parser.add_argument("-s", "--showtime", action="store_true", help="display the countdown timer")
my_cool_parser.add_argument("-d", "--delay", action="store_true", help="Delay execution for a bit")
my_cool_parser.add_argument('--verbose', '-v', action='count')
my_cool_parser.add_argument("-o", "--obfuscate", action="store_true", help="obfuscate the countdown timer!")
my_cool_parser.add_argument('-r', '--recursive', choices=['yes', 'no'], help='Recurse into subfolders')
my_cool_parser.add_argument("-w", "--writelog", default="No, NOT whatevs", help="write log to some file or something")
my_cool_parser.add_argument("-e", "--expandAll", action="store_true", help="expand all processes")
verbosity = my_cool_parser.add_mutually_exclusive_group()
verbosity.add_argument('-t', '--verboze', dest='verboze', action="store_true", help="Show more details")
verbosity.add_argument('-q', '--quiet', dest='quiet', action="store_true", help="Only output on error")
self.parser = my_cool_parser
self.mutually_exclusive_group = [
mutex_action
for group_actions in self.parser._mutually_exclusive_groups
for mutex_action in group_actions._group_actions]
self.base_actions = [action for action in self.parser._actions
if action not in self.mutually_exclusive_group]
def test_get_optionals_with_choices(self):
target_arg = self.find_arg_by_option(self.base_actions, '--recursive')
json_result = get_optionals_with_choices(self.base_actions)
self._test_parser_to_json_mapping(target_arg, json_result[0], 'Dropdown')
def test_get_optionals_without_choices(self):
target_arg = self.find_arg_by_option(self.base_actions, '--showtime')
json_result = get_optionals_without_choices(self.base_actions)
self._test_parser_to_json_mapping(target_arg, json_result[0], 'TextField')
def test_get_counter_style_optionals(self):
target_arg = self.find_arg_by_option(self.base_actions, '--verbose')
json_result = get_counter_style_optionals(self.base_actions)
print json_result
self._test_parser_to_json_mapping(target_arg, json_result[0], 'Dropdown')
def test_get_mutually_exclusive_optionals(self):
target_arg = self.find_arg_by_option(self.mutually_exclusive_group, '--verboze')
json_result = get_mutually_exclusive_optionals(self.mutually_exclusive_group)[0]
data = json_result['data'][0]
self.assertEqual('RadioGroup', json_result['type'])
self.assertEqual(target_arg.choices, data['choices'])
self.assertEqual(target_arg.help, data['help'])
self.assertEqual(target_arg.option_strings, data['commands'])
self.assertEqual(target_arg.dest, data['display_name'])
def _test_parser_to_json_mapping(self, target_arg, json_string, expected_type):
self.assertEqual(expected_type, json_string['type'])
self.assertEqual(target_arg.choices, json_string['data']['choices'])
self.assertEqual(target_arg.help, json_string['data']['help'])
self.assertEqual(target_arg.option_strings, json_string['data']['commands'])
self.assertEqual(target_arg.dest, json_string['data']['display_name'])
def find_arg_by_option(self, group, option_string):
for arg in group:
if option_string in arg.option_strings:
return arg
import pytest
from gooey.python_bindings.argparse_to_json import *
@pytest.fixture
def parser():
return argparse.ArgumentParser(description='description')
@pytest.fixture
def complete_parser():
parser = argparse.ArgumentParser(description='description')
parser.add_argument("req1", help='filename help msg') # positional
parser.add_argument("req2", help="Name of the file where you'll save the output") # positional
parser.add_argument('-r', dest="req3", default=10, type=int, help='sets the time to count down from', required=True)
parser.add_argument('--req4', dest="req4", default=10, type=int, help='sets the time to count down from', required=True)
parser.add_argument("-a", "--aa", action="store_true", help="aaa")
parser.add_argument("-b", "--bb", action="store_true", help="bbb")
parser.add_argument('-c', '--cc', action='count')
parser.add_argument("-d", "--dd", action="store_true", help="ddd")
parser.add_argument('-e', '--ee', choices=['yes', 'no'], help='eee')
parser.add_argument("-f", "--ff", default="0000", help="fff")
parser.add_argument("-g", "--gg", action="store_true", help="ggg")
verbosity = parser.add_mutually_exclusive_group()
verbosity.add_argument('-i', '--ii', action="store_true", help="iii")
verbosity.add_argument('-j', '--jj', action="store_true", help="hhh")
return parser
@pytest.fixture
def exclusive_group():
parser = argparse.ArgumentParser(description='description')
verbosity = parser.add_mutually_exclusive_group()
verbosity.add_argument('-i', dest="option1", action="store_true", help="iii")
verbosity.add_argument('-j', dest="option2", action="store_true", help="hhh")
mutually_exclusive_group = [mutex_action
for group_actions in parser._mutually_exclusive_groups
for mutex_action in group_actions._group_actions]
return mutually_exclusive_group
def test_is_required(complete_parser):
required = filter(is_required, complete_parser._actions)
assert len(required) == 4
for action in required:
print action.dest.startswith('req')
def test_is_optional(complete_parser):
optional = filter(is_optional, complete_parser._actions)
assert len(optional) == 10
for action in optional:
assert 'req' not in action.dest
def test_is_choice(parser):
parser.add_argument('--dropdown', choices=[1,2])
assert is_choice(get_action(parser, 'dropdown'))
parser.add_argument('--storetrue', action='store_true')
assert not is_choice(get_action(parser, 'storetrue'))
# make sure positionals are caught as well (issue #85)
parser.add_argument('positional', choices=[1, 2])
assert is_choice(get_action(parser, 'positional'))
def test_is_standard(parser):
parser.add_argument('--count', action='count')
assert not is_standard(get_action(parser, 'count'))
parser.add_argument('--store', action='store')
assert is_standard(get_action(parser, 'store'))
def test_is_counter(parser):
parser.add_argument('--count', action='count')
assert is_counter(get_action(parser, 'count'))
parser.add_argument('--dropdown', choices=[1,2])
assert not is_counter(get_action(parser, 'dropdown'))
def test_mutually(exclusive_group):
target_arg = find_arg_by_option(exclusive_group, '-i')
json_result = build_radio_group(exclusive_group)[0]
data = json_result['data'][0]
assert 'RadioGroup' == json_result['type']
assert target_arg.choices == data['choices']
assert target_arg.help == data['help']
assert target_arg.option_strings == data['commands']
assert target_arg.dest == data['display_name']
def get_action(parser, dest):
for action in parser._actions:
if action.dest == dest:
return action
def find_arg_by_option(group, option_string):
for arg in group:
if option_string in arg.option_strings:
return arg
Loading…
Cancel
Save