blob: 0bcd512ce41bb7bc591224f708ba841c6a8bbb3c [file] [edit]
// Copyright 2025 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "json_writer.h"
#include "test.h"
TEST(JsonWriterTest, SimpleLists) {
std::string out;
// Empty list
auto list = JsonWriter::StartList(out);
list.reset();
ASSERT_EQ(out, "[]");
out.clear();
list = JsonWriter::StartList(out);
list->AddBool(true);
list.reset();
ASSERT_EQ(out, "[ true ]");
out.clear();
list = JsonWriter::StartList(out);
list->AddBool(false);
list->AddInt(-12);
list->AddInt(1LL << 34);
list->AddFloat(1.234);
list->AddString("foo\nbar\r\n");
list.reset();
ASSERT_EQ(out, "[ false, -12, 17179869184, 1.234, \"foo\\nbar\\r\\n\" ]");
out.clear();
}
TEST(JsonWriterTest, SimpleObjects) {
std::string out;
auto empty = JsonWriter::StartDict(out);
empty.reset();
ASSERT_EQ(out, "{}");
out.clear();
{
auto obj = JsonWriter::StartDict(out);
obj->AddBool("key", true);
}
ASSERT_EQ(out, "{ \"key\": true }");
out.clear();
{
auto obj = JsonWriter::StartDict(out);
obj->AddBool("foo", false);
obj->AddInt("bar", -12);
obj->AddFloat("zoo", 3.1415);
obj->AddString("qux", "hello\r\nworld!\r\n");
}
ASSERT_EQ(out,
"{ \"foo\": false, \"bar\": -12, \"zoo\": 3.1415, \"qux\": "
"\"hello\\r\\nworld!\\r\\n\" }");
out.clear();
}
TEST(JsonWriterTest, NestedValues) {
std::string out;
{
auto list = JsonWriter::StartList(out);
{
auto dict1 = list->AddDict();
dict1->AddString("name", "bin");
dict1->AddString("path", "host_x64/test_bin");
{
auto args = dict1->AddList("args");
args->AddString("--verbose");
args->AddString("test_target");
}
{
auto properties = dict1->AddDict("properties");
properties->AddBool("is_debug", true);
properties->AddInt("expiration_ms", 10000);
}
}
{
auto dict2 = list->AddDict();
dict2->AddBool("need_qemu", false);
}
{
auto list2 = list->AddList();
list2->AddString("nested list item");
}
}
ASSERT_EQ(out,
"[ { \"name\": \"bin\", \"path\": \"host_x64/test_bin\", \"args\": "
"[ \"--verbose\", \"test_target\" ], \"properties\": { "
"\"is_debug\": true, \"expiration_ms\": 10000 } }, { "
"\"need_qemu\": false }, [ \"nested list item\" ] ]");
}
TEST(JsonWriterTest, Normalize) {
static const struct {
const char* input;
const char* expected;
} kTestCases[] = {
{ "", "" },
{ "[]", "[]" },
{ "{}", "{}" },
{ "[\"foo\"]", "[ \"foo\" ]" },
{ "[\n \"foo\",\n \"bar\"\n]", "[ \"foo\", \"bar\" ]" },
{ R"##(
{
"foo": 1,
"bar": [ ],
"zoo": [
42,
100
]
}
)##",
"{ \"foo\": 1, \"bar\": [], \"zoo\": [ 42, 100 ] }" },
};
for (const auto& test_case : kTestCases) {
EXPECT_EQ(JsonWriter::Normalize(test_case.input), test_case.expected)
<< "For input: " << test_case.input;
}
}