File: test_choices.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 (205 lines) | stat: -rw-r--r-- 7,390 bytes parent folder | download
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#ifdef WITH_MODULE
import argparse;
#else
#include <argparse/argparse.hpp>
#endif

#include <doctest.hpp>

using doctest::test_suite;

TEST_CASE("Parse argument that is provided zero choices" *
          test_suite("choices")) {
  argparse::ArgumentParser program("test");
  REQUIRE_THROWS_WITH_AS(program.add_argument("color").choices(),
                         "Zero choices provided", std::runtime_error);
}

TEST_CASE("Parse argument that is in the fixed number of allowed choices" *
          test_suite("choices")) {
  argparse::ArgumentParser program("test");
  program.add_argument("color").choices("red", "green", "blue");

  program.parse_args({"test", "red"});
}

TEST_CASE("Parse argument that is in the fixed number of allowed choices, with "
          "other positional argument" *
          test_suite("choices")) {
  argparse::ArgumentParser program("test");
  program.add_argument("--input")
      .default_value(std::string{"baz"})
      .choices("foo", "bar", "baz");
  program.add_argument("--value").scan<'i', int>().default_value(0);

  REQUIRE_NOTHROW(
      program.parse_args({"test", "--input", "foo", "--value", "1"}));
  REQUIRE(program.get("--input") == "foo");
  REQUIRE(program.get<int>("--value") == 1);
}

TEST_CASE(
    "Parse nargs argument that is in the fixed number of allowed choices, with "
    "other positional argument" *
    test_suite("choices")) {
  argparse::ArgumentParser program("test");
  program.add_argument("--input")
      .default_value(std::string{"baz"})
      .choices("foo", "bar", "baz")
      .nargs(2);
  program.add_argument("--value").scan<'i', int>().default_value(0);

  REQUIRE_NOTHROW(
      program.parse_args({"test", "--input", "foo", "bar", "--value", "1"}));
  REQUIRE((program.get<std::vector<std::string>>("--input") ==
           std::vector<std::string>{"foo", "bar"}));
  REQUIRE(program.get<int>("--value") == 1);
}

TEST_CASE("Parse argument that is in the fixed number of allowed choices, with "
          "other positional argument (reversed)" *
          test_suite("choices")) {
  argparse::ArgumentParser program("test");
  program.add_argument("--input")
      .default_value(std::string{"baz"})
      .choices("foo", "bar", "baz");
  program.add_argument("--value").scan<'i', int>().default_value(0);

  REQUIRE_NOTHROW(
      program.parse_args({"test", "--value", "1", "--input", "foo"}));
  REQUIRE(program.get("--input") == "foo");
  REQUIRE(program.get<int>("--value") == 1);
}

TEST_CASE(
    "Parse nargs argument that is in the fixed number of allowed choices, with "
    "other positional argument (reversed)" *
    test_suite("choices")) {
  argparse::ArgumentParser program("test");
  program.add_argument("--input")
      .default_value(std::string{"baz"})
      .choices("foo", "bar", "baz")
      .nargs(2);
  program.add_argument("--value").scan<'i', int>().default_value(0);

  REQUIRE_NOTHROW(
      program.parse_args({"test", "--value", "1", "--input", "foo", "bar"}));
  REQUIRE((program.get<std::vector<std::string>>("--input") ==
           std::vector<std::string>{"foo", "bar"}));
  REQUIRE(program.get<int>("--value") == 1);
}

TEST_CASE("Parse argument that is in the fixed number of allowed choices, with "
          "invalid default" *
          test_suite("choices")) {
  argparse::ArgumentParser program("test");
  program.add_argument("color").default_value("yellow").choices("red", "green",
                                                                "blue");

  REQUIRE_THROWS_WITH_AS(
      program.parse_args({"test"}),
      "Invalid default value \"yellow\" - allowed options: {red, green, blue}",
      std::runtime_error);
}

TEST_CASE("Parse invalid argument that is not in the fixed number of allowed "
          "choices" *
          test_suite("choices")) {
  argparse::ArgumentParser program("test");
  program.add_argument("color").choices("red", "green", "blue");

  REQUIRE_THROWS_WITH_AS(
      program.parse_args({"test", "red2"}),
      "Invalid argument \"red2\" - allowed options: {red, green, blue}",
      std::runtime_error);
}

TEST_CASE(
    "Parse multiple arguments that are in the fixed number of allowed choices" *
    test_suite("choices")) {
  argparse::ArgumentParser program("test");
  program.add_argument("color").nargs(2).choices("red", "green", "blue");

  program.parse_args({"test", "red", "green"});
}

TEST_CASE("Parse multiple arguments one of which is not in the fixed number of "
          "allowed choices" *
          test_suite("choices")) {
  argparse::ArgumentParser program("test");
  program.add_argument("color").nargs(2).choices("red", "green", "blue");

  REQUIRE_THROWS_WITH_AS(
      program.parse_args({"test", "red", "green2"}),
      "Invalid argument \"green2\" - allowed options: {red, green, blue}",
      std::runtime_error);
}

TEST_CASE("Parse multiple arguments that are in the fixed number of allowed "
          "INTEGER choices" *
          test_suite("choices")) {
  argparse::ArgumentParser program("test");
  program.add_argument("indices").nargs(2).choices(1, 2, 3, 4, 5);

  program.parse_args({"test", "1", "2"});
}

TEST_CASE("Parse multiple arguments that are not in fixed number of allowed "
          "INTEGER choices" *
          test_suite("choices")) {
  argparse::ArgumentParser program("test");
  program.add_argument("indices").nargs(2).choices(1, 2, 3, 4, 5);

  REQUIRE_THROWS_WITH_AS(
      program.parse_args({"test", "6", "7"}),
      "Invalid argument \"6\" - allowed options: {1, 2, 3, 4, 5}",
      std::runtime_error);
}

TEST_CASE("Parse multiple arguments that are in range of allowed "
          "INTEGER choices (Min Range case)" *
          test_suite("choices")) {
  argparse::ArgumentParser program("test");
  program.add_argument("indices").nargs(1, 3).choices(1, 2, 3, 4, 5);

  REQUIRE_NOTHROW(program.parse_args({"test", "1"}));
  REQUIRE(program.get<std::vector<std::string>>("indices") ==
          std::vector<std::string>{"1"});
}

TEST_CASE("Parse multiple arguments that are in range of allowed choices (In "
          "Range case)" *
          test_suite("choices")) {
  argparse::ArgumentParser program("test");
  program.add_argument("--foo");
  program.add_argument("--bar").nargs(1, 3).choices("a", "b", "c");

  REQUIRE_NOTHROW(
      program.parse_args({"test", "--bar", "a", "b", "--foo", "x"}));
  REQUIRE(program.get<std::vector<std::string>>("--bar") ==
          std::vector<std::string>{"a", "b"});
  REQUIRE(program.get<std::string>("--foo") == "x");
}

TEST_CASE("Parse multiple arguments that are in range of allowed "
          "INTEGER choices (Max Range case)" *
          test_suite("choices")) {
  argparse::ArgumentParser program("test");
  program.add_argument("indices").nargs(2, 3).choices(1, 2, 3, 4, 5);

  REQUIRE_NOTHROW(program.parse_args({"test", "3", "4", "5"}));
  REQUIRE(program.get<std::vector<std::string>>("indices") ==
          std::vector<std::string>{"3", "4", "5"});
}

TEST_CASE("Parse multiple arguments that are not in range of allowed choices" *
          test_suite("choices")) {
  argparse::ArgumentParser program("test");
  program.add_argument("--foo");
  program.add_argument("--bar").nargs(1, 3).choices("a", "b", "c");

  REQUIRE_THROWS_WITH_AS(
      program.parse_args({"test", "--bar", "d", "--foo", "x"}),
      "Invalid argument \"d\" - allowed options: {a, b, c}",
      std::runtime_error);
}