1 #include "google/protobuf/compiler/cpp/namespace_printer.h"
2
3 #include <string>
4
5 #include <gtest/gtest.h>
6 #include "absl/log/absl_check.h"
7 #include "absl/strings/string_view.h"
8 #include "absl/types/optional.h"
9 #include "google/protobuf/io/printer.h"
10 #include "google/protobuf/io/zero_copy_stream.h"
11 #include "google/protobuf/io/zero_copy_stream_impl_lite.h"
12
13 namespace google {
14 namespace protobuf {
15 namespace compiler {
16 namespace cpp {
17
18 namespace {
19
20 class NamespacePrinterTest : public testing::Test {
21 protected:
output()22 io::ZeroCopyOutputStream* output() {
23 ABSL_CHECK(stream_.has_value());
24 return &*stream_;
25 }
written()26 absl::string_view written() {
27 stream_.reset();
28 return out_;
29 }
30
31 std::string out_;
32 absl::optional<io::StringOutputStream> stream_{&out_};
33 };
34
TEST_F(NamespacePrinterTest,Basic)35 TEST_F(NamespacePrinterTest, Basic) {
36 {
37 io::Printer printer(output(), '$');
38
39 const NamespacePrinter namespace_printer(&printer, {"A", "B", "E"});
40
41 EXPECT_FALSE(printer.failed());
42 }
43
44 EXPECT_EQ(written(),
45 "namespace A {\n"
46 "namespace B {\n"
47 "namespace E {\n"
48 "\n"
49 "} // namespace E\n"
50 "} // namespace B\n"
51 "} // namespace A\n");
52 }
53
TEST_F(NamespacePrinterTest,DifferentDelim)54 TEST_F(NamespacePrinterTest, DifferentDelim) {
55 {
56 io::Printer printer(output(), '\0');
57
58 const NamespacePrinter namespace_printer(&printer, {"A", "B", "E"});
59
60 EXPECT_FALSE(printer.failed());
61 }
62
63 EXPECT_EQ(written(),
64 "namespace A {\n"
65 "namespace B {\n"
66 "namespace E {\n"
67 "\n"
68 "} // namespace E\n"
69 "} // namespace B\n"
70 "} // namespace A\n");
71 }
72
73 } // namespace
74
75 } // namespace cpp
76 } // namespace compiler
77 } // namespace protobuf
78 } // namespace google
79