yql_issue_manager_ut.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include <library/cpp/testing/unittest/registar.h>
  2. #include "yql_issue_manager.h"
  3. using namespace NYql;
  4. static std::function<TIssuePtr()> CreateScopeIssueFunction(TString name, ui32 column, ui32 row) {
  5. return [name, column, row]() {
  6. return new TIssue(TPosition(column, row), name);
  7. };
  8. }
  9. Y_UNIT_TEST_SUITE(TIssueManagerTest) {
  10. Y_UNIT_TEST(NoErrorNoLevelTest) {
  11. TIssueManager issueManager;
  12. auto completedIssues = issueManager.GetCompletedIssues();
  13. UNIT_ASSERT_VALUES_EQUAL(completedIssues.Size(), 0);
  14. auto issues = issueManager.GetIssues();
  15. UNIT_ASSERT_VALUES_EQUAL(issues.Size(), 0);
  16. }
  17. Y_UNIT_TEST(NoErrorOneLevelTest) {
  18. TIssueManager issueManager;
  19. issueManager.AddScope(CreateScopeIssueFunction("A", 0, 0));
  20. auto completedIssues = issueManager.GetCompletedIssues();
  21. UNIT_ASSERT_VALUES_EQUAL(completedIssues.Size(), 0);
  22. issueManager.LeaveScope();
  23. auto issues = issueManager.GetIssues();
  24. UNIT_ASSERT_VALUES_EQUAL(issues.Size(), 0);
  25. }
  26. Y_UNIT_TEST(NoErrorTwoLevelsTest) {
  27. TIssueManager issueManager;
  28. issueManager.AddScope(CreateScopeIssueFunction("A", 0, 0));
  29. issueManager.AddScope(CreateScopeIssueFunction("B", 1, 1));
  30. issueManager.LeaveScope();
  31. auto completedIssues = issueManager.GetCompletedIssues();
  32. UNIT_ASSERT_VALUES_EQUAL(completedIssues.Size(), 0);
  33. issueManager.LeaveScope();
  34. auto issues = issueManager.GetIssues();
  35. UNIT_ASSERT_VALUES_EQUAL(issues.Size(), 0);
  36. }
  37. Y_UNIT_TEST(OneErrorOneLevelTest) {
  38. TIssueManager issueManager;
  39. issueManager.AddScope(CreateScopeIssueFunction("A", 0, 0));
  40. auto completedIssues1 = issueManager.GetCompletedIssues();
  41. UNIT_ASSERT_VALUES_EQUAL(completedIssues1.Size(), 0);
  42. issueManager.RaiseIssue(TIssue(TPosition(1,2), "IssueOne"));
  43. auto completedIssues2 = issueManager.GetCompletedIssues();
  44. UNIT_ASSERT_VALUES_EQUAL(completedIssues2.Size(), 0);
  45. issueManager.LeaveScope();
  46. auto completedIssues3 = issueManager.GetCompletedIssues();
  47. UNIT_ASSERT_VALUES_EQUAL(completedIssues3.Size(), 1);
  48. auto issues = issueManager.GetIssues();
  49. UNIT_ASSERT_VALUES_EQUAL(issues.Size(), 1);
  50. auto scopeIssue = issues.begin();
  51. UNIT_ASSERT_VALUES_EQUAL(scopeIssue->GetMessage(), "A");
  52. auto subIssues = scopeIssue->GetSubIssues();
  53. UNIT_ASSERT_VALUES_EQUAL(subIssues.size(), 1);
  54. UNIT_ASSERT_VALUES_EQUAL(subIssues[0]->GetMessage(), "IssueOne");
  55. }
  56. Y_UNIT_TEST(OneErrorTwoLevelsTest) {
  57. TIssueManager issueManager;
  58. issueManager.AddScope(CreateScopeIssueFunction("A", 0, 0));
  59. issueManager.AddScope(CreateScopeIssueFunction("B", 1, 1));
  60. issueManager.RaiseIssue(TIssue(TPosition(1,2), "IssueOne"));
  61. issueManager.LeaveScope();
  62. issueManager.LeaveScope();
  63. auto issues = issueManager.GetIssues();
  64. UNIT_ASSERT_VALUES_EQUAL(issues.Size(), 1);
  65. auto scopeIssue = issues.begin();
  66. UNIT_ASSERT_VALUES_EQUAL(scopeIssue->GetMessage(), "A");
  67. auto subIssues = scopeIssue->GetSubIssues();
  68. UNIT_ASSERT_VALUES_EQUAL(subIssues.size(), 1);
  69. UNIT_ASSERT_VALUES_EQUAL(subIssues[0]->GetMessage(), "B");
  70. UNIT_ASSERT_VALUES_EQUAL(subIssues[0]->GetSubIssues().size(), 1);
  71. UNIT_ASSERT_VALUES_EQUAL(subIssues[0]->GetSubIssues()[0]->GetMessage(), "IssueOne");
  72. }
  73. Y_UNIT_TEST(MultiErrorsMultiLevelsTest) {
  74. TIssueManager issueManager;
  75. issueManager.AddScope(CreateScopeIssueFunction("A", 0, 0));
  76. issueManager.RaiseIssue(TIssue(TPosition(), "WarningScope1"));
  77. issueManager.AddScope(CreateScopeIssueFunction("B", 1, 1));
  78. issueManager.AddScope(CreateScopeIssueFunction("C", 2, 2));
  79. issueManager.RaiseIssue(TIssue(TPosition(), "ErrorScope3"));
  80. issueManager.LeaveScope();
  81. issueManager.RaiseIssue(TIssue(TPosition(), "ErrorScope2"));
  82. issueManager.LeaveScope();
  83. issueManager.LeaveScope();
  84. auto issues = issueManager.GetIssues();
  85. UNIT_ASSERT_VALUES_EQUAL(issues.Size(), 1);
  86. auto scopeIssue = issues.begin();
  87. auto subIssues = scopeIssue->GetSubIssues();
  88. UNIT_ASSERT_VALUES_EQUAL(subIssues.size(), 2);
  89. UNIT_ASSERT_VALUES_EQUAL(subIssues[0]->GetSubIssues().size(), 0); //WarningScope1
  90. UNIT_ASSERT_VALUES_EQUAL(subIssues[0]->GetMessage(), "WarningScope1");
  91. UNIT_ASSERT_VALUES_EQUAL(subIssues[1]->GetSubIssues().size(), 2);
  92. UNIT_ASSERT_VALUES_EQUAL(subIssues[1]->GetSubIssues()[0]->GetSubIssues().size(), 1);
  93. UNIT_ASSERT_VALUES_EQUAL(subIssues[1]->GetSubIssues()[0]->GetSubIssues()[0]->GetMessage(), "ErrorScope3");
  94. UNIT_ASSERT_VALUES_EQUAL(subIssues[1]->GetSubIssues()[1]->GetMessage(), "ErrorScope2");
  95. auto ref = R"___(<main>: Error: A
  96. <main>: Error: WarningScope1
  97. <main>:1:1: Error: B
  98. <main>:2:2: Error: C
  99. <main>: Error: ErrorScope3
  100. <main>: Error: ErrorScope2
  101. )___";
  102. UNIT_ASSERT_VALUES_EQUAL(issues.ToString(), ref);
  103. }
  104. Y_UNIT_TEST(TIssueScopeGuardSimpleTest) {
  105. TIssueManager issueManager;
  106. {
  107. TIssueScopeGuard guard(issueManager, CreateScopeIssueFunction("A", 0, 0));
  108. issueManager.RaiseIssue(TIssue(TPosition(1,2), "ErrorScope1"));
  109. }
  110. auto issues = issueManager.GetIssues();
  111. UNIT_ASSERT_VALUES_EQUAL(issues.Size(), 1);
  112. auto scopeIssue = issues.begin();
  113. UNIT_ASSERT_VALUES_EQUAL(scopeIssue->GetMessage(), "A");
  114. auto subIssues = scopeIssue->GetSubIssues();
  115. UNIT_ASSERT_VALUES_EQUAL(subIssues.size(), 1);
  116. UNIT_ASSERT_VALUES_EQUAL(subIssues[0]->GetMessage(), "ErrorScope1");
  117. }
  118. Y_UNIT_TEST(FuseScopesTest) {
  119. TIssueManager issueManager;
  120. issueManager.AddScope(CreateScopeIssueFunction("A", 0, 0));
  121. issueManager.AddScope(CreateScopeIssueFunction("B", 0, 0));
  122. issueManager.AddScope(CreateScopeIssueFunction("C", 0, 0));
  123. issueManager.AddScope(CreateScopeIssueFunction("D", 0, 0));
  124. issueManager.RaiseIssue(TIssue(TPosition(1,2), "IssueOne"));
  125. issueManager.LeaveScope();
  126. issueManager.RaiseIssue(TIssue(TPosition(1,2), "IssueTwo"));
  127. issueManager.LeaveScope();
  128. issueManager.LeaveScope();
  129. issueManager.LeaveScope();
  130. auto issues = issueManager.GetIssues();
  131. auto ref = R"___(<main>: Error: A
  132. <main>: Error: B, C
  133. <main>: Error: D
  134. <main>:2:1: Error: IssueOne
  135. <main>:2:1: Error: IssueTwo
  136. )___";
  137. UNIT_ASSERT_VALUES_EQUAL(issues.ToString(), ref);
  138. }
  139. Y_UNIT_TEST(UniqIssues) {
  140. TIssueManager issueManager;
  141. issueManager.AddScope(CreateScopeIssueFunction("A", 0, 0));
  142. issueManager.AddScope(CreateScopeIssueFunction("B", 1, 1));
  143. issueManager.RaiseIssue(TIssue(TPosition(1,2), "IssueOne"));
  144. issueManager.RaiseIssue(TIssue(TPosition(1,2), "IssueTwo"));
  145. issueManager.RaiseIssue(TIssue(TPosition(2,3), "IssueOne"));
  146. issueManager.RaiseWarning(TIssue(TPosition(2,3), "IssueOne").SetCode(1, ESeverity::TSeverityIds_ESeverityId_S_WARNING));
  147. issueManager.LeaveScope();
  148. issueManager.LeaveScope();
  149. issueManager.RaiseIssue(TIssue(TPosition(1,2), "IssueOne"));
  150. auto issues = issueManager.GetIssues();
  151. UNIT_ASSERT_VALUES_EQUAL(issues.Size(), 1);
  152. auto ref = R"___(<main>: Error: A
  153. <main>:1:1: Error: B
  154. <main>:2:1: Error: IssueOne
  155. <main>:2:1: Error: IssueTwo
  156. <main>:3:2: Error: IssueOne
  157. <main>:3:2: Warning: IssueOne, code: 1
  158. )___";
  159. UNIT_ASSERT_VALUES_EQUAL(issues.ToString(), ref);
  160. }
  161. Y_UNIT_TEST(Limits) {
  162. TIssueManager issueManager;
  163. issueManager.SetIssueCountLimit(2);
  164. issueManager.AddScope(CreateScopeIssueFunction("A", 0, 0));
  165. issueManager.AddScope(CreateScopeIssueFunction("B", 1, 1));
  166. issueManager.RaiseIssue(TIssue(TPosition(1,1), "Issue1"));
  167. issueManager.RaiseIssue(TIssue(TPosition(1,1), "Issue2"));
  168. issueManager.RaiseIssue(TIssue(TPosition(1,1), "Issue3"));
  169. issueManager.RaiseIssue(TIssue(TPosition(1,1), "Issue4").SetCode(1, ESeverity::TSeverityIds_ESeverityId_S_WARNING));
  170. issueManager.RaiseIssue(TIssue(TPosition(1,1), "Issue5").SetCode(1, ESeverity::TSeverityIds_ESeverityId_S_WARNING));
  171. issueManager.RaiseIssue(TIssue(TPosition(1,1), "Issue6").SetCode(1, ESeverity::TSeverityIds_ESeverityId_S_WARNING));
  172. issueManager.RaiseIssue(TIssue(TPosition(1,1), "Issue7").SetCode(2, ESeverity::TSeverityIds_ESeverityId_S_INFO));
  173. issueManager.RaiseIssue(TIssue(TPosition(1,1), "Issue8").SetCode(2, ESeverity::TSeverityIds_ESeverityId_S_INFO));
  174. issueManager.LeaveScope();
  175. issueManager.LeaveScope();
  176. auto issues = issueManager.GetIssues();
  177. UNIT_ASSERT_VALUES_EQUAL(issues.Size(), 3);
  178. auto ref = R"___(<main>: Error: Too many Error issues
  179. <main>: Warning: Too many Warning issues
  180. <main>: Error: A
  181. <main>:1:1: Error: B
  182. <main>:1:1: Error: Issue1
  183. <main>:1:1: Error: Issue2
  184. <main>:1:1: Warning: Issue4, code: 1
  185. <main>:1:1: Warning: Issue5, code: 1
  186. <main>:1:1: Info: Issue7, code: 2
  187. <main>:1:1: Info: Issue8, code: 2
  188. )___";
  189. UNIT_ASSERT_VALUES_EQUAL(issues.ToString(), ref);
  190. }
  191. }