From 4de9f89b59fe818be6d12162b637f205c8e5e465 Mon Sep 17 00:00:00 2001 From: Mike Zozu Date: Tue, 15 Dec 2020 19:00:55 +0300 Subject: [PATCH] add tests for repr() and extend tests for help msg --- test/CMakeLists.txt | 1 + test/test_help.cpp | 20 ++++++++++++---- test/test_repr.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 test/test_repr.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 592fee1..d3d2301 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -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 diff --git a/test/test_help.cpp b/test/test_help.cpp index 25b87af..78438ea 100644 --- a/test/test_help.cpp +++ b/test/test_help.cpp @@ -1,15 +1,25 @@ -#include #include +#include 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{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{}); + program.add_argument("-f").default_value(false); + } std::ostringstream s; s << program; REQUIRE_FALSE(s.str().empty()); diff --git a/test/test_repr.cpp b/test/test_repr.cpp new file mode 100644 index 0000000..dc7d1c7 --- /dev/null +++ b/test/test_repr.cpp @@ -0,0 +1,56 @@ +#include +#include +#include + +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, std::list, std::set) { + 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{}) == ""); +}