| 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 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 } | 114 } |
| 115 | 115 |
| 116 TEST(Printer, VariableSubstitution) { | 116 TEST(Printer, VariableSubstitution) { |
| 117 char buffer[8192]; | 117 char buffer[8192]; |
| 118 | 118 |
| 119 for (int block_size = 1; block_size < 512; block_size *= 2) { | 119 for (int block_size = 1; block_size < 512; block_size *= 2) { |
| 120 ArrayOutputStream output(buffer, sizeof(buffer), block_size); | 120 ArrayOutputStream output(buffer, sizeof(buffer), block_size); |
| 121 | 121 |
| 122 { | 122 { |
| 123 Printer printer(&output, '$'); | 123 Printer printer(&output, '$'); |
| 124 map<string, string> vars; | 124 std::map<string, string> vars; |
| 125 | 125 |
| 126 vars["foo"] = "World"; | 126 vars["foo"] = "World"; |
| 127 vars["bar"] = "$foo$"; | 127 vars["bar"] = "$foo$"; |
| 128 vars["abcdefg"] = "1234"; | 128 vars["abcdefg"] = "1234"; |
| 129 | 129 |
| 130 printer.Print(vars, "Hello $foo$!\nbar = $bar$\n"); | 130 printer.Print(vars, "Hello $foo$!\nbar = $bar$\n"); |
| 131 printer.PrintRaw("RawBit\n"); | 131 printer.PrintRaw("RawBit\n"); |
| 132 printer.Print(vars, "$abcdefg$\nA literal dollar sign: $$"); | 132 printer.Print(vars, "$abcdefg$\nA literal dollar sign: $$"); |
| 133 | 133 |
| 134 vars["foo"] = "blah"; | 134 vars["foo"] = "blah"; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 const string& name() const { return file_; } | 180 const string& name() const { return file_; } |
| 181 | 181 |
| 182 private: | 182 private: |
| 183 string file_; | 183 string file_; |
| 184 }; | 184 }; |
| 185 | 185 |
| 186 // MockDescriptor defines only those members that Printer uses to write out | 186 // MockDescriptor defines only those members that Printer uses to write out |
| 187 // annotations. | 187 // annotations. |
| 188 class MockDescriptor { | 188 class MockDescriptor { |
| 189 public: | 189 public: |
| 190 MockDescriptor(const string& file, const vector<int>& path) | 190 MockDescriptor(const string& file, const std::vector<int>& path) |
| 191 : file_(file), path_(path) {} | 191 : file_(file), path_(path) {} |
| 192 | 192 |
| 193 // The mock file in which this descriptor was defined. | 193 // The mock file in which this descriptor was defined. |
| 194 const MockDescriptorFile* file() const { return &file_; } | 194 const MockDescriptorFile* file() const { return &file_; } |
| 195 | 195 |
| 196 private: | 196 private: |
| 197 // Allows access to GetLocationPath. | 197 // Allows access to GetLocationPath. |
| 198 friend class ::google::protobuf::io::Printer; | 198 friend class ::google::protobuf::io::Printer; |
| 199 | 199 |
| 200 // Copies the pre-stored path to output. | 200 // Copies the pre-stored path to output. |
| 201 void GetLocationPath(std::vector<int>* output) const { *output = path_; } | 201 void GetLocationPath(std::vector<int>* output) const { *output = path_; } |
| 202 | 202 |
| 203 MockDescriptorFile file_; | 203 MockDescriptorFile file_; |
| 204 vector<int> path_; | 204 std::vector<int> path_; |
| 205 }; | 205 }; |
| 206 | 206 |
| 207 TEST(Printer, AnnotateMap) { | 207 TEST(Printer, AnnotateMap) { |
| 208 char buffer[8192]; | 208 char buffer[8192]; |
| 209 ArrayOutputStream output(buffer, sizeof(buffer)); | 209 ArrayOutputStream output(buffer, sizeof(buffer)); |
| 210 GeneratedCodeInfo info; | 210 GeneratedCodeInfo info; |
| 211 AnnotationProtoCollector<GeneratedCodeInfo> info_collector(&info); | 211 AnnotationProtoCollector<GeneratedCodeInfo> info_collector(&info); |
| 212 { | 212 { |
| 213 Printer printer(&output, '$', &info_collector); | 213 Printer printer(&output, '$', &info_collector); |
| 214 map<string, string> vars; | 214 std::map<string, string> vars; |
| 215 vars["foo"] = "3"; | 215 vars["foo"] = "3"; |
| 216 vars["bar"] = "5"; | 216 vars["bar"] = "5"; |
| 217 printer.Print(vars, "012$foo$4$bar$\n"); | 217 printer.Print(vars, "012$foo$4$bar$\n"); |
| 218 vector<int> path_1; | 218 std::vector<int> path_1; |
| 219 path_1.push_back(33); | 219 path_1.push_back(33); |
| 220 vector<int> path_2; | 220 std::vector<int> path_2; |
| 221 path_2.push_back(11); | 221 path_2.push_back(11); |
| 222 path_2.push_back(22); | 222 path_2.push_back(22); |
| 223 MockDescriptor descriptor_1("path_1", path_1); | 223 MockDescriptor descriptor_1("path_1", path_1); |
| 224 MockDescriptor descriptor_2("path_2", path_2); | 224 MockDescriptor descriptor_2("path_2", path_2); |
| 225 printer.Annotate("foo", "foo", &descriptor_1); | 225 printer.Annotate("foo", "foo", &descriptor_1); |
| 226 printer.Annotate("bar", "bar", &descriptor_2); | 226 printer.Annotate("bar", "bar", &descriptor_2); |
| 227 } | 227 } |
| 228 buffer[output.ByteCount()] = '\0'; | 228 buffer[output.ByteCount()] = '\0'; |
| 229 EXPECT_STREQ("012345\n", buffer); | 229 EXPECT_STREQ("012345\n", buffer); |
| 230 ASSERT_EQ(2, info.annotation_size()); | 230 ASSERT_EQ(2, info.annotation_size()); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 248 } | 248 } |
| 249 | 249 |
| 250 TEST(Printer, AnnotateInline) { | 250 TEST(Printer, AnnotateInline) { |
| 251 char buffer[8192]; | 251 char buffer[8192]; |
| 252 ArrayOutputStream output(buffer, sizeof(buffer)); | 252 ArrayOutputStream output(buffer, sizeof(buffer)); |
| 253 GeneratedCodeInfo info; | 253 GeneratedCodeInfo info; |
| 254 AnnotationProtoCollector<GeneratedCodeInfo> info_collector(&info); | 254 AnnotationProtoCollector<GeneratedCodeInfo> info_collector(&info); |
| 255 { | 255 { |
| 256 Printer printer(&output, '$', &info_collector); | 256 Printer printer(&output, '$', &info_collector); |
| 257 printer.Print("012$foo$4$bar$\n", "foo", "3", "bar", "5"); | 257 printer.Print("012$foo$4$bar$\n", "foo", "3", "bar", "5"); |
| 258 vector<int> path_1; | 258 std::vector<int> path_1; |
| 259 path_1.push_back(33); | 259 path_1.push_back(33); |
| 260 vector<int> path_2; | 260 std::vector<int> path_2; |
| 261 path_2.push_back(11); | 261 path_2.push_back(11); |
| 262 path_2.push_back(22); | 262 path_2.push_back(22); |
| 263 MockDescriptor descriptor_1("path_1", path_1); | 263 MockDescriptor descriptor_1("path_1", path_1); |
| 264 MockDescriptor descriptor_2("path_2", path_2); | 264 MockDescriptor descriptor_2("path_2", path_2); |
| 265 printer.Annotate("foo", "foo", &descriptor_1); | 265 printer.Annotate("foo", "foo", &descriptor_1); |
| 266 printer.Annotate("bar", "bar", &descriptor_2); | 266 printer.Annotate("bar", "bar", &descriptor_2); |
| 267 } | 267 } |
| 268 buffer[output.ByteCount()] = '\0'; | 268 buffer[output.ByteCount()] = '\0'; |
| 269 EXPECT_STREQ("012345\n", buffer); | 269 EXPECT_STREQ("012345\n", buffer); |
| 270 ASSERT_EQ(2, info.annotation_size()); | 270 ASSERT_EQ(2, info.annotation_size()); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 288 } | 288 } |
| 289 | 289 |
| 290 TEST(Printer, AnnotateRange) { | 290 TEST(Printer, AnnotateRange) { |
| 291 char buffer[8192]; | 291 char buffer[8192]; |
| 292 ArrayOutputStream output(buffer, sizeof(buffer)); | 292 ArrayOutputStream output(buffer, sizeof(buffer)); |
| 293 GeneratedCodeInfo info; | 293 GeneratedCodeInfo info; |
| 294 AnnotationProtoCollector<GeneratedCodeInfo> info_collector(&info); | 294 AnnotationProtoCollector<GeneratedCodeInfo> info_collector(&info); |
| 295 { | 295 { |
| 296 Printer printer(&output, '$', &info_collector); | 296 Printer printer(&output, '$', &info_collector); |
| 297 printer.Print("012$foo$4$bar$\n", "foo", "3", "bar", "5"); | 297 printer.Print("012$foo$4$bar$\n", "foo", "3", "bar", "5"); |
| 298 vector<int> path; | 298 std::vector<int> path; |
| 299 path.push_back(33); | 299 path.push_back(33); |
| 300 MockDescriptor descriptor("path", path); | 300 MockDescriptor descriptor("path", path); |
| 301 printer.Annotate("foo", "bar", &descriptor); | 301 printer.Annotate("foo", "bar", &descriptor); |
| 302 } | 302 } |
| 303 buffer[output.ByteCount()] = '\0'; | 303 buffer[output.ByteCount()] = '\0'; |
| 304 EXPECT_STREQ("012345\n", buffer); | 304 EXPECT_STREQ("012345\n", buffer); |
| 305 ASSERT_EQ(1, info.annotation_size()); | 305 ASSERT_EQ(1, info.annotation_size()); |
| 306 const GeneratedCodeInfo::Annotation* foobar = &info.annotation(0); | 306 const GeneratedCodeInfo::Annotation* foobar = &info.annotation(0); |
| 307 ASSERT_EQ(1, foobar->path_size()); | 307 ASSERT_EQ(1, foobar->path_size()); |
| 308 EXPECT_EQ(33, foobar->path(0)); | 308 EXPECT_EQ(33, foobar->path(0)); |
| 309 EXPECT_EQ("path", foobar->source_file()); | 309 EXPECT_EQ("path", foobar->source_file()); |
| 310 EXPECT_EQ(3, foobar->begin()); | 310 EXPECT_EQ(3, foobar->begin()); |
| 311 EXPECT_EQ(6, foobar->end()); | 311 EXPECT_EQ(6, foobar->end()); |
| 312 } | 312 } |
| 313 | 313 |
| 314 TEST(Printer, AnnotateEmptyRange) { | 314 TEST(Printer, AnnotateEmptyRange) { |
| 315 char buffer[8192]; | 315 char buffer[8192]; |
| 316 ArrayOutputStream output(buffer, sizeof(buffer)); | 316 ArrayOutputStream output(buffer, sizeof(buffer)); |
| 317 GeneratedCodeInfo info; | 317 GeneratedCodeInfo info; |
| 318 AnnotationProtoCollector<GeneratedCodeInfo> info_collector(&info); | 318 AnnotationProtoCollector<GeneratedCodeInfo> info_collector(&info); |
| 319 { | 319 { |
| 320 Printer printer(&output, '$', &info_collector); | 320 Printer printer(&output, '$', &info_collector); |
| 321 printer.Print("012$foo$4$baz$$bam$$bar$\n", "foo", "3", "bar", "5", "baz", | 321 printer.Print("012$foo$4$baz$$bam$$bar$\n", "foo", "3", "bar", "5", "baz", |
| 322 "", "bam", ""); | 322 "", "bam", ""); |
| 323 vector<int> path; | 323 std::vector<int> path; |
| 324 path.push_back(33); | 324 path.push_back(33); |
| 325 MockDescriptor descriptor("path", path); | 325 MockDescriptor descriptor("path", path); |
| 326 printer.Annotate("baz", "bam", &descriptor); | 326 printer.Annotate("baz", "bam", &descriptor); |
| 327 } | 327 } |
| 328 buffer[output.ByteCount()] = '\0'; | 328 buffer[output.ByteCount()] = '\0'; |
| 329 EXPECT_STREQ("012345\n", buffer); | 329 EXPECT_STREQ("012345\n", buffer); |
| 330 ASSERT_EQ(1, info.annotation_size()); | 330 ASSERT_EQ(1, info.annotation_size()); |
| 331 const GeneratedCodeInfo::Annotation* bazbam = &info.annotation(0); | 331 const GeneratedCodeInfo::Annotation* bazbam = &info.annotation(0); |
| 332 ASSERT_EQ(1, bazbam->path_size()); | 332 ASSERT_EQ(1, bazbam->path_size()); |
| 333 EXPECT_EQ(33, bazbam->path(0)); | 333 EXPECT_EQ(33, bazbam->path(0)); |
| 334 EXPECT_EQ("path", bazbam->source_file()); | 334 EXPECT_EQ("path", bazbam->source_file()); |
| 335 EXPECT_EQ(5, bazbam->begin()); | 335 EXPECT_EQ(5, bazbam->begin()); |
| 336 EXPECT_EQ(5, bazbam->end()); | 336 EXPECT_EQ(5, bazbam->end()); |
| 337 } | 337 } |
| 338 | 338 |
| 339 TEST(Printer, AnnotateDespiteUnrelatedMultipleUses) { | 339 TEST(Printer, AnnotateDespiteUnrelatedMultipleUses) { |
| 340 char buffer[8192]; | 340 char buffer[8192]; |
| 341 ArrayOutputStream output(buffer, sizeof(buffer)); | 341 ArrayOutputStream output(buffer, sizeof(buffer)); |
| 342 GeneratedCodeInfo info; | 342 GeneratedCodeInfo info; |
| 343 AnnotationProtoCollector<GeneratedCodeInfo> info_collector(&info); | 343 AnnotationProtoCollector<GeneratedCodeInfo> info_collector(&info); |
| 344 { | 344 { |
| 345 Printer printer(&output, '$', &info_collector); | 345 Printer printer(&output, '$', &info_collector); |
| 346 printer.Print("012$foo$4$foo$$bar$\n", "foo", "3", "bar", "5"); | 346 printer.Print("012$foo$4$foo$$bar$\n", "foo", "3", "bar", "5"); |
| 347 vector<int> path; | 347 std::vector<int> path; |
| 348 path.push_back(33); | 348 path.push_back(33); |
| 349 MockDescriptor descriptor("path", path); | 349 MockDescriptor descriptor("path", path); |
| 350 printer.Annotate("bar", "bar", &descriptor); | 350 printer.Annotate("bar", "bar", &descriptor); |
| 351 } | 351 } |
| 352 buffer[output.ByteCount()] = '\0'; | 352 buffer[output.ByteCount()] = '\0'; |
| 353 EXPECT_STREQ("0123435\n", buffer); | 353 EXPECT_STREQ("0123435\n", buffer); |
| 354 ASSERT_EQ(1, info.annotation_size()); | 354 ASSERT_EQ(1, info.annotation_size()); |
| 355 const GeneratedCodeInfo::Annotation* bar = &info.annotation(0); | 355 const GeneratedCodeInfo::Annotation* bar = &info.annotation(0); |
| 356 ASSERT_EQ(1, bar->path_size()); | 356 ASSERT_EQ(1, bar->path_size()); |
| 357 EXPECT_EQ(33, bar->path(0)); | 357 EXPECT_EQ(33, bar->path(0)); |
| 358 EXPECT_EQ("path", bar->source_file()); | 358 EXPECT_EQ("path", bar->source_file()); |
| 359 EXPECT_EQ(6, bar->begin()); | 359 EXPECT_EQ(6, bar->begin()); |
| 360 EXPECT_EQ(7, bar->end()); | 360 EXPECT_EQ(7, bar->end()); |
| 361 } | 361 } |
| 362 | 362 |
| 363 TEST(Printer, Indenting) { | 363 TEST(Printer, Indenting) { |
| 364 char buffer[8192]; | 364 char buffer[8192]; |
| 365 | 365 |
| 366 for (int block_size = 1; block_size < 512; block_size *= 2) { | 366 for (int block_size = 1; block_size < 512; block_size *= 2) { |
| 367 ArrayOutputStream output(buffer, sizeof(buffer), block_size); | 367 ArrayOutputStream output(buffer, sizeof(buffer), block_size); |
| 368 | 368 |
| 369 { | 369 { |
| 370 Printer printer(&output, '$'); | 370 Printer printer(&output, '$'); |
| 371 map<string, string> vars; | 371 std::map<string, string> vars; |
| 372 | 372 |
| 373 vars["newline"] = "\n"; | 373 vars["newline"] = "\n"; |
| 374 | 374 |
| 375 printer.Print("This is not indented.\n"); | 375 printer.Print("This is not indented.\n"); |
| 376 printer.Indent(); | 376 printer.Indent(); |
| 377 printer.Print("This is indented\nAnd so is this\n"); | 377 printer.Print("This is indented\nAnd so is this\n"); |
| 378 printer.Outdent(); | 378 printer.Outdent(); |
| 379 printer.Print("But this is not."); | 379 printer.Print("But this is not."); |
| 380 printer.Indent(); | 380 printer.Indent(); |
| 381 printer.Print(" And this is still the same line.\n" | 381 printer.Print(" And this is still the same line.\n" |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 425 } | 425 } |
| 426 | 426 |
| 427 TEST(Printer, AnnotateMultipleUsesDeath) { | 427 TEST(Printer, AnnotateMultipleUsesDeath) { |
| 428 char buffer[8192]; | 428 char buffer[8192]; |
| 429 ArrayOutputStream output(buffer, sizeof(buffer)); | 429 ArrayOutputStream output(buffer, sizeof(buffer)); |
| 430 GeneratedCodeInfo info; | 430 GeneratedCodeInfo info; |
| 431 AnnotationProtoCollector<GeneratedCodeInfo> info_collector(&info); | 431 AnnotationProtoCollector<GeneratedCodeInfo> info_collector(&info); |
| 432 { | 432 { |
| 433 Printer printer(&output, '$', &info_collector); | 433 Printer printer(&output, '$', &info_collector); |
| 434 printer.Print("012$foo$4$foo$\n", "foo", "3"); | 434 printer.Print("012$foo$4$foo$\n", "foo", "3"); |
| 435 vector<int> path; | 435 std::vector<int> path; |
| 436 path.push_back(33); | 436 path.push_back(33); |
| 437 MockDescriptor descriptor("path", path); | 437 MockDescriptor descriptor("path", path); |
| 438 EXPECT_DEBUG_DEATH(printer.Annotate("foo", "foo", &descriptor), "multiple"); | 438 EXPECT_DEBUG_DEATH(printer.Annotate("foo", "foo", &descriptor), "multiple"); |
| 439 } | 439 } |
| 440 } | 440 } |
| 441 | 441 |
| 442 TEST(Printer, AnnotateNegativeLengthDeath) { | 442 TEST(Printer, AnnotateNegativeLengthDeath) { |
| 443 char buffer[8192]; | 443 char buffer[8192]; |
| 444 ArrayOutputStream output(buffer, sizeof(buffer)); | 444 ArrayOutputStream output(buffer, sizeof(buffer)); |
| 445 GeneratedCodeInfo info; | 445 GeneratedCodeInfo info; |
| 446 AnnotationProtoCollector<GeneratedCodeInfo> info_collector(&info); | 446 AnnotationProtoCollector<GeneratedCodeInfo> info_collector(&info); |
| 447 { | 447 { |
| 448 Printer printer(&output, '$', &info_collector); | 448 Printer printer(&output, '$', &info_collector); |
| 449 printer.Print("012$foo$4$bar$\n", "foo", "3", "bar", "5"); | 449 printer.Print("012$foo$4$bar$\n", "foo", "3", "bar", "5"); |
| 450 vector<int> path; | 450 std::vector<int> path; |
| 451 path.push_back(33); | 451 path.push_back(33); |
| 452 MockDescriptor descriptor("path", path); | 452 MockDescriptor descriptor("path", path); |
| 453 EXPECT_DEBUG_DEATH(printer.Annotate("bar", "foo", &descriptor), "negative"); | 453 EXPECT_DEBUG_DEATH(printer.Annotate("bar", "foo", &descriptor), "negative"); |
| 454 } | 454 } |
| 455 } | 455 } |
| 456 | 456 |
| 457 TEST(Printer, AnnotateUndefinedDeath) { | 457 TEST(Printer, AnnotateUndefinedDeath) { |
| 458 char buffer[8192]; | 458 char buffer[8192]; |
| 459 ArrayOutputStream output(buffer, sizeof(buffer)); | 459 ArrayOutputStream output(buffer, sizeof(buffer)); |
| 460 GeneratedCodeInfo info; | 460 GeneratedCodeInfo info; |
| 461 AnnotationProtoCollector<GeneratedCodeInfo> info_collector(&info); | 461 AnnotationProtoCollector<GeneratedCodeInfo> info_collector(&info); |
| 462 { | 462 { |
| 463 Printer printer(&output, '$', &info_collector); | 463 Printer printer(&output, '$', &info_collector); |
| 464 printer.Print("012$foo$4$foo$\n", "foo", "3"); | 464 printer.Print("012$foo$4$foo$\n", "foo", "3"); |
| 465 vector<int> path; | 465 std::vector<int> path; |
| 466 path.push_back(33); | 466 path.push_back(33); |
| 467 MockDescriptor descriptor("path", path); | 467 MockDescriptor descriptor("path", path); |
| 468 EXPECT_DEBUG_DEATH(printer.Annotate("bar", "bar", &descriptor), | 468 EXPECT_DEBUG_DEATH(printer.Annotate("bar", "bar", &descriptor), |
| 469 "Undefined"); | 469 "Undefined"); |
| 470 } | 470 } |
| 471 } | 471 } |
| 472 #endif // PROTOBUF_HAS_DEATH_TEST | 472 #endif // PROTOBUF_HAS_DEATH_TEST |
| 473 | 473 |
| 474 TEST(Printer, WriteFailurePartial) { | 474 TEST(Printer, WriteFailurePartial) { |
| 475 char buffer[17]; | 475 char buffer[17]; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 514 EXPECT_TRUE(printer.failed()); | 514 EXPECT_TRUE(printer.failed()); |
| 515 | 515 |
| 516 // Buffer should contain the first 16 bytes written. | 516 // Buffer should contain the first 16 bytes written. |
| 517 EXPECT_EQ("0123456789abcdef", string(buffer, sizeof(buffer))); | 517 EXPECT_EQ("0123456789abcdef", string(buffer, sizeof(buffer))); |
| 518 } | 518 } |
| 519 | 519 |
| 520 } // namespace | 520 } // namespace |
| 521 } // namespace io | 521 } // namespace io |
| 522 } // namespace protobuf | 522 } // namespace protobuf |
| 523 } // namespace google | 523 } // namespace google |
| OLD | NEW |