testDebuggerVariablesHelper.cxx 21 KB

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