aboutsummaryrefslogtreecommitdiff
path: root/util/json/json_serialization_unittest.cc
blob: 82b97756a63932c9c4aa480d7816a9f1cc8b133a (plain)
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
// 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