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 #include <google/protobuf/util/internal/expecting_objectwriter.h>
33 #include <google/protobuf/util/internal/testdata/default_value_test.pb.h>
34 #include <google/protobuf/util/internal/type_info_test_helper.h>
35 #include <google/protobuf/util/internal/constants.h>
36 #include <gtest/gtest.h>
37
38 namespace google {
39 namespace protobuf {
40 namespace util {
41 namespace converter {
42 namespace testing {
43
44 using proto_util_converter::testing::DefaultValueTest;
45
46 // Base class for setting up required state for running default values tests on
47 // different descriptors.
48 class BaseDefaultValueObjectWriterTest
49 : public ::testing::TestWithParam<testing::TypeInfoSource> {
50 protected:
BaseDefaultValueObjectWriterTest(const Descriptor * descriptor)51 explicit BaseDefaultValueObjectWriterTest(const Descriptor* descriptor)
52 : helper_(GetParam()), mock_(), expects_(&mock_) {
53 helper_.ResetTypeInfo(descriptor);
54 testing_.reset(helper_.NewDefaultValueWriter(
55 std::string(kTypeServiceBaseUrl) + "/" + descriptor->full_name(),
56 &mock_));
57 }
58
~BaseDefaultValueObjectWriterTest()59 virtual ~BaseDefaultValueObjectWriterTest() {}
60
61 TypeInfoTestHelper helper_;
62 MockObjectWriter mock_;
63 ExpectingObjectWriter expects_;
64 std::unique_ptr<DefaultValueObjectWriter> testing_;
65 };
66
67 // Tests to cover some basic DefaultValueObjectWriter use cases. More tests are
68 // in the marshalling_test.cc and translator_integration_test.cc.
69 class DefaultValueObjectWriterTest : public BaseDefaultValueObjectWriterTest {
70 protected:
DefaultValueObjectWriterTest()71 DefaultValueObjectWriterTest()
72 : BaseDefaultValueObjectWriterTest(DefaultValueTest::descriptor()) {}
~DefaultValueObjectWriterTest()73 virtual ~DefaultValueObjectWriterTest() {}
74 };
75
76 INSTANTIATE_TEST_SUITE_P(DifferentTypeInfoSourceTest,
77 DefaultValueObjectWriterTest,
78 ::testing::Values(
79 testing::USE_TYPE_RESOLVER));
80
TEST_P(DefaultValueObjectWriterTest,Empty)81 TEST_P(DefaultValueObjectWriterTest, Empty) {
82 // Set expectation
83 expects_.StartObject("")
84 ->RenderDouble("doubleValue", 0.0)
85 ->StartList("repeatedDouble")
86 ->EndList()
87 ->RenderFloat("floatValue", 0.0)
88 ->RenderInt64("int64Value", 0)
89 ->RenderUint64("uint64Value", 0)
90 ->RenderInt32("int32Value", 0)
91 ->RenderUint32("uint32Value", 0)
92 ->RenderBool("boolValue", false)
93 ->RenderString("stringValue", "")
94 ->RenderBytes("bytesValue", "")
95 ->RenderString("enumValue", "ENUM_FIRST")
96 ->EndObject();
97
98 // Actual testing
99 testing_->StartObject("")->EndObject();
100 }
101
TEST_P(DefaultValueObjectWriterTest,NonDefaultDouble)102 TEST_P(DefaultValueObjectWriterTest, NonDefaultDouble) {
103 // Set expectation
104 expects_.StartObject("")
105 ->RenderDouble("doubleValue", 1.0)
106 ->StartList("repeatedDouble")
107 ->EndList()
108 ->RenderFloat("floatValue", 0.0)
109 ->RenderInt64("int64Value", 0)
110 ->RenderUint64("uint64Value", 0)
111 ->RenderInt32("int32Value", 0)
112 ->RenderUint32("uint32Value", 0)
113 ->RenderBool("boolValue", false)
114 ->RenderString("stringValue", "")
115 ->RenderString("enumValue", "ENUM_FIRST")
116 ->EndObject();
117
118 // Actual testing
119 testing_->StartObject("")->RenderDouble("doubleValue", 1.0)->EndObject();
120 }
121
TEST_P(DefaultValueObjectWriterTest,ShouldRetainUnknownField)122 TEST_P(DefaultValueObjectWriterTest, ShouldRetainUnknownField) {
123 // Set expectation
124 expects_.StartObject("")
125 ->RenderDouble("doubleValue", 1.0)
126 ->StartList("repeatedDouble")
127 ->EndList()
128 ->RenderFloat("floatValue", 0.0)
129 ->RenderInt64("int64Value", 0)
130 ->RenderUint64("uint64Value", 0)
131 ->RenderInt32("int32Value", 0)
132 ->RenderUint32("uint32Value", 0)
133 ->RenderBool("boolValue", false)
134 ->RenderString("stringValue", "")
135 ->RenderString("unknown", "abc")
136 ->StartObject("unknownObject")
137 ->RenderString("unknown", "def")
138 ->EndObject()
139 ->RenderString("enumValue", "ENUM_FIRST")
140 ->EndObject();
141
142 // Actual testing
143 testing_->StartObject("")
144 ->RenderDouble("doubleValue", 1.0)
145 ->RenderString("unknown", "abc")
146 ->StartObject("unknownObject")
147 ->RenderString("unknown", "def")
148 ->EndObject()
149 ->EndObject();
150 }
151
152
153 class DefaultValueObjectWriterSuppressListTest
154 : public BaseDefaultValueObjectWriterTest {
155 protected:
DefaultValueObjectWriterSuppressListTest()156 DefaultValueObjectWriterSuppressListTest()
157 : BaseDefaultValueObjectWriterTest(DefaultValueTest::descriptor()) {
158 testing_->set_suppress_empty_list(true);
159 }
~DefaultValueObjectWriterSuppressListTest()160 ~DefaultValueObjectWriterSuppressListTest() override {}
161 };
162
163 INSTANTIATE_TEST_SUITE_P(DifferentTypeInfoSourceTest,
164 DefaultValueObjectWriterSuppressListTest,
165 ::testing::Values(
166 testing::USE_TYPE_RESOLVER));
167
TEST_P(DefaultValueObjectWriterSuppressListTest,Empty)168 TEST_P(DefaultValueObjectWriterSuppressListTest, Empty) {
169 // Set expectation. Emtpy lists should be suppressed.
170 expects_.StartObject("")
171 ->RenderDouble("doubleValue", 0.0)
172 ->RenderFloat("floatValue", 0.0)
173 ->RenderInt64("int64Value", 0)
174 ->RenderUint64("uint64Value", 0)
175 ->RenderInt32("int32Value", 0)
176 ->RenderUint32("uint32Value", 0)
177 ->RenderBool("boolValue", false)
178 ->RenderString("stringValue", "")
179 ->RenderBytes("bytesValue", "")
180 ->RenderString("enumValue", "ENUM_FIRST")
181 ->EndObject();
182
183 // Actual testing
184 testing_->StartObject("")->EndObject();
185 }
186 } // namespace testing
187 } // namespace converter
188 } // namespace util
189 } // namespace protobuf
190 } // namespace google
191