Skip to content

Commit 5b97a5b

Browse files
committed
[lldb-dap] Add unit tests for GetString and GetBoolean
These are simple functions but they're used a lot. Add some simple unit tests for them.
1 parent c1ecd0a commit 5b97a5b

File tree

1 file changed

+66
-3
lines changed

1 file changed

+66
-3
lines changed

lldb/unittests/DAP/JSONUtilsTest.cpp

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,72 @@ using namespace lldb;
1616
using namespace lldb_dap;
1717

1818
TEST(JSONUtilsTest, GetAsString) {
19-
StringRef str = "foo";
20-
json::Value value("foo");
21-
EXPECT_EQ(str, GetAsString(value));
19+
json::Value string_value("foo");
20+
EXPECT_EQ(GetAsString(string_value), "foo");
21+
22+
json::Value int_value(42);
23+
EXPECT_EQ(GetAsString(int_value), "");
24+
25+
json::Value null_value(nullptr);
26+
EXPECT_EQ(GetAsString(null_value), "");
27+
}
28+
29+
TEST(JSONUtilsTest, GetString_Ref) {
30+
json::Object obj;
31+
obj.try_emplace("key", "value");
32+
33+
auto result = GetString(obj, "key");
34+
ASSERT_TRUE(result.has_value());
35+
EXPECT_EQ(result.value(), "value");
36+
37+
result = GetString(obj, "nonexistent_key");
38+
EXPECT_FALSE(result.has_value());
39+
}
40+
41+
TEST(JSONUtilsTest, GetString_Pointer) {
42+
json::Object obj;
43+
obj.try_emplace("key", "value");
44+
45+
auto result = GetString(&obj, "key");
46+
ASSERT_TRUE(result.has_value());
47+
EXPECT_EQ(result.value(), "value");
48+
49+
result = GetString(nullptr, "key");
50+
EXPECT_FALSE(result.has_value());
51+
}
52+
53+
TEST(JSONUtilsTest, GetBoolean_Ref) {
54+
json::Object obj;
55+
obj.try_emplace("key_true", true);
56+
obj.try_emplace("key_false", false);
57+
obj.try_emplace("key_int", 1);
58+
59+
auto result = GetBoolean(obj, "key_true");
60+
ASSERT_TRUE(result.has_value());
61+
EXPECT_TRUE(result.value());
62+
63+
result = GetBoolean(obj, "key_false");
64+
ASSERT_TRUE(result.has_value());
65+
EXPECT_FALSE(result.value());
66+
67+
result = GetBoolean(obj, "key_int");
68+
ASSERT_TRUE(result.has_value());
69+
EXPECT_TRUE(result.value());
70+
71+
result = GetBoolean(obj, "nonexistent_key");
72+
EXPECT_FALSE(result.has_value());
73+
}
74+
75+
TEST(JSONUtilsTest, GetBoolean_Pointer) {
76+
json::Object obj;
77+
obj.try_emplace("key", true);
78+
79+
auto result = GetBoolean(&obj, "key");
80+
ASSERT_TRUE(result.has_value());
81+
EXPECT_TRUE(result.value());
82+
83+
result = GetBoolean(nullptr, "key");
84+
EXPECT_FALSE(result.has_value());
2285
}
2386

2487
TEST(JSONUtilsTest, CreateModule) {

0 commit comments

Comments
 (0)