File: test_mutually_exclusive_group.cpp

package info (click to toggle)
libargparse 3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 896 kB
  • sloc: cpp: 11,319; python: 8; makefile: 4; sh: 3
file content (85 lines) | stat: -rw-r--r-- 2,691 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#ifdef WITH_MODULE
import argparse;
#else
#include <argparse/argparse.hpp>
#endif
#include <doctest.hpp>

using doctest::test_suite;

TEST_CASE("Create mutually exclusive group with 2 arguments" *
          test_suite("mutex_args")) {
  argparse::ArgumentParser program("test");

  auto &group = program.add_mutually_exclusive_group();
  group.add_argument("--first");
  group.add_argument("--second");

  REQUIRE_THROWS_WITH_AS(
      program.parse_args({"test", "--first", "1", "--second", "2"}),
      "Argument '--second VAR' not allowed with '--first VAR'",
      std::runtime_error);
}

TEST_CASE(
    "Create mutually exclusive group with 2 arguments with required flag" *
    test_suite("mutex_args")) {
  argparse::ArgumentParser program("test");

  auto &group = program.add_mutually_exclusive_group(true);
  group.add_argument("--first");
  group.add_argument("--second");

  REQUIRE_THROWS_WITH_AS(
      program.parse_args({"test"}),
      "One of the arguments '--first VAR' or '--second VAR' is required",
      std::runtime_error);
}

TEST_CASE(
    "Create mutually exclusive group with 3 arguments with required flag" *
    test_suite("mutex_args")) {
  argparse::ArgumentParser program("test");

  auto &group = program.add_mutually_exclusive_group(true);
  group.add_argument("--first");
  group.add_argument("--second");
  group.add_argument("--third");

  REQUIRE_THROWS_WITH_AS(program.parse_args({"test"}),
                         "One of the arguments '--first VAR' or '--second VAR' "
                         "or '--third VAR' is required",
                         std::runtime_error);
}

TEST_CASE("Create mutually exclusive group with 3 arguments" *
          test_suite("mutex_args")) {
  argparse::ArgumentParser program("test");

  auto &group = program.add_mutually_exclusive_group();
  group.add_argument("--first");
  group.add_argument("--second");
  group.add_argument("--third");

  REQUIRE_THROWS_WITH_AS(
      program.parse_args({"test", "--first", "1", "--third", "2"}),
      "Argument '--third VAR' not allowed with '--first VAR'",
      std::runtime_error);
}

TEST_CASE("Create two mutually exclusive groups" * test_suite("mutex_args")) {
  argparse::ArgumentParser program("test");

  auto &group_1 = program.add_mutually_exclusive_group();
  group_1.add_argument("--first");
  group_1.add_argument("--second");
  group_1.add_argument("--third");

  auto &group_2 = program.add_mutually_exclusive_group();
  group_2.add_argument("-a");
  group_2.add_argument("-b");

  REQUIRE_THROWS_WITH_AS(
      program.parse_args({"test", "--first", "1", "-a", "2", "-b", "3"}),
      "Argument '-b VAR' not allowed with '-a VAR'", std::runtime_error);
}