default_value_objectwriter_test.cc
Go to the documentation of this file.
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 
33 #include <google/protobuf/util/internal/testdata/default_value_test.pb.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.
49  : public ::testing::TestWithParam<testing::TypeInfoSource> {
50  protected:
52  : helper_(GetParam()), mock_(), expects_(&mock_) {
55  std::string(kTypeServiceBaseUrl) + "/" + descriptor->full_name(),
56  &mock_));
57  }
58 
60 
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.
70  protected:
72  : BaseDefaultValueObjectWriterTest(DefaultValueTest::descriptor()) {}
74 };
75 
76 INSTANTIATE_TEST_SUITE_P(DifferentTypeInfoSourceTest,
77  DefaultValueObjectWriterTest,
80 
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 
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 
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 
155  protected:
157  : BaseDefaultValueObjectWriterTest(DefaultValueTest::descriptor()) {
158  testing_->set_suppress_empty_list(true);
159  }
161 };
162 
163 INSTANTIATE_TEST_SUITE_P(DifferentTypeInfoSourceTest,
164  DefaultValueObjectWriterSuppressListTest,
165  ::testing::Values(
167 
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
testing
Definition: gmock-actions.h:59
google::protobuf::util::converter::testing::INSTANTIATE_TEST_SUITE_P
INSTANTIATE_TEST_SUITE_P(DifferentTypeInfoSourceTest, DefaultValueObjectWriterTest, ::testing::Values(testing::USE_TYPE_RESOLVER))
google::protobuf::util::converter::testing::TypeInfoTestHelper::ResetTypeInfo
void ResetTypeInfo(const std::vector< const Descriptor * > &descriptors)
Definition: type_info_test_helper.cc:54
google::protobuf::util::converter::testing::BaseDefaultValueObjectWriterTest::expects_
ExpectingObjectWriter expects_
Definition: default_value_objectwriter_test.cc:63
google::protobuf::util::converter::testing::BaseDefaultValueObjectWriterTest::testing_
std::unique_ptr< DefaultValueObjectWriter > testing_
Definition: default_value_objectwriter_test.cc:64
gtest.h
google::protobuf::util::converter::testing::TypeInfoTestHelper::NewDefaultValueWriter
DefaultValueObjectWriter * NewDefaultValueWriter(const std::string &type_url, ObjectWriter *writer)
Definition: type_info_test_helper.cc:115
google::protobuf::util::converter::testing::TypeInfoTestHelper
Definition: type_info_test_helper.h:59
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
type_info_test_helper.h
google::protobuf::util::converter::ExpectingObjectWriter
Definition: expecting_objectwriter.h:91
descriptor
Descriptor * descriptor
Definition: php/ext/google/protobuf/protobuf.h:936
google::protobuf::util::converter::kTypeServiceBaseUrl
const char kTypeServiceBaseUrl[]
Definition: constants.h:43
google::protobuf::util::converter::testing::USE_TYPE_RESOLVER
@ USE_TYPE_RESOLVER
Definition: type_info_test_helper.h:52
testing::TestWithParam
Definition: gtest.h:1910
google::protobuf::util::converter::MockObjectWriter
Definition: expecting_objectwriter.h:70
expecting_objectwriter.h
google::protobuf::util::converter::testing::BaseDefaultValueObjectWriterTest::mock_
MockObjectWriter mock_
Definition: default_value_objectwriter_test.cc:62
google::protobuf::util::converter::testing::DefaultValueObjectWriterSuppressListTest::DefaultValueObjectWriterSuppressListTest
DefaultValueObjectWriterSuppressListTest()
Definition: default_value_objectwriter_test.cc:156
google::protobuf::util::converter::testing::BaseDefaultValueObjectWriterTest::~BaseDefaultValueObjectWriterTest
virtual ~BaseDefaultValueObjectWriterTest()
Definition: default_value_objectwriter_test.cc:59
google::protobuf::util::converter::testing::DefaultValueObjectWriterTest
Definition: default_value_objectwriter_test.cc:69
google::protobuf::util::converter::testing::BaseDefaultValueObjectWriterTest
Definition: default_value_objectwriter_test.cc:48
google::protobuf::util::converter::testing::BaseDefaultValueObjectWriterTest::BaseDefaultValueObjectWriterTest
BaseDefaultValueObjectWriterTest(const Descriptor *descriptor)
Definition: default_value_objectwriter_test.cc:51
google::protobuf::util::converter::testing::DefaultValueObjectWriterTest::DefaultValueObjectWriterTest
DefaultValueObjectWriterTest()
Definition: default_value_objectwriter_test.cc:71
default_value_objectwriter.h
google::protobuf::util::converter::testing::DefaultValueObjectWriterSuppressListTest::~DefaultValueObjectWriterSuppressListTest
~DefaultValueObjectWriterSuppressListTest() override
Definition: default_value_objectwriter_test.cc:160
google::protobuf::util::converter::testing::DefaultValueObjectWriterSuppressListTest
Definition: default_value_objectwriter_test.cc:153
google::protobuf::util::converter::testing::DefaultValueObjectWriterTest::~DefaultValueObjectWriterTest
virtual ~DefaultValueObjectWriterTest()
Definition: default_value_objectwriter_test.cc:73
Empty
Definition: empty.pb.h:69
google::protobuf::util::converter::testing::BaseDefaultValueObjectWriterTest::helper_
TypeInfoTestHelper helper_
Definition: default_value_objectwriter_test.cc:61
constants.h
testing::WithParamInterface< testing::TypeInfoSource >::GetParam
static const ParamType & GetParam()
Definition: gtest.h:1882
google::protobuf::Descriptor
Definition: src/google/protobuf/descriptor.h:231
google::protobuf::util::converter::testing::TEST_P
TEST_P(DefaultValueObjectWriterTest, Empty)
Definition: default_value_objectwriter_test.cc:81
testing::Values
internal::ValueArray< T... > Values(T... v)
Definition: gtest-param-test.h:340
google
Definition: data_proto2_to_proto3_util.h:11


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:49