Skip to content
This repository has been archived by the owner on Nov 17, 2024. It is now read-only.

Commit

Permalink
Basic unit testing implemented. I don't think its perfect but it shou…
Browse files Browse the repository at this point in the history
…ld catch some small issues.
  • Loading branch information
Captainjamason committed Feb 8, 2024
1 parent 60975ec commit 4c4c1c3
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 37 deletions.
1 change: 1 addition & 0 deletions .github/workflows/c-cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ jobs:
- run: pip install meson ninja
- run: meson setup build
- run: meson compile -C build
- run: meson test -C build
18 changes: 0 additions & 18 deletions .github/workflows/docker-image.yml

This file was deleted.

14 changes: 7 additions & 7 deletions include/cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ namespace opencxx_cli {
};

// Basic commands.
void help(std::vector<entryData> entries);
void ver(struct programInfo info);
int help(std::vector<entryData> entries);
int ver(struct programInfo info);

// addEntry will reference the struct above, Used to add into a vector that
// is provided from the application.
void addEntry(std::string lhand, std::string shand, int (*func)(), std::vector<CLI::entryData> *entries);
int addEntry(std::string lhand, std::string shand, int (*func)(), std::vector<CLI::entryData> *entries);

// Useful function to quickly make an argument vector, this can be run
// inline.
std::vector<std::string> vectorize(int argc, char *argv[]);
void parse(std::vector<entryData> entries, std::vector<std::string> args);
int parse(std::vector<entryData> entries, std::vector<std::string> args);

// CLI output messages.
void error(std::string s);
void warn(std::string s);
void info(std::string s);
int error(std::string s);
int warn(std::string s);
int info(std::string s);
};
}

Expand Down
21 changes: 13 additions & 8 deletions src/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,51 @@ using namespace std;

// Initialize the main 3 debug/error message commands,
// along with some nice formatting from the color library.
void CLI::error(string s) {
int CLI::error(string s) {
colors color;
color.setColor(color.fgRed);
cout << "[ERROR] ";
color.reset();
cout << s << "\n";
return 0;
}
void CLI::warn(string s) {
int CLI::warn(string s) {
colors color;
color.setColor(color.fgYellow);
cout << "[WARN] ";
color.reset();
cout << s << "\n";
return 0;
}
void CLI::info(string s) {
int CLI::info(string s) {
colors color;
color.setColor(color.fgBlue);
cout << "[INFO] ";
color.reset();
cout << s << "\n";
return 0;
}

// addEntry() will push a new entryData struct (cli.h) into an entries
// vector, which is established application side and a vector is provided.
void CLI::addEntry(string lhand, string shand, int(*func)(), vector<CLI::entryData> *entries) {
int CLI::addEntry(string lhand, string shand, int(*func)(), vector<CLI::entryData> *entries) {
CLI::entryData entry;
entry.lhand = lhand;
entry.shand = shand;
entry.func = func;
entries -> push_back(entry);
return 0;
}

// Basic version command, This may not ever be called but is available for
// implementation client side.
void CLI::ver(struct programInfo info) {
int CLI::ver(struct programInfo info) {
string output;
output.append("[" + info.name + "]\n");
output.append(info.version + "\n");
output.append(info.author + "\n");
cout << output;
return 0;
}

// Vectorize allows for a quick function to take argc and argv[] and return
Expand All @@ -72,14 +77,14 @@ vector<string> CLI::vectorize(int argc, char *argv[]) {
// TO-DO: Help command, This is hard coded into the library and will
// always be available in the program. This will need to iterate over
// a vector of the entries.
void CLI::help(vector<CLI::entryData> entries) {
int CLI::help(vector<CLI::entryData> entries) {
cout << "[opencxx-cli] Help is under construction...\n";
exit(1);
return 0;
}

// Here's the big one, This will iterate and parse arguments, along with calling the
// application side function...
void CLI::parse(vector<CLI::entryData> entries, vector<string> args) {
int CLI::parse(vector<CLI::entryData> entries, vector<string> args) {
// if no args then stop, no need to be wasting time.
if(args.size() == 0) {
error("No arguments provided... Please use --help");
Expand Down
9 changes: 8 additions & 1 deletion src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Copyright 2024 - hurbIndustries
## JPD

testDep = dependency('opencxx-cli')

pkg = import('pkgconfig')

Expand All @@ -16,6 +17,12 @@ opencxx_cli = library('opencxx-cli',
include_directories : inc,
)

unitTests = executable('unitTests', 'unitTests.cpp')
unitTests = executable('unitTests',
'unitTests.cpp',
include_directories : inc,
dependencies : [testDep]
)

test('test', unitTests)

pkg.generate(opencxx_cli)
53 changes: 50 additions & 3 deletions src/unitTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,62 @@
// Copyright 2024 - hurbIndustries
// JPD

#include <iostream>
#include <vector>
#include "../include/cli.h"

// Some basic initialization...
using namespace opencxx_cli;
CLI cli;
std::vector<CLI::entryData> entries;

int main() {
int testFunc() {
return 0;
}

int main(int argc, char* argv[]) {
CLI cli;
// test output messages...
if()
if(cli.error("testerr") != 0) {
std::cout << "cli.error() failed!\n";
return 1;
}
if(cli.warn("testwarn") != 0) {
std::cout << "cli.warn() failed!\n";
return 1;
}
if(cli.info("testinfo") != 0) {
std::cout << "cli.info() failed!\n";
return 1;
}

//test add entry
if(cli.addEntry("--test", "-t", testFunc, &entries)) {
if(entries[0].func != 0) {
std::cout << "err: entries not stored successfully...\n";
return 1;
}
// Test mutliple entries!
if(cli.addEntry("--test", "-t", testFunc, &entries)) {
if(entries[1].func != 0) {
std::cout << "err: multiple entries not working...\n";
return 1;
}
}

// Test parsing...
if(argv[1] != "") {
if(cli.parse(entries, cli.vectorize(argc, argv)) != 0) {
std::cout << "err: parse() failed!\n";
return 1;
}
} else {
std::cout << "info: no args passed, skipping parse()\n";
}
}

// test help()
if(cli.help(entries) != 0) {
std::cout << "err: help() failed!\n";
return 1;
}
}

0 comments on commit 4c4c1c3

Please sign in to comment.