/src/protobuf/src/google/protobuf/test_util.h

http://decs.googlecode.com/ · C Header · 156 lines · 80 code · 23 blank · 53 comment · 0 complexity · e12efc86906164eac5743a1b93ee625a MD5 · raw file

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