aboutsummaryrefslogtreecommitdiffstats
path: root/tests/command_map.cc
blob: 75b6f0d4c37415decb69b515279791a93e0ad7a8 (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
/*
 * This source code is released into the public domain.
 */

#include <vector>

#include <catch2/catch_test_macros.hpp>

import nihil;

namespace {

auto cmd_sub1_called = false;
auto cmd_sub1 = nihil::command<int>("cmd sub1", [](int, int, char **) -> int
{
	cmd_sub1_called = true;
	return 0;
});

} // anonymous namespace

TEST_CASE("command_map: basic", "[command_map]")
{
	auto args = std::vector<char const *>{
		"cmd", "sub1", nullptr
	};
	auto argv = const_cast<char **>(args.data());
	nihil::dispatch_command(0, args.size(), argv);
	REQUIRE(cmd_sub1_called == true);
}

TEST_CASE("command_map: unknown command", "[command_map]")
{
	auto args = std::vector<char const *>{
		"cmd", "nonesuch", nullptr
	};
	auto argv = const_cast<char **>(args.data());

	REQUIRE_THROWS_AS(nihil::dispatch_command(0, args.size(), argv),
			  nihil::usage_error);
}