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.

114 lines
4.3 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_json_iterable_conversion(self):
  9. """
  10. Issue #312 - tuples weren't being coerced to list during argparse
  11. conversion causing downstream issues when concatenating
  12. """
  13. # our original functionality accepted only lists as the choices arg
  14. parser = ArgumentParser()
  15. parser.add_argument("-foo", choices=['foo','bar', 'baz'])
  16. result = argparse_to_json.action_to_json(parser._actions[-1], "Dropdown", {})
  17. choices = result['data']['choices']
  18. self.assertTrue(isinstance(choices, list))
  19. self.assertEqual(choices, ['foo','bar', 'baz'])
  20. # Now we allow tuples as well.
  21. parser = ArgumentParser()
  22. parser.add_argument("-foo", choices=('foo','bar', 'baz'))
  23. result = argparse_to_json.action_to_json(parser._actions[-1], "Dropdown", {})
  24. choices = result['data']['choices']
  25. self.assertTrue(isinstance(choices, list))
  26. self.assertEqual(choices, ['foo','bar', 'baz'])
  27. def test_choice_string_cooersion(self):
  28. """
  29. Issue 321 - must coerce choice types to string to support wx.ComboBox
  30. """
  31. parser = ArgumentParser()
  32. parser.add_argument('--foo', default=1, choices=[1, 2, 3])
  33. choice_action = parser._actions[-1]
  34. result = argparse_to_json.action_to_json(choice_action, 'Dropdown', {})
  35. self.assertEqual(getin(result, ['data', 'choices']), ['1', '2', '3'])
  36. # default value is also converted to a string type
  37. self.assertEqual(getin(result, ['data', 'default']), '1')
  38. def test_choice_string_cooersion_no_default(self):
  39. """
  40. Make sure that choice types without a default don't create
  41. the literal string "None" but stick with the value None
  42. """
  43. parser = ArgumentParser()
  44. parser.add_argument('--foo', choices=[1, 2, 3])
  45. choice_action = parser._actions[-1]
  46. result = argparse_to_json.action_to_json(choice_action, 'Dropdown', {})
  47. self.assertEqual(getin(result, ['data', 'default']), None)
  48. def test_listbox_defaults_cast_correctly(self):
  49. """
  50. Issue XXX - defaults supplied in a list were turned into a string
  51. wholesale (list and all). The defaults should be stored as a list
  52. proper with only the _internal_ values coerced to strings.
  53. """
  54. parser = GooeyParser()
  55. parser.add_argument('--foo', widget="Listbox", nargs="*", choices=[1, 2, 3], default=[1, 2])
  56. choice_action = parser._actions[-1]
  57. result = argparse_to_json.action_to_json(choice_action, 'Listbox', {})
  58. self.assertEqual(getin(result, ['data', 'default']), ['1', '2'])
  59. def test_listbox_single_default_cast_correctly(self):
  60. """
  61. Single arg defaults to listbox should be wrapped in a list and
  62. their contents coerced as usual.
  63. """
  64. parser = GooeyParser()
  65. parser.add_argument('--foo', widget="Listbox",
  66. nargs="*", choices=[1, 2, 3], default="sup")
  67. choice_action = parser._actions[-1]
  68. result = argparse_to_json.action_to_json(choice_action, 'Listbox', {})
  69. self.assertEqual(getin(result, ['data', 'default']), ['sup'])
  70. def test_non_data_defaults_are_dropped_entirely(self):
  71. """
  72. This is a refinement in understanding of Issue #147
  73. Caused by Issue 377 - passing arbitrary objects as defaults
  74. causes failures.
  75. """
  76. # passing plain data to cleaning function results in plain data
  77. # being returned
  78. data = ['abc',
  79. 123,
  80. ['a', 'b'],
  81. [1, 2, 3]]
  82. for datum in data:
  83. result = argparse_to_json.clean_default(datum)
  84. self.assertEqual(result, datum)
  85. # passing in complex objects results in None
  86. objects = [sys.stdout, sys.stdin, object(), max, min]
  87. for obj in objects:
  88. result = argparse_to_json.clean_default(obj)
  89. self.assertEqual(result, None)