Main.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. tests++;
  13. successTests++;
  14. cout << GREEN << tests << ". " << text << endl;
  15. }
  16. void notifyFail(string text) {
  17. tests++;
  18. cout << RED << tests << ". " << text << endl;
  19. }
  20. void finalizeTest() {
  21. cout << successTests << " / " << tests << " succeeded" << endl;
  22. tests = 0;
  23. successTests = 0;
  24. }
  25. // -----------------------------------------------------------------------------
  26. // checks
  27. // -----------------------------------------------------------------------------
  28. void checkEqual(int a, int b, string text) {
  29. if(a == b) {
  30. notifySuccess(text);
  31. } else {
  32. notifyFail(text + " - expected '" + std::to_string(a) + "' got '" + std::to_string(b) + "'");
  33. }
  34. }
  35. void checkEqual(string a, string b, string text) {
  36. if(a == b) {
  37. notifySuccess(text);
  38. } else {
  39. notifyFail(text + " - expected '" + a + "' got '" + b + "'");
  40. }
  41. }
  42. void checkBool(bool a, bool b, string text) {
  43. if(a == b) {
  44. notifySuccess(text);
  45. } else {
  46. notifyFail(text + " - expected '" + std::to_string(a) + "' got '" + std::to_string(b) + "'");
  47. }
  48. }
  49. void checkGreaterOrEqual(int a, int b, string text) {
  50. if(a >= b) {
  51. notifySuccess(text);
  52. } else {
  53. notifyFail(text + " - expected " + std::to_string(a) + " >= " + std::to_string(b));
  54. }
  55. }
  56. // -----------------------------------------------------------------------------
  57. // tests of command parser
  58. // -----------------------------------------------------------------------------
  59. void checkCommandParser(string rawCommand, string command, vector<string>& args, string text) {
  60. string parsedCommand;
  61. vector<string> parsedArgs;
  62. CommandUtils::splitString(rawCommand, parsedCommand, parsedArgs);
  63. checkEqual(command, parsedCommand, text);
  64. checkEqual(args.size(), parsedArgs.size(), text);
  65. for(unsigned long i = 0; i < args.size() && i < parsedArgs.size(); i++) {
  66. checkEqual(args[i], parsedArgs[i], text);
  67. }
  68. }
  69. void testCommandParser() {
  70. vector<string> args;
  71. args = {};
  72. checkCommandParser("test", "test", args, "command without arguments");
  73. args = {"aaa"};
  74. checkCommandParser("test aaa", "test", args, "command with one argument");
  75. args = {"aaa", "bbbb"};
  76. checkCommandParser("test aaa bbbb", "test", args, "command with two arguments");
  77. args = {"aaa", "bbbb", "ccccc"};
  78. checkCommandParser("test aaa bbbb ccccc", "test", args, "command with three arguments");
  79. args = {};
  80. checkCommandParser("test ", "test", args, "command with spaces");
  81. args = {"aaa", "bbbb"};
  82. checkCommandParser("test aaa bbbb", "test", args, "command with arguments and spaces");
  83. args = {"aaa", "bbbb"};
  84. checkCommandParser("test aaa bbbb", "test", args, "command with arguments and spaces");
  85. args = {"aaa bbbb"};
  86. checkCommandParser("test \"aaa bbbb\"", "test", args, "command with one argument and quotation marks");
  87. args = {"aaa bbbb", "ccc"};
  88. checkCommandParser("test \"aaa bbbb\" ccc", "test", args, "command with two arguments and quotation marks");
  89. args = {"ddd", "aaa bbbb", "ccc"};
  90. checkCommandParser("test ddd \"aaa bbbb\" ccc", "test", args, "command with tree arguments and quotation marks");
  91. args = {};
  92. checkCommandParser("test \"", "", args, "command syntax exception 1");
  93. args = {};
  94. checkCommandParser("test aaa\"", "", args, "command syntax exception 2");
  95. args = {};
  96. checkCommandParser("test aaa\"bbb\"", "", args, "command syntax exception 3");
  97. args = {};
  98. checkCommandParser("test aaa \"bbb\"ccc", "", args, "command syntax exception 4");
  99. }
  100. // -----------------------------------------------------------------------------
  101. // main
  102. // -----------------------------------------------------------------------------
  103. int main(int argc, char** argv) {
  104. testCommandParser();
  105. return 0;
  106. }