aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.cli/test.cc
diff options
context:
space:
mode:
authorLexi Winter <lexi@le-fay.org>2025-07-01 22:02:48 +0100
committerLexi Winter <lexi@le-fay.org>2025-07-01 22:02:48 +0100
commit47999457e647352ae7e71d43c65e7b39ae5ca567 (patch)
tree7e5411fdacb357080db892058173879665238981 /nihil.cli/test.cc
parent762fdfaa953b35ed4c402c74890c5c30155a5dab (diff)
downloadnihil-47999457e647352ae7e71d43c65e7b39ae5ca567.tar.gz
nihil-47999457e647352ae7e71d43c65e7b39ae5ca567.tar.bz2
cli: add tests for command_tree
Diffstat (limited to 'nihil.cli/test.cc')
-rw-r--r--nihil.cli/test.cc102
1 files changed, 0 insertions, 102 deletions
diff --git a/nihil.cli/test.cc b/nihil.cli/test.cc
deleted file mode 100644
index 8253bdc..0000000
--- a/nihil.cli/test.cc
+++ /dev/null
@@ -1,102 +0,0 @@
-// This source code is released into the public domain.
-
-#include <sysexits.h>
-
-#include <catch2/catch_test_macros.hpp>
-
-import nihil.std;
-import nihil.cli;
-import nihil.util;
-
-namespace {
-
-auto cmd_sub1_called = false;
-auto cmd_sub1 = nihil::command("cmd sub1", "", [](int, char **) -> int
-{
- cmd_sub1_called = true;
- return 0;
-});
-
-auto cmd_sub2_called = false;
-auto cmd_sub2 = nihil::command("cmd sub2", "", [](int, char **) -> int
-{
- cmd_sub2_called = true;
- return 0;
-});
-
-} // anonymous namespace
-
-TEST_CASE("nihil.cli: dispatch_command: basic", "[nihil.cli]")
-{
- SECTION("cmd sub1") {
- auto args = std::vector<char const *>{
- "cmd", "sub1", nullptr
- };
- auto *argv = const_cast<char **>(args.data());
-
- auto const ret = nihil::dispatch_command(
- static_cast<int>(args.size()) - 1, argv);
- REQUIRE(ret == 0);
- REQUIRE(cmd_sub1_called == true);
- REQUIRE(cmd_sub2_called == false);
- }
-
- SECTION("cmd sub2") {
- auto args = std::vector<char const *>{
- "cmd", "sub2", nullptr
- };
- auto *argv = const_cast<char **>(args.data());
-
- auto const ret = nihil::dispatch_command(
- static_cast<int>(args.size()) - 1, argv);
- REQUIRE(ret == 0);
- REQUIRE(cmd_sub2_called == true);
- }
-}
-
-TEST_CASE("nihil.cli: dispatch_command: unknown command", "[nihil.cli]")
-{
- auto args = std::vector<char const *>{
- "nocomd", "sub", nullptr
- };
- auto *argv = const_cast<char **>(args.data());
-
- auto output = std::string();
- auto ret = int{};
- {
- auto capture = nihil::capture_stream(std::cerr);
- ret = nihil::dispatch_command(
- static_cast<int>(args.size()) - 1, argv);
- std::cerr.flush();
- output = capture.str();
- }
-
- REQUIRE(ret == EX_USAGE);
-
- auto const *progname = ::getprogname();
- REQUIRE(output == std::format("{}: usage:\n cmd\n", progname));
-}
-
-TEST_CASE("nihil.cli: dispatch_command: incomplete command", "[nihil.cli]")
-{
- auto args = std::vector<char const *>{
- "cmd", nullptr
- };
- auto *argv = const_cast<char **>(args.data());
-
- auto output = std::string();
- auto ret = int{};
- {
- auto capture = nihil::capture_stream(std::cerr);
- ret = nihil::dispatch_command(
- static_cast<int>(args.size()) - 1, argv);
- std::cerr.flush();
- output = capture.str();
- }
-
- REQUIRE(ret == EX_USAGE);
-
- auto const *progname = ::getprogname();
- REQUIRE(output == std::format("{}: usage:\n cmd sub1\n cmd sub2\n",
- progname));
-}