1
0

testDebuggerVariables.cxx 7.0 KB

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