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
38namespace google {
39namespace protobuf {
40namespace util {
41namespace converter {
42namespace testing {
43
44using google::protobuf::testing::DefaultValueTest;
45
46// Base class for setting up required state for running default values tests on
47// different descriptors.
48class BaseDefaultValueObjectWriterTest
49    : public ::testing::TestWithParam<testing::TypeInfoSource> {
50 protected:
51  explicit BaseDefaultValueObjectWriterTest(const Descriptor* descriptor)
52      : helper_(GetParam()), mock_(), expects_(&mock_) {
53    helper_.ResetTypeInfo(descriptor);
54    testing_.reset(helper_.NewDefaultValueWriter(
55        string(kTypeServiceBaseUrl) + "/" + descriptor->full_name(), &mock_));
56  }
57
58  virtual ~BaseDefaultValueObjectWriterTest() {}
59
60  TypeInfoTestHelper helper_;
61  MockObjectWriter mock_;
62  ExpectingObjectWriter expects_;
63  google::protobuf::scoped_ptr<DefaultValueObjectWriter> testing_;
64};
65
66// Tests to cover some basic DefaultValueObjectWriter use cases. More tests are
67// in the marshalling_test.cc and translator_integration_test.cc.
68class DefaultValueObjectWriterTest : public BaseDefaultValueObjectWriterTest {
69 protected:
70  DefaultValueObjectWriterTest()
71      : BaseDefaultValueObjectWriterTest(DefaultValueTest::descriptor()) {}
72  virtual ~DefaultValueObjectWriterTest() {}
73};
74
75INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest,
76                        DefaultValueObjectWriterTest,
77                        ::testing::Values(
78                            testing::USE_TYPE_RESOLVER));
79
80TEST_P(DefaultValueObjectWriterTest, Empty) {
81  // Set expectation
82  expects_.StartObject("")
83      ->RenderDouble("doubleValue", 0.0)
84      ->StartList("repeatedDouble")
85      ->EndList()
86      ->RenderFloat("floatValue", 0.0)
87      ->RenderInt64("int64Value", 0)
88      ->RenderUint64("uint64Value", 0)
89      ->RenderInt32("int32Value", 0)
90      ->RenderUint32("uint32Value", 0)
91      ->RenderBool("boolValue", false)
92      ->RenderString("stringValue", "")
93      ->RenderBytes("bytesValue", "")
94      ->RenderString("enumValue", "ENUM_FIRST")
95      ->EndObject();
96
97  // Actual testing
98  testing_->StartObject("")->EndObject();
99}
100
101TEST_P(DefaultValueObjectWriterTest, NonDefaultDouble) {
102  // Set expectation
103  expects_.StartObject("")
104      ->RenderDouble("doubleValue", 1.0)
105      ->StartList("repeatedDouble")
106      ->EndList()
107      ->RenderFloat("floatValue", 0.0)
108      ->RenderInt64("int64Value", 0)
109      ->RenderUint64("uint64Value", 0)
110      ->RenderInt32("int32Value", 0)
111      ->RenderUint32("uint32Value", 0)
112      ->RenderBool("boolValue", false)
113      ->RenderString("stringValue", "")
114      ->RenderString("enumValue", "ENUM_FIRST")
115      ->EndObject();
116
117  // Actual testing
118  testing_->StartObject("")->RenderDouble("doubleValue", 1.0)->EndObject();
119}
120
121TEST_P(DefaultValueObjectWriterTest, ShouldRetainUnknownField) {
122  // Set expectation
123  expects_.StartObject("")
124      ->RenderDouble("doubleValue", 1.0)
125      ->StartList("repeatedDouble")
126      ->EndList()
127      ->RenderFloat("floatValue", 0.0)
128      ->RenderInt64("int64Value", 0)
129      ->RenderUint64("uint64Value", 0)
130      ->RenderInt32("int32Value", 0)
131      ->RenderUint32("uint32Value", 0)
132      ->RenderBool("boolValue", false)
133      ->RenderString("stringValue", "")
134      ->RenderString("unknown", "abc")
135      ->StartObject("unknownObject")
136      ->RenderString("unknown", "def")
137      ->EndObject()
138      ->RenderString("enumValue", "ENUM_FIRST")
139      ->EndObject();
140
141  // Actual testing
142  testing_->StartObject("")
143      ->RenderDouble("doubleValue", 1.0)
144      ->RenderString("unknown", "abc")
145      ->StartObject("unknownObject")
146      ->RenderString("unknown", "def")
147      ->EndObject()
148      ->EndObject();
149}
150
151
152}  // namespace testing
153}  // namespace converter
154}  // namespace util
155}  // namespace protobuf
156}  // namespace google
157