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.

89 lines
3.4 KiB

2 years ago
  1. import argparse
  2. import os
  3. import shlex
  4. import unittest
  5. from gooey.gui import formatters
  6. class TestFormatters(unittest.TestCase):
  7. def test_counter_formatter(self):
  8. """
  9. Should return the first option repeated N times
  10. None if N is unspecified
  11. Issue #316 - using long-form argument caused formatter to produce incorrect output
  12. """
  13. expected_outputs = [
  14. (['-v', '--verbose'], '-v', 1),
  15. (['-v', '--verbose'], '-v -v', 2),
  16. (['-v', '--verbose'], '-v -v -v', 3),
  17. (['-v', '--verbose'], '', 0),
  18. # ensuring that log-forms are handled correctly
  19. (['--verbose', '-v'], '--verbose', 1),
  20. (['--verbose', '-v'], '--verbose --verbose', 2),
  21. (['--verbose', '-v'], '--verbose --verbose --verbose', 3),
  22. # single args
  23. (['-v'], '-v', 1),
  24. (['-v'], '-v -v', 2),
  25. (['--verbose'], '--verbose', 1),
  26. # bad inputs
  27. (['-v'], None, None),
  28. (['-v'], None, 'some-garbage'),
  29. (['-v'], None, 'af3gd'),
  30. ]
  31. for commands, expected, vebosity_level in expected_outputs:
  32. result = formatters.counter({'commands': commands}, vebosity_level)
  33. self.assertEqual(result, expected)
  34. # make sure that argparse actually accepts it as valid.
  35. if result:
  36. parser = argparse.ArgumentParser()
  37. parser.add_argument('-v', '--verbose', action='count')
  38. parser.parse_args(result.split())
  39. def test_multifilechooser_formatter(self):
  40. """
  41. Should return files (quoted), separated by spaces if there is more
  42. than one, preceded by optional command if the argument is optional.
  43. Assumes the argument has been created with some form of nargs, which
  44. only makes sense for possibly choosing multiple values.
  45. """
  46. # Helper function to generalize the variants we need to test
  47. def multifilechooser_helper(names):
  48. # Note that the MultiFileChooser widget produces a single string with
  49. # paths separated by os.pathsep.
  50. if names:
  51. prefix = names[0] + ' '
  52. else:
  53. prefix = ''
  54. expected_outputs = [
  55. (names, None, ''),
  56. (names, prefix + '"abc"', 'abc'),
  57. (names, prefix + '"abc" "def"', os.pathsep.join(['abc', 'def'])),
  58. # paths with spaces
  59. (names, prefix + '"a b c"', 'a b c'),
  60. (names, prefix + '"a b c" "d e f"', os.pathsep.join(['a b c', 'd e f'])),
  61. ]
  62. for commands, expected, widget_result in expected_outputs:
  63. result = formatters.multiFileChooser({'commands': commands}, widget_result)
  64. self.assertEqual(result, expected)
  65. # make sure that argparse actually accepts it as valid.
  66. if result:
  67. parser = argparse.ArgumentParser()
  68. if not names:
  69. names = ["file"]
  70. parser.add_argument(names[0], nargs='+')
  71. parser.parse_args(shlex.split(result))
  72. # Positional argument, with nargs
  73. multifilechooser_helper([])
  74. # Optional argument, with nargs
  75. multifilechooser_helper(["-f", "--file"])