first commit
This commit is contained in:
32
test/CMakeLists.txt
Normal file
32
test/CMakeLists.txt
Normal file
@@ -0,0 +1,32 @@
|
||||
macro(add_args tgt)
|
||||
target_compile_options(
|
||||
${tgt}
|
||||
PUBLIC
|
||||
-Wall;
|
||||
-Wextra;
|
||||
-Wpedantic;
|
||||
-Wcast-align;
|
||||
-Wdisabled-optimization;
|
||||
-Winit-self;
|
||||
-Wlogical-op;
|
||||
-Wmissing-include-dirs;
|
||||
-Woverloaded-virtual;
|
||||
-Wpointer-arith;
|
||||
-Wshadow;
|
||||
-Wstrict-aliasing;
|
||||
-Wswitch-enum;
|
||||
-Wundef;
|
||||
-Wvla;
|
||||
-Wformat=2;
|
||||
)
|
||||
endmacro()
|
||||
|
||||
|
||||
add_executable(test_all test_main.cpp
|
||||
test_argparse.cpp
|
||||
)
|
||||
add_args(test_all)
|
||||
target_include_directories(test_all SYSTEM PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../include)
|
||||
target_include_directories(test_all SYSTEM PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../thirdparty)
|
||||
add_test(NAME test_all COMMAND test_all -a)
|
||||
|
132
test/test_argparse.cpp
Normal file
132
test/test_argparse.cpp
Normal file
@@ -0,0 +1,132 @@
|
||||
#include "catch2/catch.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "argparse/argparse.hpp"
|
||||
|
||||
TEST_CASE("argparse") {
|
||||
|
||||
SECTION("types") {
|
||||
char *argv[] = {
|
||||
"some-exe", "--campi", "--f", "10", "1.7", "1.8",
|
||||
"--", // stop looking for options
|
||||
"--a string",
|
||||
"-6",
|
||||
};
|
||||
int argc = sizeof(argv) / sizeof(argv[0]);
|
||||
|
||||
Parser p;
|
||||
|
||||
bool campi = false;
|
||||
size_t x;
|
||||
double d;
|
||||
float f;
|
||||
int i;
|
||||
std::string s;
|
||||
p.add_flag(campi, "--campi");
|
||||
p.add_positional(x);
|
||||
p.add_positional(d);
|
||||
p.add_positional(f);
|
||||
p.add_positional(s);
|
||||
p.add_positional(i);
|
||||
REQUIRE(p.parse(argc, argv));
|
||||
|
||||
REQUIRE(campi == true);
|
||||
REQUIRE(x == 10);
|
||||
REQUIRE(d == 1.7);
|
||||
REQUIRE(f == 1.8f);
|
||||
REQUIRE(s == "--a string");
|
||||
REQUIRE(i == -6);
|
||||
REQUIRE(p.need_help() == false);
|
||||
|
||||
REQUIRE(argc == 2); // does not use --f or some-exe
|
||||
}
|
||||
|
||||
SECTION("no args") {
|
||||
char *argv[] = {nullptr};
|
||||
int argc = sizeof(argv) / sizeof(argv[0]);
|
||||
Parser p;
|
||||
REQUIRE(p.parse(argc, argv));
|
||||
}
|
||||
|
||||
|
||||
SECTION("description") {
|
||||
char *argv[] = {
|
||||
"some-exe",
|
||||
};
|
||||
int argc = sizeof(argv) / sizeof(argv[0]);
|
||||
|
||||
Parser p("a test program");
|
||||
REQUIRE(p.parse(argc, argv));
|
||||
}
|
||||
|
||||
SECTION("skip-first") {
|
||||
char *argv[] = {"some-exe"};
|
||||
int argc = sizeof(argv) / sizeof(argv[0]);
|
||||
|
||||
Parser p;
|
||||
p.no_unrecognized();
|
||||
REQUIRE(p.parse(argc, argv));
|
||||
}
|
||||
|
||||
SECTION("no-unrecognized") {
|
||||
char *argv[] = {"some-exe", "-f"};
|
||||
int argc = sizeof(argv) / sizeof(argv[0]);
|
||||
|
||||
Parser p;
|
||||
p.no_unrecognized();
|
||||
REQUIRE(false == p.parse(argc, argv));
|
||||
}
|
||||
|
||||
SECTION("missing-reqd-posnl") {
|
||||
char *argv[] = {"some-exe", "a"};
|
||||
int argc = sizeof(argv) / sizeof(argv[0]);
|
||||
|
||||
std::string a;
|
||||
std::string b;
|
||||
|
||||
Parser p;
|
||||
p.add_positional(a)->required();
|
||||
p.add_positional(b)->required();
|
||||
REQUIRE(false == p.parse(argc, argv));
|
||||
}
|
||||
|
||||
SECTION("-h") {
|
||||
char *argv[] = {"some-exe", "-h"};
|
||||
int argc = sizeof(argv) / sizeof(argv[0]);
|
||||
|
||||
std::string a;
|
||||
std::string b;
|
||||
|
||||
Parser p;
|
||||
REQUIRE(true == p.parse(argc, argv));
|
||||
REQUIRE(p.need_help());
|
||||
|
||||
std::cerr << p.help() << "\n";
|
||||
}
|
||||
|
||||
SECTION("--help") {
|
||||
char *argv[] = {"some-exe", "--help"};
|
||||
int argc = sizeof(argv) / sizeof(argv[0]);
|
||||
|
||||
std::string a;
|
||||
std::string b;
|
||||
|
||||
Parser p;
|
||||
REQUIRE(true == p.parse(argc, argv));
|
||||
REQUIRE(p.need_help());
|
||||
}
|
||||
|
||||
SECTION("--help") {
|
||||
char *argv[] = {"some-exe", "--help", "--"};
|
||||
int argc = sizeof(argv) / sizeof(argv[0]);
|
||||
|
||||
std::string a;
|
||||
std::string b;
|
||||
|
||||
Parser p;
|
||||
REQUIRE(true == p.parse(argc, argv));
|
||||
REQUIRE(p.need_help());
|
||||
}
|
||||
|
||||
}
|
2
test/test_main.cpp
Normal file
2
test/test_main.cpp
Normal file
@@ -0,0 +1,2 @@
|
||||
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
|
||||
#include "catch2/catch.hpp"
|
Reference in New Issue
Block a user