test_completions.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from argparse import Action
  2. import pytest
  3. from borgmatic.commands.completion import (
  4. has_choice_options,
  5. has_exact_options,
  6. has_file_options,
  7. has_unknown_required_param_options,
  8. )
  9. file_options_test_data = [
  10. (Action('--flag', 'flag'), False),
  11. (Action('--flag', 'flag', metavar='FILENAME'), True),
  12. (Action('--flag', 'flag', metavar='PATH'), True),
  13. (Action('--flag', dest='config_paths'), True),
  14. (Action('--flag', 'flag', metavar='OTHER'), False),
  15. ]
  16. @pytest.mark.parametrize('action, expected', file_options_test_data)
  17. def test_has_file_options_detects_file_options(action: Action, expected: bool):
  18. assert has_file_options(action) == expected
  19. # if has_file_options(action) was true, has_exact_options(action) should also be true
  20. if expected:
  21. assert has_exact_options(action)
  22. choices_test_data = [
  23. (Action('--flag', 'flag'), False),
  24. (Action('--flag', 'flag', choices=['a', 'b']), True),
  25. (Action('--flag', 'flag', choices=None), False),
  26. ]
  27. @pytest.mark.parametrize('action, expected', choices_test_data)
  28. def test_has_choice_options_detects_choice_options(action: Action, expected: bool):
  29. assert has_choice_options(action) == expected
  30. # if has_choice_options(action) was true, has_exact_options(action) should also be true
  31. if expected:
  32. assert has_exact_options(action)
  33. unknown_required_param_test_data = [
  34. (Action('--flag', 'flag'), False),
  35. (Action('--flag', 'flag', required=True), True),
  36. *((Action('--flag', 'flag', nargs=nargs), True) for nargs in ('+', '*')),
  37. *((Action('--flag', 'flag', metavar=metavar), True) for metavar in ('PATTERN', 'KEYS', 'N')),
  38. *((Action('--flag', 'flag', type=type, default=None), True) for type in (int, str)),
  39. (Action('--flag', 'flag', type=int, default=1), False),
  40. ]
  41. @pytest.mark.parametrize('action, expected', unknown_required_param_test_data)
  42. def test_has_unknown_required_param_options_detects_unknown_required_param_options(
  43. action: Action, expected: bool
  44. ):
  45. assert has_unknown_required_param_options(action) == expected
  46. # if has_unknown_required_param_options(action) was true, has_exact_options(action) should also be true
  47. if expected:
  48. assert has_exact_options(action)