Main.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include <iostream>
  2. #include "server/commands/CommandUtils.h"
  3. using namespace std;
  4. // -----------------------------------------------------------------------------
  5. // test feedback
  6. // -----------------------------------------------------------------------------
  7. const char* RED = "\033[0;31m";
  8. const char* GREEN = "\033[0;32m";
  9. int tests = 0;
  10. int successTests = 0;
  11. void notifySuccess(string text)
  12. {
  13. tests++;
  14. successTests++;
  15. cout << GREEN << tests << ". " << text << endl;
  16. }
  17. void notifyFail(string text)
  18. {
  19. tests++;
  20. cout << RED << tests << ". " << text << endl;
  21. }
  22. void finalizeTest()
  23. {
  24. cout << successTests << " / " << tests << " succeeded" << endl;
  25. tests = 0;
  26. successTests = 0;
  27. }
  28. // -----------------------------------------------------------------------------
  29. // checks
  30. // -----------------------------------------------------------------------------
  31. void checkEqual(int a, int b, string text)
  32. {
  33. if(a == b)
  34. {
  35. notifySuccess(text);
  36. }
  37. else
  38. {
  39. notifyFail(text + " - expected '" + std::to_string(a) + "' got '" + std::to_string(b) + "'");
  40. }
  41. }
  42. void checkEqual(string a, string b, string text)
  43. {
  44. if(a == b)
  45. {
  46. notifySuccess(text);
  47. }
  48. else
  49. {
  50. notifyFail(text + " - expected '" + a + "' got '" + b + "'");
  51. }
  52. }
  53. void checkBool(bool a, bool b, string text)
  54. {
  55. if(a == b)
  56. {
  57. notifySuccess(text);
  58. }
  59. else
  60. {
  61. notifyFail(text + " - expected '" + std::to_string(a) + "' got '" + std::to_string(b) + "'");
  62. }
  63. }
  64. void checkGreaterOrEqual(int a, int b, string text)
  65. {
  66. if(a >= b)
  67. {
  68. notifySuccess(text);
  69. }
  70. else
  71. {
  72. notifyFail(text + " - expected " + std::to_string(a) + " >= " + std::to_string(b));
  73. }
  74. }
  75. // -----------------------------------------------------------------------------
  76. // tests of command parser
  77. // -----------------------------------------------------------------------------
  78. void checkCommandParser(string rawCommand, string command, vector<string>& args, string text)
  79. {
  80. string parsedCommand;
  81. vector<string> parsedArgs;
  82. CommandUtils::splitString(rawCommand, parsedCommand, parsedArgs);
  83. checkEqual(command, parsedCommand, text);
  84. checkEqual(args.size(), parsedArgs.size(), text);
  85. for(unsigned long i = 0; i < args.size() && i < parsedArgs.size(); i++)
  86. {
  87. checkEqual(args[i], parsedArgs[i], text);
  88. }
  89. }
  90. void testCommandParser()
  91. {
  92. vector<string> args;
  93. args = {};
  94. checkCommandParser("test", "test", args, "command without arguments");
  95. args = {"aaa"};
  96. checkCommandParser("test aaa", "test", args, "command with one argument");
  97. args = {"aaa", "bbbb"};
  98. checkCommandParser("test aaa bbbb", "test", args, "command with two arguments");
  99. args = {"aaa", "bbbb", "ccccc"};
  100. checkCommandParser("test aaa bbbb ccccc", "test", args, "command with three arguments");
  101. args = {};
  102. checkCommandParser("test ", "test", args, "command with spaces");
  103. args = {"aaa", "bbbb"};
  104. checkCommandParser("test aaa bbbb", "test", args, "command with arguments and spaces");
  105. args = {"aaa", "bbbb"};
  106. checkCommandParser("test aaa bbbb", "test", args, "command with arguments and spaces");
  107. args = {"aaa bbbb"};
  108. checkCommandParser("test \"aaa bbbb\"", "test", args, "command with one argument and quotation marks");
  109. args = {"aaa bbbb", "ccc"};
  110. checkCommandParser("test \"aaa bbbb\" ccc", "test", args, "command with two arguments and quotation marks");
  111. args = {"ddd", "aaa bbbb", "ccc"};
  112. checkCommandParser("test ddd \"aaa bbbb\" ccc", "test", args, "command with tree arguments and quotation marks");
  113. args = {};
  114. checkCommandParser("test \"", "", args, "command syntax exception 1");
  115. args = {};
  116. checkCommandParser("test aaa\"", "", args, "command syntax exception 2");
  117. args = {};
  118. checkCommandParser("test aaa\"bbb\"", "", args, "command syntax exception 3");
  119. args = {};
  120. checkCommandParser("test aaa \"bbb\"ccc", "", args, "command syntax exception 4");
  121. }
  122. // -----------------------------------------------------------------------------
  123. // main
  124. // -----------------------------------------------------------------------------
  125. int main(int argc, char** argv)
  126. {
  127. testCommandParser();
  128. return 0;
  129. }