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