aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.cli/command.test.cc
blob: 5ac52edff68a69b1ca09da833c81b81b6bfa9f4a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// This source code is released into the public domain.

#include <catch2/catch_test_macros.hpp>

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<nihil::command>);
	static_assert(!std::copy_constructible<nihil::command>);
	static_assert(!std::is_copy_assignable_v<nihil::command>);
	static_assert(!std::is_move_assignable_v<nihil::command>);

	static_assert(std::destructible<nihil::command>);
}

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