Initial import of v2.0.0beta
[protobuf.git] / src / google / protobuf / test_util.h
blob4157d095f4c62a5ef2955843fa99c566e42f81ee
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.
3 // http://code.google.com/p/protobuf/
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 // Author: kenton@google.com (Kenton Varda)
18 // Based on original Protocol Buffers design by
19 // Sanjay Ghemawat, Jeff Dean, and others.
21 #ifndef GOOGLE_PROTOBUF_TEST_UTIL_H__
22 #define GOOGLE_PROTOBUF_TEST_UTIL_H__
24 #include <stack>
25 #include <string>
26 #include <google/protobuf/message.h>
27 #include <google/protobuf/unittest.pb.h>
29 namespace google {
30 namespace protobuf {
32 namespace unittest = protobuf_unittest;
33 namespace unittest_import = protobuf_unittest_import;
35 class TestUtil {
36 public:
37 // Set every field in the message to a unique value.
38 static void SetAllFields(unittest::TestAllTypes* message);
39 static void SetAllExtensions(unittest::TestAllExtensions* message);
40 static void SetAllFieldsAndExtensions(unittest::TestFieldOrderings* message);
42 // Use the repeated versions of the set_*() accessors to modify all the
43 // repeated fields of the messsage (which should already have been
44 // initialized with SetAllFields()). SetAllFields() itself only tests
45 // the add_*() accessors.
46 static void ModifyRepeatedFields(unittest::TestAllTypes* message);
47 static void ModifyRepeatedExtensions(unittest::TestAllExtensions* message);
49 // Check that all fields have the values that they should have after
50 // SetAllFields() is called.
51 static void ExpectAllFieldsSet(const unittest::TestAllTypes& message);
52 static void ExpectAllExtensionsSet(
53 const unittest::TestAllExtensions& message);
55 // Expect that the message is modified as would be expected from
56 // ModifyRepeatedFields().
57 static void ExpectRepeatedFieldsModified(
58 const unittest::TestAllTypes& message);
59 static void ExpectRepeatedExtensionsModified(
60 const unittest::TestAllExtensions& message);
62 // Check that all fields have their default values.
63 static void ExpectClear(const unittest::TestAllTypes& message);
64 static void ExpectExtensionsClear(const unittest::TestAllExtensions& message);
66 // Check that the passed-in serialization is the canonical serialization we
67 // expect for a TestFieldOrderings message filled in by
68 // SetAllFieldsAndExtensions().
69 static void ExpectAllFieldsAndExtensionsInOrder(const string& serialized);
71 // Like above, but use the reflection interface.
72 class ReflectionTester {
73 public:
74 // base_descriptor must be a descriptor for TestAllTypes or
75 // TestAllExtensions. In the former case, ReflectionTester fetches from
76 // it the FieldDescriptors needed to use the reflection interface. In
77 // the latter case, ReflectionTester searches for extension fields in
78 // its file.
79 explicit ReflectionTester(const Descriptor* base_descriptor);
81 void SetAllFieldsViaReflection(Message::Reflection* message);
82 void ModifyRepeatedFieldsViaReflection(Message::Reflection* message);
83 void ExpectAllFieldsSetViaReflection(
84 const Message::Reflection& message);
85 void ExpectClearViaReflection(const Message::Reflection& message);
87 private:
88 const FieldDescriptor* F(const string& name);
90 const Descriptor* base_descriptor_;
92 const FieldDescriptor* group_a_;
93 const FieldDescriptor* repeated_group_a_;
94 const FieldDescriptor* nested_b_;
95 const FieldDescriptor* foreign_c_;
96 const FieldDescriptor* import_d_;
98 const EnumValueDescriptor* nested_foo_;
99 const EnumValueDescriptor* nested_bar_;
100 const EnumValueDescriptor* nested_baz_;
101 const EnumValueDescriptor* foreign_foo_;
102 const EnumValueDescriptor* foreign_bar_;
103 const EnumValueDescriptor* foreign_baz_;
104 const EnumValueDescriptor* import_foo_;
105 const EnumValueDescriptor* import_bar_;
106 const EnumValueDescriptor* import_baz_;
108 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ReflectionTester);
111 private:
112 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TestUtil);
115 } // namespace protobuf
117 } // namespace google
118 #endif // GOOGLE_PROTOBUF_TEST_UTIL_H__