• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
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@google.com (Kenton Varda)
32 
33 #ifndef GOOGLE_PROTOBUF_COMPILER_MOCK_CODE_GENERATOR_H__
34 #define GOOGLE_PROTOBUF_COMPILER_MOCK_CODE_GENERATOR_H__
35 
36 #include <string>
37 
38 #include <google/protobuf/compiler/code_generator.h>
39 
40 namespace google {
41 namespace protobuf {
42 class FileDescriptor;
43 }  // namespace protobuf
44 }  // namespace google
45 
46 namespace google {
47 namespace protobuf {
48 namespace compiler {
49 
50 // A mock CodeGenerator, used by command_line_interface_unittest.  This is in
51 // its own file so that it can be used both directly and as a plugin.
52 //
53 // Generate() produces some output which can be checked by ExpectCalled().  The
54 // generator can run in a different process (e.g. a plugin).
55 //
56 // If the parameter is "insert=NAMES", the MockCodeGenerator will insert lines
57 // into the files generated by other MockCodeGenerators instead of creating
58 // its own file.  NAMES is a comma-separated list of the names of those other
59 // MockCodeGenerators.  If the parameter is "insert_endlines=NAMES", the
60 // MockCodeGenerator will insert data guaranteed to contain more than one
61 // endline into the files generated by NAMES.
62 //
63 // MockCodeGenerator will also modify its behavior slightly if the input file
64 // contains a message type with one of the following names:
65 //   MockCodeGenerator_Error:  Causes Generate() to return false and set the
66 //     error message to "Saw message type MockCodeGenerator_Error."
67 //   MockCodeGenerator_Exit:  Generate() prints "Saw message type
68 //     MockCodeGenerator_Exit." to stderr and then calls exit(123).
69 //   MockCodeGenerator_Abort:  Generate() prints "Saw message type
70 //     MockCodeGenerator_Abort." to stderr and then calls abort().
71 //   MockCodeGenerator_HasSourceCodeInfo:  Causes Generate() to abort after
72 //     printing "Saw message type MockCodeGenerator_HasSourceCodeInfo: FOO." to
73 //     stderr, where FOO is "1" if the supplied FileDescriptorProto has source
74 //     code info, and "0" otherwise.
75 //   MockCodeGenerator_Annotate:  Generate() will add annotations to its output
76 //     that can later be verified with CheckGeneratedAnnotations.
77 class MockCodeGenerator : public CodeGenerator {
78  public:
79   MockCodeGenerator(const std::string& name);
80   virtual ~MockCodeGenerator();
81 
82   // Expect (via gTest) that a MockCodeGenerator with the given name was called
83   // with the given parameters by inspecting the output location.
84   //
85   // |insertions| is a comma-separated list of names of MockCodeGenerators which
86   // should have inserted lines into this file.
87   // |parsed_file_list| is a comma-separated list of names of the files
88   // that are being compiled together in this run.
89   static void ExpectGenerated(const std::string& name,
90                               const std::string& parameter,
91                               const std::string& insertions,
92                               const std::string& file,
93                               const std::string& first_message_name,
94                               const std::string& parsed_file_list,
95                               const std::string& output_directory);
96 
97   // Checks that the correct text ranges were annotated by the
98   // MockCodeGenerator_Annotate directive.
99   static void CheckGeneratedAnnotations(const std::string& name,
100                                         const std::string& file,
101                                         const std::string& output_directory);
102 
103   // Get the name of the file which would be written by the given generator.
104   static std::string GetOutputFileName(const std::string& generator_name,
105                                        const FileDescriptor* file);
106   static std::string GetOutputFileName(const std::string& generator_name,
107                                        const std::string& file);
108 
109   // implements CodeGenerator ----------------------------------------
110 
111   bool Generate(const FileDescriptor* file, const std::string& parameter,
112                 GeneratorContext* context, std::string* error) const override;
113 
114   uint64_t GetSupportedFeatures() const override;
115   void SuppressFeatures(uint64 features);
116 
117  private:
118   std::string name_;
119   uint64 suppressed_features_ = 0;
120 
121   static std::string GetOutputFileContent(const std::string& generator_name,
122                                           const std::string& parameter,
123                                           const FileDescriptor* file,
124                                           GeneratorContext* context);
125   static std::string GetOutputFileContent(
126       const std::string& generator_name, const std::string& parameter,
127       const std::string& file, const std::string& parsed_file_list,
128       const std::string& first_message_name);
129 };
130 
131 }  // namespace compiler
132 }  // namespace protobuf
133 }  // namespace google
134 
135 #endif  // GOOGLE_PROTOBUF_COMPILER_MOCK_CODE_GENERATOR_H__
136