testDebuggerVariables.cxx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include <cstdint>
  4. #include <functional>
  5. #include <memory>
  6. #include <string>
  7. #include <unordered_set>
  8. #include <vector>
  9. #include <cm3p/cppdap/protocol.h>
  10. #include <cm3p/cppdap/types.h>
  11. #include "cmDebuggerVariables.h"
  12. #include "cmDebuggerVariablesManager.h"
  13. #include "testCommon.h"
  14. #include "testDebugger.h"
  15. static dap::VariablesRequest CreateVariablesRequest(int64_t reference)
  16. {
  17. dap::VariablesRequest variableRequest;
  18. variableRequest.variablesReference = reference;
  19. return variableRequest;
  20. }
  21. static bool testUniqueIds()
  22. {
  23. auto variablesManager =
  24. std::make_shared<cmDebugger::cmDebuggerVariablesManager>();
  25. std::unordered_set<int64_t> variableIds;
  26. bool noDuplicateIds = true;
  27. for (int i = 0; i < 10000 && noDuplicateIds; ++i) {
  28. auto variable =
  29. cmDebugger::cmDebuggerVariables(variablesManager, "Locals", true, []() {
  30. return std::vector<cmDebugger::cmDebuggerVariableEntry>();
  31. });
  32. if (variableIds.find(variable.GetId()) != variableIds.end()) {
  33. noDuplicateIds = false;
  34. }
  35. variableIds.insert(variable.GetId());
  36. }
  37. ASSERT_TRUE(noDuplicateIds);
  38. return true;
  39. }
  40. static bool testConstructors()
  41. {
  42. auto variablesManager =
  43. std::make_shared<cmDebugger::cmDebuggerVariablesManager>();
  44. auto parent = std::make_shared<cmDebugger::cmDebuggerVariables>(
  45. variablesManager, "Parent", true, [=]() {
  46. return std::vector<cmDebugger::cmDebuggerVariableEntry>{
  47. { "ParentKey", "ParentValue", "ParentType" }
  48. };
  49. });
  50. auto children1 = std::make_shared<cmDebugger::cmDebuggerVariables>(
  51. variablesManager, "Children1", true, [=]() {
  52. return std::vector<cmDebugger::cmDebuggerVariableEntry>{
  53. { "ChildKey1", "ChildValue1", "ChildType1" },
  54. { "ChildKey2", "ChildValue2", "ChildType2" }
  55. };
  56. });
  57. parent->AddSubVariables(children1);
  58. auto children2 = std::make_shared<cmDebugger::cmDebuggerVariables>(
  59. variablesManager, "Children2", true);
  60. auto grandChildren21 = std::make_shared<cmDebugger::cmDebuggerVariables>(
  61. variablesManager, "GrandChildren21", true);
  62. grandChildren21->SetValue("GrandChildren21 Value");
  63. children2->AddSubVariables(grandChildren21);
  64. parent->AddSubVariables(children2);
  65. dap::array<dap::Variable> variables =
  66. variablesManager->HandleVariablesRequest(
  67. CreateVariablesRequest(parent->GetId()));
  68. ASSERT_TRUE(variables.size() == 3);
  69. ASSERT_VARIABLE_REFERENCE(variables[0], "Children1", "", "collection",
  70. children1->GetId());
  71. ASSERT_VARIABLE_REFERENCE(variables[1], "Children2", "", "collection",
  72. children2->GetId());
  73. ASSERT_VARIABLE(variables[2], "ParentKey", "ParentValue", "ParentType");
  74. variables = variablesManager->HandleVariablesRequest(
  75. CreateVariablesRequest(children1->GetId()));
  76. ASSERT_TRUE(variables.size() == 2);
  77. ASSERT_VARIABLE(variables[0], "ChildKey1", "ChildValue1", "ChildType1");
  78. ASSERT_VARIABLE(variables[1], "ChildKey2", "ChildValue2", "ChildType2");
  79. variables = variablesManager->HandleVariablesRequest(
  80. CreateVariablesRequest(children2->GetId()));
  81. ASSERT_TRUE(variables.size() == 1);
  82. ASSERT_VARIABLE_REFERENCE(variables[0], "GrandChildren21",
  83. "GrandChildren21 Value", "collection",
  84. grandChildren21->GetId());
  85. return true;
  86. }
  87. static bool testIgnoreEmptyStringEntries()
  88. {
  89. auto variablesManager =
  90. std::make_shared<cmDebugger::cmDebuggerVariablesManager>();
  91. auto vars = std::make_shared<cmDebugger::cmDebuggerVariables>(
  92. variablesManager, "Variables", true, []() {
  93. return std::vector<cmDebugger::cmDebuggerVariableEntry>{
  94. { "IntValue1", 5 }, { "StringValue1", "" },
  95. { "StringValue2", "foo" }, { "StringValue3", "" },
  96. { "StringValue4", "bar" }, { "StringValue5", "" },
  97. { "IntValue2", int64_t(99) }, { "BooleanTrue", true },
  98. { "BooleanFalse", false },
  99. };
  100. });
  101. vars->SetIgnoreEmptyStringEntries(true);
  102. vars->SetEnableSorting(false);
  103. dap::array<dap::Variable> variables =
  104. variablesManager->HandleVariablesRequest(
  105. CreateVariablesRequest(vars->GetId()));
  106. ASSERT_TRUE(variables.size() == 6);
  107. ASSERT_VARIABLE(variables[0], "IntValue1", "5", "int");
  108. ASSERT_VARIABLE(variables[1], "StringValue2", "foo", "string");
  109. ASSERT_VARIABLE(variables[2], "StringValue4", "bar", "string");
  110. ASSERT_VARIABLE(variables[3], "IntValue2", "99", "int");
  111. ASSERT_VARIABLE(variables[4], "BooleanTrue", "TRUE", "bool");
  112. ASSERT_VARIABLE(variables[5], "BooleanFalse", "FALSE", "bool");
  113. return true;
  114. }
  115. static bool testSortTheResult()
  116. {
  117. auto variablesManager =
  118. std::make_shared<cmDebugger::cmDebuggerVariablesManager>();
  119. auto vars = std::make_shared<cmDebugger::cmDebuggerVariables>(
  120. variablesManager, "Variables", true, []() {
  121. return std::vector<cmDebugger::cmDebuggerVariableEntry>{
  122. { "4", "4" }, { "2", "2" }, { "1", "1" }, { "3", "3" }, { "5", "5" },
  123. };
  124. });
  125. dap::array<dap::Variable> variables =
  126. variablesManager->HandleVariablesRequest(
  127. CreateVariablesRequest(vars->GetId()));
  128. ASSERT_TRUE(variables.size() == 5);
  129. ASSERT_VARIABLE(variables[0], "1", "1", "string");
  130. ASSERT_VARIABLE(variables[1], "2", "2", "string");
  131. ASSERT_VARIABLE(variables[2], "3", "3", "string");
  132. ASSERT_VARIABLE(variables[3], "4", "4", "string");
  133. ASSERT_VARIABLE(variables[4], "5", "5", "string");
  134. vars->SetEnableSorting(false);
  135. variables = variablesManager->HandleVariablesRequest(
  136. CreateVariablesRequest(vars->GetId()));
  137. ASSERT_TRUE(variables.size() == 5);
  138. ASSERT_VARIABLE(variables[0], "4", "4", "string");
  139. ASSERT_VARIABLE(variables[1], "2", "2", "string");
  140. ASSERT_VARIABLE(variables[2], "1", "1", "string");
  141. ASSERT_VARIABLE(variables[3], "3", "3", "string");
  142. ASSERT_VARIABLE(variables[4], "5", "5", "string");
  143. return true;
  144. }
  145. int testDebuggerVariables(int, char*[])
  146. {
  147. return runTests(std::vector<std::function<bool()>>{
  148. testUniqueIds,
  149. testConstructors,
  150. testIgnoreEmptyStringEntries,
  151. testSortTheResult,
  152. });
  153. }