1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
base / test / test_proto_loader.h [blame]
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef BASE_TEST_TEST_PROTO_LOADER_H_
#define BASE_TEST_TEST_PROTO_LOADER_H_
#include <memory>
#include <string>
#include <string_view>
#include "base/files/file_path.h"
namespace base {
#if defined(COMPONENT_BUILD)
#if defined(WIN32)
#if defined(PROTO_TEST_IMPLEMENTATION)
#define PROTO_TEST_EXPORT __declspec(dllexport)
#else
#define PROTO_TEST_EXPORT __declspec(dllimport)
#endif // defined(PROTO_TEST_IMPLEMENTATION)
#else // defined(WIN32)
#if defined(PROTO_TEST_IMPLEMENTATION)
#define PROTO_TEST_EXPORT __attribute__((visibility("default")))
#else
#define PROTO_TEST_EXPORT
#endif
#endif
#else // defined(COMPONENT_BUILD)
#define PROTO_TEST_EXPORT
#endif
// This class works around the fact that chrome only includes the lite runtime
// of protobufs. Lite protobufs inherit from |MessageLite| and cannot be used to
// parse from text format. Parsing from text
// format is useful in tests. We cannot include the full version of a protobuf
// in test code because it would clash with the lite version.
//
// This class uses the protobuf descriptors (generated at compile time) to
// generate a |Message| that can be used to parse from text. This message can
// then be serialized to binary which can be parsed by the |MessageLite|.
class PROTO_TEST_EXPORT TestProtoSetLoader {
public:
explicit TestProtoSetLoader(const base::FilePath& descriptor_path);
explicit TestProtoSetLoader(std::string_view descriptor_binary_proto);
~TestProtoSetLoader();
TestProtoSetLoader(const TestProtoSetLoader&) = delete;
TestProtoSetLoader& operator=(const TestProtoSetLoader&) = delete;
// Parse a text proto into a binary proto. `type_name` is the full message
// type name including the package. This CHECK fails if the type is not
// found, or if the text cannot be parsed.
std::string ParseFromText(std::string_view type_name,
const std::string& proto_text) const;
// Returns the text proto format of `message`. This CHECK fails on error.
std::string PrintToText(std::string_view type_name,
const std::string& message) const;
private:
// Hide dependencies of protobuf_full from the header, so that
// proto_test_support doesn't need to add protobuf_full as a public dep.
class State;
std::unique_ptr<State> state_;
};
// Same as TestProtoSetLoader, but for a single message type.
class PROTO_TEST_EXPORT TestProtoLoader {
public:
TestProtoLoader(const base::FilePath& descriptor_path,
std::string_view type_name);
TestProtoLoader(std::string_view descriptor_binary_proto,
std::string_view type_name);
~TestProtoLoader();
TestProtoLoader(const TestProtoLoader&) = delete;
TestProtoLoader& operator=(const TestProtoLoader&) = delete;
void ParseFromText(const std::string& proto_text, std::string& message) const;
void PrintToText(const std::string& message, std::string& proto_text) const;
private:
TestProtoSetLoader set_loader_;
std::string type_name_;
};
} // namespace base
#endif // BASE_TEST_TEST_PROTO_LOADER_H_