File: test_default_value.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 (83 lines) | stat: -rw-r--r-- 2,947 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
#ifdef WITH_MODULE
import argparse;
#else
#include <argparse/argparse.hpp>
#endif
#include <doctest.hpp>
#include <string>

using doctest::test_suite;

TEST_CASE("Use a 'string' default value" * test_suite("default_value")) {
  argparse::ArgumentParser program("test");

  SUBCASE("Use a const char[] default value") {
    program.add_argument("--arg").default_value("array of char");
    REQUIRE_NOTHROW(program.parse_args({"test"}));
    REQUIRE(program.get("--arg") == std::string("array of char"));
  }

  SUBCASE("Use a std::string default value") {
    program.add_argument("--arg").default_value(std::string("string object"));
    REQUIRE_NOTHROW(program.parse_args({"test"}));
    REQUIRE(program.get("--arg") == std::string("string object"));
  }
}

TEST_CASE("Use a default value with flag arguments" *
          test_suite("default_value")) {

  argparse::ArgumentParser program("test");

  program.add_argument("-inc_chr", "--include_chromes")
      .help(std::string{"only process the anchor whose one of the end is "
                        "contained on the specified "
                        "chromatin, used ',' to split."})
      .default_value("all");

  program.add_argument("-l").flag();
  program.add_argument("-o").flag();

  program.add_argument("filename");

  SUBCASE("Leading optional argument with default_value") {
    REQUIRE_NOTHROW(program.parse_args({"test", "-inc_chr", "-lo", "my.log"}));
    REQUIRE(program.get("-inc_chr") == std::string{"all"});
  }

  SUBCASE("Trailing optional argument with default_value") {
    REQUIRE_NOTHROW(program.parse_args({"test", "-lo", "my.log", "-inc_chr"}));
    REQUIRE(program.get("-inc_chr") == std::string{"all"});
  }
}

TEST_CASE("Position of the argument with default value") {
  argparse::ArgumentParser program("test");
  program.add_argument("-g").default_value("the_default_value");
  program.add_argument("-s");

  SUBCASE("Arg with default value not passed") {
    REQUIRE_NOTHROW(program.parse_args({"test", "-s", "./src"}));
    REQUIRE(program.get("-g") == std::string("the_default_value"));
    REQUIRE(program.get("-s") == std::string("./src"));
  }

  SUBCASE("Arg with default value passed last") {
    REQUIRE_NOTHROW(program.parse_args({"test", "-s", "./src", "-g"}));
    REQUIRE(program.get("-g") == std::string("the_default_value"));
    REQUIRE(program.get("-s") == std::string("./src"));
  }

  SUBCASE("Arg with default value passed before last") {
    REQUIRE_NOTHROW(program.parse_args({"test", "-g", "-s", "./src"}));
    REQUIRE(program.get("-g") == std::string("the_default_value"));
    REQUIRE(program.get("-s") == std::string("./src"));
  }

  SUBCASE("Arg with default value replaces the value if given") {
    REQUIRE_NOTHROW(
        program.parse_args({"test", "-g", "a_different_value", "-s", "./src"}));
    REQUIRE(program.get("-g") == std::string("a_different_value"));
    REQUIRE(program.get("-s") == std::string("./src"));
  }
}