mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-03 22:54:39 +00:00
add tests for repr() and extend tests for help msg
This commit is contained in:
parent
6b30a65ffd
commit
4de9f89b59
@ -35,6 +35,7 @@ file(GLOB ARGPARSE_TEST_SOURCES
|
||||
test_parent_parsers.cpp
|
||||
test_parse_args.cpp
|
||||
test_positional_arguments.cpp
|
||||
test_repr.cpp
|
||||
test_required_arguments.cpp
|
||||
test_scan.cpp
|
||||
test_value_semantics.cpp
|
||||
|
@ -1,15 +1,25 @@
|
||||
#include <doctest.hpp>
|
||||
#include <argparse/argparse.hpp>
|
||||
#include <doctest.hpp>
|
||||
|
||||
using doctest::test_suite;
|
||||
|
||||
TEST_CASE("Users can format help message" * test_suite("help")) {
|
||||
argparse::ArgumentParser program("test");
|
||||
program.add_argument("input")
|
||||
.help("positional input");
|
||||
program.add_argument("-c")
|
||||
.help("optional input");
|
||||
|
||||
SUBCASE("Simple arguments") {
|
||||
program.add_argument("input").help("positional input");
|
||||
program.add_argument("-c").help("optional input");
|
||||
}
|
||||
SUBCASE("Default values") {
|
||||
program.add_argument("-a").default_value(42);
|
||||
program.add_argument("-b").default_value(4.4e-7);
|
||||
program.add_argument("-c")
|
||||
.default_value(std::vector<int>{1, 2, 3, 4, 5})
|
||||
.nargs(5);
|
||||
program.add_argument("-d").default_value("I am a string");
|
||||
program.add_argument("-e").default_value(std::optional<float>{});
|
||||
program.add_argument("-f").default_value(false);
|
||||
}
|
||||
std::ostringstream s;
|
||||
s << program;
|
||||
REQUIRE_FALSE(s.str().empty());
|
||||
|
56
test/test_repr.cpp
Normal file
56
test/test_repr.cpp
Normal file
@ -0,0 +1,56 @@
|
||||
#include <argparse/argparse.hpp>
|
||||
#include <doctest.hpp>
|
||||
#include <set>
|
||||
|
||||
using doctest::test_suite;
|
||||
|
||||
TEST_CASE("Test bool representation" * test_suite("repr")) {
|
||||
REQUIRE(argparse::details::repr(true) == "true");
|
||||
REQUIRE(argparse::details::repr(false) == "false");
|
||||
}
|
||||
|
||||
TEST_CASE_TEMPLATE("Test built-in int types representation" *
|
||||
test_suite("repr"),
|
||||
T, char, short, int, long long, unsigned char, unsigned,
|
||||
unsigned long long) {
|
||||
std::stringstream ss;
|
||||
T v = 42;
|
||||
ss << v;
|
||||
REQUIRE(argparse::details::repr(v) == ss.str());
|
||||
}
|
||||
|
||||
TEST_CASE_TEMPLATE("Test built-in float types representation" *
|
||||
test_suite("repr"),
|
||||
T, float, double, long double) {
|
||||
std::stringstream ss;
|
||||
T v = 0.3333333333;
|
||||
ss << v;
|
||||
REQUIRE(argparse::details::repr(v) == ss.str());
|
||||
}
|
||||
|
||||
TEST_CASE_TEMPLATE("Test container representation" * test_suite("repr"), T,
|
||||
std::vector<int>, std::list<int>, std::set<int>) {
|
||||
T empty;
|
||||
T one = {42};
|
||||
T small = {1, 2, 3};
|
||||
T big = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
|
||||
|
||||
REQUIRE(argparse::details::repr(empty) == "{}");
|
||||
REQUIRE(argparse::details::repr(one) == "{42}");
|
||||
REQUIRE(argparse::details::repr(small) == "{1 2 3}");
|
||||
REQUIRE(argparse::details::repr(big) == "{1 2 3 4...15}");
|
||||
}
|
||||
|
||||
TEST_CASE_TEMPLATE("Test string representation" * test_suite("repr"), T,
|
||||
char const *, std::string, std::string_view) {
|
||||
T empty = "";
|
||||
T str = "A A A#";
|
||||
|
||||
REQUIRE(argparse::details::repr(empty) == "\"\"");
|
||||
REQUIRE(argparse::details::repr(str) == "\"A A A#\"");
|
||||
}
|
||||
|
||||
TEST_CASE("Test unknown representation" * test_suite("repr")) {
|
||||
struct TestClass {};
|
||||
REQUIRE(argparse::details::repr(TestClass{}) == "<not representable>");
|
||||
}
|
Loading…
Reference in New Issue
Block a user