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.

95 lines
3.8 KiB

9 years ago
  1. import unittest
  2. from argparse import ArgumentParser
  3. from gooey import GooeyParser
  4. from gooey.python_bindings import argparse_to_json
  5. from gooey.util.functional import getin
  6. class TestArgparse(unittest.TestCase):
  7. def test_json_iterable_conversion(self):
  8. """
  9. Issue #312 - tuples weren't being coerced to list during argparse
  10. conversion causing downstream issues when concatenating
  11. """
  12. # our original functionality accepted only lists as the choices arg
  13. parser = ArgumentParser()
  14. parser.add_argument("-foo", choices=['foo','bar', 'baz'])
  15. result = argparse_to_json.action_to_json(parser._actions[-1], "Dropdown", {})
  16. choices = result['data']['choices']
  17. self.assertTrue(isinstance(choices, list))
  18. self.assertEqual(choices, ['foo','bar', 'baz'])
  19. # Now we allow tuples as well.
  20. parser = ArgumentParser()
  21. parser.add_argument("-foo", choices=('foo','bar', 'baz'))
  22. result = argparse_to_json.action_to_json(parser._actions[-1], "Dropdown", {})
  23. choices = result['data']['choices']
  24. self.assertTrue(isinstance(choices, list))
  25. self.assertEqual(choices, ['foo','bar', 'baz'])
  26. def test_choice_string_cooersion(self):
  27. """
  28. Issue 321 - must coerce choice types to string to support wx.ComboBox
  29. """
  30. parser = ArgumentParser()
  31. parser.add_argument('--foo', default=1, choices=[1, 2, 3])
  32. choice_action = parser._actions[-1]
  33. result = argparse_to_json.action_to_json(choice_action, 'Dropdown', {})
  34. self.assertEqual(getin(result, ['data', 'choices']), ['1', '2', '3'])
  35. # default value is also converted to a string type
  36. self.assertEqual(getin(result, ['data', 'default']), '1')
  37. def test_choice_string_cooersion_no_default(self):
  38. """
  39. Make sure that choice types without a default don't create
  40. the literal string "None" but stick with the value None
  41. """
  42. parser = ArgumentParser()
  43. parser.add_argument('--foo', choices=[1, 2, 3])
  44. choice_action = parser._actions[-1]
  45. result = argparse_to_json.action_to_json(choice_action, 'Dropdown', {})
  46. self.assertEqual(getin(result, ['data', 'default']), None)
  47. def test_listbox_defaults_cast_correctly(self):
  48. """
  49. Issue XXX - defaults supplied in a list were turned into a string
  50. wholesale (list and all). The defaults should be stored as a list
  51. proper with only the _internal_ values coerced to strings.
  52. """
  53. parser = GooeyParser()
  54. parser.add_argument('--foo', widget="Listbox", nargs="*", choices=[1, 2, 3], default=[1, 2])
  55. choice_action = parser._actions[-1]
  56. result = argparse_to_json.action_to_json(choice_action, 'Listbox', {})
  57. self.assertEqual(getin(result, ['data', 'default']), ['1', '2'])
  58. def test_listbox_single_default_cast_correctly(self):
  59. """
  60. Single arg defaults to listbox should be wrapped in a list and
  61. their contents coerced as usual.
  62. """
  63. parser = GooeyParser()
  64. parser.add_argument('--foo', widget="Listbox",
  65. nargs="*", choices=[1, 2, 3], default="sup")
  66. choice_action = parser._actions[-1]
  67. result = argparse_to_json.action_to_json(choice_action, 'Listbox', {})
  68. self.assertEqual(getin(result, ['data', 'default']), ['sup'])
  69. def test_callables_as_default_args_are_cast_to_their_name(self):
  70. """ Issue 147 """
  71. parser = ArgumentParser()
  72. parser.add_argument('--foo', default=max)
  73. choice_action = parser._actions[-1]
  74. result = argparse_to_json.action_to_json(choice_action, 'Textfield', {})
  75. self.assertEqual(getin(result, ['data', 'default']), 'max')