• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
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 
31 #include <memory>
32 
33 #include <google/protobuf/compiler/cpp/cpp_helpers.h>
34 #include <google/protobuf/compiler/cpp/cpp_generator.h>
35 #include <google/protobuf/compiler/annotation_test_util.h>
36 #include <google/protobuf/compiler/command_line_interface.h>
37 #include <google/protobuf/descriptor.pb.h>
38 
39 #include <google/protobuf/testing/file.h>
40 #include <google/protobuf/testing/file.h>
41 #include <google/protobuf/testing/googletest.h>
42 #include <gtest/gtest.h>
43 
44 namespace google {
45 namespace protobuf {
46 namespace compiler {
47 namespace cpp {
48 
49 namespace atu = annotation_test_util;
50 
51 namespace {
52 
53 class CppMetadataTest : public ::testing::Test {
54  public:
55   // Tries to capture a FileDescriptorProto, GeneratedCodeInfo, and output
56   // code from the previously added file with name `filename`. Returns true on
57   // success. If pb_h is non-null, expects a .pb.h and a .pb.h.meta (copied to
58   // pb_h and pb_h_info respecfively); similarly for proto_h and proto_h_info.
CaptureMetadata(const std::string & filename,FileDescriptorProto * file,std::string * pb_h,GeneratedCodeInfo * pb_h_info,std::string * proto_h,GeneratedCodeInfo * proto_h_info,std::string * pb_cc)59   bool CaptureMetadata(const std::string& filename, FileDescriptorProto* file,
60                        std::string* pb_h, GeneratedCodeInfo* pb_h_info,
61                        std::string* proto_h, GeneratedCodeInfo* proto_h_info,
62                        std::string* pb_cc) {
63     CommandLineInterface cli;
64     CppGenerator cpp_generator;
65     cli.RegisterGenerator("--cpp_out", &cpp_generator, "");
66     std::string cpp_out =
67         "--cpp_out=annotate_headers=true,"
68         "annotation_pragma_name=pragma_name,"
69         "annotation_guard_name=guard_name:" +
70         TestTempDir();
71 
72     const bool result = atu::RunProtoCompiler(filename, cpp_out, &cli, file);
73 
74     if (!result) {
75       return result;
76     }
77 
78     std::string output_base = TestTempDir() + "/" + StripProto(filename);
79 
80     if (pb_cc != NULL) {
81       GOOGLE_CHECK_OK(
82           File::GetContents(output_base + ".pb.cc", pb_cc, true));
83     }
84 
85     if (pb_h != NULL && pb_h_info != NULL) {
86       GOOGLE_CHECK_OK(
87           File::GetContents(output_base + ".pb.h", pb_h, true));
88       if (!atu::DecodeMetadata(output_base + ".pb.h.meta", pb_h_info)) {
89         return false;
90       }
91     }
92 
93     if (proto_h != NULL && proto_h_info != NULL) {
94       GOOGLE_CHECK_OK(File::GetContents(output_base + ".proto.h", proto_h,
95                                  true));
96       if (!atu::DecodeMetadata(output_base + ".proto.h.meta", proto_h_info)) {
97         return false;
98       }
99     }
100 
101     return true;
102   }
103 };
104 
105 const char kSmallTestFile[] =
106     "syntax = \"proto2\";\n"
107     "package foo;\n"
108     "enum Enum { VALUE = 0; }\n"
109     "message Message { }\n";
110 
TEST_F(CppMetadataTest,CapturesEnumNames)111 TEST_F(CppMetadataTest, CapturesEnumNames) {
112   FileDescriptorProto file;
113   GeneratedCodeInfo info;
114   std::string pb_h;
115   atu::AddFile("test.proto", kSmallTestFile);
116   EXPECT_TRUE(
117       CaptureMetadata("test.proto", &file, &pb_h, &info, NULL, NULL, NULL));
118   EXPECT_EQ("Enum", file.enum_type(0).name());
119   std::vector<int> enum_path;
120   enum_path.push_back(FileDescriptorProto::kEnumTypeFieldNumber);
121   enum_path.push_back(0);
122   const GeneratedCodeInfo::Annotation* enum_annotation =
123       atu::FindAnnotationOnPath(info, "test.proto", enum_path);
124   EXPECT_TRUE(NULL != enum_annotation);
125   EXPECT_TRUE(atu::AnnotationMatchesSubstring(pb_h, enum_annotation, "Enum"));
126 }
127 
TEST_F(CppMetadataTest,AddsPragma)128 TEST_F(CppMetadataTest, AddsPragma) {
129   FileDescriptorProto file;
130   GeneratedCodeInfo info;
131   std::string pb_h;
132   atu::AddFile("test.proto", kSmallTestFile);
133   EXPECT_TRUE(
134       CaptureMetadata("test.proto", &file, &pb_h, &info, NULL, NULL, NULL));
135   EXPECT_TRUE(pb_h.find("#ifdef guard_name") != string::npos);
136   EXPECT_TRUE(pb_h.find("#pragma pragma_name \"test.pb.h.meta\"") !=
137               std::string::npos);
138 }
139 
TEST_F(CppMetadataTest,CapturesMessageNames)140 TEST_F(CppMetadataTest, CapturesMessageNames) {
141   FileDescriptorProto file;
142   GeneratedCodeInfo info;
143   std::string pb_h;
144   atu::AddFile("test.proto", kSmallTestFile);
145   EXPECT_TRUE(
146       CaptureMetadata("test.proto", &file, &pb_h, &info, NULL, NULL, NULL));
147   EXPECT_EQ("Message", file.message_type(0).name());
148   std::vector<int> message_path;
149   message_path.push_back(FileDescriptorProto::kMessageTypeFieldNumber);
150   message_path.push_back(0);
151   const GeneratedCodeInfo::Annotation* message_annotation =
152       atu::FindAnnotationOnPath(info, "test.proto", message_path);
153   EXPECT_TRUE(NULL != message_annotation);
154   EXPECT_TRUE(
155       atu::AnnotationMatchesSubstring(pb_h, message_annotation, "Message"));
156 }
157 
158 }  // namespace
159 }  // namespace cpp
160 }  // namespace compiler
161 }  // namespace protobuf
162 }  // namespace google
163