综合平台编程器项目的远程存储
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

77 lines
1.8 KiB

  1. #pragma once
  2. #include "domain/project_storage.h"
  3. #include <cstddef>
  4. #include <initializer_list>
  5. #include <iostream>
  6. #include <stdexcept>
  7. #include <string>
  8. namespace TestSupport
  9. {
  10. inline void require(bool condition, const std::string &message)
  11. {
  12. if (!condition)
  13. {
  14. throw std::runtime_error(message);
  15. }
  16. }
  17. struct TestCase
  18. {
  19. const char *name;
  20. void (*run)();
  21. };
  22. // 每个用例独立记录结果,避免首个失败掩盖同一目标中的其他回归
  23. inline int runTestSuite(
  24. const char *suite_name,
  25. std::initializer_list<TestCase> test_cases)
  26. {
  27. std::size_t failed_count = 0U;
  28. for (const TestCase &test_case : test_cases)
  29. {
  30. try
  31. {
  32. test_case.run();
  33. std::cout << "[PASS] " << test_case.name << '\n';
  34. }
  35. catch (const std::exception &error)
  36. {
  37. ++failed_count;
  38. std::cerr << "[FAIL] " << test_case.name << ": "
  39. << error.what() << '\n';
  40. }
  41. catch (...)
  42. {
  43. ++failed_count;
  44. std::cerr << "[FAIL] " << test_case.name
  45. << ": unknown exception\n";
  46. }
  47. }
  48. const std::size_t passed_count = test_cases.size() - failed_count;
  49. std::cout << suite_name << ": " << passed_count << "/"
  50. << test_cases.size() << " test cases passed\n";
  51. return failed_count == 0U ? 0 : 1;
  52. }
  53. // 编辑服务测试只验证内存模型和服务契约,不应依赖真实文件系统
  54. class InMemoryProjectStorage final : public ProjectStorage
  55. {
  56. public:
  57. ProjectSaveResult save(const Project &, const std::string &) override
  58. {
  59. return {true, ProjectStorageError::None, {}};
  60. }
  61. ProjectLoadResult load(const std::string &) override
  62. {
  63. return {false, {}, ProjectStorageError::FileReadFailed, {}};
  64. }
  65. };
  66. } // namespace TestSupport