-
Notifications
You must be signed in to change notification settings - Fork 886
Expand file tree
/
Copy pathjson.cpp
More file actions
71 lines (64 loc) · 1.69 KB
/
Copy pathjson.cpp
File metadata and controls
71 lines (64 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "support/json.h"
#include "gtest/gtest.h"
using JSONTest = ::testing::Test;
TEST_F(JSONTest, RoundtripString) {
// TODO: change the API to not require a copy
auto input = "[\"hello\",\"world\"]";
auto* copy = strdup(input);
json::Value value;
value.parse(copy, json::Value::ASCII);
std::stringstream ss;
value.stringify(ss);
EXPECT_EQ(ss.str(), input);
free(copy);
}
static void
checkOutput(json::Value::Ref ref, std::string expected, bool pretty = false) {
std::stringstream ss;
ref->stringify(ss, pretty);
EXPECT_EQ(ss.str(), expected);
}
static void checkPrettyOutput(json::Value::Ref ref, std::string expected) {
checkOutput(ref, expected, true);
}
TEST_F(JSONTest, StringifyArray) {
auto array = json::Value::makeArray();
array->push_back(json::Value::make(42));
array->push_back(json::Value::make("1337"));
array->push_back(json::Value::make()); // null
checkOutput(array, "[42,\"1337\",null]");
checkPrettyOutput(array, R"([
42,
"1337",
null
])");
}
TEST_F(JSONTest, StringifyObject) {
auto object = json::Value::makeObject();
object["foo"] = json::Value::make(42);
object["bar"] = json::Value::make("1337");
checkOutput(object, "{\"foo\":42,\"bar\":\"1337\"}");
checkPrettyOutput(object, R"({
"foo": 42,
"bar": "1337"
})");
}
TEST_F(JSONTest, StringifyNesting) {
auto array = json::Value::makeArray();
auto object = json::Value::makeObject();
auto array1 = json::Value::makeArray();
auto object1 = json::Value::makeObject();
array->push_back(object);
object["body"] = array1;
array1->push_back(object1);
object1["value"] = json::Value::make(42);
checkPrettyOutput(array, R"([
{
"body": [
{
"value": 42
}
]
}
])");
}