• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "google/protobuf/compiler/cpp/ifndef_guard.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 IfnDefGuardTest : 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(IfnDefGuardTest,Basic)35 TEST_F(IfnDefGuardTest, Basic) {
36   {
37     io::Printer printer(output(), '$');
38 
39     const IfdefGuardPrinter ifdef_guard(&printer, "A/B-E.alpha");
40 
41     EXPECT_FALSE(printer.failed());
42   }
43 
44   EXPECT_EQ(written(),
45             "#ifndef A_B_E_ALPHA_\n"
46             "#define A_B_E_ALPHA_\n"
47             "\n"
48             "\n"
49             "#endif  // A_B_E_ALPHA_\n");
50 }
51 
TEST_F(IfnDefGuardTest,DifferentDelim)52 TEST_F(IfnDefGuardTest, DifferentDelim) {
53   {
54     io::Printer printer(output(), '\0');
55 
56     const IfdefGuardPrinter ifdef_guard(&printer, "A/B/E/alpha");
57 
58     EXPECT_FALSE(printer.failed());
59   }
60 
61   EXPECT_EQ(written(),
62             "#ifndef A_B_E_ALPHA_\n"
63             "#define A_B_E_ALPHA_\n"
64             "\n"
65             "\n"
66             "#endif  // A_B_E_ALPHA_\n");
67 }
68 
TEST_F(IfnDefGuardTest,DifferentSubstitutionFunction)69 TEST_F(IfnDefGuardTest, DifferentSubstitutionFunction) {
70   {
71     io::Printer printer(output(), '$');
72 
73     const IfdefGuardPrinter ifdef_guard(
74         &printer, "A/B/E/alpha", [](absl::string_view) { return "FOO_BAR_"; });
75 
76     EXPECT_FALSE(printer.failed());
77   }
78 
79   EXPECT_EQ(written(),
80             "#ifndef FOO_BAR_\n"
81             "#define FOO_BAR_\n"
82             "\n"
83             "\n"
84             "#endif  // FOO_BAR_\n");
85 }
86 
87 }  // namespace
88 
89 }  // namespace cpp
90 }  // namespace compiler
91 }  // namespace protobuf
92 }  // namespace google
93