mirror of https://github.com/chriskiehl/Gooey.git
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.
61 lines
2.3 KiB
61 lines
2.3 KiB
import unittest
|
|
from argparse import ArgumentParser
|
|
|
|
from python_bindings import argparse_to_json
|
|
from util.functional import getin
|
|
|
|
|
|
class TestArgparse(unittest.TestCase):
|
|
"""
|
|
TODO:
|
|
"""
|
|
|
|
def test_json_iterable_conversion(self):
|
|
"""
|
|
Issue #312 - tuples weren't being coerced to list during argparse
|
|
conversion causing downstream issues when concatenating
|
|
"""
|
|
# our original functionality accepted only lists as the choices arg
|
|
parser = ArgumentParser()
|
|
parser.add_argument("-foo", choices=['foo','bar', 'baz'])
|
|
result = argparse_to_json.action_to_json(parser._actions[-1], "Dropdown", {})
|
|
|
|
choices = result['data']['choices']
|
|
self.assertTrue(isinstance(choices, list))
|
|
self.assertEqual(choices, ['foo','bar', 'baz'])
|
|
|
|
# Now we allow tuples as well.
|
|
parser = ArgumentParser()
|
|
parser.add_argument("-foo", choices=('foo','bar', 'baz'))
|
|
result = argparse_to_json.action_to_json(parser._actions[-1], "Dropdown", {})
|
|
|
|
choices = result['data']['choices']
|
|
self.assertTrue(isinstance(choices, list))
|
|
self.assertEqual(choices, ['foo','bar', 'baz'])
|
|
|
|
|
|
def test_choice_string_cooersion(self):
|
|
"""
|
|
Issue 321 - must coerce choice types to string to support wx.ComboBox
|
|
"""
|
|
parser = ArgumentParser()
|
|
parser.add_argument('--foo', default=1, choices=[1, 2, 3])
|
|
choice_action = parser._actions[-1]
|
|
result = argparse_to_json.action_to_json(choice_action, 'Dropdown', {})
|
|
self.assertEqual(getin(result, ['data', 'choices']), ['1', '2', '3'])
|
|
# default value is also converted to a string type
|
|
self.assertEqual(getin(result, ['data', 'default']), '1')
|
|
|
|
def test_choice_string_cooersion_no_default(self):
|
|
"""
|
|
Make sure that choice types without a default don't create
|
|
the literal string "None" but stick with the value None
|
|
"""
|
|
parser = ArgumentParser()
|
|
parser.add_argument('--foo', choices=[1, 2, 3])
|
|
|
|
choice_action = parser._actions[-1]
|
|
result = argparse_to_json.action_to_json(choice_action, 'Dropdown', {})
|
|
self.assertEqual(getin(result, ['data', 'default']), None)
|
|
|
|
|