Skip to content

Commit 998dca4

Browse files
authored
[lldb-dap] Add unit test for protocol enum types (#139848)
Add dedicated unit tests for the protocol enum types.
1 parent 93f19cb commit 998dca4

File tree

3 files changed

+212
-1
lines changed

3 files changed

+212
-1
lines changed

lldb/tools/lldb-dap/Protocol/ProtocolTypes.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ bool fromJSON(const json::Value &Params, ColumnType &CT, json::Path P) {
105105
.Case("string", eColumnTypeString)
106106
.Case("number", eColumnTypeNumber)
107107
.Case("boolean", eColumnTypeBoolean)
108-
.Case("unixTimestampUTC ", eColumnTypeTimestamp)
108+
.Case("unixTimestampUTC", eColumnTypeTimestamp)
109109
.Default(std::nullopt);
110110
if (!columnType) {
111111
P.report("unexpected value, expected 'string', 'number', 'boolean', or "
@@ -482,6 +482,18 @@ bool fromJSON(const llvm::json::Value &Params, SteppingGranularity &SG,
482482
return true;
483483
}
484484

485+
llvm::json::Value toJSON(const SteppingGranularity &SG) {
486+
switch (SG) {
487+
case eSteppingGranularityStatement:
488+
return "statement";
489+
case eSteppingGranularityLine:
490+
return "line";
491+
case eSteppingGranularityInstruction:
492+
return "instruction";
493+
}
494+
llvm_unreachable("unhandled stepping granularity.");
495+
}
496+
485497
bool fromJSON(const llvm::json::Value &Params, ValueFormat &VF,
486498
llvm::json::Path P) {
487499
json::ObjectMapper O(Params, P);

lldb/tools/lldb-dap/Protocol/ProtocolTypes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ enum SteppingGranularity : unsigned {
339339
};
340340
bool fromJSON(const llvm::json::Value &, SteppingGranularity &,
341341
llvm::json::Path);
342+
llvm::json::Value toJSON(const SteppingGranularity &);
342343

343344
/// Provides formatting information for a value.
344345
struct ValueFormat {

lldb/unittests/DAP/ProtocolTypesTest.cpp

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,201 @@ TEST(ProtocolTypesTest, Capabilities) {
291291
EXPECT_EQ(capabilities.lldbExtVersion,
292292
deserialized_capabilities->lldbExtVersion);
293293
}
294+
295+
TEST(ProtocolTypesTest, PresentationHint) {
296+
// Test all PresentationHint values.
297+
std::vector<std::pair<PresentationHint, llvm::StringRef>> test_cases = {
298+
{ePresentationHintNormal, "normal"},
299+
{ePresentationHintEmphasize, "emphasize"},
300+
{ePresentationHintDeemphasize, "deemphasize"}};
301+
302+
for (const auto &test_case : test_cases) {
303+
// Serialize the PresentationHint to JSON.
304+
llvm::json::Value serialized = toJSON(test_case.first);
305+
ASSERT_EQ(serialized.kind(), llvm::json::Value::Kind::String);
306+
EXPECT_EQ(serialized.getAsString(), test_case.second);
307+
308+
// Deserialize the JSON back to PresentationHint.
309+
PresentationHint deserialized;
310+
llvm::json::Path::Root root;
311+
ASSERT_TRUE(fromJSON(serialized, deserialized, root))
312+
<< llvm::toString(root.getError());
313+
EXPECT_EQ(deserialized, test_case.first);
314+
}
315+
316+
// Test invalid value.
317+
llvm::json::Value invalid_value = "invalid_hint";
318+
PresentationHint deserialized_invalid;
319+
llvm::json::Path::Root root;
320+
EXPECT_FALSE(fromJSON(invalid_value, deserialized_invalid, root));
321+
}
322+
323+
TEST(ProtocolTypesTest, SteppingGranularity) {
324+
// Test all SteppingGranularity values.
325+
std::vector<std::pair<SteppingGranularity, llvm::StringRef>> test_cases = {
326+
{eSteppingGranularityStatement, "statement"},
327+
{eSteppingGranularityLine, "line"},
328+
{eSteppingGranularityInstruction, "instruction"}};
329+
330+
for (const auto &test_case : test_cases) {
331+
// Serialize the SteppingGranularity to JSON.
332+
llvm::json::Value serialized = toJSON(test_case.first);
333+
ASSERT_EQ(serialized.kind(), llvm::json::Value::Kind::String);
334+
EXPECT_EQ(serialized.getAsString(), test_case.second);
335+
336+
// Deserialize the JSON back to SteppingGranularity.
337+
SteppingGranularity deserialized;
338+
llvm::json::Path::Root root;
339+
ASSERT_TRUE(fromJSON(serialized, deserialized, root))
340+
<< llvm::toString(root.getError());
341+
EXPECT_EQ(deserialized, test_case.first);
342+
}
343+
344+
// Test invalid value.
345+
llvm::json::Value invalid_value = "invalid_granularity";
346+
SteppingGranularity deserialized_invalid;
347+
llvm::json::Path::Root root;
348+
EXPECT_FALSE(fromJSON(invalid_value, deserialized_invalid, root));
349+
}
350+
351+
TEST(ProtocolTypesTest, BreakpointReason) {
352+
// Test all BreakpointReason values.
353+
std::vector<std::pair<BreakpointReason, llvm::StringRef>> test_cases = {
354+
{BreakpointReason::eBreakpointReasonPending, "pending"},
355+
{BreakpointReason::eBreakpointReasonFailed, "failed"}};
356+
357+
for (const auto &test_case : test_cases) {
358+
// Serialize the BreakpointReason to JSON.
359+
llvm::json::Value serialized = toJSON(test_case.first);
360+
ASSERT_EQ(serialized.kind(), llvm::json::Value::Kind::String);
361+
EXPECT_EQ(serialized.getAsString(), test_case.second);
362+
363+
// Deserialize the JSON back to BreakpointReason.
364+
BreakpointReason deserialized;
365+
llvm::json::Path::Root root;
366+
ASSERT_TRUE(fromJSON(serialized, deserialized, root))
367+
<< llvm::toString(root.getError());
368+
EXPECT_EQ(deserialized, test_case.first);
369+
}
370+
371+
// Test invalid value.
372+
llvm::json::Value invalid_value = "invalid_reason";
373+
BreakpointReason deserialized_invalid;
374+
llvm::json::Path::Root root;
375+
EXPECT_FALSE(fromJSON(invalid_value, deserialized_invalid, root));
376+
}
377+
378+
TEST(ProtocolTypesTest, DataBreakpointAccessType) {
379+
// Test all DataBreakpointAccessType values.
380+
std::vector<std::pair<DataBreakpointAccessType, llvm::StringRef>> test_cases =
381+
{{eDataBreakpointAccessTypeRead, "read"},
382+
{eDataBreakpointAccessTypeWrite, "write"},
383+
{eDataBreakpointAccessTypeReadWrite, "readWrite"}};
384+
385+
for (const auto &test_case : test_cases) {
386+
// Serialize the DataBreakpointAccessType to JSON.
387+
llvm::json::Value serialized = toJSON(test_case.first);
388+
ASSERT_EQ(serialized.kind(), llvm::json::Value::Kind::String);
389+
EXPECT_EQ(serialized.getAsString(), test_case.second);
390+
391+
// Deserialize the JSON back to DataBreakpointAccessType.
392+
DataBreakpointAccessType deserialized;
393+
llvm::json::Path::Root root;
394+
ASSERT_TRUE(fromJSON(serialized, deserialized, root))
395+
<< llvm::toString(root.getError());
396+
EXPECT_EQ(deserialized, test_case.first);
397+
}
398+
399+
// Test invalid value
400+
llvm::json::Value invalid_value = "invalid_access_type";
401+
DataBreakpointAccessType deserialized_invalid;
402+
llvm::json::Path::Root root;
403+
EXPECT_FALSE(fromJSON(invalid_value, deserialized_invalid, root));
404+
}
405+
406+
TEST(ProtocolTypesTest, ColumnType) {
407+
// Test all ColumnType values.
408+
std::vector<std::pair<ColumnType, llvm::StringRef>> test_cases = {
409+
{eColumnTypeString, "string"},
410+
{eColumnTypeNumber, "number"},
411+
{eColumnTypeBoolean, "boolean"},
412+
{eColumnTypeTimestamp, "unixTimestampUTC"}};
413+
414+
for (const auto &test_case : test_cases) {
415+
// Serialize the ColumnType to JSON.
416+
llvm::json::Value serialized = toJSON(test_case.first);
417+
ASSERT_EQ(serialized.kind(), llvm::json::Value::Kind::String);
418+
EXPECT_EQ(serialized.getAsString(), test_case.second);
419+
420+
// Deserialize the JSON back to ColumnType.
421+
ColumnType deserialized;
422+
llvm::json::Path::Root root;
423+
ASSERT_TRUE(fromJSON(serialized, deserialized, root))
424+
<< llvm::toString(root.getError());
425+
EXPECT_EQ(deserialized, test_case.first);
426+
}
427+
428+
// Test invalid value.
429+
llvm::json::Value invalid_value = "invalid_column_type";
430+
ColumnType deserialized_invalid;
431+
llvm::json::Path::Root root;
432+
EXPECT_FALSE(fromJSON(invalid_value, deserialized_invalid, root));
433+
}
434+
435+
TEST(ProtocolTypesTest, BreakpointModeApplicability) {
436+
// Test all BreakpointModeApplicability values.
437+
std::vector<std::pair<BreakpointModeApplicability, llvm::StringRef>>
438+
test_cases = {{eBreakpointModeApplicabilitySource, "source"},
439+
{eBreakpointModeApplicabilityException, "exception"},
440+
{eBreakpointModeApplicabilityData, "data"},
441+
{eBreakpointModeApplicabilityInstruction, "instruction"}};
442+
443+
for (const auto &test_case : test_cases) {
444+
// Serialize the BreakpointModeApplicability to JSON.
445+
llvm::json::Value serialized = toJSON(test_case.first);
446+
ASSERT_EQ(serialized.kind(), llvm::json::Value::Kind::String);
447+
EXPECT_EQ(serialized.getAsString(), test_case.second);
448+
449+
// Deserialize the JSON back to BreakpointModeApplicability.
450+
BreakpointModeApplicability deserialized;
451+
llvm::json::Path::Root root;
452+
ASSERT_TRUE(fromJSON(serialized, deserialized, root))
453+
<< llvm::toString(root.getError());
454+
EXPECT_EQ(deserialized, test_case.first);
455+
}
456+
457+
// Test invalid value.
458+
llvm::json::Value invalid_value = "invalid_applicability";
459+
BreakpointModeApplicability deserialized_invalid;
460+
llvm::json::Path::Root root;
461+
EXPECT_FALSE(fromJSON(invalid_value, deserialized_invalid, root));
462+
}
463+
464+
TEST(ProtocolTypesTest, ChecksumAlgorithm) {
465+
// Test all ChecksumAlgorithm values.
466+
std::vector<std::pair<ChecksumAlgorithm, llvm::StringRef>> test_cases = {
467+
{eChecksumAlgorithmMD5, "MD5"},
468+
{eChecksumAlgorithmSHA1, "SHA1"},
469+
{eChecksumAlgorithmSHA256, "SHA256"},
470+
{eChecksumAlgorithmTimestamp, "timestamp"}};
471+
472+
for (const auto &test_case : test_cases) {
473+
// Serialize the ChecksumAlgorithm to JSON.
474+
llvm::json::Value serialized = toJSON(test_case.first);
475+
ASSERT_EQ(serialized.kind(), llvm::json::Value::Kind::String);
476+
EXPECT_EQ(serialized.getAsString(), test_case.second);
477+
478+
// Deserialize the JSON back to ChecksumAlgorithm.
479+
ChecksumAlgorithm deserialized;
480+
llvm::json::Path::Root root;
481+
ASSERT_TRUE(fromJSON(serialized, deserialized, root))
482+
<< llvm::toString(root.getError());
483+
EXPECT_EQ(deserialized, test_case.first);
484+
}
485+
486+
// Test invalid value.
487+
llvm::json::Value invalid_value = "invalid_algorithm";
488+
ChecksumAlgorithm deserialized_invalid;
489+
llvm::json::Path::Root root;
490+
EXPECT_FALSE(fromJSON(invalid_value, deserialized_invalid, root));
491+
}

0 commit comments

Comments
 (0)