YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/util/jsonwriter-test.cc
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
//
18
// The following only applies to changes made to this file as part of YugaByte development.
19
//
20
// Portions Copyright (c) YugaByte, Inc.
21
//
22
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23
// in compliance with the License.  You may obtain a copy of the License at
24
//
25
// http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing, software distributed under the License
28
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29
// or implied.  See the License for the specific language governing permissions and limitations
30
// under the License.
31
//
32
33
#include <gtest/gtest.h>
34
35
#include "yb/util/jsonwriter.h"
36
#include "yb/util/jsonwriter_test.pb.h"
37
#include "yb/util/test_util.h"
38
39
using jsonwriter_test::TestAllTypes;
40
41
namespace yb {
42
43
class TestJsonWriter : public YBTest {};
44
45
1
TEST_F(TestJsonWriter, TestPBEmpty) {
46
1
  TestAllTypes pb;
47
1
  ASSERT_EQ("{}", JsonWriter::ToJson(pb, JsonWriter::PRETTY));
48
1
}
49
50
1
TEST_F(TestJsonWriter, TestPBAllFieldTypes) {
51
1
  TestAllTypes pb;
52
1
  pb.set_optional_int32(1);
53
1
  pb.set_optional_int64(2);
54
1
  pb.set_optional_uint32(3);
55
1
  pb.set_optional_uint64(4);
56
1
  pb.set_optional_sint32(5);
57
1
  pb.set_optional_sint64(6);
58
1
  pb.set_optional_fixed32(7);
59
1
  pb.set_optional_fixed64(8);
60
1
  pb.set_optional_sfixed32(9);
61
1
  pb.set_optional_sfixed64(10);
62
1
  pb.set_optional_float(11);
63
1
  pb.set_optional_double(12);
64
1
  pb.set_optional_bool(true);
65
1
  pb.set_optional_string("hello world");
66
1
  pb.set_optional_nested_enum(TestAllTypes::FOO);
67
1
  ASSERT_EQ("{\n"
68
1
            "    \"optional_int32\": 1,\n"
69
1
            "    \"optional_int64\": 2,\n"
70
1
            "    \"optional_uint32\": 3,\n"
71
1
            "    \"optional_uint64\": 4,\n"
72
1
            "    \"optional_sint32\": 5,\n"
73
1
            "    \"optional_sint64\": 6,\n"
74
1
            "    \"optional_fixed32\": 7,\n"
75
1
            "    \"optional_fixed64\": 8,\n"
76
1
            "    \"optional_sfixed32\": 9,\n"
77
1
            "    \"optional_sfixed64\": 10,\n"
78
1
            "    \"optional_float\": 11.0,\n"
79
1
            "    \"optional_double\": 12.0,\n"
80
1
            "    \"optional_bool\": true,\n"
81
1
            "    \"optional_string\": \"hello world\",\n"
82
1
            "    \"optional_nested_enum\": \"FOO\"\n"
83
1
            "}", JsonWriter::ToJson(pb, JsonWriter::PRETTY));
84
1
  ASSERT_EQ("{"
85
1
            "\"optional_int32\":1,"
86
1
            "\"optional_int64\":2,"
87
1
            "\"optional_uint32\":3,"
88
1
            "\"optional_uint64\":4,"
89
1
            "\"optional_sint32\":5,"
90
1
            "\"optional_sint64\":6,"
91
1
            "\"optional_fixed32\":7,"
92
1
            "\"optional_fixed64\":8,"
93
1
            "\"optional_sfixed32\":9,"
94
1
            "\"optional_sfixed64\":10,"
95
1
            "\"optional_float\":11.0,"
96
1
            "\"optional_double\":12.0,"
97
1
            "\"optional_bool\":true,"
98
1
            "\"optional_string\":\"hello world\","
99
1
            "\"optional_nested_enum\":\"FOO\""
100
1
            "}", JsonWriter::ToJson(pb, JsonWriter::COMPACT));
101
102
1
}
103
104
1
TEST_F(TestJsonWriter, TestPBRepeatedPrimitives) {
105
1
  TestAllTypes pb;
106
5
  for (int i = 0; i <= 3; i++) {
107
4
    pb.add_repeated_int32(i);
108
4
  }
109
1
  ASSERT_EQ("{\n"
110
1
            "    \"repeated_int32\": [\n"
111
1
            "        0,\n"
112
1
            "        1,\n"
113
1
            "        2,\n"
114
1
            "        3\n"
115
1
            "    ]\n"
116
1
            "}", JsonWriter::ToJson(pb, JsonWriter::PRETTY));
117
1
  ASSERT_EQ("{\"repeated_int32\":[0,1,2,3]}",
118
1
            JsonWriter::ToJson(pb, JsonWriter::COMPACT));
119
1
}
120
121
1
TEST_F(TestJsonWriter, TestPBNestedMessage) {
122
1
  TestAllTypes pb;
123
1
  pb.add_repeated_nested_message()->set_int_field(12345);
124
1
  pb.mutable_optional_nested_message()->set_int_field(54321);
125
1
  ASSERT_EQ("{\n"
126
1
            "    \"optional_nested_message\": {\n"
127
1
            "        \"int_field\": 54321\n"
128
1
            "    },\n"
129
1
            "    \"repeated_nested_message\": [\n"
130
1
            "        {\n"
131
1
            "            \"int_field\": 12345\n"
132
1
            "        }\n"
133
1
            "    ]\n"
134
1
            "}", JsonWriter::ToJson(pb, JsonWriter::PRETTY));
135
1
  ASSERT_EQ("{\"optional_nested_message\":{\"int_field\":54321},"
136
1
            "\"repeated_nested_message\":"
137
1
            "[{\"int_field\":12345}]}",
138
1
            JsonWriter::ToJson(pb, JsonWriter::COMPACT));
139
1
}
140
141
} // namespace yb