1// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc.  All rights reserved.
3// http://code.google.com/p/protobuf/
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// Author: kenton@google.com (Kenton Varda)
32//  Based on original Protocol Buffers design by
33//  Sanjay Ghemawat, Jeff Dean, and others.
34
35#ifndef GOOGLE_PROTOBUF_TEST_UTIL_H__
36#define GOOGLE_PROTOBUF_TEST_UTIL_H__
37
38#include <stack>
39#include <string>
40#include <google/protobuf/message.h>
41#include <google/protobuf/unittest.pb.h>
42
43namespace google {
44namespace protobuf {
45
46namespace unittest = protobuf_unittest;
47namespace unittest_import = protobuf_unittest_import;
48
49class TestUtil {
50 public:
51  // Set every field in the message to a unique value.
52  static void SetAllFields(unittest::TestAllTypes* message);
53  static void SetAllExtensions(unittest::TestAllExtensions* message);
54  static void SetAllFieldsAndExtensions(unittest::TestFieldOrderings* message);
55  static void SetPackedFields(unittest::TestPackedTypes* message);
56  static void SetPackedExtensions(unittest::TestPackedExtensions* message);
57  static void SetUnpackedFields(unittest::TestUnpackedTypes* message);
58
59  // Use the repeated versions of the set_*() accessors to modify all the
60  // repeated fields of the messsage (which should already have been
61  // initialized with Set*Fields()).  Set*Fields() itself only tests
62  // the add_*() accessors.
63  static void ModifyRepeatedFields(unittest::TestAllTypes* message);
64  static void ModifyRepeatedExtensions(unittest::TestAllExtensions* message);
65  static void ModifyPackedFields(unittest::TestPackedTypes* message);
66  static void ModifyPackedExtensions(unittest::TestPackedExtensions* message);
67
68  // Check that all fields have the values that they should have after
69  // Set*Fields() is called.
70  static void ExpectAllFieldsSet(const unittest::TestAllTypes& message);
71  static void ExpectAllExtensionsSet(
72      const unittest::TestAllExtensions& message);
73  static void ExpectPackedFieldsSet(const unittest::TestPackedTypes& message);
74  static void ExpectPackedExtensionsSet(
75      const unittest::TestPackedExtensions& message);
76  static void ExpectUnpackedFieldsSet(
77      const unittest::TestUnpackedTypes& message);
78
79  // Expect that the message is modified as would be expected from
80  // Modify*Fields().
81  static void ExpectRepeatedFieldsModified(
82      const unittest::TestAllTypes& message);
83  static void ExpectRepeatedExtensionsModified(
84      const unittest::TestAllExtensions& message);
85  static void ExpectPackedFieldsModified(
86      const unittest::TestPackedTypes& message);
87  static void ExpectPackedExtensionsModified(
88      const unittest::TestPackedExtensions& message);
89
90  // Check that all fields have their default values.
91  static void ExpectClear(const unittest::TestAllTypes& message);
92  static void ExpectExtensionsClear(const unittest::TestAllExtensions& message);
93  static void ExpectPackedClear(const unittest::TestPackedTypes& message);
94  static void ExpectPackedExtensionsClear(
95      const unittest::TestPackedExtensions& message);
96
97  // Check that the passed-in serialization is the canonical serialization we
98  // expect for a TestFieldOrderings message filled in by
99  // SetAllFieldsAndExtensions().
100  static void ExpectAllFieldsAndExtensionsInOrder(const string& serialized);
101
102  // Check that all repeated fields have had their last elements removed.
103  static void ExpectLastRepeatedsRemoved(
104      const unittest::TestAllTypes& message);
105  static void ExpectLastRepeatedExtensionsRemoved(
106      const unittest::TestAllExtensions& message);
107
108  // Check that all repeated fields have had their first and last elements
109  // swapped.
110  static void ExpectRepeatedsSwapped(const unittest::TestAllTypes& message);
111  static void ExpectRepeatedExtensionsSwapped(
112      const unittest::TestAllExtensions& message);
113
114  // Like above, but use the reflection interface.
115  class ReflectionTester {
116   public:
117    // base_descriptor must be a descriptor for TestAllTypes or
118    // TestAllExtensions.  In the former case, ReflectionTester fetches from
119    // it the FieldDescriptors needed to use the reflection interface.  In
120    // the latter case, ReflectionTester searches for extension fields in
121    // its file.
122    explicit ReflectionTester(const Descriptor* base_descriptor);
123
124    void SetAllFieldsViaReflection(Message* message);
125    void ModifyRepeatedFieldsViaReflection(Message* message);
126    void ExpectAllFieldsSetViaReflection(const Message& message);
127    void ExpectClearViaReflection(const Message& message);
128
129    void SetPackedFieldsViaReflection(Message* message);
130    void ModifyPackedFieldsViaReflection(Message* message);
131    void ExpectPackedFieldsSetViaReflection(const Message& message);
132    void ExpectPackedClearViaReflection(const Message& message);
133
134    void RemoveLastRepeatedsViaReflection(Message* message);
135    void SwapRepeatedsViaReflection(Message* message);
136
137   private:
138    const FieldDescriptor* F(const string& name);
139
140    const Descriptor* base_descriptor_;
141
142    const FieldDescriptor* group_a_;
143    const FieldDescriptor* repeated_group_a_;
144    const FieldDescriptor* nested_b_;
145    const FieldDescriptor* foreign_c_;
146    const FieldDescriptor* import_d_;
147
148    const EnumValueDescriptor* nested_foo_;
149    const EnumValueDescriptor* nested_bar_;
150    const EnumValueDescriptor* nested_baz_;
151    const EnumValueDescriptor* foreign_foo_;
152    const EnumValueDescriptor* foreign_bar_;
153    const EnumValueDescriptor* foreign_baz_;
154    const EnumValueDescriptor* import_foo_;
155    const EnumValueDescriptor* import_bar_;
156    const EnumValueDescriptor* import_baz_;
157
158    // We have to split this into three function otherwise it creates a stack
159    // frame so large that it triggers a warning.
160    void ExpectAllFieldsSetViaReflection1(const Message& message);
161    void ExpectAllFieldsSetViaReflection2(const Message& message);
162    void ExpectAllFieldsSetViaReflection3(const Message& message);
163
164    GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ReflectionTester);
165  };
166
167 private:
168  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TestUtil);
169};
170
171}  // namespace protobuf
172
173}  // namespace google
174#endif  // GOOGLE_PROTOBUF_TEST_UTIL_H__
175