Skip to content

Commit d747010

Browse files
committed
[JSONSerialization] Add basic unit tests for JSONSerialization
1 parent d48a2a4 commit d747010

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed

unittests/Basic/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ add_swift_unittest(SwiftBasicTests
1515
EncodedSequenceTest.cpp
1616
FileSystemTest.cpp
1717
ImmutablePointerSetTest.cpp
18+
JSONSerialization.cpp
1819
OptionSetTest.cpp
1920
OwnedStringTest.cpp
2021
PointerIntEnumTest.cpp

unittests/Basic/JSONSerialization.cpp

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
//===--- CacheTest.cpp ----------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "swift/Basic/JSONSerialization.h"
14+
#include "gtest/gtest.h"
15+
16+
namespace {
17+
struct Leaf {
18+
int Val;
19+
};
20+
21+
struct Root {
22+
std::string Name;
23+
std::vector<Leaf *> Children;
24+
std::vector<Leaf *> Empty;
25+
Leaf *Side;
26+
};
27+
} // namespace
28+
29+
namespace swift {
30+
namespace json {
31+
32+
template <> struct ObjectTraits<Leaf> {
33+
static void mapping(Output &out, Leaf &value) {
34+
switch (value.Val) {
35+
case 0:
36+
break;
37+
case 1:
38+
out.mapRequired("Value", value.Val);
39+
break;
40+
case 2: {
41+
std::string two("two");
42+
out.mapRequired("Value", two);
43+
break;
44+
}
45+
default:
46+
break;
47+
}
48+
}
49+
};
50+
51+
template <> struct NullableTraits<Leaf *> {
52+
static bool isNull(Leaf *&value) { return !value; }
53+
static Leaf &get(Leaf *&value) { return *value; }
54+
};
55+
56+
template <> struct ArrayTraits<std::vector<Leaf *>> {
57+
58+
using value_type = Leaf *;
59+
60+
static size_t size(Output &out, std::vector<Leaf *> &seq) {
61+
return seq.size();
62+
}
63+
static Leaf *&element(Output &out, std::vector<Leaf *> &seq, size_t index) {
64+
return seq[index];
65+
}
66+
};
67+
68+
template <> struct ObjectTraits<Root> {
69+
static void mapping(Output &out, Root &value) {
70+
out.mapRequired("Name", value.Name);
71+
out.mapRequired("Children", value.Children);
72+
out.mapRequired("Empty", value.Empty);
73+
out.mapRequired("Side", value.Side);
74+
}
75+
};
76+
77+
} // namespace json
78+
} // namespace swift
79+
80+
TEST(JSONSerialization, basicCompact) {
81+
Leaf LeafObj0{0};
82+
Leaf LeafObj1{1};
83+
Leaf LeafObj2{2};
84+
Root RootObj{"foo", {&LeafObj0, &LeafObj1, nullptr, &LeafObj2}, {}, nullptr};
85+
std::string Buffer;
86+
llvm::raw_string_ostream Stream(Buffer);
87+
swift::json::Output Out(Stream, /*PrettyPrint=*/false);
88+
89+
Out << RootObj;
90+
Stream.flush();
91+
92+
EXPECT_EQ(Buffer, "{\"Name\":\"foo\",\"Children\":[{},{\"Value\":1},null,{"
93+
"\"Value\":\"two\"}],\"Empty\":[],\"Side\":null}");
94+
}
95+
96+
TEST(JSONSerialization, basicPretty) {
97+
Leaf LeafObj0{0};
98+
Leaf LeafObj1{1};
99+
Leaf LeafObj2{2};
100+
Root RootObj{"foo", {&LeafObj0, &LeafObj1, nullptr, &LeafObj2}, {}, nullptr};
101+
std::string Buffer;
102+
llvm::raw_string_ostream Stream(Buffer);
103+
swift::json::Output Out(Stream);
104+
105+
Out << RootObj;
106+
Stream.flush();
107+
108+
EXPECT_EQ(Buffer, R"""({
109+
"Name": "foo",
110+
"Children": [
111+
{},
112+
{
113+
"Value": 1
114+
},
115+
null,
116+
{
117+
"Value": "two"
118+
}
119+
],
120+
"Empty": [],
121+
"Side": null
122+
})""");
123+
}

0 commit comments

Comments
 (0)