A 2D tile-based sandbox game.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include "test.h"
  2. #include <stdlib.h>
  3. #include <string_view>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <iostream>
  7. #include <sstream>
  8. namespace testlib {
  9. static const std::string color_reset = "\033[0m";
  10. static const std::string color_highlight = "\033[1m";
  11. static const std::string color_testing = color_highlight;
  12. static const std::string color_desc = "\033[33m";
  13. static const std::string color_maybe = "\033[35m";
  14. static const std::string color_success = "\033[32m";
  15. static const std::string color_fail = "\033[31m";
  16. static const std::string color_errormsg = "\033[95m";
  17. std::string color(const std::string &color, std::string_view str) {
  18. return std::string(color) + std::string(str) + std::string(color_reset);
  19. }
  20. struct TestCase {
  21. TestCase(TestSpec *spec):
  22. func(spec->func), description(spec->description),
  23. filename(spec->filename), linenum(spec->linenum), index(spec->index) {}
  24. void (*func)();
  25. std::string_view description;
  26. std::string_view filename;
  27. int linenum;
  28. int index;
  29. };
  30. static std::vector<TestCase> cases;
  31. void addTestCase(TestSpec *testcase) {
  32. cases.emplace_back(testcase);
  33. }
  34. static std::stringstream printFailure(const std::string &msg) {
  35. std::stringstream str;
  36. str
  37. << "\r" << color(color_highlight + color_fail, "✕ ")
  38. << color(color_fail, "Failed: ") << "\n"
  39. << " " << msg << "\n";
  40. return str;
  41. }
  42. static std::stringstream printFailure(const TestFailure &failure) {
  43. std::stringstream str;
  44. str
  45. << printFailure(failure.message).str()
  46. << " at " << failure.filename << ":" << failure.linenum << "\n";
  47. return str;
  48. }
  49. }
  50. int main() {
  51. using namespace testlib;
  52. std::sort(begin(cases), end(cases), [](TestCase &a, TestCase &b) {
  53. if (a.filename != b.filename)
  54. return a.filename < b.filename;
  55. return a.index < b.index;
  56. });
  57. std::string_view currfile = "";
  58. bool failed = false;
  59. for (TestCase &testcase: cases) {
  60. if (currfile != testcase.filename) {
  61. currfile = testcase.filename;
  62. size_t lastslash = currfile.find_last_of('/');
  63. std::cout << '\n' << color(color_testing, currfile.substr(lastslash + 1)) << ":\n";
  64. }
  65. std::cout
  66. << color(color_highlight + color_maybe, "? ")
  67. << color(color_maybe, "Testing: ")
  68. << color(color_desc, testcase.description) << " " << std::flush;
  69. try {
  70. testcase.func();
  71. std::cout
  72. << "\r" << color(color_highlight + color_success, "✓ ")
  73. << color(color_success, "Success: ")
  74. << color(color_desc, testcase.description) << "\n";
  75. } catch (const TestFailure &failure) {
  76. failed = true;
  77. std::cout << printFailure(failure).str();
  78. } catch (const std::exception &ex) {
  79. failed = true;
  80. std::cout << printFailure(ex.what()).str();
  81. } catch (const std::string &str) {
  82. failed = true;
  83. std::cout << printFailure(str).str();
  84. } catch (const char *str) {
  85. failed = true;
  86. std::cout << printFailure(str).str();
  87. } catch (...) {
  88. failed = true;
  89. std::cout << printFailure("Unknown error.").str();
  90. }
  91. }
  92. std::cout << '\n';
  93. if (failed)
  94. return EXIT_FAILURE;
  95. return EXIT_SUCCESS;
  96. }