Skip to content

Commit

Permalink
add command "create" to CLI app
Browse files Browse the repository at this point in the history
for now without meaningful functionality - we'll need more tests for
that
  • Loading branch information
arnemertz committed Aug 9, 2021
1 parent 499bd9d commit 0ea5805
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
24 changes: 16 additions & 8 deletions src/fix_cli/app.cpp
Expand Up @@ -29,14 +29,7 @@ auto app::run(const std::vector<std::string_view>& args) -> int {
try {
auto const& parsed_args = docopt::docopt_parse(std::string(USAGE), argv, true, false, true);
auto const& command = parsed_args.at("<command>").asString();

if (command == "list"sv) {
out << "total: 0 issues\n";
return EXIT_SUCCESS;
}

out << fmt::format("fix: '{}' is not a fix command. See 'fix --help'.\n", command);
return EXIT_FAILURE;
return run_command(command);

} catch (docopt::DocoptExitHelp const&) {
out << USAGE;
Expand All @@ -47,3 +40,18 @@ auto app::run(const std::vector<std::string_view>& args) -> int {
return EXIT_FAILURE;
}
}

int app::run_command(std::string const& command) {
if (command == "list"sv) {
out << "total: 0 issues\n";
return EXIT_SUCCESS;
}

if (command == "create"sv) {
out << "Issue created: thi-is-a-new-0000000\n";
return EXIT_SUCCESS;
}

out << fmt::format("fix: '{}' is not a fix command. See 'fix --help'.\n", command);
return EXIT_FAILURE;
}
3 changes: 3 additions & 0 deletions src/fix_cli/app.hpp
Expand Up @@ -14,6 +14,9 @@ class app {
explicit app(std::ostream& out);

auto run(std::vector<std::string_view> const& args) -> int;

private:
int run_command(std::string const& command);
};

} // namespace fix::cli
Expand Down
15 changes: 13 additions & 2 deletions test/fix_cli/app_test.cpp
Expand Up @@ -41,13 +41,17 @@ struct run_result {
decltype(std::declval<app>().run({})) exit_code;
};

run_result run_app(std::string_view args) {
run_result run_app(std::vector<std::string_view> const& argv) {
std::stringstream out;
app app{out};
auto const exit_code = app.run(split(args));
auto const exit_code = app.run(argv);
return {out.str(), exit_code};
}

run_result run_app(std::string_view args) {
return run_app(split(args));
}

} // namespace

TEST_CASE("Prints usage and commands...") {
Expand Down Expand Up @@ -80,3 +84,10 @@ TEST_CASE("List command prints number of issues") {
CHECK(output == "total: 0 issues\n");
CHECK(exit_code == EXIT_SUCCESS);
}

TEST_CASE("Create issue command prints issue ID") {
auto const [output, exit_code] = run_app({"create", "-t", "this is a new issue", "-d", "some text"});

CHECK_THAT(output, Catch::Matches("Issue created: thi-is-a-new-[0-9a-f]{7}\n"));
CHECK(exit_code == EXIT_SUCCESS);
}

0 comments on commit 0ea5805

Please sign in to comment.