aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.cli/command_tree.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/command_tree.test.cc
parent762fdfaa953b35ed4c402c74890c5c30155a5dab (diff)
downloadnihil-47999457e647352ae7e71d43c65e7b39ae5ca567.tar.gz
nihil-47999457e647352ae7e71d43c65e7b39ae5ca567.tar.bz2
cli: add tests for command_tree
Diffstat (limited to 'nihil.cli/command_tree.test.cc')
-rw-r--r--nihil.cli/command_tree.test.cc45
1 files changed, 45 insertions, 0 deletions
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 <catch2/catch_test_macros.hpp>
+
+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<nihil::command>("cmd1 sub1"));
+ tree.insert(std::make_shared<nihil::command>("cmd1 sub2"));
+ tree.insert(std::make_shared<nihil::command>("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