OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // A test utility that makes it easy to assert the exact presence of | |
6 // protobuf extensions in an extensible message. Example code: | |
7 // | |
8 // sync_pb::EntitySpecifics value; // Set up a test value. | |
9 // value.MutableExtension(sync_pb::bookmark); | |
10 // | |
11 // ProtoExtensionValidator<sync_pb::EntitySpecifics> good_expectation(value); | |
12 // good_expectation.ExpectHasExtension(sync_pb::bookmark); | |
13 // good_expectation.ExpectHasNoOtherFieldsOrExtensions(); | |
14 // | |
15 // ProtoExtensionValidator<sync_pb::EntitySpecifics> bad_expectation(value); | |
16 // bad_expectation.ExpectHasExtension(sync_pb::preference); // Fails, no pref | |
17 // bad_expectation.ExpectHasNoOtherFieldsOrExtensions(); // Fails, bookmark | |
18 | |
19 #ifndef CHROME_BROWSER_SYNC_TEST_ENGINE_PROTO_EXTENSION_VALIDATOR_H_ | |
20 #define CHROME_BROWSER_SYNC_TEST_ENGINE_PROTO_EXTENSION_VALIDATOR_H_ | |
21 #pragma once | |
22 | |
23 #include "base/string_util.h" | |
24 #include "chrome/browser/sync/test/engine/syncer_command_test.h" | |
25 #include "testing/gtest/include/gtest/gtest.h" | |
26 | |
27 namespace browser_sync { | |
28 | |
29 template<typename MessageType> | |
30 class ProtoExtensionValidator { | |
31 public: | |
32 explicit ProtoExtensionValidator(const MessageType& proto) | |
33 : proto_(proto) { | |
34 } | |
35 template<typename ExtensionTokenType> | |
36 void ExpectHasExtension(ExtensionTokenType token) { | |
37 EXPECT_TRUE(proto_.HasExtension(token)) | |
38 << "Proto failed to contain expected extension"; | |
39 proto_.ClearExtension(token); | |
40 EXPECT_FALSE(proto_.HasExtension(token)); | |
41 } | |
42 void ExpectNoOtherFieldsOrExtensions() { | |
43 EXPECT_EQ(MessageType::default_instance().SerializeAsString(), | |
44 proto_.SerializeAsString()) | |
45 << "Proto contained additional unexpected extensions."; | |
46 } | |
47 | |
48 private: | |
49 MessageType proto_; | |
50 DISALLOW_COPY_AND_ASSIGN(ProtoExtensionValidator); | |
51 }; | |
52 | |
53 } // namespace browser_sync | |
54 | |
55 #endif // CHROME_BROWSER_SYNC_TEST_ENGINE_PROTO_EXTENSION_VALIDATOR_H_ | |
OLD | NEW |