| OLD | NEW |
| 1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
| 3 // https://developers.google.com/protocol-buffers/ | 3 // http://code.google.com/p/protobuf/ |
| 4 // | 4 // |
| 5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
| 7 // met: | 7 // met: |
| 8 // | 8 // |
| 9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
| 11 // * Redistributions in binary form must reproduce the above | 11 // * Redistributions in binary form must reproduce the above |
| 12 // copyright notice, this list of conditions and the following disclaimer | 12 // copyright notice, this list of conditions and the following disclaimer |
| 13 // in the documentation and/or other materials provided with the | 13 // in the documentation and/or other materials provided with the |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 namespace unittest_import = protobuf_unittest_import; | 47 namespace unittest_import = protobuf_unittest_import; |
| 48 | 48 |
| 49 class TestUtil { | 49 class TestUtil { |
| 50 public: | 50 public: |
| 51 // Set every field in the message to a unique value. | 51 // Set every field in the message to a unique value. |
| 52 static void SetAllFields(unittest::TestAllTypes* message); | 52 static void SetAllFields(unittest::TestAllTypes* message); |
| 53 static void SetOptionalFields(unittest::TestAllTypes* message); | 53 static void SetOptionalFields(unittest::TestAllTypes* message); |
| 54 static void AddRepeatedFields1(unittest::TestAllTypes* message); | 54 static void AddRepeatedFields1(unittest::TestAllTypes* message); |
| 55 static void AddRepeatedFields2(unittest::TestAllTypes* message); | 55 static void AddRepeatedFields2(unittest::TestAllTypes* message); |
| 56 static void SetDefaultFields(unittest::TestAllTypes* message); | 56 static void SetDefaultFields(unittest::TestAllTypes* message); |
| 57 static void SetOneofFields(unittest::TestAllTypes* message); | |
| 58 static void SetAllExtensions(unittest::TestAllExtensions* message); | 57 static void SetAllExtensions(unittest::TestAllExtensions* message); |
| 59 static void SetOneofFields(unittest::TestAllExtensions* message); | |
| 60 static void SetAllFieldsAndExtensions(unittest::TestFieldOrderings* message); | 58 static void SetAllFieldsAndExtensions(unittest::TestFieldOrderings* message); |
| 61 static void SetPackedFields(unittest::TestPackedTypes* message); | 59 static void SetPackedFields(unittest::TestPackedTypes* message); |
| 62 static void SetPackedExtensions(unittest::TestPackedExtensions* message); | 60 static void SetPackedExtensions(unittest::TestPackedExtensions* message); |
| 63 static void SetUnpackedFields(unittest::TestUnpackedTypes* message); | 61 static void SetUnpackedFields(unittest::TestUnpackedTypes* message); |
| 64 static void SetOneof1(unittest::TestOneof2* message); | |
| 65 static void SetOneof2(unittest::TestOneof2* message); | |
| 66 | 62 |
| 67 // Use the repeated versions of the set_*() accessors to modify all the | 63 // Use the repeated versions of the set_*() accessors to modify all the |
| 68 // repeated fields of the messsage (which should already have been | 64 // repeated fields of the messsage (which should already have been |
| 69 // initialized with Set*Fields()). Set*Fields() itself only tests | 65 // initialized with Set*Fields()). Set*Fields() itself only tests |
| 70 // the add_*() accessors. | 66 // the add_*() accessors. |
| 71 static void ModifyRepeatedFields(unittest::TestAllTypes* message); | 67 static void ModifyRepeatedFields(unittest::TestAllTypes* message); |
| 72 static void ModifyRepeatedExtensions(unittest::TestAllExtensions* message); | 68 static void ModifyRepeatedExtensions(unittest::TestAllExtensions* message); |
| 73 static void ModifyPackedFields(unittest::TestPackedTypes* message); | 69 static void ModifyPackedFields(unittest::TestPackedTypes* message); |
| 74 static void ModifyPackedExtensions(unittest::TestPackedExtensions* message); | 70 static void ModifyPackedExtensions(unittest::TestPackedExtensions* message); |
| 75 | 71 |
| 76 // Check that all fields have the values that they should have after | 72 // Check that all fields have the values that they should have after |
| 77 // Set*Fields() is called. | 73 // Set*Fields() is called. |
| 78 static void ExpectAllFieldsSet(const unittest::TestAllTypes& message); | 74 static void ExpectAllFieldsSet(const unittest::TestAllTypes& message); |
| 79 static void ExpectAllExtensionsSet( | 75 static void ExpectAllExtensionsSet( |
| 80 const unittest::TestAllExtensions& message); | 76 const unittest::TestAllExtensions& message); |
| 81 static void ExpectPackedFieldsSet(const unittest::TestPackedTypes& message); | 77 static void ExpectPackedFieldsSet(const unittest::TestPackedTypes& message); |
| 82 static void ExpectPackedExtensionsSet( | 78 static void ExpectPackedExtensionsSet( |
| 83 const unittest::TestPackedExtensions& message); | 79 const unittest::TestPackedExtensions& message); |
| 84 static void ExpectUnpackedFieldsSet( | 80 static void ExpectUnpackedFieldsSet( |
| 85 const unittest::TestUnpackedTypes& message); | 81 const unittest::TestUnpackedTypes& message); |
| 86 static void ExpectUnpackedExtensionsSet( | |
| 87 const unittest::TestUnpackedExtensions& message); | |
| 88 static void ExpectOneofSet1(const unittest::TestOneof2& message); | |
| 89 static void ExpectOneofSet2(const unittest::TestOneof2& message); | |
| 90 | 82 |
| 91 // Expect that the message is modified as would be expected from | 83 // Expect that the message is modified as would be expected from |
| 92 // Modify*Fields(). | 84 // Modify*Fields(). |
| 93 static void ExpectRepeatedFieldsModified( | 85 static void ExpectRepeatedFieldsModified( |
| 94 const unittest::TestAllTypes& message); | 86 const unittest::TestAllTypes& message); |
| 95 static void ExpectRepeatedExtensionsModified( | 87 static void ExpectRepeatedExtensionsModified( |
| 96 const unittest::TestAllExtensions& message); | 88 const unittest::TestAllExtensions& message); |
| 97 static void ExpectPackedFieldsModified( | 89 static void ExpectPackedFieldsModified( |
| 98 const unittest::TestPackedTypes& message); | 90 const unittest::TestPackedTypes& message); |
| 99 static void ExpectPackedExtensionsModified( | 91 static void ExpectPackedExtensionsModified( |
| 100 const unittest::TestPackedExtensions& message); | 92 const unittest::TestPackedExtensions& message); |
| 101 | 93 |
| 102 // Check that all fields have their default values. | 94 // Check that all fields have their default values. |
| 103 static void ExpectClear(const unittest::TestAllTypes& message); | 95 static void ExpectClear(const unittest::TestAllTypes& message); |
| 104 static void ExpectExtensionsClear(const unittest::TestAllExtensions& message); | 96 static void ExpectExtensionsClear(const unittest::TestAllExtensions& message); |
| 105 static void ExpectPackedClear(const unittest::TestPackedTypes& message); | 97 static void ExpectPackedClear(const unittest::TestPackedTypes& message); |
| 106 static void ExpectPackedExtensionsClear( | 98 static void ExpectPackedExtensionsClear( |
| 107 const unittest::TestPackedExtensions& message); | 99 const unittest::TestPackedExtensions& message); |
| 108 static void ExpectOneofClear(const unittest::TestOneof2& message); | |
| 109 | 100 |
| 110 // Check that the passed-in serialization is the canonical serialization we | 101 // Check that the passed-in serialization is the canonical serialization we |
| 111 // expect for a TestFieldOrderings message filled in by | 102 // expect for a TestFieldOrderings message filled in by |
| 112 // SetAllFieldsAndExtensions(). | 103 // SetAllFieldsAndExtensions(). |
| 113 static void ExpectAllFieldsAndExtensionsInOrder(const string& serialized); | 104 static void ExpectAllFieldsAndExtensionsInOrder(const string& serialized); |
| 114 | 105 |
| 115 // Check that all repeated fields have had their last elements removed. | 106 // Check that all repeated fields have had their last elements removed. |
| 116 static void ExpectLastRepeatedsRemoved( | 107 static void ExpectLastRepeatedsRemoved( |
| 117 const unittest::TestAllTypes& message); | 108 const unittest::TestAllTypes& message); |
| 118 static void ExpectLastRepeatedExtensionsRemoved( | 109 static void ExpectLastRepeatedExtensionsRemoved( |
| 119 const unittest::TestAllExtensions& message); | 110 const unittest::TestAllExtensions& message); |
| 120 static void ExpectLastRepeatedsReleased( | 111 static void ExpectLastRepeatedsReleased( |
| 121 const unittest::TestAllTypes& message); | 112 const unittest::TestAllTypes& message); |
| 122 static void ExpectLastRepeatedExtensionsReleased( | 113 static void ExpectLastRepeatedExtensionsReleased( |
| 123 const unittest::TestAllExtensions& message); | 114 const unittest::TestAllExtensions& message); |
| 124 | 115 |
| 125 // Check that all repeated fields have had their first and last elements | 116 // Check that all repeated fields have had their first and last elements |
| 126 // swapped. | 117 // swapped. |
| 127 static void ExpectRepeatedsSwapped(const unittest::TestAllTypes& message); | 118 static void ExpectRepeatedsSwapped(const unittest::TestAllTypes& message); |
| 128 static void ExpectRepeatedExtensionsSwapped( | 119 static void ExpectRepeatedExtensionsSwapped( |
| 129 const unittest::TestAllExtensions& message); | 120 const unittest::TestAllExtensions& message); |
| 130 | 121 |
| 131 static void ExpectAtMostOneFieldSetInOneof( | |
| 132 const unittest::TestOneof2 &message); | |
| 133 | |
| 134 // Like above, but use the reflection interface. | 122 // Like above, but use the reflection interface. |
| 135 class ReflectionTester { | 123 class ReflectionTester { |
| 136 public: | 124 public: |
| 137 // base_descriptor must be a descriptor for TestAllTypes or | 125 // base_descriptor must be a descriptor for TestAllTypes or |
| 138 // TestAllExtensions. In the former case, ReflectionTester fetches from | 126 // TestAllExtensions. In the former case, ReflectionTester fetches from |
| 139 // it the FieldDescriptors needed to use the reflection interface. In | 127 // it the FieldDescriptors needed to use the reflection interface. In |
| 140 // the latter case, ReflectionTester searches for extension fields in | 128 // the latter case, ReflectionTester searches for extension fields in |
| 141 // its file. | 129 // its file. |
| 142 explicit ReflectionTester(const Descriptor* base_descriptor); | 130 explicit ReflectionTester(const Descriptor* base_descriptor); |
| 143 | 131 |
| 144 void SetAllFieldsViaReflection(Message* message); | 132 void SetAllFieldsViaReflection(Message* message); |
| 145 void ModifyRepeatedFieldsViaReflection(Message* message); | 133 void ModifyRepeatedFieldsViaReflection(Message* message); |
| 146 void ExpectAllFieldsSetViaReflection(const Message& message); | 134 void ExpectAllFieldsSetViaReflection(const Message& message); |
| 147 void ExpectClearViaReflection(const Message& message); | 135 void ExpectClearViaReflection(const Message& message); |
| 148 | 136 |
| 149 void SetPackedFieldsViaReflection(Message* message); | 137 void SetPackedFieldsViaReflection(Message* message); |
| 150 void ModifyPackedFieldsViaReflection(Message* message); | 138 void ModifyPackedFieldsViaReflection(Message* message); |
| 151 void ExpectPackedFieldsSetViaReflection(const Message& message); | 139 void ExpectPackedFieldsSetViaReflection(const Message& message); |
| 152 void ExpectPackedClearViaReflection(const Message& message); | 140 void ExpectPackedClearViaReflection(const Message& message); |
| 153 | 141 |
| 154 void RemoveLastRepeatedsViaReflection(Message* message); | 142 void RemoveLastRepeatedsViaReflection(Message* message); |
| 155 void ReleaseLastRepeatedsViaReflection( | 143 void ReleaseLastRepeatedsViaReflection( |
| 156 Message* message, bool expect_extensions_notnull); | 144 Message* message, bool expect_extensions_notnull); |
| 157 void SwapRepeatedsViaReflection(Message* message); | 145 void SwapRepeatedsViaReflection(Message* message); |
| 158 void SetAllocatedOptionalMessageFieldsToNullViaReflection( | |
| 159 Message* message); | |
| 160 static void SetAllocatedOptionalMessageFieldsToMessageViaReflection( | |
| 161 Message* from_message, | |
| 162 Message* to_message); | |
| 163 | 146 |
| 164 enum MessageReleaseState { | 147 enum MessageReleaseState { |
| 165 IS_NULL, | 148 IS_NULL, |
| 166 CAN_BE_NULL, | 149 CAN_BE_NULL, |
| 167 NOT_NULL, | 150 NOT_NULL, |
| 168 }; | 151 }; |
| 169 void ExpectMessagesReleasedViaReflection( | 152 void ExpectMessagesReleasedViaReflection( |
| 170 Message* message, MessageReleaseState expected_release_state); | 153 Message* message, MessageReleaseState expected_release_state); |
| 171 | 154 |
| 172 // Set and check functions for TestOneof2 messages. No need to construct | |
| 173 // the ReflectionTester by TestAllTypes nor TestAllExtensions. | |
| 174 static void SetOneofViaReflection(Message* message); | |
| 175 static void ExpectOneofSetViaReflection(const Message& message); | |
| 176 | |
| 177 private: | 155 private: |
| 178 const FieldDescriptor* F(const string& name); | 156 const FieldDescriptor* F(const string& name); |
| 179 | 157 |
| 180 const Descriptor* base_descriptor_; | 158 const Descriptor* base_descriptor_; |
| 181 | 159 |
| 182 const FieldDescriptor* group_a_; | 160 const FieldDescriptor* group_a_; |
| 183 const FieldDescriptor* repeated_group_a_; | 161 const FieldDescriptor* repeated_group_a_; |
| 184 const FieldDescriptor* nested_b_; | 162 const FieldDescriptor* nested_b_; |
| 185 const FieldDescriptor* foreign_c_; | 163 const FieldDescriptor* foreign_c_; |
| 186 const FieldDescriptor* import_d_; | 164 const FieldDescriptor* import_d_; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 206 }; | 184 }; |
| 207 | 185 |
| 208 private: | 186 private: |
| 209 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TestUtil); | 187 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TestUtil); |
| 210 }; | 188 }; |
| 211 | 189 |
| 212 } // namespace protobuf | 190 } // namespace protobuf |
| 213 | 191 |
| 214 } // namespace google | 192 } // namespace google |
| 215 #endif // GOOGLE_PROTOBUF_TEST_UTIL_H__ | 193 #endif // GOOGLE_PROTOBUF_TEST_UTIL_H__ |
| OLD | NEW |