Skip to content

Commit

Permalink
add more context parsing tests (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcosentino11 authored Jun 2, 2024
1 parent 51ecdbe commit 3ec5520
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 32 deletions.
42 changes: 17 additions & 25 deletions src/Context.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "Context.h"
#include <getopt.h>
#include <iostream>
#include <string>

namespace MqttClient {
Expand All @@ -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;
Expand All @@ -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"};
Expand All @@ -61,19 +54,18 @@ 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) +
" messages were provided. Currently, only 1 message at a "
"time is supported"};
}

context.message = argv[optind];
context.message = argv[optind + 1];

return {ParseResultCode::SUCCESS, {}, context};
}
Expand Down
62 changes: 55 additions & 7 deletions test/ContextTest.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,74 @@

#include "Context.h"
#include <gtest/gtest.h>
#include <string>
#include <vector>

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<std::string> args) {
if (args.empty()) {
return parseContext(0, {});
}
std::vector<char *> argv;
for (size_t i = 0; i < args.size(); ++i) {
argv.push_back(const_cast<char *>(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);
}

0 comments on commit 3ec5520

Please sign in to comment.