From 47999457e647352ae7e71d43c65e7b39ae5ca567 Mon Sep 17 00:00:00 2001 From: Lexi Winter Date: Tue, 1 Jul 2025 22:02:48 +0100 Subject: cli: add tests for command_tree --- nihil.cli/CMakeLists.txt | 3 +- nihil.cli/command_tree.ccm | 16 +++--- nihil.cli/command_tree.test.cc | 45 ++++++++++++++++ nihil.cli/dispatch_command.test.cc | 102 +++++++++++++++++++++++++++++++++++++ nihil.cli/test.cc | 102 ------------------------------------- 5 files changed, 156 insertions(+), 112 deletions(-) create mode 100644 nihil.cli/command_tree.test.cc create mode 100644 nihil.cli/dispatch_command.test.cc delete mode 100644 nihil.cli/test.cc (limited to 'nihil.cli') diff --git a/nihil.cli/CMakeLists.txt b/nihil.cli/CMakeLists.txt index 41fae52..091c34f 100644 --- a/nihil.cli/CMakeLists.txt +++ b/nihil.cli/CMakeLists.txt @@ -25,8 +25,9 @@ if(NIHIL_TESTS) enable_testing() add_executable(nihil.cli.test - test.cc command.test.cc + command_tree.test.cc + dispatch_command.test.cc ) target_link_libraries(nihil.cli.test PRIVATE nihil.cli diff --git a/nihil.cli/command_tree.ccm b/nihil.cli/command_tree.ccm index 6cfabe9..7399357 100644 --- a/nihil.cli/command_tree.ccm +++ b/nihil.cli/command_tree.ccm @@ -120,18 +120,15 @@ private: }; // The command tree stores commands in a tree structure suitable for searching. -struct command_tree +export struct command_tree { // Add a node to the tree. Returns false if the node already exists. - auto insert(this command_tree &self, std::ranges::range auto &&path, - std::shared_ptr command) -> void - requires(std::constructible_from>) + auto insert(this command_tree &self, std::shared_ptr command) -> void { auto *this_node = &self.m_root_node; // Find the node for this key. - for (auto &&this_word : path) + for (auto &&this_word : command->path() | std::views::split(' ')) this_node = this_node->get_or_create_child(std::string_view(this_word)); // Set the new value. @@ -143,7 +140,7 @@ struct command_tree requires(std::constructible_from>) { - auto const *this_node = &self.m_root_node; + auto *this_node = &self.m_root_node; auto rest = args | std::views::take_while([&](auto &&str) { @@ -169,9 +166,10 @@ private: { auto tree = command_tree(); - for (auto &&command : get_registered_commands()) + for (auto &&command : get_registered_commands()) { // Throws std::logic_error on duplicates. - tree.insert(command->path() | std::views::split(' '), command); + tree.insert(command); + } return tree; } diff --git a/nihil.cli/command_tree.test.cc b/nihil.cli/command_tree.test.cc new file mode 100644 index 0000000..a1e7d32 --- /dev/null +++ b/nihil.cli/command_tree.test.cc @@ -0,0 +1,45 @@ +// This source code is released into the public domain. + +#include + +import nihil.std; +import nihil.cli; + +namespace { + +inline auto constexpr *test_tags = "[nihil][nihil.cli]"; + +SCENARIO("Inserting and retrieving nodes in a command_tree", test_tags) +{ + GIVEN ("A command tree") { + auto tree = nihil::command_tree(); + + THEN ("We can insert nodes into the tree") { + tree.insert(std::make_shared("cmd1 sub1")); + tree.insert(std::make_shared("cmd1 sub2")); + tree.insert(std::make_shared("cmd2 sub1")); + + AND_THEN ("We can retrieve nodes from the tree") { + auto c1s1 = tree.find(std::vector{"cmd1", "sub1"}); + REQUIRE(c1s1.first->command().path() == "cmd1 sub1"); + + auto c1s2 = tree.find(std::vector{"cmd1", "sub2"}); + REQUIRE(c1s2.first->command().path() == "cmd1 sub2"); + + auto c2s1 = tree.find(std::vector{"cmd2", "sub1"}); + REQUIRE(c2s1.first->command().path() == "cmd2 sub1"); + } + + AND_THEN ("Fetching an unknown command returns the root node") { + auto cmd = tree.find(std::vector{"cmd3", "sub1"}); + REQUIRE(cmd.first->command().path() == ""); + } + + AND_THEN ("Fetching an unknown subcommand returns the cmd1 node") { + auto cmd = tree.find(std::vector{"cmd1", "sub3"}); + REQUIRE(cmd.first->command().path() == "cmd1"); + } + } + } +} +} // anonymous namespace diff --git a/nihil.cli/dispatch_command.test.cc b/nihil.cli/dispatch_command.test.cc new file mode 100644 index 0000000..8253bdc --- /dev/null +++ b/nihil.cli/dispatch_command.test.cc @@ -0,0 +1,102 @@ +// This source code is released into the public domain. + +#include + +#include + +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{ + "cmd", "sub1", nullptr + }; + auto *argv = const_cast(args.data()); + + auto const ret = nihil::dispatch_command( + static_cast(args.size()) - 1, argv); + REQUIRE(ret == 0); + REQUIRE(cmd_sub1_called == true); + REQUIRE(cmd_sub2_called == false); + } + + SECTION("cmd sub2") { + auto args = std::vector{ + "cmd", "sub2", nullptr + }; + auto *argv = const_cast(args.data()); + + auto const ret = nihil::dispatch_command( + static_cast(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{ + "nocomd", "sub", nullptr + }; + auto *argv = const_cast(args.data()); + + auto output = std::string(); + auto ret = int{}; + { + auto capture = nihil::capture_stream(std::cerr); + ret = nihil::dispatch_command( + static_cast(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{ + "cmd", nullptr + }; + auto *argv = const_cast(args.data()); + + auto output = std::string(); + auto ret = int{}; + { + auto capture = nihil::capture_stream(std::cerr); + ret = nihil::dispatch_command( + static_cast(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)); +} 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 - -#include - -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{ - "cmd", "sub1", nullptr - }; - auto *argv = const_cast(args.data()); - - auto const ret = nihil::dispatch_command( - static_cast(args.size()) - 1, argv); - REQUIRE(ret == 0); - REQUIRE(cmd_sub1_called == true); - REQUIRE(cmd_sub2_called == false); - } - - SECTION("cmd sub2") { - auto args = std::vector{ - "cmd", "sub2", nullptr - }; - auto *argv = const_cast(args.data()); - - auto const ret = nihil::dispatch_command( - static_cast(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{ - "nocomd", "sub", nullptr - }; - auto *argv = const_cast(args.data()); - - auto output = std::string(); - auto ret = int{}; - { - auto capture = nihil::capture_stream(std::cerr); - ret = nihil::dispatch_command( - static_cast(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{ - "cmd", nullptr - }; - auto *argv = const_cast(args.data()); - - auto output = std::string(); - auto ret = int{}; - { - auto capture = nihil::capture_stream(std::cerr); - ret = nihil::dispatch_command( - static_cast(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)); -} -- cgit v1.2.3