1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 #include <google/protobuf/util/internal/default_value_objectwriter.h>
32
33 #include <google/protobuf/util/internal/expecting_objectwriter.h>
34 #include <google/protobuf/util/internal/testdata/default_value_test.pb.h>
35 #include <google/protobuf/util/internal/type_info_test_helper.h>
36 #include <google/protobuf/util/internal/constants.h>
37 #include <gtest/gtest.h>
38
39 namespace google {
40 namespace protobuf {
41 namespace util {
42 namespace converter {
43 namespace testing {
44
45 using proto_util_converter::testing::DefaultValueTest;
46
47 // Base class for setting up required state for running default values tests on
48 // different descriptors.
49 class BaseDefaultValueObjectWriterTest
50 : public ::testing::TestWithParam<testing::TypeInfoSource> {
51 protected:
BaseDefaultValueObjectWriterTest(const Descriptor * descriptor)52 explicit BaseDefaultValueObjectWriterTest(const Descriptor* descriptor)
53 : helper_(GetParam()), mock_(), expects_(&mock_) {
54 helper_.ResetTypeInfo(descriptor);
55 testing_.reset(helper_.NewDefaultValueWriter(
56 std::string(kTypeServiceBaseUrl) + "/" + descriptor->full_name(),
57 &mock_));
58 }
59
~BaseDefaultValueObjectWriterTest()60 virtual ~BaseDefaultValueObjectWriterTest() {}
61
62 TypeInfoTestHelper helper_;
63 MockObjectWriter mock_;
64 ExpectingObjectWriter expects_;
65 std::unique_ptr<DefaultValueObjectWriter> testing_;
66 };
67
68 // Tests to cover some basic DefaultValueObjectWriter use cases. More tests are
69 // in the marshalling_test.cc and translator_integration_test.cc.
70 class DefaultValueObjectWriterTest : public BaseDefaultValueObjectWriterTest {
71 protected:
DefaultValueObjectWriterTest()72 DefaultValueObjectWriterTest()
73 : BaseDefaultValueObjectWriterTest(DefaultValueTest::descriptor()) {}
~DefaultValueObjectWriterTest()74 virtual ~DefaultValueObjectWriterTest() {}
75 };
76
77 INSTANTIATE_TEST_SUITE_P(DifferentTypeInfoSourceTest,
78 DefaultValueObjectWriterTest,
79 ::testing::Values(
80 testing::USE_TYPE_RESOLVER));
81
TEST_P(DefaultValueObjectWriterTest,Empty)82 TEST_P(DefaultValueObjectWriterTest, Empty) {
83 // Set expectation
84 expects_.StartObject("")
85 ->RenderDouble("doubleValue", 0.0)
86 ->StartList("repeatedDouble")
87 ->EndList()
88 ->RenderFloat("floatValue", 0.0)
89 ->RenderInt64("int64Value", 0)
90 ->RenderUint64("uint64Value", 0)
91 ->RenderInt32("int32Value", 0)
92 ->RenderUint32("uint32Value", 0)
93 ->RenderBool("boolValue", false)
94 ->RenderString("stringValue", "")
95 ->RenderBytes("bytesValue", "")
96 ->RenderString("enumValue", "ENUM_FIRST")
97 ->EndObject();
98
99 // Actual testing
100 testing_->StartObject("")->EndObject();
101 }
102
TEST_P(DefaultValueObjectWriterTest,NonDefaultDouble)103 TEST_P(DefaultValueObjectWriterTest, NonDefaultDouble) {
104 // Set expectation
105 expects_.StartObject("")
106 ->RenderDouble("doubleValue", 1.0)
107 ->StartList("repeatedDouble")
108 ->EndList()
109 ->RenderFloat("floatValue", 0.0)
110 ->RenderInt64("int64Value", 0)
111 ->RenderUint64("uint64Value", 0)
112 ->RenderInt32("int32Value", 0)
113 ->RenderUint32("uint32Value", 0)
114 ->RenderBool("boolValue", false)
115 ->RenderString("stringValue", "")
116 ->RenderString("enumValue", "ENUM_FIRST")
117 ->EndObject();
118
119 // Actual testing
120 testing_->StartObject("")->RenderDouble("doubleValue", 1.0)->EndObject();
121 }
122
TEST_P(DefaultValueObjectWriterTest,ShouldRetainUnknownField)123 TEST_P(DefaultValueObjectWriterTest, ShouldRetainUnknownField) {
124 // Set expectation
125 expects_.StartObject("")
126 ->RenderDouble("doubleValue", 1.0)
127 ->StartList("repeatedDouble")
128 ->EndList()
129 ->RenderFloat("floatValue", 0.0)
130 ->RenderInt64("int64Value", 0)
131 ->RenderUint64("uint64Value", 0)
132 ->RenderInt32("int32Value", 0)
133 ->RenderUint32("uint32Value", 0)
134 ->RenderBool("boolValue", false)
135 ->RenderString("stringValue", "")
136 ->RenderString("unknown", "abc")
137 ->StartObject("unknownObject")
138 ->RenderString("unknown", "def")
139 ->EndObject()
140 ->RenderString("enumValue", "ENUM_FIRST")
141 ->EndObject();
142
143 // Actual testing
144 testing_->StartObject("")
145 ->RenderDouble("doubleValue", 1.0)
146 ->RenderString("unknown", "abc")
147 ->StartObject("unknownObject")
148 ->RenderString("unknown", "def")
149 ->EndObject()
150 ->EndObject();
151 }
152
153
154 class DefaultValueObjectWriterSuppressListTest
155 : public BaseDefaultValueObjectWriterTest {
156 protected:
DefaultValueObjectWriterSuppressListTest()157 DefaultValueObjectWriterSuppressListTest()
158 : BaseDefaultValueObjectWriterTest(DefaultValueTest::descriptor()) {
159 testing_->set_suppress_empty_list(true);
160 }
~DefaultValueObjectWriterSuppressListTest()161 ~DefaultValueObjectWriterSuppressListTest() override {}
162 };
163
164 INSTANTIATE_TEST_SUITE_P(DifferentTypeInfoSourceTest,
165 DefaultValueObjectWriterSuppressListTest,
166 ::testing::Values(
167 testing::USE_TYPE_RESOLVER));
168
TEST_P(DefaultValueObjectWriterSuppressListTest,Empty)169 TEST_P(DefaultValueObjectWriterSuppressListTest, Empty) {
170 // Set expectation. Empty lists should be suppressed.
171 expects_.StartObject("")
172 ->RenderDouble("doubleValue", 0.0)
173 ->RenderFloat("floatValue", 0.0)
174 ->RenderInt64("int64Value", 0)
175 ->RenderUint64("uint64Value", 0)
176 ->RenderInt32("int32Value", 0)
177 ->RenderUint32("uint32Value", 0)
178 ->RenderBool("boolValue", false)
179 ->RenderString("stringValue", "")
180 ->RenderBytes("bytesValue", "")
181 ->RenderString("enumValue", "ENUM_FIRST")
182 ->EndObject();
183
184 // Actual testing
185 testing_->StartObject("")->EndObject();
186 }
187 } // namespace testing
188 } // namespace converter
189 } // namespace util
190 } // namespace protobuf
191 } // namespace google
192