OsmAnd
jni/protobuf/google/protobuf/test_util.h
Go to the documentation of this file.
00001 // Protocol Buffers - Google's data interchange format
00002 // Copyright 2008 Google Inc.  All rights reserved.
00003 // http://code.google.com/p/protobuf/
00004 //
00005 // Redistribution and use in source and binary forms, with or without
00006 // modification, are permitted provided that the following conditions are
00007 // met:
00008 //
00009 //     * Redistributions of source code must retain the above copyright
00010 // notice, this list of conditions and the following disclaimer.
00011 //     * Redistributions in binary form must reproduce the above
00012 // copyright notice, this list of conditions and the following disclaimer
00013 // in the documentation and/or other materials provided with the
00014 // distribution.
00015 //     * Neither the name of Google Inc. nor the names of its
00016 // contributors may be used to endorse or promote products derived from
00017 // this software without specific prior written permission.
00018 //
00019 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00022 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00023 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00024 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00025 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00026 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00027 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00029 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 
00031 // Author: kenton@google.com (Kenton Varda)
00032 //  Based on original Protocol Buffers design by
00033 //  Sanjay Ghemawat, Jeff Dean, and others.
00034 
00035 #ifndef GOOGLE_PROTOBUF_TEST_UTIL_H__
00036 #define GOOGLE_PROTOBUF_TEST_UTIL_H__
00037 
00038 #include <stack>
00039 #include <string>
00040 #include <google/protobuf/message.h>
00041 #include <google/protobuf/unittest.pb.h>
00042 
00043 namespace google {
00044 namespace protobuf {
00045 
00046 namespace unittest = protobuf_unittest;
00047 namespace unittest_import = protobuf_unittest_import;
00048 
00049 class TestUtil {
00050  public:
00051   // Set every field in the message to a unique value.
00052   static void SetAllFields(unittest::TestAllTypes* message);
00053   static void SetAllExtensions(unittest::TestAllExtensions* message);
00054   static void SetAllFieldsAndExtensions(unittest::TestFieldOrderings* message);
00055   static void SetPackedFields(unittest::TestPackedTypes* message);
00056   static void SetPackedExtensions(unittest::TestPackedExtensions* message);
00057   static void SetUnpackedFields(unittest::TestUnpackedTypes* message);
00058 
00059   // Use the repeated versions of the set_*() accessors to modify all the
00060   // repeated fields of the messsage (which should already have been
00061   // initialized with Set*Fields()).  Set*Fields() itself only tests
00062   // the add_*() accessors.
00063   static void ModifyRepeatedFields(unittest::TestAllTypes* message);
00064   static void ModifyRepeatedExtensions(unittest::TestAllExtensions* message);
00065   static void ModifyPackedFields(unittest::TestPackedTypes* message);
00066   static void ModifyPackedExtensions(unittest::TestPackedExtensions* message);
00067 
00068   // Check that all fields have the values that they should have after
00069   // Set*Fields() is called.
00070   static void ExpectAllFieldsSet(const unittest::TestAllTypes& message);
00071   static void ExpectAllExtensionsSet(
00072       const unittest::TestAllExtensions& message);
00073   static void ExpectPackedFieldsSet(const unittest::TestPackedTypes& message);
00074   static void ExpectPackedExtensionsSet(
00075       const unittest::TestPackedExtensions& message);
00076   static void ExpectUnpackedFieldsSet(
00077       const unittest::TestUnpackedTypes& message);
00078 
00079   // Expect that the message is modified as would be expected from
00080   // Modify*Fields().
00081   static void ExpectRepeatedFieldsModified(
00082       const unittest::TestAllTypes& message);
00083   static void ExpectRepeatedExtensionsModified(
00084       const unittest::TestAllExtensions& message);
00085   static void ExpectPackedFieldsModified(
00086       const unittest::TestPackedTypes& message);
00087   static void ExpectPackedExtensionsModified(
00088       const unittest::TestPackedExtensions& message);
00089 
00090   // Check that all fields have their default values.
00091   static void ExpectClear(const unittest::TestAllTypes& message);
00092   static void ExpectExtensionsClear(const unittest::TestAllExtensions& message);
00093   static void ExpectPackedClear(const unittest::TestPackedTypes& message);
00094   static void ExpectPackedExtensionsClear(
00095       const unittest::TestPackedExtensions& message);
00096 
00097   // Check that the passed-in serialization is the canonical serialization we
00098   // expect for a TestFieldOrderings message filled in by
00099   // SetAllFieldsAndExtensions().
00100   static void ExpectAllFieldsAndExtensionsInOrder(const string& serialized);
00101 
00102   // Check that all repeated fields have had their last elements removed.
00103   static void ExpectLastRepeatedsRemoved(
00104       const unittest::TestAllTypes& message);
00105   static void ExpectLastRepeatedExtensionsRemoved(
00106       const unittest::TestAllExtensions& message);
00107 
00108   // Check that all repeated fields have had their first and last elements
00109   // swapped.
00110   static void ExpectRepeatedsSwapped(const unittest::TestAllTypes& message);
00111   static void ExpectRepeatedExtensionsSwapped(
00112       const unittest::TestAllExtensions& message);
00113 
00114   // Like above, but use the reflection interface.
00115   class ReflectionTester {
00116    public:
00117     // base_descriptor must be a descriptor for TestAllTypes or
00118     // TestAllExtensions.  In the former case, ReflectionTester fetches from
00119     // it the FieldDescriptors needed to use the reflection interface.  In
00120     // the latter case, ReflectionTester searches for extension fields in
00121     // its file.
00122     explicit ReflectionTester(const Descriptor* base_descriptor);
00123 
00124     void SetAllFieldsViaReflection(Message* message);
00125     void ModifyRepeatedFieldsViaReflection(Message* message);
00126     void ExpectAllFieldsSetViaReflection(const Message& message);
00127     void ExpectClearViaReflection(const Message& message);
00128 
00129     void SetPackedFieldsViaReflection(Message* message);
00130     void ModifyPackedFieldsViaReflection(Message* message);
00131     void ExpectPackedFieldsSetViaReflection(const Message& message);
00132     void ExpectPackedClearViaReflection(const Message& message);
00133 
00134     void RemoveLastRepeatedsViaReflection(Message* message);
00135     void SwapRepeatedsViaReflection(Message* message);
00136 
00137    private:
00138     const FieldDescriptor* F(const string& name);
00139 
00140     const Descriptor* base_descriptor_;
00141 
00142     const FieldDescriptor* group_a_;
00143     const FieldDescriptor* repeated_group_a_;
00144     const FieldDescriptor* nested_b_;
00145     const FieldDescriptor* foreign_c_;
00146     const FieldDescriptor* import_d_;
00147 
00148     const EnumValueDescriptor* nested_foo_;
00149     const EnumValueDescriptor* nested_bar_;
00150     const EnumValueDescriptor* nested_baz_;
00151     const EnumValueDescriptor* foreign_foo_;
00152     const EnumValueDescriptor* foreign_bar_;
00153     const EnumValueDescriptor* foreign_baz_;
00154     const EnumValueDescriptor* import_foo_;
00155     const EnumValueDescriptor* import_bar_;
00156     const EnumValueDescriptor* import_baz_;
00157 
00158     // We have to split this into three function otherwise it creates a stack
00159     // frame so large that it triggers a warning.
00160     void ExpectAllFieldsSetViaReflection1(const Message& message);
00161     void ExpectAllFieldsSetViaReflection2(const Message& message);
00162     void ExpectAllFieldsSetViaReflection3(const Message& message);
00163 
00164     GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ReflectionTester);
00165   };
00166 
00167  private:
00168   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TestUtil);
00169 };
00170 
00171 }  // namespace protobuf
00172 
00173 }  // namespace google
00174 #endif  // GOOGLE_PROTOBUF_TEST_UTIL_H__
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines