1//===-- JSONUtilsTest.cpp -------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "JSONUtils.h"
10#include "lldb/API/SBModule.h"
11#include "lldb/API/SBTarget.h"
12#include "llvm/Support/JSON.h"
13#include "gtest/gtest.h"
14#include <optional>
15
16using namespace llvm;
17using namespace lldb;
18using namespace lldb_dap;
19
20TEST(JSONUtilsTest, GetAsString) {
21 json::Value string_value("foo");
22 EXPECT_EQ(GetAsString(string_value), "foo");
23
24 json::Value int_value(42);
25 EXPECT_EQ(GetAsString(int_value), "");
26
27 json::Value null_value(nullptr);
28 EXPECT_EQ(GetAsString(null_value), "");
29}
30
31TEST(JSONUtilsTest, GetString_Ref) {
32 json::Object obj;
33 obj.try_emplace(K: "key", Args: "value");
34
35 auto result = GetString(obj, key: "key");
36 ASSERT_TRUE(result.has_value());
37 EXPECT_EQ(result.value(), "value");
38
39 result = GetString(obj, key: "nonexistent_key");
40 EXPECT_FALSE(result.has_value());
41}
42
43TEST(JSONUtilsTest, GetString_Pointer) {
44 json::Object obj;
45 obj.try_emplace(K: "key", Args: "value");
46
47 auto result = GetString(obj: &obj, key: "key");
48 ASSERT_TRUE(result.has_value());
49 EXPECT_EQ(result.value(), "value");
50
51 result = GetString(obj: nullptr, key: "key");
52 EXPECT_FALSE(result.has_value());
53}
54
55TEST(JSONUtilsTest, GetBoolean_Ref) {
56 json::Object obj;
57 obj.try_emplace(K: "key_true", Args: true);
58 obj.try_emplace(K: "key_false", Args: false);
59 obj.try_emplace(K: "key_int", Args: 1);
60
61 auto result = GetBoolean(obj, key: "key_true");
62 ASSERT_TRUE(result.has_value());
63 EXPECT_TRUE(result.value());
64
65 result = GetBoolean(obj, key: "key_false");
66 ASSERT_TRUE(result.has_value());
67 EXPECT_FALSE(result.value());
68
69 result = GetBoolean(obj, key: "key_int");
70 ASSERT_TRUE(result.has_value());
71 EXPECT_TRUE(result.value());
72
73 result = GetBoolean(obj, key: "nonexistent_key");
74 EXPECT_FALSE(result.has_value());
75}
76
77TEST(JSONUtilsTest, GetBoolean_Pointer) {
78 json::Object obj;
79 obj.try_emplace(K: "key", Args: true);
80
81 auto result = GetBoolean(obj: &obj, key: "key");
82 ASSERT_TRUE(result.has_value());
83 EXPECT_TRUE(result.value());
84
85 result = GetBoolean(obj: nullptr, key: "key");
86 EXPECT_FALSE(result.has_value());
87}
88
89TEST(JSONUtilsTest, GetInteger_Ref) {
90 json::Object obj;
91 obj.try_emplace(K: "key", Args: 123);
92
93 auto result = GetInteger<int>(obj, key: "key");
94 ASSERT_TRUE(result.has_value());
95 EXPECT_EQ(result.value(), 123);
96
97 result = GetInteger<int>(obj, key: "nonexistent_key");
98 EXPECT_FALSE(result.has_value());
99
100 obj.try_emplace(K: "key_float", Args: 123.45);
101 result = GetInteger<int>(obj, key: "key_float");
102 EXPECT_FALSE(result.has_value());
103
104 obj.try_emplace(K: "key_string", Args: "123");
105 result = GetInteger<int>(obj, key: "key_string");
106 EXPECT_FALSE(result.has_value());
107}
108
109TEST(JSONUtilsTest, GetInteger_Pointer) {
110 json::Object obj;
111 obj.try_emplace(K: "key", Args: 456);
112
113 auto result = GetInteger<int>(obj: &obj, key: "key");
114 ASSERT_TRUE(result.has_value());
115 EXPECT_EQ(result.value(), 456);
116
117 result = GetInteger<int>(obj: nullptr, key: "key");
118 EXPECT_FALSE(result.has_value());
119
120 obj.try_emplace(K: "key_invalid", Args: "not_an_integer");
121 result = GetInteger<int>(obj: &obj, key: "key_invalid");
122 EXPECT_FALSE(result.has_value());
123}
124
125TEST(JSONUtilsTest, GetInteger_DifferentTypes) {
126 json::Object obj;
127 obj.try_emplace(K: "key", Args: 789);
128
129 auto result = GetInteger<int64_t>(obj, key: "key");
130 ASSERT_TRUE(result.has_value());
131 EXPECT_EQ(result.value(), 789);
132
133 result = GetInteger<uint32_t>(obj, key: "key");
134 ASSERT_TRUE(result.has_value());
135 EXPECT_EQ(result.value(), 789U);
136
137 result = GetInteger<int16_t>(obj, key: "key");
138 ASSERT_TRUE(result.has_value());
139 EXPECT_EQ(result.value(), static_cast<int16_t>(789));
140}
141
142TEST(JSONUtilsTest, CreateModule) {
143 SBTarget target;
144 SBModule module;
145
146 json::Value value = CreateModule(target, module);
147 json::Object *object = value.getAsObject();
148
149 ASSERT_NE(object, nullptr);
150 EXPECT_EQ(object->size(), 0UL);
151}
152
153TEST(JSONUtilsTest, GetStrings_EmptyArray) {
154 llvm::json::Object obj;
155 obj.try_emplace(K: "key", Args: llvm::json::Array());
156 auto result = GetStrings(obj: &obj, key: "key");
157 EXPECT_TRUE(result.empty());
158}
159
160TEST(JSONUtilsTest, GetStrings_NullKey) {
161 llvm::json::Object obj;
162 auto result = GetStrings(obj: &obj, key: "nonexistent_key");
163 EXPECT_TRUE(result.empty());
164}
165
166TEST(JSONUtilsTest, GetStrings_StringValues) {
167 llvm::json::Object obj;
168 llvm::json::Array arr{"value1", "value2", "value3"};
169 obj.try_emplace(K: "key", Args: std::move(arr));
170 auto result = GetStrings(obj: &obj, key: "key");
171 ASSERT_EQ(result.size(), 3UL);
172 EXPECT_EQ(result[0], "value1");
173 EXPECT_EQ(result[1], "value2");
174 EXPECT_EQ(result[2], "value3");
175}
176
177TEST(JSONUtilsTest, GetStrings_MixedValues) {
178 llvm::json::Object obj;
179 llvm::json::Array arr{"string", 42, true, nullptr};
180 obj.try_emplace(K: "key", Args: std::move(arr));
181 auto result = GetStrings(obj: &obj, key: "key");
182 ASSERT_EQ(result.size(), 3UL);
183 EXPECT_EQ(result[0], "string");
184 EXPECT_EQ(result[1], "42");
185 EXPECT_EQ(result[2], "true");
186}
187
188TEST(JSONUtilsTest, GetStrings_NestedArray) {
189 llvm::json::Object obj;
190 llvm::json::Array nested_array{"string", llvm::json::Array{"nested"}};
191 obj.try_emplace(K: "key", Args: std::move(nested_array));
192 auto result = GetStrings(obj: &obj, key: "key");
193 ASSERT_EQ(result.size(), 1UL);
194 EXPECT_EQ(result[0], "string");
195}
196

source code of lldb/unittests/DAP/JSONUtilsTest.cpp