File: test_equals_form.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 (47 lines) | stat: -rw-r--r-- 1,552 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
#ifdef WITH_MODULE
import argparse;
#else
#include <argparse/argparse.hpp>
#endif
#include <doctest.hpp>

#include <iostream>
#include <string>
#include <vector>

using doctest::test_suite;

TEST_CASE("Basic --value=value" * test_suite("equals_form")) {
  argparse::ArgumentParser parser("test");
  parser.add_argument("--long");
  parser.parse_args({"test", "--long=value"});
  std::string result{parser.get("--long")};
  REQUIRE(result == "value");
}

TEST_CASE("Fallback = in regular option name" * test_suite("equals_form")) {
  argparse::ArgumentParser parser("test");
  parser.add_argument("--long=mislead");
  parser.parse_args({"test", "--long=mislead", "value"});
  std::string result{parser.get("--long=mislead")};
  REQUIRE(result == "value");
}

TEST_CASE("Duplicate =-named and standard" * test_suite("equals_form")) {
  argparse::ArgumentParser parser("test");
  parser.add_argument("--long=mislead");
  parser.add_argument("--long").default_value(std::string{"NO_VALUE"});
  parser.parse_args({"test", "--long=mislead", "value"});
  std::string result{parser.get("--long=mislead")};
  REQUIRE(result == "value");
  std::string result2{parser.get("--long")};
  REQUIRE(result2 == "NO_VALUE");
}

TEST_CASE("Basic --value=value with nargs(2)" * test_suite("equals_form")) {
  argparse::ArgumentParser parser("test");
  parser.add_argument("--long").nargs(2);
  parser.parse_args({"test", "--long=value1", "value2"});
  REQUIRE((parser.get<std::vector<std::string>>("--long") ==
           std::vector<std::string>{"value1", "value2"}));
}