Home | History | Annotate | Download | only in cpp
      1 // Protocol Buffers - Google's data interchange format
      2 // Copyright 2008 Google Inc.  All rights reserved.
      3 // http://code.google.com/p/protobuf/
      4 //
      5 // Redistribution and use in source and binary forms, with or without
      6 // modification, are permitted provided that the following conditions are
      7 // met:
      8 //
      9 //     * Redistributions of source code must retain the above copyright
     10 // notice, this list of conditions and the following disclaimer.
     11 //     * Redistributions in binary form must reproduce the above
     12 // copyright notice, this list of conditions and the following disclaimer
     13 // in the documentation and/or other materials provided with the
     14 // distribution.
     15 //     * Neither the name of Google Inc. nor the names of its
     16 // contributors may be used to endorse or promote products derived from
     17 // this software without specific prior written permission.
     18 //
     19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30 
     31 // Author: kenton (at) google.com (Kenton Varda)
     32 //
     33 // TODO(kenton):  Share code with the versions of this test in other languages?
     34 //   It seemed like parameterizing it would add more complexity than it is
     35 //   worth.
     36 
     37 #include <google/protobuf/compiler/cpp/cpp_generator.h>
     38 #include <google/protobuf/compiler/command_line_interface.h>
     39 #include <google/protobuf/io/zero_copy_stream.h>
     40 #include <google/protobuf/io/printer.h>
     41 
     42 #include <google/protobuf/testing/googletest.h>
     43 #include <gtest/gtest.h>
     44 #include <google/protobuf/testing/file.h>
     45 
     46 namespace google {
     47 namespace protobuf {
     48 namespace compiler {
     49 namespace cpp {
     50 namespace {
     51 
     52 class TestGenerator : public CodeGenerator {
     53  public:
     54   TestGenerator() {}
     55   ~TestGenerator() {}
     56 
     57   virtual bool Generate(const FileDescriptor* file,
     58                         const string& parameter,
     59                         GeneratorContext* context,
     60                         string* error) const {
     61     TryInsert("test.pb.h", "includes", context);
     62     TryInsert("test.pb.h", "namespace_scope", context);
     63     TryInsert("test.pb.h", "global_scope", context);
     64     TryInsert("test.pb.h", "class_scope:foo.Bar", context);
     65     TryInsert("test.pb.h", "class_scope:foo.Bar.Baz", context);
     66 
     67     TryInsert("test.pb.cc", "includes", context);
     68     TryInsert("test.pb.cc", "namespace_scope", context);
     69     TryInsert("test.pb.cc", "global_scope", context);
     70     return true;
     71   }
     72 
     73   void TryInsert(const string& filename, const string& insertion_point,
     74                  GeneratorContext* context) const {
     75     scoped_ptr<io::ZeroCopyOutputStream> output(
     76       context->OpenForInsert(filename, insertion_point));
     77     io::Printer printer(output.get(), '$');
     78     printer.Print("// inserted $name$\n", "name", insertion_point);
     79   }
     80 };
     81 
     82 // This test verifies that all the expected insertion points exist.  It does
     83 // not verify that they are correctly-placed; that would require actually
     84 // compiling the output which is a bit more than I care to do for this test.
     85 TEST(CppPluginTest, PluginTest) {
     86   File::WriteStringToFileOrDie(
     87       "syntax = \"proto2\";\n"
     88       "package foo;\n"
     89       "message Bar {\n"
     90       "  message Baz {}\n"
     91       "}\n",
     92       TestTempDir() + "/test.proto");
     93 
     94   google::protobuf::compiler::CommandLineInterface cli;
     95   cli.SetInputsAreProtoPathRelative(true);
     96 
     97   CppGenerator cpp_generator;
     98   TestGenerator test_generator;
     99   cli.RegisterGenerator("--cpp_out", &cpp_generator, "");
    100   cli.RegisterGenerator("--test_out", &test_generator, "");
    101 
    102   string proto_path = "-I" + TestTempDir();
    103   string cpp_out = "--cpp_out=" + TestTempDir();
    104   string test_out = "--test_out=" + TestTempDir();
    105 
    106   const char* argv[] = {
    107     "protoc",
    108     proto_path.c_str(),
    109     cpp_out.c_str(),
    110     test_out.c_str(),
    111     "test.proto"
    112   };
    113 
    114   EXPECT_EQ(0, cli.Run(5, argv));
    115 }
    116 
    117 }  // namespace
    118 }  // namespace cpp
    119 }  // namespace compiler
    120 }  // namespace protobuf
    121 }  // namespace google
    122