Main.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include <iostream>
  2. #include <array>
  3. #include "common/utils/SplitString.h"
  4. #include "common/utils/List.h"
  5. typedef List<const char*, 16> StringList;
  6. const char* RED = "\033[0;31m";
  7. const char* GREEN = "\033[0;32m";
  8. uint tests = 0;
  9. uint successTests = 0;
  10. void finalizeTest() {
  11. std::cout << ((successTests == tests) ? GREEN : RED);
  12. std::cout << successTests << " / " << tests << " succeeded\n";
  13. tests = 0;
  14. successTests = 0;
  15. }
  16. template<typename T>
  17. void checkEqual(const T& wanted, const T& actual, const char* text) {
  18. if(wanted == actual) {
  19. tests++;
  20. successTests++;
  21. std::cout << GREEN << tests << ": " << text << "\n";
  22. } else {
  23. tests++;
  24. std::cout << RED << tests << ": " << text << " - ";
  25. std::cout << RED << "expected '" << wanted << "' got '" << actual << "'\n";
  26. }
  27. }
  28. void checkCommandParser(const char* rawCommand, const StringList& result, const char* text) {
  29. SplitString split(rawCommand);
  30. checkEqual(result.getLength(), split.getLength(), text);
  31. for(uint i = 0; i < result.getLength() && i < split.getLength(); i++) {
  32. checkEqual(String(result[i]), String(split[i]), text);
  33. }
  34. }
  35. void testCommandParser() {
  36. StringList list;
  37. list.clear().add("test");
  38. checkCommandParser("test", list, "command without arguments");
  39. list.clear().add("test").add("aaa");
  40. checkCommandParser("test aaa", list, "command with one argument");
  41. list.clear().add("test").add("aaa").add("bbbb");
  42. checkCommandParser("test aaa bbbb", list, "command with two arguments");
  43. list.clear().add("test").add("aaa").add("bbbb").add("ccccc");
  44. checkCommandParser("test aaa bbbb ccccc", list, "command with three arguments");
  45. list.clear().add("test");
  46. checkCommandParser("test ", list, "command with spaces");
  47. list.clear().add("test").add("aaa").add("bbbb");
  48. checkCommandParser("test aaa bbbb", list, "command with arguments and spaces");
  49. list.clear().add("test").add("aaa bbbb");
  50. checkCommandParser("test \"aaa bbbb\"", list, "command with one argument and quotation marks");
  51. list.clear().add("test").add("aaa bbbb").add("ccc");
  52. checkCommandParser("test \"aaa bbbb\" ccc", list, "command with two arguments and quotation marks");
  53. list.clear().add("test").add("ddd").add("aaa bbbb").add("ccc");
  54. checkCommandParser("test ddd \"aaa bbbb\" ccc", list, "command with tree arguments and quotation marks");
  55. list.clear().add("test").add("");
  56. checkCommandParser("test \"\"", list, "command with empty argument");
  57. list.clear();
  58. checkCommandParser("test \"", list, "command syntax exception 1");
  59. checkCommandParser("test aaa\"", list, "command syntax exception 2");
  60. checkCommandParser("test aaa\"bbb\"", list, "command syntax exception 3");
  61. checkCommandParser("test aaa \"bbb\"ccc", list, "command syntax exception 4");
  62. finalizeTest();
  63. }
  64. int main() {
  65. testCommandParser();
  66. return 0;
  67. }