From 378dd663a402fe196f2b56c6413eb3f623aecbbf Mon Sep 17 00:00:00 2001 From: Lexi Winter Date: Tue, 1 Jul 2025 19:18:42 +0100 Subject: cli: refactoring --- nihil.cli/command.test.cc | 57 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 nihil.cli/command.test.cc (limited to 'nihil.cli/command.test.cc') diff --git a/nihil.cli/command.test.cc b/nihil.cli/command.test.cc new file mode 100644 index 0000000..5ac52ed --- /dev/null +++ b/nihil.cli/command.test.cc @@ -0,0 +1,57 @@ +// This source code is released into the public domain. + +#include + +import nihil.std; +import nihil.cli; + +namespace { +inline constexpr auto *test_tags = "[nihil][nihil.cli][nihil.cli.command]"; + +TEST_CASE("nihil::command invariants", test_tags) +{ + static_assert(!std::move_constructible); + static_assert(!std::copy_constructible); + static_assert(!std::is_copy_assignable_v); + static_assert(!std::is_move_assignable_v); + + static_assert(std::destructible); +} + +SCENARIO("A command has a path", test_tags) +{ + GIVEN ("A command object with a path") { + auto cmd = nihil::command("foo bar baz"); + + THEN ("The path is correct") { + REQUIRE(cmd.path() == "foo bar baz"); + } + } +} + +SCENARIO("A command has a handler", test_tags) +{ + GIVEN ("A command object with a handler") { + auto handler_called = false; + auto cmd = nihil::command("foo bar baz", "foo bar baz -x", [&](int, char **) { + handler_called = true; + return 0; + }); + + THEN ("The usage is correct") { + REQUIRE(cmd.usage() == "foo bar baz -x"); + } + + AND_WHEN ("The command is invoked") { + auto ret = cmd.invoke(0, nullptr); + + THEN ("The return value is 0") { + REQUIRE(ret.value() == 0); + } + AND_THEN ("The handler was called") { + REQUIRE(handler_called == true); + } + } + } +} +} // namespace -- cgit v1.2.3