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.

140 lines
5.4 KiB

9 years ago
  1. import sys
  2. import unittest
  3. from argparse import ArgumentParser
  4. from gooey import GooeyParser
  5. from gooey.python_bindings import argparse_to_json
  6. from gooey.util.functional import getin
  7. class TestArgparse(unittest.TestCase):
  8. def test_mutex_groups_conversion(self):
  9. """
  10. Ensure multiple mutex groups are processed correctly.
  11. """
  12. parser = ArgumentParser()
  13. g1 = parser.add_mutually_exclusive_group(required=True)
  14. g1.add_argument('--choose1')
  15. g1.add_argument('--choose2')
  16. g2 = parser.add_mutually_exclusive_group(required=True)
  17. g2.add_argument('--choose3')
  18. g2.add_argument('--choose4')
  19. output = argparse_to_json.process(parser, {}, {}, {})
  20. # assert that we get two groups of two choices back
  21. items = output[0]['items']
  22. self.assertTrue(len(items) == 2)
  23. group1 = items[0]
  24. group2 = items[1]
  25. self.assertTrue(['--choose1'] in group1['data']['commands'])
  26. self.assertTrue(['--choose2'] in group1['data']['commands'])
  27. self.assertTrue(['--choose3'] in group2['data']['commands'])
  28. self.assertTrue(['--choose4'] in group2['data']['commands'])
  29. self.assertTrue(group1['type'] == 'RadioGroup')
  30. self.assertTrue(group2['type'] == 'RadioGroup')
  31. def test_json_iterable_conversion(self):
  32. """
  33. Issue #312 - tuples weren't being coerced to list during argparse
  34. conversion causing downstream issues when concatenating
  35. """
  36. # our original functionality accepted only lists as the choices arg
  37. parser = ArgumentParser()
  38. parser.add_argument("-foo", choices=['foo','bar', 'baz'])
  39. result = argparse_to_json.action_to_json(parser._actions[-1], "Dropdown", {})
  40. choices = result['data']['choices']
  41. self.assertTrue(isinstance(choices, list))
  42. self.assertEqual(choices, ['foo','bar', 'baz'])
  43. # Now we allow tuples as well.
  44. parser = ArgumentParser()
  45. parser.add_argument("-foo", choices=('foo','bar', 'baz'))
  46. result = argparse_to_json.action_to_json(parser._actions[-1], "Dropdown", {})
  47. choices = result['data']['choices']
  48. self.assertTrue(isinstance(choices, list))
  49. self.assertEqual(choices, ['foo','bar', 'baz'])
  50. def test_choice_string_cooersion(self):
  51. """
  52. Issue 321 - must coerce choice types to string to support wx.ComboBox
  53. """
  54. parser = ArgumentParser()
  55. parser.add_argument('--foo', default=1, choices=[1, 2, 3])
  56. choice_action = parser._actions[-1]
  57. result = argparse_to_json.action_to_json(choice_action, 'Dropdown', {})
  58. self.assertEqual(getin(result, ['data', 'choices']), ['1', '2', '3'])
  59. # default value is also converted to a string type
  60. self.assertEqual(getin(result, ['data', 'default']), '1')
  61. def test_choice_string_cooersion_no_default(self):
  62. """
  63. Make sure that choice types without a default don't create
  64. the literal string "None" but stick with the value None
  65. """
  66. parser = ArgumentParser()
  67. parser.add_argument('--foo', choices=[1, 2, 3])
  68. choice_action = parser._actions[-1]
  69. result = argparse_to_json.action_to_json(choice_action, 'Dropdown', {})
  70. self.assertEqual(getin(result, ['data', 'default']), None)
  71. def test_listbox_defaults_cast_correctly(self):
  72. """
  73. Issue XXX - defaults supplied in a list were turned into a string
  74. wholesale (list and all). The defaults should be stored as a list
  75. proper with only the _internal_ values coerced to strings.
  76. """
  77. parser = GooeyParser()
  78. parser.add_argument('--foo', widget="Listbox", nargs="*", choices=[1, 2, 3], default=[1, 2])
  79. choice_action = parser._actions[-1]
  80. result = argparse_to_json.action_to_json(choice_action, 'Listbox', {})
  81. self.assertEqual(getin(result, ['data', 'default']), ['1', '2'])
  82. def test_listbox_single_default_cast_correctly(self):
  83. """
  84. Single arg defaults to listbox should be wrapped in a list and
  85. their contents coerced as usual.
  86. """
  87. parser = GooeyParser()
  88. parser.add_argument('--foo', widget="Listbox",
  89. nargs="*", choices=[1, 2, 3], default="sup")
  90. choice_action = parser._actions[-1]
  91. result = argparse_to_json.action_to_json(choice_action, 'Listbox', {})
  92. self.assertEqual(getin(result, ['data', 'default']), ['sup'])
  93. def test_non_data_defaults_are_dropped_entirely(self):
  94. """
  95. This is a refinement in understanding of Issue #147
  96. Caused by Issue 377 - passing arbitrary objects as defaults
  97. causes failures.
  98. """
  99. # passing plain data to cleaning function results in plain data
  100. # being returned
  101. data = ['abc',
  102. 123,
  103. ['a', 'b'],
  104. [1, 2, 3]]
  105. for datum in data:
  106. result = argparse_to_json.clean_default(datum)
  107. self.assertEqual(result, datum)
  108. # passing in complex objects results in None
  109. objects = [sys.stdout, sys.stdin, object(), max, min]
  110. for obj in objects:
  111. result = argparse_to_json.clean_default(obj)
  112. self.assertEqual(result, None)