综合平台编程器项目的远程存储
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

152 行
6.0 KiB

  1. #include "domain/project_storage.h"
  2. #include "services/logic_editor_service.h"
  3. #include "services/project_service.h"
  4. #include <iostream>
  5. #include <stdexcept>
  6. namespace {
  7. class TestProjectStorage final : public ProjectStorage
  8. {
  9. public:
  10. ProjectSaveResult save(const Project &, const std::string &) override
  11. {
  12. return {true, ProjectStorageError::None, {}};
  13. }
  14. ProjectLoadResult load(const std::string &) override
  15. {
  16. return {false, {}, ProjectStorageError::FileReadFailed, {}};
  17. }
  18. };
  19. void require(bool condition, const std::string &message)
  20. {
  21. if (!condition)
  22. {
  23. throw std::runtime_error(message);
  24. }
  25. }
  26. ContactNodeConfig contact(int address)
  27. {
  28. return {RegisterAddress{RegisterArea::M, address}, ContactMode::NormallyOpen};
  29. }
  30. void testStructuredEditingAndNormalization()
  31. {
  32. TestProjectStorage storage;
  33. ProjectService project_service(storage);
  34. LogicEditorService service(project_service);
  35. const std::string logic_id = service.ensureDefaultLogic().id;
  36. const std::string rung_id = service.firstRungId(logic_id);
  37. const LogicEditorResult first = service.appendCondition(logic_id, rung_id, contact(0));
  38. const LogicEditorResult second = service.appendCondition(logic_id, rung_id, contact(1));
  39. require(first.succeeded && second.succeeded, "series append must succeed");
  40. const LadderRung *rung = service.findRung(logic_id, rung_id);
  41. require(rung->condition->kind == ConditionExpressionKind::Series
  42. && rung->condition->children.size() == 2U,
  43. "two appended nodes must form a series expression");
  44. const std::string second_expression_id = second.id;
  45. const LogicEditorResult parallel = service.addParallelBranch(
  46. logic_id, rung_id, {second_expression_id}, contact(2));
  47. require(parallel.succeeded, "parallel insertion must succeed");
  48. rung = service.findRung(logic_id, rung_id);
  49. const ConditionExpression *parallel_expression = service.findExpression(
  50. logic_id, rung_id, rung->condition->children.at(1).id);
  51. require(parallel_expression != nullptr
  52. && parallel_expression->kind == ConditionExpressionKind::Parallel,
  53. "selected node must become a parallel expression");
  54. const LogicEditorResult nested_series = service.insertConditionAfter(
  55. logic_id,
  56. rung_id,
  57. parallel.id,
  58. contact(3));
  59. require(nested_series.succeeded, "a parallel branch must accept a series node");
  60. rung = service.findRung(logic_id, rung_id);
  61. require(rung->condition->kind == ConditionExpressionKind::Series,
  62. "root must remain a series expression");
  63. const ConditionExpression &nested_parallel_expression = rung->condition->children.at(1);
  64. require(nested_parallel_expression.kind == ConditionExpressionKind::Parallel
  65. && nested_parallel_expression.children.at(1).kind
  66. == ConditionExpressionKind::Series,
  67. "editor must express A AND (B OR (C AND D))");
  68. require(service.removeNode(logic_id, nested_series.id).succeeded,
  69. "nested series node deletion must succeed");
  70. rung = service.findRung(logic_id, rung_id);
  71. require(rung->condition->children.at(1).kind == ConditionExpressionKind::Parallel
  72. && rung->condition->children.at(1).children.at(1).kind
  73. == ConditionExpressionKind::Node,
  74. "single-child series container must collapse after deletion");
  75. require(service.removeNode(logic_id, parallel.id).succeeded,
  76. "parallel leaf deletion must succeed");
  77. rung = service.findRung(logic_id, rung_id);
  78. require(rung->condition->kind == ConditionExpressionKind::Series
  79. && rung->condition->children.size() == 2U,
  80. "single-child parallel container must collapse after deletion");
  81. require(service.setOutput(
  82. logic_id,
  83. rung_id,
  84. CoilNodeConfig{RegisterAddress{RegisterArea::M, 10}, CoilMode::Normal})
  85. .succeeded,
  86. "output coil must be set");
  87. require(service.findLogic(logic_id)->validate(),
  88. "structured editing result must remain a valid draft");
  89. }
  90. void testRangeParallelInsertion()
  91. {
  92. TestProjectStorage storage;
  93. ProjectService project_service(storage);
  94. LogicEditorService service(project_service);
  95. const std::string logic_id = service.ensureDefaultLogic().id;
  96. const std::string rung_id = service.firstRungId(logic_id);
  97. service.appendCondition(logic_id, rung_id, contact(0));
  98. service.appendCondition(logic_id, rung_id, contact(1));
  99. service.appendCondition(logic_id, rung_id, contact(2));
  100. const LogicEditorResult branch = service.addParallelBranch(
  101. logic_id, rung_id, {"contact-2", "contact-3"}, contact(3));
  102. require(branch.succeeded, "a continuous series range must accept a parallel branch");
  103. const ConditionExpression &root = *service.findRung(logic_id, rung_id)->condition;
  104. require(root.kind == ConditionExpressionKind::Series
  105. && root.children.size() == 2U
  106. && root.children.at(1).kind == ConditionExpressionKind::Parallel
  107. && root.children.at(1).children.front().kind
  108. == ConditionExpressionKind::Series,
  109. "range insertion must express A AND ((B AND C) OR D)");
  110. require(root.validate(), "range insertion must preserve normalized topology");
  111. const LogicEditorResult invalid = service.addParallelBranch(
  112. logic_id, rung_id, {"contact-1", "contact-3"}, contact(4));
  113. require(!invalid.succeeded
  114. && invalid.error == LogicEditorError::InvalidOperation,
  115. "a non-contiguous selection must be rejected");
  116. }
  117. } // namespace
  118. int main()
  119. {
  120. try
  121. {
  122. testStructuredEditingAndNormalization();
  123. testRangeParallelInsertion();
  124. }
  125. catch (const std::exception &error)
  126. {
  127. std::cerr << "logic editor service tests failed: " << error.what() << '\n';
  128. return 1;
  129. }
  130. std::cout << "logic editor service tests passed\n";
  131. return 0;
  132. }