1//===-- JSONTest.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 "gmock/gmock.h"
10#include "gtest/gtest.h"
11
12#include "JSON.h"
13
14template <typename T>
15void TestJSON(JSONValue *json_val, const std::function<void(T &)> &test_func) {
16 ASSERT_THAT(json_val, testing::NotNull());
17 ASSERT_TRUE(T::classof(json_val));
18 test_func(static_cast<T &>(*json_val));
19}
20
21JSONValue::SP ParseJSON(const char *json_string) {
22 return JSONParser(json_string).ParseJSONValue();
23}
24
25template <typename T>
26void ParseAndTestJSON(
27 const char *json_string,
28 const std::function<void(T &)> &test_func = [](T &) {}) {
29 auto json_val = ParseJSON(json_string);
30 TestJSON<T>(json_val.get(), test_func);
31}
32
33TEST(JSON, Parse) {
34 ParseAndTestJSON<JSONString>("\"foo\"", [](JSONString &string_val) {
35 EXPECT_EQ(string_val.GetData(), "foo");
36 });
37 EXPECT_THAT(ParseJSON("\"foo"), testing::IsNull());
38 ParseAndTestJSON<JSONNumber>("3", [](JSONNumber &number_val) {
39 EXPECT_EQ(number_val.GetAsSigned(), 3);
40 EXPECT_EQ(number_val.GetAsUnsigned(), 3u);
41 EXPECT_EQ(number_val.GetAsDouble(), 3.0);
42 });
43 ParseAndTestJSON<JSONNumber>("-5", [](JSONNumber &number_val) {
44 EXPECT_EQ(number_val.GetAsSigned(), -5);
45 EXPECT_EQ(number_val.GetAsDouble(), -5.0);
46 });
47 ParseAndTestJSON<JSONNumber>("-6.4", [](JSONNumber &number_val) {
48 EXPECT_EQ(number_val.GetAsSigned(), -6);
49 EXPECT_EQ(number_val.GetAsDouble(), -6.4);
50 });
51 EXPECT_THAT(ParseJSON("-1.2.3"), testing::IsNull());
52 ParseAndTestJSON<JSONTrue>("true");
53 ParseAndTestJSON<JSONFalse>("false");
54 ParseAndTestJSON<JSONNull>("null");
55 ParseAndTestJSON<JSONObject>(
56 "{ \"key1\": 4, \"key2\": \"foobar\" }", [](JSONObject &obj_val) {
57 TestJSON<JSONNumber>(obj_val.GetObject("key1").get(),
58 [](JSONNumber &number_val) {
59 EXPECT_EQ(number_val.GetAsSigned(), 4);
60 EXPECT_EQ(number_val.GetAsUnsigned(), 4u);
61 EXPECT_EQ(number_val.GetAsDouble(), 4.0);
62 });
63 TestJSON<JSONString>(obj_val.GetObject("key2").get(),
64 [](JSONString &string_val) {
65 EXPECT_EQ(string_val.GetData(), "foobar");
66 });
67 });
68 ParseAndTestJSON<JSONArray>("[1, \"bar\", 3.14]", [](JSONArray &array_val) {
69 EXPECT_EQ(array_val.GetNumElements(), 3u);
70 TestJSON<JSONNumber>(array_val.GetObject(0).get(),
71 [](JSONNumber &number_val) {
72 EXPECT_EQ(number_val.GetAsSigned(), 1);
73 EXPECT_EQ(number_val.GetAsUnsigned(), 1u);
74 EXPECT_EQ(number_val.GetAsDouble(), 1.0);
75 });
76 TestJSON<JSONString>(
77 array_val.GetObject(1).get(),
78 [](JSONString &string_val) { EXPECT_EQ(string_val.GetData(), "bar"); });
79 TestJSON<JSONNumber>(array_val.GetObject(2).get(),
80 [](JSONNumber &number_val) {
81 EXPECT_EQ(number_val.GetAsSigned(), 3);
82 EXPECT_EQ(number_val.GetAsUnsigned(), 3u);
83 EXPECT_EQ(number_val.GetAsDouble(), 3.14);
84 });
85 });
86 ParseAndTestJSON<JSONArray>("[]", [](JSONArray &array_val) {
87 EXPECT_EQ(array_val.GetNumElements(), 0u);
88 });
89}
90

source code of lldb/unittests/debugserver/JSONTest.cpp