Files
rk35xx-android14/external/openscreen/util/json/json_serialization_unittest.cc
T
hmz007 36ed224bac Rockchip Anroid14_SDK 20240628-rkr5 (2556df1a)
Signed-off-by: hmz007 <hmz007@gmail.com>
Change-Id: Ib87fdbe7a0be7dc961af3500fe9ea4589a127f9f
2024-10-29 18:12:02 +08:00

63 lines
1.8 KiB
C++

// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "util/json/json_serialization.h"
#include <array>
#include <string>
#include "gtest/gtest.h"
#include "platform/base/error.h"
namespace openscreen {
namespace {
template <typename Value>
void AssertError(ErrorOr<Value> error_or, Error::Code code) {
EXPECT_EQ(error_or.error().code(), code);
}
} // namespace
TEST(JsonSerializationTest, MalformedDocumentReturnsParseError) {
const std::array<std::string, 4> kMalformedDocuments{
{"", "{", "{ foo: bar }", R"({"foo": "bar", "foo": baz})"}};
for (auto& document : kMalformedDocuments) {
AssertError(json::Parse(document), Error::Code::kJsonParseError);
}
}
TEST(JsonSerializationTest, ValidEmptyDocumentParsedCorrectly) {
const auto actual = json::Parse("{}");
EXPECT_TRUE(actual.is_value());
EXPECT_EQ(actual.value().getMemberNames().size(), 0u);
}
// Jsoncpp has its own suite of tests ensure that things are parsed correctly,
// so we only do some rudimentary checks here to make sure we didn't mangle
// the value.
TEST(JsonSerializationTest, ValidDocumentParsedCorrectly) {
const auto actual = json::Parse(R"({"foo": "bar", "baz": 1337})");
EXPECT_TRUE(actual.is_value());
EXPECT_EQ(actual.value().getMemberNames().size(), 2u);
}
TEST(JsonSerializationTest, NullValueReturnsError) {
const auto null_value = Json::Value();
const auto actual = json::Stringify(null_value);
EXPECT_TRUE(actual.is_error());
EXPECT_EQ(actual.error().code(), Error::Code::kJsonWriteError);
}
TEST(JsonSerializationTest, ValidValueReturnsString) {
const Json::Int64 value = 31337;
const auto actual = json::Stringify(value);
EXPECT_TRUE(actual.is_value());
EXPECT_EQ(actual.value(), "31337");
}
} // namespace openscreen