/tests/util/string.hpp

https://gitlab.com/lwiz/BasicEventEngine · C++ Header · 136 lines · 105 code · 24 blank · 7 comment · 58 complexity · 5442e0b25c09032e5af0b9662c004bf3 MD5 · raw file

  1. /*
  2. * Copyright (c) 2015-20 Louise Montalvo <louanmontalvo@gmail.com>
  3. *
  4. * This file is part of BEE.
  5. * BEE is free software and comes with ABSOLUTELY NO WARRANTY.
  6. * See LICENSE for more details.
  7. */
  8. #ifndef TESTS_UTIL_STRING_H
  9. #define TESTS_UTIL_STRING_H 1
  10. #include "doctest.h" // Include the required unit testing library
  11. #include "../../bee/util/string.hpp"
  12. #include "../../bee/init/gameoptions.hpp"
  13. TEST_SUITE_BEGIN("util");
  14. TEST_CASE("string/charcode") {
  15. CHECK(util::chr(65) == "A");
  16. Uint8 ca1[] = {65, 66, 67};
  17. CHECK(util::chra(3, ca1) == std::string("ABC"));
  18. std::vector<Uint8> ca2 = util::orda("ABC");
  19. CHECK(util::chra(ca2) == util::chra(ca2.size(), ca1));
  20. }
  21. TEST_CASE("string/vector") {
  22. std::vector<std::string> v1 = {"ab", "1.0", "\"c d\"", "test"};
  23. CHECK(util::splitv("ab 1.0 \"c d\" test", ' ', true) == v1);
  24. std::vector<std::string> v2 = {"ab", "1.0", "test"};
  25. CHECK(util::splitv("ab 1.0 test", ' ', true) == v2);
  26. std::vector<std::string> v3 = {"a", "b", "c"};
  27. CHECK(util::splitv("a,b,c", ',', true) == v3);
  28. CHECK(util::joinv(v3, ',') == "a,b,c");
  29. CHECK(util::splitv(util::joinv(v3, ','), ',', true) == v3);
  30. std::vector<std::string> v4 = {"\"a", "b\"", "c"};
  31. std::vector<std::string> vr4 = {"\"a,b\"", "c"};
  32. CHECK(util::splitv(util::joinv(v4, ','), ',', true) == vr4);
  33. std::vector<std::string> v5 = {"ab \"a;b\" \"b\"", " cd ef", " \"g;\"\\\"", " \\\"h"};
  34. CHECK(util::splitv("ab \"a;b\" \"b\"; cd ef; \"g;\"\\\"; \\\"h", ';', true) == v5);
  35. }
  36. TEST_CASE("string/manipulation") {
  37. CHECK(util::ltrim(" ABC ") == "ABC ");
  38. CHECK(util::rtrim(" ABC ") == " ABC");
  39. CHECK(util::trim(" ABC ") == "ABC");
  40. CHECK(util::trim("\tABC\t") == "ABC");
  41. CHECK(util::trim("\nABC\n") == "ABC");
  42. CHECK(util::trim(" \t\n") == "");
  43. CHECK(util::string::lower("ABC") == "abc");
  44. CHECK(util::string::upper("abc") == "ABC");
  45. CHECK(util::string::title("abc") == "Abc");
  46. CHECK(util::string::title("abc def") == "Abc Def");
  47. CHECK(util::string::letters("ABC123,./") == "ABC");
  48. CHECK(util::string::digits("ABC123,./") == "123");
  49. CHECK(util::string::lettersdigits("ABC123,./") == "ABC123");
  50. CHECK(util::string::tobool("true") == true);
  51. CHECK(util::string::tobool("false") == false);
  52. CHECK(util::string::tobool("0") == false);
  53. CHECK(util::string::tobool("1") == true);
  54. CHECK(util::string::tobool("True") == true);
  55. CHECK(util::string::tobool("False") == false);
  56. CHECK(util::string::tobool("random text") == true);
  57. CHECK(util::string::tobool("20") == true);
  58. CHECK(util::string::frombool(true) == "true");
  59. CHECK(util::string::frombool(false) == "false");
  60. CHECK(util::string::replace("a,b,c", ",", ":") == "a:b:c");
  61. CHECK(util::string::replace("a:test:b:test:c", ":test:", ",") == "a,b,c");
  62. CHECK(util::string::escape("\"Test\"/\\test") == "\\\"Test\\\"/\\\\test");
  63. CHECK(util::string::unescape("\\\"Test\\\"/\\\\test") == "\"Test\"/\\test");
  64. CHECK(util::string::unescape(util::string::escape("\"Test\"/\\test")) == "\"Test\"/\\test");
  65. CHECK(util::string::repeat(5, "a") == "aaaaa");
  66. CHECK(util::string::repeat(5, "abc") == "abcabcabcabcabc");
  67. std::vector<std::vector<std::string>> table {
  68. {"in.", "age", "instrument"},
  69. {"lam", "22", "bass"},
  70. {"esd", "21", "piano"},
  71. {"javj", "21", "guitar"}
  72. };
  73. CHECK("\n"+util::string::tabulate(table, true) == "\n\
  74. in. age instrument\n\
  75. -------------------\n\
  76. lam 22 bass \n\
  77. esd 21 piano \n\
  78. javj 21 guitar \n"
  79. );
  80. CHECK("\n"+util::string::tabulate(table) == "\n\
  81. in. age instrument\n\
  82. lam 22 bass \n\
  83. esd 21 piano \n\
  84. javj 21 guitar \n"
  85. );
  86. CHECK(util::string::is_floating("") == false);
  87. CHECK(util::string::is_floating("-3.14") == true);
  88. CHECK(util::string::is_floating("1e5") == true);
  89. CHECK(util::string::is_floating("-.e") == false);
  90. CHECK(util::string::is_floating("-.e2") == false);
  91. CHECK(util::string::is_floating("-a.e2") == false);
  92. CHECK(util::string::matches("abc", "[a-z]*") == true);
  93. CHECK(util::string::matches("abc", "...") == true);
  94. CHECK(util::string::matches("abc", ".*") == true);
  95. }
  96. TEST_CASE("string/clipboard") {
  97. if ((!bee::get_option("extensive_assert").i)||(bee::get_option("is_headless").i)) {
  98. return;
  99. }
  100. SDL_SetMainReady();
  101. REQUIRE(SDL_Init(SDL_INIT_VIDEO) == 0);
  102. std::string s (util::clipboard_get_text());
  103. REQUIRE(util::clipboard_set_text("test") == 0);
  104. REQUIRE(util::clipboard_get_text() == "test");
  105. REQUIRE(util::clipboard_has_text() == true);
  106. REQUIRE(util::clipboard_set_text("") == 0);
  107. REQUIRE(util::clipboard_has_text() == false);
  108. util::clipboard_set_text(s);
  109. SDL_Quit();
  110. }
  111. TEST_SUITE_END();
  112. #endif // TESTS_UTIL_STRING_H