|
@@ -1,140 +1,88 @@
|
|
|
#include <iostream>
|
|
|
-#include "server/commands/CommandUtils.h"
|
|
|
+#include <array>
|
|
|
|
|
|
-using namespace std;
|
|
|
+#include "common/utils/SplitString.h"
|
|
|
+#include "common/utils/List.h"
|
|
|
|
|
|
-// -----------------------------------------------------------------------------
|
|
|
-// test feedback
|
|
|
-// -----------------------------------------------------------------------------
|
|
|
+typedef List<const char*, 16> StringList;
|
|
|
|
|
|
const char* RED = "\033[0;31m";
|
|
|
const char* GREEN = "\033[0;32m";
|
|
|
|
|
|
-int tests = 0;
|
|
|
-int successTests = 0;
|
|
|
-
|
|
|
-void notifySuccess(string text) {
|
|
|
- tests++;
|
|
|
- successTests++;
|
|
|
- cout << GREEN << tests << ". " << text << endl;
|
|
|
-}
|
|
|
-
|
|
|
-void notifyFail(string text) {
|
|
|
- tests++;
|
|
|
- cout << RED << tests << ". " << text << endl;
|
|
|
-}
|
|
|
+uint tests = 0;
|
|
|
+uint successTests = 0;
|
|
|
|
|
|
void finalizeTest() {
|
|
|
- cout << successTests << " / " << tests << " succeeded" << endl;
|
|
|
+ std::cout << ((successTests == tests) ? GREEN : RED);
|
|
|
+ std::cout << successTests << " / " << tests << " succeeded\n";
|
|
|
tests = 0;
|
|
|
successTests = 0;
|
|
|
}
|
|
|
|
|
|
-// -----------------------------------------------------------------------------
|
|
|
-// checks
|
|
|
-// -----------------------------------------------------------------------------
|
|
|
-
|
|
|
-void checkEqual(int a, int b, string text) {
|
|
|
- if(a == b) {
|
|
|
- notifySuccess(text);
|
|
|
+template<typename T>
|
|
|
+void checkEqual(const T& wanted, const T& actual, const char* text) {
|
|
|
+ if(wanted == actual) {
|
|
|
+ tests++;
|
|
|
+ successTests++;
|
|
|
+ std::cout << GREEN << tests << ": " << text << "\n";
|
|
|
} else {
|
|
|
- notifyFail(text + " - expected '" + std::to_string(a) + "' got '" + std::to_string(b) + "'");
|
|
|
+ tests++;
|
|
|
+ std::cout << RED << tests << ": " << text << " - ";
|
|
|
+ std::cout << RED << "expected '" << wanted << "' got '" << actual << "'\n";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void checkEqual(string a, string b, string text) {
|
|
|
- if(a == b) {
|
|
|
- notifySuccess(text);
|
|
|
- } else {
|
|
|
- notifyFail(text + " - expected '" + a + "' got '" + b + "'");
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-void checkBool(bool a, bool b, string text) {
|
|
|
- if(a == b) {
|
|
|
- notifySuccess(text);
|
|
|
- } else {
|
|
|
- notifyFail(text + " - expected '" + std::to_string(a) + "' got '" + std::to_string(b) + "'");
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-void checkGreaterOrEqual(int a, int b, string text) {
|
|
|
- if(a >= b) {
|
|
|
- notifySuccess(text);
|
|
|
- } else {
|
|
|
- notifyFail(text + " - expected " + std::to_string(a) + " >= " + std::to_string(b));
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-// -----------------------------------------------------------------------------
|
|
|
-// tests of command parser
|
|
|
-// -----------------------------------------------------------------------------
|
|
|
-
|
|
|
-void checkCommandParser(string rawCommand, string command, vector<string>& args, string text) {
|
|
|
- string parsedCommand;
|
|
|
- vector<string> parsedArgs;
|
|
|
-
|
|
|
- CommandUtils::splitString(rawCommand, parsedCommand, parsedArgs);
|
|
|
-
|
|
|
- checkEqual(command, parsedCommand, text);
|
|
|
- checkEqual(args.size(), parsedArgs.size(), text);
|
|
|
-
|
|
|
- for(unsigned long i = 0; i < args.size() && i < parsedArgs.size(); i++) {
|
|
|
- checkEqual(args[i], parsedArgs[i], text);
|
|
|
+void checkCommandParser(const char* rawCommand, const StringList& result, const char* text) {
|
|
|
+ SplitString split(rawCommand);
|
|
|
+ checkEqual(result.getLength(), split.getLength(), text);
|
|
|
+ for(uint i = 0; i < result.getLength() && i < split.getLength(); i++) {
|
|
|
+ checkEqual(String(result[i]), String(split[i]), text);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void testCommandParser() {
|
|
|
- vector<string> args;
|
|
|
-
|
|
|
- args = {};
|
|
|
- checkCommandParser("test", "test", args, "command without arguments");
|
|
|
+ StringList list;
|
|
|
|
|
|
- args = {"aaa"};
|
|
|
- checkCommandParser("test aaa", "test", args, "command with one argument");
|
|
|
+ list.clear().add("test");
|
|
|
+ checkCommandParser("test", list, "command without arguments");
|
|
|
|
|
|
- args = {"aaa", "bbbb"};
|
|
|
- checkCommandParser("test aaa bbbb", "test", args, "command with two arguments");
|
|
|
+ list.clear().add("test").add("aaa");
|
|
|
+ checkCommandParser("test aaa", list, "command with one argument");
|
|
|
|
|
|
- args = {"aaa", "bbbb", "ccccc"};
|
|
|
- checkCommandParser("test aaa bbbb ccccc", "test", args, "command with three arguments");
|
|
|
+ list.clear().add("test").add("aaa").add("bbbb");
|
|
|
+ checkCommandParser("test aaa bbbb", list, "command with two arguments");
|
|
|
|
|
|
- args = {};
|
|
|
- checkCommandParser("test ", "test", args, "command with spaces");
|
|
|
+ list.clear().add("test").add("aaa").add("bbbb").add("ccccc");
|
|
|
+ checkCommandParser("test aaa bbbb ccccc", list, "command with three arguments");
|
|
|
|
|
|
- args = {"aaa", "bbbb"};
|
|
|
- checkCommandParser("test aaa bbbb", "test", args, "command with arguments and spaces");
|
|
|
+ list.clear().add("test");
|
|
|
+ checkCommandParser("test ", list, "command with spaces");
|
|
|
|
|
|
- args = {"aaa", "bbbb"};
|
|
|
- checkCommandParser("test aaa bbbb", "test", args, "command with arguments and spaces");
|
|
|
+ list.clear().add("test").add("aaa").add("bbbb");
|
|
|
+ checkCommandParser("test aaa bbbb", list, "command with arguments and spaces");
|
|
|
|
|
|
- args = {"aaa bbbb"};
|
|
|
- checkCommandParser("test \"aaa bbbb\"", "test", args, "command with one argument and quotation marks");
|
|
|
+ list.clear().add("test").add("aaa bbbb");
|
|
|
+ checkCommandParser("test \"aaa bbbb\"", list, "command with one argument and quotation marks");
|
|
|
|
|
|
- args = {"aaa bbbb", "ccc"};
|
|
|
- checkCommandParser("test \"aaa bbbb\" ccc", "test", args, "command with two arguments and quotation marks");
|
|
|
+ list.clear().add("test").add("aaa bbbb").add("ccc");
|
|
|
+ checkCommandParser("test \"aaa bbbb\" ccc", list, "command with two arguments and quotation marks");
|
|
|
|
|
|
- args = {"ddd", "aaa bbbb", "ccc"};
|
|
|
- checkCommandParser("test ddd \"aaa bbbb\" ccc", "test", args, "command with tree arguments and quotation marks");
|
|
|
+ list.clear().add("test").add("ddd").add("aaa bbbb").add("ccc");
|
|
|
+ checkCommandParser("test ddd \"aaa bbbb\" ccc", list, "command with tree arguments and quotation marks");
|
|
|
|
|
|
- args = {};
|
|
|
- checkCommandParser("test \"", "", args, "command syntax exception 1");
|
|
|
+ list.clear().add("test").add("");
|
|
|
+ checkCommandParser("test \"\"", list, "command with empty argument");
|
|
|
+
|
|
|
+ list.clear();
|
|
|
+ checkCommandParser("test \"", list, "command syntax exception 1");
|
|
|
+ checkCommandParser("test aaa\"", list, "command syntax exception 2");
|
|
|
+ checkCommandParser("test aaa\"bbb\"", list, "command syntax exception 3");
|
|
|
+ checkCommandParser("test aaa \"bbb\"ccc", list, "command syntax exception 4");
|
|
|
|
|
|
- args = {};
|
|
|
- checkCommandParser("test aaa\"", "", args, "command syntax exception 2");
|
|
|
-
|
|
|
- args = {};
|
|
|
- checkCommandParser("test aaa\"bbb\"", "", args, "command syntax exception 3");
|
|
|
-
|
|
|
- args = {};
|
|
|
- checkCommandParser("test aaa \"bbb\"ccc", "", args, "command syntax exception 4");
|
|
|
+ finalizeTest();
|
|
|
}
|
|
|
|
|
|
-// -----------------------------------------------------------------------------
|
|
|
-// main
|
|
|
-// -----------------------------------------------------------------------------
|
|
|
-
|
|
|
-int main(int argc, char** argv) {
|
|
|
+int main() {
|
|
|
testCommandParser();
|
|
|
return 0;
|
|
|
}
|