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.

308 lines
13 KiB

2 years ago
2 years ago
9 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. import argparse
  2. import sys
  3. import unittest
  4. from argparse import ArgumentParser, FileType
  5. from gooey import GooeyParser
  6. from gooey.python_bindings import argparse_to_json
  7. from gooey.util.functional import getin
  8. from gooey.tests import *
  9. from gooey.gui.components.options.options import FileChooser
  10. from gooey.gui.components.widgets import FileSaver
  11. class TestArgparse(unittest.TestCase):
  12. def test_mutex_groups_conversion(self):
  13. """
  14. Ensure multiple mutex groups are processed correctly.
  15. """
  16. parser = ArgumentParser()
  17. g1 = parser.add_mutually_exclusive_group(required=True)
  18. g1.add_argument('--choose1')
  19. g1.add_argument('--choose2')
  20. g2 = parser.add_mutually_exclusive_group(required=True)
  21. g2.add_argument('--choose3')
  22. g2.add_argument('--choose4')
  23. output = argparse_to_json.process(parser, {}, {}, {})
  24. # assert that we get two groups of two choices back
  25. items = output[0]['items']
  26. self.assertTrue(len(items) == 2)
  27. group1 = items[0]
  28. group2 = items[1]
  29. self.assertTrue(['--choose1'] in group1['data']['commands'])
  30. self.assertTrue(['--choose2'] in group1['data']['commands'])
  31. self.assertTrue(['--choose3'] in group2['data']['commands'])
  32. self.assertTrue(['--choose4'] in group2['data']['commands'])
  33. self.assertTrue(group1['type'] == 'RadioGroup')
  34. self.assertTrue(group2['type'] == 'RadioGroup')
  35. def test_json_iterable_conversion(self):
  36. """
  37. Issue #312 - tuples weren't being coerced to list during argparse
  38. conversion causing downstream issues when concatenating
  39. """
  40. # our original functionality accepted only lists as the choices arg
  41. parser = ArgumentParser()
  42. parser.add_argument("-foo", choices=['foo','bar', 'baz'])
  43. result = argparse_to_json.action_to_json(parser._actions[-1], "Dropdown", {})
  44. choices = result['data']['choices']
  45. self.assertTrue(isinstance(choices, list))
  46. self.assertEqual(choices, ['foo','bar', 'baz'])
  47. # Now we allow tuples as well.
  48. parser = ArgumentParser()
  49. parser.add_argument("-foo", choices=('foo','bar', 'baz'))
  50. result = argparse_to_json.action_to_json(parser._actions[-1], "Dropdown", {})
  51. choices = result['data']['choices']
  52. self.assertTrue(isinstance(choices, list))
  53. self.assertEqual(choices, ['foo','bar', 'baz'])
  54. def test_choice_string_cooersion(self):
  55. """
  56. Issue 321 - must coerce choice types to string to support wx.ComboBox
  57. """
  58. parser = ArgumentParser()
  59. parser.add_argument('--foo', default=1, choices=[1, 2, 3])
  60. choice_action = parser._actions[-1]
  61. result = argparse_to_json.action_to_json(choice_action, 'Dropdown', {})
  62. self.assertEqual(getin(result, ['data', 'choices']), ['1', '2', '3'])
  63. # default value is also converted to a string type
  64. self.assertEqual(getin(result, ['data', 'default']), '1')
  65. def test_choice_string_cooersion_no_default(self):
  66. """
  67. Make sure that choice types without a default don't create
  68. the literal string "None" but stick with the value None
  69. """
  70. parser = ArgumentParser()
  71. parser.add_argument('--foo', choices=[1, 2, 3])
  72. choice_action = parser._actions[-1]
  73. result = argparse_to_json.action_to_json(choice_action, 'Dropdown', {})
  74. self.assertEqual(getin(result, ['data', 'default']), None)
  75. def test_listbox_defaults_cast_correctly(self):
  76. """
  77. Issue XXX - defaults supplied in a list were turned into a string
  78. wholesale (list and all). The defaults should be stored as a list
  79. proper with only the _internal_ values coerced to strings.
  80. """
  81. parser = GooeyParser()
  82. parser.add_argument('--foo', widget="Listbox", nargs="*", choices=[1, 2, 3], default=[1, 2])
  83. choice_action = parser._actions[-1]
  84. result = argparse_to_json.action_to_json(choice_action, 'Listbox', {})
  85. self.assertEqual(getin(result, ['data', 'default']), ['1', '2'])
  86. def test_listbox_single_default_cast_correctly(self):
  87. """
  88. Single arg defaults to listbox should be wrapped in a list and
  89. their contents coerced as usual.
  90. """
  91. parser = GooeyParser()
  92. parser.add_argument('--foo', widget="Listbox",
  93. nargs="*", choices=[1, 2, 3], default="sup")
  94. choice_action = parser._actions[-1]
  95. result = argparse_to_json.action_to_json(choice_action, 'Listbox', {})
  96. self.assertEqual(getin(result, ['data', 'default']), ['sup'])
  97. def test_non_data_defaults_are_dropped_entirely(self):
  98. """
  99. This is a refinement in understanding of Issue #147
  100. Caused by Issue 377 - passing arbitrary objects as defaults
  101. causes failures.
  102. """
  103. # passing plain data to cleaning function results in plain data
  104. # being returned
  105. data = ['abc',
  106. 123,
  107. ['a', 'b'],
  108. [1, 2, 3]]
  109. for datum in data:
  110. result = argparse_to_json.clean_default(datum)
  111. self.assertEqual(result, datum)
  112. # passing in complex objects results in None
  113. objects = [sys.stdout, sys.stdin, object(), max, min]
  114. for obj in objects:
  115. result = argparse_to_json.clean_default(obj)
  116. self.assertEqual(result, None)
  117. def test_suppress_is_removed_as_default_value(self):
  118. """
  119. Issue #469
  120. Argparse uses the literal string ==SUPPRESS== as an internal flag.
  121. When encountered in Gooey, these should be dropped and mapped to `None`.
  122. """
  123. parser = ArgumentParser(prog='test_program')
  124. parser.add_argument("--foo", default=argparse.SUPPRESS)
  125. parser.add_argument('--version', action='version', version='1.0')
  126. result = argparse_to_json.convert(parser, required_cols=2, optional_cols=2)
  127. groups = getin(result, ['widgets', 'test_program', 'contents'])
  128. for item in groups[0]['items']:
  129. self.assertEqual(getin(item, ['data', 'default']), None)
  130. def test_version_maps_to_checkbox(self):
  131. testcases = [
  132. [['--version'], {}, 'TextField'],
  133. # we only remap if the action is version
  134. # i.e. we don't care about the argument name itself
  135. [['--version'], {'action': 'store'}, 'TextField'],
  136. # should get mapped to CheckBox because of the action
  137. [['--version'], {'action': 'version'}, 'CheckBox'],
  138. # ditto, even through the 'name' isn't 'version'
  139. [['--foobar'], {'action': 'version'}, 'CheckBox'],
  140. ]
  141. for args, kwargs, expectedType in testcases:
  142. with self.subTest([args, kwargs]):
  143. parser = argparse.ArgumentParser(prog='test')
  144. parser.add_argument(*args, **kwargs)
  145. result = argparse_to_json.convert(parser, required_cols=2, optional_cols=2)
  146. contents = getin(result, ['widgets', 'test', 'contents'])[0]
  147. self.assertEqual(contents['items'][0]['type'], expectedType)
  148. def test_textinput_with_list_default_mapped_to_cli_friendly_value(self):
  149. """
  150. Issue: #500
  151. Using nargs and a `default` value with a list causes the literal list string
  152. to be put into the UI.
  153. """
  154. testcases = [
  155. {'nargs': '+', 'default': ['a b', 'c'], 'gooey_default': '"a b" "c"', 'w': 'TextField'},
  156. {'nargs': '*', 'default': ['a b', 'c'], 'gooey_default': '"a b" "c"', 'w': 'TextField'},
  157. {'nargs': '...', 'default': ['a b', 'c'], 'gooey_default': '"a b" "c"', 'w': 'TextField'},
  158. {'nargs': 2, 'default': ['a b', 'c'], 'gooey_default': '"a b" "c"', 'w': 'TextField'},
  159. # TODO: this demos the current nargs behavior for string defaults, but
  160. # TODO: it is wrong! These should be wrapped in quotes so spaces aren't
  161. # TODO: interpreted as unique arguments.
  162. {'nargs': '+', 'default': 'a b', 'gooey_default': 'a b', 'w': 'TextField'},
  163. {'nargs': '*', 'default': 'a b', 'gooey_default': 'a b', 'w': 'TextField'},
  164. {'nargs': '...', 'default': 'a b', 'gooey_default': 'a b', 'w': 'TextField'},
  165. {'nargs': 1, 'default': 'a b', 'gooey_default': 'a b', 'w': 'TextField'},
  166. # Listbox has special nargs handling which keeps the list in tact.
  167. {'nargs': '+', 'default': ['a b', 'c'], 'gooey_default': ['a b', 'c'], 'w': 'Listbox'},
  168. {'nargs': '*', 'default': ['a b', 'c'], 'gooey_default': ['a b', 'c'], 'w': 'Listbox'},
  169. {'nargs': '...', 'default': ['a b', 'c'], 'gooey_default': ['a b', 'c'],'w': 'Listbox'},
  170. {'nargs': 2, 'default': ['a b', 'c'], 'gooey_default': ['a b', 'c'], 'w': 'Listbox'},
  171. {'nargs': '+', 'default': 'a b', 'gooey_default': ['a b'], 'w': 'Listbox'},
  172. {'nargs': '*', 'default': 'a b', 'gooey_default': ['a b'], 'w': 'Listbox'},
  173. {'nargs': '...', 'default': 'a b', 'gooey_default': ['a b'], 'w': 'Listbox'},
  174. {'nargs': 1, 'default': 'a b', 'gooey_default': ['a b'], 'w': 'Listbox'},
  175. ]
  176. for case in testcases:
  177. with self.subTest(case):
  178. parser = ArgumentParser(prog='test_program')
  179. parser.add_argument('--foo', nargs=case['nargs'], default=case['default'])
  180. action = parser._actions[-1]
  181. result = argparse_to_json.handle_initial_values(action, case['w'], action.default)
  182. self.assertEqual(result, case['gooey_default'])
  183. def test_nargs(self):
  184. """
  185. so there are just a few simple rules here:
  186. if nargs in [*, N, +, remainder]:
  187. default MUST be a list OR we must map it to one
  188. action:_StoreAction
  189. - nargs '?'
  190. - default:validate list is invalid
  191. - default:coerce stringify
  192. - nargs #{*, N, +, REMAINDER}
  193. - default:validate None
  194. - default:coerce
  195. if string: stringify
  196. if list: convert from list to cli style input string
  197. action:_StoreConstAction
  198. - nargs: invalid
  199. - defaults:stringify
  200. action:{_StoreFalseAction, _StoreTrueAction}
  201. - nargs: invalid
  202. - defaults:validate: require bool
  203. - defaults:coerce: no stringify; leave bool
  204. action:_CountAction
  205. - nargs: invalid
  206. - default:validate: must be numeric index within range OR None
  207. - default:coerce: integer or None
  208. action:_AppendAction
  209. TODO: NOT CURRENTLY SUPPORTED BY GOOEY
  210. nargs behavior is weird and needs to be understood.
  211. - nargs
  212. action:CustomUserAction:
  213. - nargs: no way to know expected behavior. Ignore
  214. - default: jsonify type if possible.
  215. """
  216. parser = ArgumentParser()
  217. parser.add_argument(
  218. '--bar',
  219. nargs='+',
  220. choices=["one", "two"],
  221. default="one",
  222. )
  223. def test_filetype_chooses_good_widget(self):
  224. """
  225. #743 chose the picker type based on the FileType mode
  226. when available.
  227. """
  228. cases = [
  229. (FileType(), 'FileChooser'),
  230. (FileType('r'), 'FileChooser'),
  231. (FileType('rb'), 'FileChooser'),
  232. (FileType('rt'), 'FileChooser'),
  233. (FileType('w'), 'FileSaver'),
  234. (FileType('wt'), 'FileSaver'),
  235. (FileType('wb'), 'FileSaver'),
  236. (FileType('a'), 'FileSaver'),
  237. (FileType('x'), 'FileSaver'),
  238. (FileType('+'), 'FileSaver'),
  239. ]
  240. for filetype, expected_widget in cases:
  241. with self.subTest(f'expect {filetype} to produce {expected_widget})'):
  242. parser = ArgumentParser()
  243. parser.add_argument('foo', type=filetype)
  244. action = [parser._actions[-1]]
  245. result = next(argparse_to_json.categorize(action, {}, {}))
  246. self.assertEqual(result['type'], expected_widget)
  247. def test_nargs_with_choices_chooses_good_widget(self):
  248. """
  249. #763 argument with nargs in {+, *} and a list of choices should use
  250. a Listbox widget
  251. """
  252. cases = ['*', '+']
  253. for nargs in cases:
  254. with self.subTest(f'expect {nargs} to produce a Listbox'):
  255. parser = ArgumentParser()
  256. parser.add_argument('foo', nargs=nargs, choices=['choice', 'choice1'])
  257. action = [parser._actions[-1]]
  258. result = next(argparse_to_json.categorize(action, {}, {}))
  259. self.assertEqual(result['type'], 'Listbox')