testDebuggerVariablesHelper.cxx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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 <functional>
  4. #include <memory>
  5. #include <set>
  6. #include <string>
  7. #include <utility>
  8. #include <vector>
  9. #include <cm3p/cppdap/optional.h>
  10. #include <cm3p/cppdap/protocol.h>
  11. #include <cm3p/cppdap/types.h>
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include "cmDebuggerStackFrame.h"
  15. #include "cmDebuggerVariables.h"
  16. #include "cmDebuggerVariablesHelper.h"
  17. #include "cmDebuggerVariablesManager.h"
  18. #include "cmFileSet.h"
  19. #include "cmGlobalGenerator.h"
  20. #include "cmListFileCache.h"
  21. #include "cmMakefile.h"
  22. #include "cmPolicies.h"
  23. #include "cmPropertyMap.h"
  24. #include "cmState.h"
  25. #include "cmStateDirectory.h"
  26. #include "cmStateSnapshot.h"
  27. #include "cmStateTypes.h"
  28. #include "cmTarget.h"
  29. #include "cmTest.h"
  30. #include "cmake.h"
  31. #include "testCommon.h"
  32. #include "testDebugger.h"
  33. static dap::VariablesRequest CreateVariablesRequest(int64_t reference)
  34. {
  35. dap::VariablesRequest variableRequest;
  36. variableRequest.variablesReference = reference;
  37. return variableRequest;
  38. }
  39. struct Dummies
  40. {
  41. std::shared_ptr<cmake> CMake;
  42. std::shared_ptr<cmMakefile> Makefile;
  43. std::shared_ptr<cmGlobalGenerator> GlobalGenerator;
  44. };
  45. static Dummies CreateDummies(
  46. std::string targetName,
  47. std::string currentSourceDirectory = "c:/CurrentSourceDirectory",
  48. std::string currentBinaryDirectory = "c:/CurrentBinaryDirectory")
  49. {
  50. Dummies dummies;
  51. dummies.CMake =
  52. std::make_shared<cmake>(cmake::RoleProject, cmState::Project);
  53. cmState* state = dummies.CMake->GetState();
  54. dummies.GlobalGenerator =
  55. std::make_shared<cmGlobalGenerator>(dummies.CMake.get());
  56. cmStateSnapshot snapshot = state->CreateBaseSnapshot();
  57. snapshot.GetDirectory().SetCurrentSource(currentSourceDirectory);
  58. snapshot.GetDirectory().SetCurrentBinary(currentBinaryDirectory);
  59. dummies.Makefile =
  60. std::make_shared<cmMakefile>(dummies.GlobalGenerator.get(), snapshot);
  61. dummies.Makefile->CreateNewTarget(targetName, cmStateEnums::EXECUTABLE);
  62. return dummies;
  63. }
  64. static bool testCreateFromPolicyMap()
  65. {
  66. auto variablesManager =
  67. std::make_shared<cmDebugger::cmDebuggerVariablesManager>();
  68. cmPolicies::PolicyMap policyMap;
  69. policyMap.Set(cmPolicies::CMP0000, cmPolicies::NEW);
  70. policyMap.Set(cmPolicies::CMP0003, cmPolicies::WARN);
  71. policyMap.Set(cmPolicies::CMP0005, cmPolicies::OLD);
  72. auto vars = cmDebugger::cmDebuggerVariablesHelper::Create(
  73. variablesManager, "Locals", true, policyMap);
  74. dap::array<dap::Variable> variables =
  75. variablesManager->HandleVariablesRequest(
  76. CreateVariablesRequest(vars->GetId()));
  77. ASSERT_TRUE(variables.size() == 3);
  78. ASSERT_VARIABLE(variables[0], "CMP0000", "NEW", "string");
  79. ASSERT_VARIABLE(variables[1], "CMP0003", "WARN", "string");
  80. ASSERT_VARIABLE(variables[2], "CMP0005", "OLD", "string");
  81. return true;
  82. }
  83. static bool testCreateFromPairVector()
  84. {
  85. auto variablesManager =
  86. std::make_shared<cmDebugger::cmDebuggerVariablesManager>();
  87. std::vector<std::pair<std::string, std::string>> pairs = {
  88. { "Foo1", "Bar1" }, { "Foo2", "Bar2" }
  89. };
  90. auto vars = cmDebugger::cmDebuggerVariablesHelper::CreateIfAny(
  91. variablesManager, "Locals", true, pairs);
  92. dap::array<dap::Variable> variables =
  93. variablesManager->HandleVariablesRequest(
  94. CreateVariablesRequest(vars->GetId()));
  95. ASSERT_TRUE(vars->GetValue() == std::to_string(pairs.size()));
  96. ASSERT_TRUE(variables.size() == 2);
  97. ASSERT_VARIABLE(variables[0], "Foo1", "Bar1", "string");
  98. ASSERT_VARIABLE(variables[1], "Foo2", "Bar2", "string");
  99. auto none = cmDebugger::cmDebuggerVariablesHelper::CreateIfAny(
  100. variablesManager, "Locals", true,
  101. std::vector<std::pair<std::string, std::string>>());
  102. ASSERT_TRUE(none == nullptr);
  103. return true;
  104. }
  105. static bool testCreateFromSet()
  106. {
  107. auto variablesManager =
  108. std::make_shared<cmDebugger::cmDebuggerVariablesManager>();
  109. std::set<std::string> set = { "Foo", "Bar" };
  110. auto vars = cmDebugger::cmDebuggerVariablesHelper::CreateIfAny(
  111. variablesManager, "Locals", true, set);
  112. dap::array<dap::Variable> variables =
  113. variablesManager->HandleVariablesRequest(
  114. CreateVariablesRequest(vars->GetId()));
  115. ASSERT_TRUE(vars->GetValue() == std::to_string(set.size()));
  116. ASSERT_TRUE(variables.size() == 2);
  117. ASSERT_VARIABLE(variables[0], "[0]", "Bar", "string");
  118. ASSERT_VARIABLE(variables[1], "[1]", "Foo", "string");
  119. auto none = cmDebugger::cmDebuggerVariablesHelper::CreateIfAny(
  120. variablesManager, "Locals", true, std::set<std::string>());
  121. ASSERT_TRUE(none == nullptr);
  122. return true;
  123. }
  124. static bool testCreateFromStringVector()
  125. {
  126. auto variablesManager =
  127. std::make_shared<cmDebugger::cmDebuggerVariablesManager>();
  128. std::vector<std::string> list = { "Foo", "Bar" };
  129. auto vars = cmDebugger::cmDebuggerVariablesHelper::CreateIfAny(
  130. variablesManager, "Locals", true, list);
  131. dap::array<dap::Variable> variables =
  132. variablesManager->HandleVariablesRequest(
  133. CreateVariablesRequest(vars->GetId()));
  134. ASSERT_TRUE(vars->GetValue() == std::to_string(list.size()));
  135. ASSERT_TRUE(variables.size() == 2);
  136. ASSERT_VARIABLE(variables[0], "[0]", "Foo", "string");
  137. ASSERT_VARIABLE(variables[1], "[1]", "Bar", "string");
  138. auto none = cmDebugger::cmDebuggerVariablesHelper::CreateIfAny(
  139. variablesManager, "Locals", true, std::vector<std::string>());
  140. ASSERT_TRUE(none == nullptr);
  141. return true;
  142. }
  143. static bool testCreateFromTarget()
  144. {
  145. auto variablesManager =
  146. std::make_shared<cmDebugger::cmDebuggerVariablesManager>();
  147. auto dummies = CreateDummies("Foo");
  148. auto vars = cmDebugger::cmDebuggerVariablesHelper::CreateIfAny(
  149. variablesManager, "Locals", true, dummies.Makefile->GetOrderedTargets());
  150. dap::array<dap::Variable> variables =
  151. variablesManager->HandleVariablesRequest(
  152. CreateVariablesRequest(vars->GetId()));
  153. ASSERT_TRUE(variables.size() == 1);
  154. ASSERT_VARIABLE(variables[0], "Foo", "EXECUTABLE", "collection");
  155. variables = variablesManager->HandleVariablesRequest(
  156. CreateVariablesRequest(variables[0].variablesReference));
  157. ASSERT_TRUE(variables.size() == 15);
  158. ASSERT_VARIABLE(variables[0], "GlobalGenerator", "Generic", "collection");
  159. ASSERT_VARIABLE(variables[1], "IsAIX", "FALSE", "bool");
  160. ASSERT_VARIABLE(variables[2], "IsAndroidGuiExecutable", "FALSE", "bool");
  161. ASSERT_VARIABLE(variables[3], "IsAppBundleOnApple", "FALSE", "bool");
  162. ASSERT_VARIABLE(variables[4], "IsDLLPlatform", "FALSE", "bool");
  163. ASSERT_VARIABLE(variables[5], "IsExecutableWithExports", "FALSE", "bool");
  164. ASSERT_VARIABLE(variables[6], "IsFrameworkOnApple", "FALSE", "bool");
  165. ASSERT_VARIABLE(variables[7], "IsImported", "FALSE", "bool");
  166. ASSERT_VARIABLE(variables[8], "IsImportedGloballyVisible", "FALSE", "bool");
  167. ASSERT_VARIABLE(variables[9], "IsPerConfig", "TRUE", "bool");
  168. ASSERT_VARIABLE(variables[10], "Makefile",
  169. dummies.Makefile->GetDirectoryId().String, "collection");
  170. ASSERT_VARIABLE(variables[11], "Name", "Foo", "string");
  171. ASSERT_VARIABLE(variables[12], "PolicyMap", "", "collection");
  172. ASSERT_VARIABLE(variables[13], "Properties",
  173. std::to_string(dummies.Makefile->GetOrderedTargets()[0]
  174. ->GetProperties()
  175. .GetList()
  176. .size()),
  177. "collection");
  178. ASSERT_VARIABLE(variables[14], "Type", "EXECUTABLE", "string");
  179. auto none = cmDebugger::cmDebuggerVariablesHelper::CreateIfAny(
  180. variablesManager, "Locals", true, std::vector<cmTarget*>());
  181. ASSERT_TRUE(none == nullptr);
  182. return true;
  183. }
  184. static bool testCreateFromGlobalGenerator()
  185. {
  186. auto variablesManager =
  187. std::make_shared<cmDebugger::cmDebuggerVariablesManager>();
  188. auto dummies = CreateDummies("Foo");
  189. auto vars = cmDebugger::cmDebuggerVariablesHelper::CreateIfAny(
  190. variablesManager, "Locals", true, dummies.GlobalGenerator.get());
  191. dap::array<dap::Variable> variables =
  192. variablesManager->HandleVariablesRequest(
  193. CreateVariablesRequest(vars->GetId()));
  194. ASSERT_TRUE(variables.size() == 10);
  195. ASSERT_VARIABLE(variables[0], "AllTargetName", "ALL_BUILD", "string");
  196. ASSERT_VARIABLE(variables[1], "ForceUnixPaths", "FALSE", "bool");
  197. ASSERT_VARIABLE(variables[2], "InstallTargetName", "INSTALL", "string");
  198. ASSERT_VARIABLE(variables[3], "IsMultiConfig", "FALSE", "bool");
  199. ASSERT_VARIABLE(variables[4], "MakefileEncoding", "None", "string");
  200. ASSERT_VARIABLE(variables[5], "Name", "Generic", "string");
  201. ASSERT_VARIABLE(variables[6], "NeedSymbolicMark", "FALSE", "bool");
  202. ASSERT_VARIABLE(variables[7], "PackageTargetName", "PACKAGE", "string");
  203. ASSERT_VARIABLE(variables[8], "TestTargetName", "RUN_TESTS", "string");
  204. ASSERT_VARIABLE(variables[9], "UseLinkScript", "FALSE", "bool");
  205. auto none = cmDebugger::cmDebuggerVariablesHelper::CreateIfAny(
  206. variablesManager, "Locals", true,
  207. static_cast<cmGlobalGenerator*>(nullptr));
  208. ASSERT_TRUE(none == nullptr);
  209. return true;
  210. }
  211. static bool testCreateFromTests()
  212. {
  213. auto variablesManager =
  214. std::make_shared<cmDebugger::cmDebuggerVariablesManager>();
  215. auto dummies = CreateDummies("Foo");
  216. cmTest test1 = cmTest(dummies.Makefile.get());
  217. test1.SetName("Test1");
  218. test1.SetOldStyle(false);
  219. test1.SetCommandExpandLists(true);
  220. test1.SetCommand(std::vector<std::string>{ "Foo1", "arg1" });
  221. test1.SetProperty("Prop1", "Prop1");
  222. cmTest test2 = cmTest(dummies.Makefile.get());
  223. test2.SetName("Test2");
  224. test2.SetOldStyle(false);
  225. test2.SetCommandExpandLists(false);
  226. test2.SetCommand(std::vector<std::string>{ "Bar1", "arg1", "arg2" });
  227. test2.SetProperty("Prop2", "Prop2");
  228. test2.SetProperty("Prop3", "Prop3");
  229. std::vector<cmTest*> tests = { &test1, &test2 };
  230. auto vars = cmDebugger::cmDebuggerVariablesHelper::CreateIfAny(
  231. variablesManager, "Locals", true, tests);
  232. dap::array<dap::Variable> variables =
  233. variablesManager->HandleVariablesRequest(
  234. CreateVariablesRequest(vars->GetId()));
  235. ASSERT_TRUE(vars->GetValue() == std::to_string(tests.size()));
  236. ASSERT_TRUE(variables.size() == 2);
  237. ASSERT_VARIABLE_REFERENCE_NOT_ZERO(variables[0], test1.GetName(), "",
  238. "collection");
  239. ASSERT_VARIABLE_REFERENCE_NOT_ZERO(variables[1], test2.GetName(), "",
  240. "collection");
  241. dap::array<dap::Variable> testVariables =
  242. variablesManager->HandleVariablesRequest(
  243. CreateVariablesRequest(variables[0].variablesReference));
  244. ASSERT_TRUE(testVariables.size() == 5);
  245. ASSERT_VARIABLE_REFERENCE_NOT_ZERO(testVariables[0], "Command",
  246. std::to_string(test1.GetCommand().size()),
  247. "collection");
  248. ASSERT_VARIABLE(testVariables[1], "CommandExpandLists",
  249. BOOL_STRING(test1.GetCommandExpandLists()), "bool");
  250. ASSERT_VARIABLE(testVariables[2], "Name", test1.GetName(), "string");
  251. ASSERT_VARIABLE(testVariables[3], "OldStyle",
  252. BOOL_STRING(test1.GetOldStyle()), "bool");
  253. ASSERT_VARIABLE_REFERENCE_NOT_ZERO(testVariables[4], "Properties", "1",
  254. "collection");
  255. dap::array<dap::Variable> commandVariables =
  256. variablesManager->HandleVariablesRequest(
  257. CreateVariablesRequest(testVariables[0].variablesReference));
  258. ASSERT_TRUE(commandVariables.size() == test1.GetCommand().size());
  259. for (size_t i = 0; i < commandVariables.size(); ++i) {
  260. ASSERT_VARIABLE(commandVariables[i], "[" + std::to_string(i) + "]",
  261. test1.GetCommand()[i], "string");
  262. }
  263. dap::array<dap::Variable> propertiesVariables =
  264. variablesManager->HandleVariablesRequest(
  265. CreateVariablesRequest(testVariables[4].variablesReference));
  266. ASSERT_TRUE(propertiesVariables.size() == 1);
  267. ASSERT_VARIABLE(propertiesVariables[0], "Prop1", "Prop1", "string");
  268. testVariables = variablesManager->HandleVariablesRequest(
  269. CreateVariablesRequest(variables[1].variablesReference));
  270. ASSERT_TRUE(testVariables.size() == 5);
  271. ASSERT_VARIABLE_REFERENCE_NOT_ZERO(testVariables[0], "Command",
  272. std::to_string(test2.GetCommand().size()),
  273. "collection");
  274. ASSERT_VARIABLE(testVariables[1], "CommandExpandLists",
  275. BOOL_STRING(test2.GetCommandExpandLists()), "bool");
  276. ASSERT_VARIABLE(testVariables[2], "Name", test2.GetName(), "string");
  277. ASSERT_VARIABLE(testVariables[3], "OldStyle",
  278. BOOL_STRING(test2.GetOldStyle()), "bool");
  279. ASSERT_VARIABLE_REFERENCE_NOT_ZERO(testVariables[4], "Properties", "2",
  280. "collection");
  281. commandVariables = variablesManager->HandleVariablesRequest(
  282. CreateVariablesRequest(testVariables[0].variablesReference));
  283. ASSERT_TRUE(commandVariables.size() == test2.GetCommand().size());
  284. for (size_t i = 0; i < commandVariables.size(); ++i) {
  285. ASSERT_VARIABLE(commandVariables[i], "[" + std::to_string(i) + "]",
  286. test2.GetCommand()[i], "string");
  287. }
  288. propertiesVariables = variablesManager->HandleVariablesRequest(
  289. CreateVariablesRequest(testVariables[4].variablesReference));
  290. ASSERT_TRUE(propertiesVariables.size() == 2);
  291. ASSERT_VARIABLE(propertiesVariables[0], "Prop2", "Prop2", "string");
  292. ASSERT_VARIABLE(propertiesVariables[1], "Prop3", "Prop3", "string");
  293. auto none = cmDebugger::cmDebuggerVariablesHelper::CreateIfAny(
  294. variablesManager, "Locals", true, std::vector<cmTest*>());
  295. ASSERT_TRUE(none == nullptr);
  296. return true;
  297. }
  298. static bool testCreateFromMakefile()
  299. {
  300. auto variablesManager =
  301. std::make_shared<cmDebugger::cmDebuggerVariablesManager>();
  302. auto dummies = CreateDummies("Foo");
  303. auto snapshot = dummies.Makefile->GetStateSnapshot();
  304. auto state = dummies.Makefile->GetState();
  305. state->SetSourceDirectory("c:/HomeDirectory");
  306. state->SetBinaryDirectory("c:/HomeOutputDirectory");
  307. auto vars = cmDebugger::cmDebuggerVariablesHelper::CreateIfAny(
  308. variablesManager, "Locals", true, dummies.Makefile.get());
  309. dap::array<dap::Variable> variables =
  310. variablesManager->HandleVariablesRequest(
  311. CreateVariablesRequest(vars->GetId()));
  312. ASSERT_TRUE(variables.size() == 12);
  313. ASSERT_VARIABLE(variables[0], "AppleSDKType", "MacOS", "string");
  314. ASSERT_VARIABLE(variables[1], "CurrentBinaryDirectory",
  315. snapshot.GetDirectory().GetCurrentBinary(), "string");
  316. ASSERT_VARIABLE(variables[2], "CurrentSourceDirectory",
  317. snapshot.GetDirectory().GetCurrentSource(), "string");
  318. ASSERT_VARIABLE(variables[3], "DefineFlags", " ", "string");
  319. ASSERT_VARIABLE(variables[4], "DirectoryId",
  320. dummies.Makefile->GetDirectoryId().String, "string");
  321. ASSERT_VARIABLE(variables[5], "HomeDirectory", state->GetSourceDirectory(),
  322. "string");
  323. ASSERT_VARIABLE(variables[6], "HomeOutputDirectory",
  324. state->GetBinaryDirectory(), "string");
  325. ASSERT_VARIABLE(variables[7], "IsRootMakefile", "TRUE", "bool");
  326. ASSERT_VARIABLE(variables[8], "PlatformIs32Bit", "FALSE", "bool");
  327. ASSERT_VARIABLE(variables[9], "PlatformIs64Bit", "FALSE", "bool");
  328. ASSERT_VARIABLE(variables[10], "PlatformIsAppleEmbedded", "FALSE", "bool");
  329. ASSERT_VARIABLE(variables[11], "PlatformIsx32", "FALSE", "bool");
  330. auto none = cmDebugger::cmDebuggerVariablesHelper::CreateIfAny(
  331. variablesManager, "Locals", true, static_cast<cmMakefile*>(nullptr));
  332. ASSERT_TRUE(none == nullptr);
  333. return true;
  334. }
  335. static bool testCreateFromStackFrame()
  336. {
  337. auto variablesManager =
  338. std::make_shared<cmDebugger::cmDebuggerVariablesManager>();
  339. auto dummies = CreateDummies("Foo");
  340. cmListFileFunction lff = cmListFileFunction("set", 99, 99, {});
  341. auto frame = std::make_shared<cmDebugger::cmDebuggerStackFrame>(
  342. dummies.Makefile.get(), "c:/CMakeLists.txt", lff);
  343. dummies.CMake->AddCacheEntry("CMAKE_BUILD_TYPE", "Debug", "Build Type",
  344. cmStateEnums::CacheEntryType::STRING);
  345. auto locals = cmDebugger::cmDebuggerVariablesHelper::Create(
  346. variablesManager, "Locals", true, frame);
  347. dap::array<dap::Variable> variables =
  348. variablesManager->HandleVariablesRequest(
  349. CreateVariablesRequest(locals->GetId()));
  350. ASSERT_TRUE(variables.size() == 5);
  351. ASSERT_VARIABLE(variables[0], "CacheVariables", "1", "collection");
  352. ASSERT_VARIABLE(variables[1], "CurrentLine", std::to_string(lff.Line()),
  353. "int");
  354. ASSERT_VARIABLE(variables[2], "Directories", "2", "collection");
  355. ASSERT_VARIABLE(variables[3], "Locals", "2", "collection");
  356. ASSERT_VARIABLE(variables[4], "Targets", "1", "collection");
  357. dap::array<dap::Variable> cacheVariables =
  358. variablesManager->HandleVariablesRequest(
  359. CreateVariablesRequest(variables[0].variablesReference));
  360. ASSERT_TRUE(cacheVariables.size() == 1);
  361. ASSERT_VARIABLE(cacheVariables[0], "CMAKE_BUILD_TYPE:STRING", "Debug",
  362. "collection");
  363. dap::array<dap::Variable> directoriesVariables =
  364. variablesManager->HandleVariablesRequest(
  365. CreateVariablesRequest(variables[2].variablesReference));
  366. ASSERT_TRUE(directoriesVariables.size() == 2);
  367. ASSERT_VARIABLE(
  368. directoriesVariables[0], "CMAKE_CURRENT_BINARY_DIR",
  369. dummies.Makefile->GetStateSnapshot().GetDirectory().GetCurrentBinary(),
  370. "string");
  371. ASSERT_VARIABLE(
  372. directoriesVariables[1], "CMAKE_CURRENT_SOURCE_DIR",
  373. dummies.Makefile->GetStateSnapshot().GetDirectory().GetCurrentSource(),
  374. "string");
  375. dap::array<dap::Variable> propertiesVariables =
  376. variablesManager->HandleVariablesRequest(
  377. CreateVariablesRequest(cacheVariables[0].variablesReference));
  378. ASSERT_TRUE(propertiesVariables.size() == 3);
  379. ASSERT_VARIABLE(propertiesVariables[0], "HELPSTRING", "Build Type",
  380. "string");
  381. ASSERT_VARIABLE(propertiesVariables[1], "TYPE", "STRING", "string");
  382. ASSERT_VARIABLE(propertiesVariables[2], "VALUE", "Debug", "string");
  383. return true;
  384. }
  385. static bool testCreateFromBTStringVector()
  386. {
  387. auto variablesManager =
  388. std::make_shared<cmDebugger::cmDebuggerVariablesManager>();
  389. std::vector<BT<std::string>> list(2);
  390. list[0].Value = "Foo";
  391. list[1].Value = "Bar";
  392. auto vars = cmDebugger::cmDebuggerVariablesHelper::CreateIfAny(
  393. variablesManager, "Locals", true, list);
  394. dap::array<dap::Variable> variables =
  395. variablesManager->HandleVariablesRequest(
  396. CreateVariablesRequest(vars->GetId()));
  397. ASSERT_TRUE(vars->GetValue() == std::to_string(list.size()));
  398. ASSERT_TRUE(variables.size() == 2);
  399. ASSERT_VARIABLE(variables[0], "[0]", "Foo", "string");
  400. ASSERT_VARIABLE(variables[1], "[1]", "Bar", "string");
  401. auto none = cmDebugger::cmDebuggerVariablesHelper::CreateIfAny(
  402. variablesManager, "Locals", true, std::vector<std::string>());
  403. ASSERT_TRUE(none == nullptr);
  404. return true;
  405. }
  406. static bool testCreateFromFileSet()
  407. {
  408. auto variablesManager =
  409. std::make_shared<cmDebugger::cmDebuggerVariablesManager>();
  410. cmake cm(cmake::RoleScript, cmState::Unknown);
  411. cmFileSet fileSet(cm, "Foo", "HEADERS", cmFileSetVisibility::Public);
  412. BT<std::string> directory;
  413. directory.Value = "c:/";
  414. fileSet.AddDirectoryEntry(directory);
  415. BT<std::string> file;
  416. file.Value = "c:/foo.cxx";
  417. fileSet.AddFileEntry(file);
  418. auto vars = cmDebugger::cmDebuggerVariablesHelper::CreateIfAny(
  419. variablesManager, "Locals", true, &fileSet);
  420. dap::array<dap::Variable> variables =
  421. variablesManager->HandleVariablesRequest(
  422. CreateVariablesRequest(vars->GetId()));
  423. ASSERT_TRUE(variables.size() == 5);
  424. ASSERT_VARIABLE_REFERENCE_NOT_ZERO(variables[0], "Directories", "1",
  425. "collection");
  426. ASSERT_VARIABLE_REFERENCE_NOT_ZERO(variables[1], "Files", "1", "collection");
  427. ASSERT_VARIABLE(variables[2], "Name", "Foo", "string");
  428. ASSERT_VARIABLE(variables[3], "Type", "HEADERS", "string");
  429. ASSERT_VARIABLE(variables[4], "Visibility", "Public", "string");
  430. dap::array<dap::Variable> directoriesVariables =
  431. variablesManager->HandleVariablesRequest(
  432. CreateVariablesRequest(variables[0].variablesReference));
  433. ASSERT_TRUE(directoriesVariables.size() == 1);
  434. ASSERT_VARIABLE(directoriesVariables[0], "[0]", directory.Value, "string");
  435. dap::array<dap::Variable> filesVariables =
  436. variablesManager->HandleVariablesRequest(
  437. CreateVariablesRequest(variables[1].variablesReference));
  438. ASSERT_TRUE(filesVariables.size() == 1);
  439. ASSERT_VARIABLE(filesVariables[0], "[0]", file.Value, "string");
  440. return true;
  441. }
  442. static bool testCreateFromFileSets()
  443. {
  444. auto variablesManager =
  445. std::make_shared<cmDebugger::cmDebuggerVariablesManager>();
  446. cmake cm(cmake::RoleScript, cmState::Unknown);
  447. cmFileSet fileSet(cm, "Foo", "HEADERS", cmFileSetVisibility::Public);
  448. BT<std::string> directory;
  449. directory.Value = "c:/";
  450. fileSet.AddDirectoryEntry(directory);
  451. BT<std::string> file;
  452. file.Value = "c:/foo.cxx";
  453. fileSet.AddFileEntry(file);
  454. auto fileSets = std::vector<cmFileSet*>{ &fileSet };
  455. auto vars = cmDebugger::cmDebuggerVariablesHelper::CreateIfAny(
  456. variablesManager, "Locals", true, fileSets);
  457. dap::array<dap::Variable> variables =
  458. variablesManager->HandleVariablesRequest(
  459. CreateVariablesRequest(vars->GetId()));
  460. ASSERT_TRUE(variables.size() == 1);
  461. ASSERT_VARIABLE_REFERENCE_NOT_ZERO(variables[0], "Foo", "", "collection");
  462. return true;
  463. }
  464. int testDebuggerVariablesHelper(int, char*[])
  465. {
  466. return runTests(std::vector<std::function<bool()>>{
  467. testCreateFromPolicyMap,
  468. testCreateFromPairVector,
  469. testCreateFromSet,
  470. testCreateFromStringVector,
  471. testCreateFromTarget,
  472. testCreateFromGlobalGenerator,
  473. testCreateFromMakefile,
  474. testCreateFromStackFrame,
  475. testCreateFromTests,
  476. testCreateFromBTStringVector,
  477. testCreateFromFileSet,
  478. testCreateFromFileSets,
  479. });
  480. }