Skip to content

Commit 8757aa0

Browse files
committed
[lldb-dap] Add unit tests for GetInteger
1 parent 5b97a5b commit 8757aa0

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

lldb/unittests/DAP/JSONUtilsTest.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
#include "JSONUtils.h"
1010
#include "lldb/API/SBModule.h"
1111
#include "lldb/API/SBTarget.h"
12+
#include "llvm/Support/JSON.h"
1213
#include "gtest/gtest.h"
14+
#include <optional>
1315

1416
using namespace llvm;
1517
using namespace lldb;
@@ -84,6 +86,59 @@ TEST(JSONUtilsTest, GetBoolean_Pointer) {
8486
EXPECT_FALSE(result.has_value());
8587
}
8688

89+
TEST(JSONUtilsTest, GetInteger_Ref) {
90+
json::Object obj;
91+
obj.try_emplace("key", 123);
92+
93+
auto result = GetInteger<int>(obj, "key");
94+
ASSERT_TRUE(result.has_value());
95+
EXPECT_EQ(result.value(), 123);
96+
97+
result = GetInteger<int>(obj, "nonexistent_key");
98+
EXPECT_FALSE(result.has_value());
99+
100+
obj.try_emplace("key_float", 123.45);
101+
result = GetInteger<int>(obj, "key_float");
102+
EXPECT_FALSE(result.has_value());
103+
104+
obj.try_emplace("key_string", "123");
105+
result = GetInteger<int>(obj, "key_string");
106+
EXPECT_FALSE(result.has_value());
107+
}
108+
109+
TEST(JSONUtilsTest, GetInteger_Pointer) {
110+
json::Object obj;
111+
obj.try_emplace("key", 456);
112+
113+
auto result = GetInteger<int>(&obj, "key");
114+
ASSERT_TRUE(result.has_value());
115+
EXPECT_EQ(result.value(), 456);
116+
117+
result = GetInteger<int>(nullptr, "key");
118+
EXPECT_FALSE(result.has_value());
119+
120+
obj.try_emplace("key_invalid", "not_an_integer");
121+
result = GetInteger<int>(&obj, "key_invalid");
122+
EXPECT_FALSE(result.has_value());
123+
}
124+
125+
TEST(JSONUtilsTest, GetInteger_DifferentTypes) {
126+
json::Object obj;
127+
obj.try_emplace("key", 789);
128+
129+
auto result = GetInteger<int64_t>(obj, "key");
130+
ASSERT_TRUE(result.has_value());
131+
EXPECT_EQ(result.value(), 789);
132+
133+
result = GetInteger<uint32_t>(obj, "key");
134+
ASSERT_TRUE(result.has_value());
135+
EXPECT_EQ(result.value(), 789U);
136+
137+
result = GetInteger<int16_t>(obj, "key");
138+
ASSERT_TRUE(result.has_value());
139+
EXPECT_EQ(result.value(), static_cast<int16_t>(789));
140+
}
141+
87142
TEST(JSONUtilsTest, CreateModule) {
88143
SBTarget target;
89144
SBModule module;

0 commit comments

Comments
 (0)