|
26 | 26 | #include "absl/status/statusor.h" |
27 | 27 | #include "absl/strings/str_cat.h" |
28 | 28 | #include "absl/strings/string_view.h" |
| 29 | +#include "sandboxed_api/testing.h" |
29 | 30 | #include "sandboxed_api/tools/clang_generator/emitter_base.h" |
30 | 31 | #include "sandboxed_api/tools/clang_generator/frontend_action_test_util.h" |
31 | 32 | #include "sandboxed_api/tools/clang_generator/generator.h" |
@@ -525,6 +526,93 @@ TEST_F(EmitterTest, SkipProtobufMessagesInternals) { |
525 | 526 | ElementsAre("class MyMessage")); |
526 | 527 | } |
527 | 528 |
|
| 529 | +TEST_F(EmitterTest, Namespaced) { |
| 530 | + GeneratorOptions options; |
| 531 | + EmitterForTesting emitter(&options); |
| 532 | + EXPECT_THAT( |
| 533 | + RunFrontendAction(R"(namespace sandboxed { |
| 534 | + struct S { int member; }; |
| 535 | + } // namespace sandboxed |
| 536 | + extern "C" sandboxed::S* Structize();)", |
| 537 | + std::make_unique<GeneratorAction>(&emitter, &options)), |
| 538 | + IsOk()); |
| 539 | + EXPECT_THAT(emitter.GetRenderedFunctions(), SizeIs(1)); |
| 540 | + |
| 541 | + EXPECT_THAT(UglifyAll(emitter.SpellingsForNS("sandboxed")), |
| 542 | + ElementsAre("struct S { int member; }")); |
| 543 | + |
| 544 | + SAPI_ASSERT_OK_AND_ASSIGN(std::string header, emitter.EmitHeader()); |
| 545 | + // Expect the namespace to be preserved. |
| 546 | + EXPECT_THAT(header, HasSubstr("::absl::StatusOr<sandboxed::S*> Structize()")); |
| 547 | +} |
| 548 | + |
| 549 | +TEST_F(EmitterTest, StripNamespacePrefix) { |
| 550 | + GeneratorOptions options; |
| 551 | + options.namespace_name = "sandboxed"; |
| 552 | + EmitterForTesting emitter(&options); |
| 553 | + EXPECT_THAT( |
| 554 | + RunFrontendAction(R"(namespace sandboxed { |
| 555 | + struct S { int member; }; |
| 556 | + } // namespace sandboxed |
| 557 | + extern "C" sandboxed::S* Structize();)", |
| 558 | + std::make_unique<GeneratorAction>(&emitter, &options)), |
| 559 | + IsOk()); |
| 560 | + EXPECT_THAT(emitter.GetRenderedFunctions(), SizeIs(1)); |
| 561 | + |
| 562 | + EXPECT_THAT(UglifyAll(emitter.SpellingsForNS("sandboxed")), |
| 563 | + ElementsAre("struct S { int member; }")); |
| 564 | + |
| 565 | + SAPI_ASSERT_OK_AND_ASSIGN(std::string header, emitter.EmitHeader()); |
| 566 | + // Expect the namespace prefix to be stripped, as `Structize` will also be |
| 567 | + // emitted in the `sandboxed` namespace. |
| 568 | + EXPECT_THAT(header, HasSubstr("::absl::StatusOr<S*> Structize()")); |
| 569 | +} |
| 570 | + |
| 571 | +TEST_F(EmitterTest, KeepTextualNamespacePrefix) { |
| 572 | + GeneratorOptions options; |
| 573 | + options.namespace_name = "sandboxed"; |
| 574 | + EmitterForTesting emitter(&options); |
| 575 | + EXPECT_THAT( |
| 576 | + RunFrontendAction(R"(namespace sandboxed_ns { |
| 577 | + struct S { int member; }; |
| 578 | + } // namespace sandboxed_ns |
| 579 | + extern "C" sandboxed_ns::S* Structize();)", |
| 580 | + std::make_unique<GeneratorAction>(&emitter, &options)), |
| 581 | + IsOk()); |
| 582 | + EXPECT_THAT(emitter.GetRenderedFunctions(), SizeIs(1)); |
| 583 | + |
| 584 | + EXPECT_THAT(UglifyAll(emitter.SpellingsForNS("sandboxed_ns")), |
| 585 | + ElementsAre("struct S { int member; }")); |
| 586 | + |
| 587 | + SAPI_ASSERT_OK_AND_ASSIGN(std::string header, emitter.EmitHeader()); |
| 588 | + // Keep the namespace prefix, `sandboxed` and `sandboxed_ns` are different |
| 589 | + // namespaces. |
| 590 | + EXPECT_THAT(header, |
| 591 | + HasSubstr("::absl::StatusOr<sandboxed_ns::S*> Structize()")); |
| 592 | +} |
| 593 | + |
| 594 | +TEST_F(EmitterTest, StripNamespacePrefixNested) { |
| 595 | + GeneratorOptions options; |
| 596 | + options.namespace_name = "sandboxed"; |
| 597 | + EmitterForTesting emitter(&options); |
| 598 | + EXPECT_THAT( |
| 599 | + RunFrontendAction(R"(namespace sandboxed::nested { |
| 600 | + struct S { int member; }; |
| 601 | + } // namespace sandboxed::nested |
| 602 | + extern "C" sandboxed::nested::S* Structize();)", |
| 603 | + std::make_unique<GeneratorAction>(&emitter, &options)), |
| 604 | + IsOk()); |
| 605 | + EXPECT_THAT(emitter.GetRenderedFunctions(), SizeIs(1)); |
| 606 | + |
| 607 | + EXPECT_THAT(UglifyAll(emitter.SpellingsForNS("sandboxed::nested")), |
| 608 | + ElementsAre("struct S { int member; }")); |
| 609 | + |
| 610 | + SAPI_ASSERT_OK_AND_ASSIGN(std::string header, emitter.EmitHeader()); |
| 611 | + // Expect the namespace prefix to be stripped, similar to the |
| 612 | + // StripNamespacePrefix test. |
| 613 | + EXPECT_THAT(header, HasSubstr("::absl::StatusOr<nested::S*> Structize()")); |
| 614 | +} |
| 615 | + |
528 | 616 | TEST_F(EmitterTest, SymbolListTest) { |
529 | 617 | constexpr absl::string_view kInputFile = "simple_functions.cc"; |
530 | 618 | GeneratorOptions options; |
|
0 commit comments