From 3ec5520e32830cb87d1232d7b2d2a822bd081c80 Mon Sep 17 00:00:00 2001 From: Joseph Cosentino Date: Sat, 1 Jun 2024 20:37:51 -0700 Subject: [PATCH] add more context parsing tests (#5) --- src/Context.cpp | 42 ++++++++++++------------------ test/ContextTest.cpp | 62 +++++++++++++++++++++++++++++++++++++++----- 2 files changed, 72 insertions(+), 32 deletions(-) diff --git a/src/Context.cpp b/src/Context.cpp index a7d8b45..8bc27c1 100644 --- a/src/Context.cpp +++ b/src/Context.cpp @@ -1,6 +1,5 @@ #include "Context.h" #include -#include #include namespace MqttClient { @@ -13,26 +12,15 @@ ParseResult parseContext(int argc, char *argv[]) noexcept { Context context; context.command = argv[1]; - if (context.command != "pub") { - return {ParseResultCode::FAILURE, - "Unknown command: " + context.command}; - } - - // discard the command - argc -= 1; - argv += 1; - - struct option long_options[] = {{"topic", required_argument, 0, 't'}, - {"address", required_argument, 0, 'a'}, - {"verbose", no_argument, 0, 'v'}, - {"help", no_argument, 0, 'h'}, - {0, 0, 0, 0}}; - - int option_index = 0; - int option; - while ((option = getopt_long(argc, argv, "t:a:vh", long_options, - &option_index)) != -1) { - switch (option) { + struct option opts[] = {{"topic", required_argument, 0, 't'}, + {"address", required_argument, 0, 'a'}, + {"verbose", no_argument, 0, 'v'}, + {"help", no_argument, 0, 'h'}, + {0, 0, 0, 0}}; + int opt; + int optInd = 0; + while ((opt = getopt_long(argc, argv, "t:a:vh", opts, &optInd)) != -1) { + switch (opt) { case 't': context.topic = optarg; break; @@ -51,6 +39,11 @@ ParseResult parseContext(int argc, char *argv[]) noexcept { } } + if (context.command != "pub") { + return {ParseResultCode::FAILURE, + "Unknown command: " + context.command}; + } + if (context.topic.empty()) { // TODO more validations return {ParseResultCode::FAILURE, "Error: Topic is required"}; @@ -61,11 +54,10 @@ ParseResult parseContext(int argc, char *argv[]) noexcept { return {ParseResultCode::FAILURE, "Error: Address is required"}; } - if (optind == argc) { + auto numMessages = argc - 1 - optind; + if (numMessages == 0) { return {ParseResultCode::FAILURE, "Error: Message is required"}; } - - auto numMessages = argc - optind; if (numMessages > 1) { return {ParseResultCode::FAILURE, "Error: " + std::to_string(numMessages) + @@ -73,7 +65,7 @@ ParseResult parseContext(int argc, char *argv[]) noexcept { "time is supported"}; } - context.message = argv[optind]; + context.message = argv[optind + 1]; return {ParseResultCode::SUCCESS, {}, context}; } diff --git a/test/ContextTest.cpp b/test/ContextTest.cpp index c25a97d..d5bd598 100644 --- a/test/ContextTest.cpp +++ b/test/ContextTest.cpp @@ -1,26 +1,74 @@ #include "Context.h" #include +#include +#include using namespace MqttClient; class ContextTest : public testing::Test { public: - ParseResult parse(char *args[]) { - int argc = sizeof(args) / sizeof(args[0]); - return parseContext(argc, args); + ParseResult parse(std::vector args) { + if (args.empty()) { + return parseContext(0, {}); + } + std::vector argv; + for (size_t i = 0; i < args.size(); ++i) { + argv.push_back(const_cast(args[i].c_str())); + } + return parseContext(argv.size(), &argv[0]); } }; +TEST_F(ContextTest, AllOpts) { + auto res = parse({"mqtt", "pub", "-v", "-t", "topic", "-a", + "127.0.0.1:8883", "message"}); + EXPECT_EQ(ParseResultCode::SUCCESS, res.code); +} + +TEST_F(ContextTest, RequiredOpts) { + auto res = parse( + {"mqtt", "pub", "-t", "topic", "-a", "127.0.0.1:8883", "message"}); + EXPECT_EQ(ParseResultCode::SUCCESS, res.code); +} + +TEST_F(ContextTest, Help) { + auto res = parse({"mqtt", "-h"}); + EXPECT_EQ(ParseResultCode::HELP, res.code); +} + TEST_F(ContextTest, NoArgs) { auto res = parse({}); EXPECT_EQ(ParseResultCode::FAILURE, res.code); } -TEST_F(ContextTest, WithAllRequiredOptions) { - char *args[] = {"mqtt", "pub", "-t", "topic", "-a", "127.0.0.1:8883"}; - auto res = parse(args); +TEST_F(ContextTest, MissingMessage) { + auto res = parse({"mqtt", "pub", "-t", "topic", "-a", "127.0.0.1:8883"}); + EXPECT_EQ(ParseResultCode::FAILURE, res.code); +} + +TEST_F(ContextTest, MissingTopic) { + auto res = parse({"mqtt", "pub", "-a", "127.0.0.1:8883", "message"}); + EXPECT_EQ(ParseResultCode::FAILURE, res.code); +} + +TEST_F(ContextTest, MissingTopicValue) { + auto res = parse({"mqtt", "pub", "-t", "-a", "127.0.0.1:8883", "message"}); + EXPECT_EQ(ParseResultCode::FAILURE, res.code); +} + +TEST_F(ContextTest, MissingAddress) { + auto res = parse({"mqtt", "pub", "-t", "topic", "message"}); EXPECT_EQ(ParseResultCode::FAILURE, res.code); } -// TODO more cases +TEST_F(ContextTest, MissingAddressValue) { + auto res = parse({"mqtt", "pub", "-t", "topic", "-a", "message"}); + EXPECT_EQ(ParseResultCode::FAILURE, res.code); +} + +TEST_F(ContextTest, MultipleMessages) { + auto res = parse({"mqtt", "pub", "-v", "-t", "topic", "-a", + "127.0.0.1:8883", "message", "message2"}); + EXPECT_EQ(ParseResultCode::FAILURE, res.code); +}