#include #include "server/commands/CommandUtils.h" using namespace std; // ----------------------------------------------------------------------------- // test feedback // ----------------------------------------------------------------------------- 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; } void finalizeTest() { cout << successTests << " / " << tests << " succeeded" << endl; tests = 0; successTests = 0; } // ----------------------------------------------------------------------------- // checks // ----------------------------------------------------------------------------- void checkEqual(int a, int b, string text) { if(a == b) { notifySuccess(text); } else { notifyFail(text + " - expected '" + std::to_string(a) + "' got '" + std::to_string(b) + "'"); } } 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& args, string text) { string parsedCommand; vector 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 testCommandParser() { vector args; args = {}; checkCommandParser("test", "test", args, "command without arguments"); args = {"aaa"}; checkCommandParser("test aaa", "test", args, "command with one argument"); args = {"aaa", "bbbb"}; checkCommandParser("test aaa bbbb", "test", args, "command with two arguments"); args = {"aaa", "bbbb", "ccccc"}; checkCommandParser("test aaa bbbb ccccc", "test", args, "command with three arguments"); args = {}; checkCommandParser("test ", "test", args, "command with spaces"); args = {"aaa", "bbbb"}; checkCommandParser("test aaa bbbb", "test", args, "command with arguments and spaces"); args = {"aaa", "bbbb"}; checkCommandParser("test aaa bbbb", "test", args, "command with arguments and spaces"); args = {"aaa bbbb"}; checkCommandParser("test \"aaa bbbb\"", "test", args, "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"); args = {"ddd", "aaa bbbb", "ccc"}; checkCommandParser("test ddd \"aaa bbbb\" ccc", "test", args, "command with tree arguments and quotation marks"); args = {}; checkCommandParser("test \"", "", args, "command syntax exception 1"); 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"); } // ----------------------------------------------------------------------------- // main // ----------------------------------------------------------------------------- int main(int argc, char** argv) { testCommandParser(); return 0; }