123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- #include <iostream>
- #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<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 testCommandParser() {
- vector<string> 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;
- }
|