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 // https://developers.google.com/protocol-buffers/ |
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. |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 const char kSmallTestFile[] = | 149 const char kSmallTestFile[] = |
150 "syntax = \"proto2\";\n" | 150 "syntax = \"proto2\";\n" |
151 "package foo;\n" | 151 "package foo;\n" |
152 "enum Enum { VALUE = 0; }\n" | 152 "enum Enum { VALUE = 0; }\n" |
153 "message Message { }\n"; | 153 "message Message { }\n"; |
154 | 154 |
155 // Finds the Annotation for a given source file and path (or returns null if it | 155 // Finds the Annotation for a given source file and path (or returns null if it |
156 // couldn't). | 156 // couldn't). |
157 const GeneratedCodeInfo::Annotation* FindAnnotationOnPath( | 157 const GeneratedCodeInfo::Annotation* FindAnnotationOnPath( |
158 const GeneratedCodeInfo& info, const string& source_file, | 158 const GeneratedCodeInfo& info, const string& source_file, |
159 const std::vector<int>& path) { | 159 const vector<int>& path) { |
160 for (int i = 0; i < info.annotation_size(); ++i) { | 160 for (int i = 0; i < info.annotation_size(); ++i) { |
161 const GeneratedCodeInfo::Annotation* annotation = &info.annotation(i); | 161 const GeneratedCodeInfo::Annotation* annotation = &info.annotation(i); |
162 if (annotation->source_file() != source_file || | 162 if (annotation->source_file() != source_file || |
163 annotation->path_size() != path.size()) { | 163 annotation->path_size() != path.size()) { |
164 continue; | 164 continue; |
165 } | 165 } |
166 int node = 0; | 166 int node = 0; |
167 for (; node < path.size(); ++node) { | 167 for (; node < path.size(); ++node) { |
168 if (annotation->path(node) != path[node]) { | 168 if (annotation->path(node) != path[node]) { |
169 break; | 169 break; |
(...skipping 20 matching lines...) Expand all Loading... |
190 } | 190 } |
191 | 191 |
192 TEST_F(CppMetadataTest, CapturesEnumNames) { | 192 TEST_F(CppMetadataTest, CapturesEnumNames) { |
193 FileDescriptorProto file; | 193 FileDescriptorProto file; |
194 GeneratedCodeInfo info; | 194 GeneratedCodeInfo info; |
195 string pb_h; | 195 string pb_h; |
196 AddFile("test.proto", kSmallTestFile); | 196 AddFile("test.proto", kSmallTestFile); |
197 EXPECT_TRUE( | 197 EXPECT_TRUE( |
198 CaptureMetadata("test.proto", &file, &pb_h, &info, NULL, NULL, NULL)); | 198 CaptureMetadata("test.proto", &file, &pb_h, &info, NULL, NULL, NULL)); |
199 EXPECT_EQ("Enum", file.enum_type(0).name()); | 199 EXPECT_EQ("Enum", file.enum_type(0).name()); |
200 std::vector<int> enum_path; | 200 vector<int> enum_path; |
201 enum_path.push_back(FileDescriptorProto::kEnumTypeFieldNumber); | 201 enum_path.push_back(FileDescriptorProto::kEnumTypeFieldNumber); |
202 enum_path.push_back(0); | 202 enum_path.push_back(0); |
203 const GeneratedCodeInfo::Annotation* enum_annotation = | 203 const GeneratedCodeInfo::Annotation* enum_annotation = |
204 FindAnnotationOnPath(info, "test.proto", enum_path); | 204 FindAnnotationOnPath(info, "test.proto", enum_path); |
205 EXPECT_TRUE(NULL != enum_annotation); | 205 EXPECT_TRUE(NULL != enum_annotation); |
206 EXPECT_TRUE(AnnotationMatchesSubstring(pb_h, enum_annotation, "Enum")); | 206 EXPECT_TRUE(AnnotationMatchesSubstring(pb_h, enum_annotation, "Enum")); |
207 } | 207 } |
208 | 208 |
209 TEST_F(CppMetadataTest, AddsPragma) { | 209 TEST_F(CppMetadataTest, AddsPragma) { |
210 FileDescriptorProto file; | 210 FileDescriptorProto file; |
211 GeneratedCodeInfo info; | 211 GeneratedCodeInfo info; |
212 string pb_h; | 212 string pb_h; |
213 AddFile("test.proto", kSmallTestFile); | 213 AddFile("test.proto", kSmallTestFile); |
214 EXPECT_TRUE( | 214 EXPECT_TRUE( |
215 CaptureMetadata("test.proto", &file, &pb_h, &info, NULL, NULL, NULL)); | 215 CaptureMetadata("test.proto", &file, &pb_h, &info, NULL, NULL, NULL)); |
216 EXPECT_TRUE(pb_h.find("#ifdef guard_name") != string::npos); | 216 EXPECT_TRUE(pb_h.find("#ifdef guard_name") != string::npos); |
217 EXPECT_TRUE(pb_h.find("#pragma pragma_name \"test.pb.h.meta\"") != | 217 EXPECT_TRUE(pb_h.find("#pragma pragma_name \"test.pb.h.meta\"") != |
218 string::npos); | 218 string::npos); |
219 } | 219 } |
220 | 220 |
221 TEST_F(CppMetadataTest, CapturesMessageNames) { | 221 TEST_F(CppMetadataTest, CapturesMessageNames) { |
222 FileDescriptorProto file; | 222 FileDescriptorProto file; |
223 GeneratedCodeInfo info; | 223 GeneratedCodeInfo info; |
224 string pb_h; | 224 string pb_h; |
225 AddFile("test.proto", kSmallTestFile); | 225 AddFile("test.proto", kSmallTestFile); |
226 EXPECT_TRUE( | 226 EXPECT_TRUE( |
227 CaptureMetadata("test.proto", &file, &pb_h, &info, NULL, NULL, NULL)); | 227 CaptureMetadata("test.proto", &file, &pb_h, &info, NULL, NULL, NULL)); |
228 EXPECT_EQ("Message", file.message_type(0).name()); | 228 EXPECT_EQ("Message", file.message_type(0).name()); |
229 std::vector<int> message_path; | 229 vector<int> message_path; |
230 message_path.push_back(FileDescriptorProto::kMessageTypeFieldNumber); | 230 message_path.push_back(FileDescriptorProto::kMessageTypeFieldNumber); |
231 message_path.push_back(0); | 231 message_path.push_back(0); |
232 const GeneratedCodeInfo::Annotation* message_annotation = | 232 const GeneratedCodeInfo::Annotation* message_annotation = |
233 FindAnnotationOnPath(info, "test.proto", message_path); | 233 FindAnnotationOnPath(info, "test.proto", message_path); |
234 EXPECT_TRUE(NULL != message_annotation); | 234 EXPECT_TRUE(NULL != message_annotation); |
235 EXPECT_TRUE(AnnotationMatchesSubstring(pb_h, message_annotation, "Message")); | 235 EXPECT_TRUE(AnnotationMatchesSubstring(pb_h, message_annotation, "Message")); |
236 } | 236 } |
237 | 237 |
238 } // namespace | 238 } // namespace |
239 } // namespace cpp | 239 } // namespace cpp |
240 } // namespace compiler | 240 } // namespace compiler |
241 } // namespace protobuf | 241 } // namespace protobuf |
242 } // namespace google | 242 } // namespace google |
OLD | NEW |