• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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@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 #include <google/protobuf/compiler/code_generator.h>
38 
39 namespace google {
40 namespace protobuf {
41 namespace compiler {
42 
43 // A mock CodeGenerator, used by command_line_interface_unittest.  This is in
44 // its own file so that it can be used both directly and as a plugin.
45 //
46 // Generate() produces some output which can be checked by ExpectCalled().  The
47 // generator can run in a different process (e.g. a plugin).
48 //
49 // If the parameter is "insert=NAMES", the MockCodeGenerator will insert lines
50 // into the files generated by other MockCodeGenerators instead of creating
51 // its own file.  NAMES is a comma-separated list of the names of those other
52 // MockCodeGenerators.
53 //
54 // MockCodeGenerator will also modify its behavior slightly if the input file
55 // contains a message type with one of the following names:
56 //   MockCodeGenerator_Error:  Causes Generate() to return false and set the
57 //     error message to "Saw message type MockCodeGenerator_Error."
58 //   MockCodeGenerator_Exit:  Generate() prints "Saw message type
59 //     MockCodeGenerator_Exit." to stderr and then calls exit(123).
60 //   MockCodeGenerator_Abort:  Generate() prints "Saw message type
61 //     MockCodeGenerator_Abort." to stderr and then calls abort().
62 class MockCodeGenerator : public CodeGenerator {
63  public:
64   MockCodeGenerator(const string& name);
65   virtual ~MockCodeGenerator();
66 
67   // Expect (via gTest) that a MockCodeGenerator with the given name was called
68   // with the given parameters by inspecting the output location.
69   //
70   // |insertions| is a comma-separated list of names of MockCodeGenerators which
71   // should have inserted lines into this file.
72   static void ExpectGenerated(const string& name,
73                               const string& parameter,
74                               const string& insertions,
75                               const string& file,
76                               const string& first_message_name,
77                               const string& output_directory);
78 
79   // Get the name of the file which would be written by the given generator.
80   static string GetOutputFileName(const string& generator_name,
81                                   const FileDescriptor* file);
82   static string GetOutputFileName(const string& generator_name,
83                                   const string& file);
84 
85   // implements CodeGenerator ----------------------------------------
86 
87   virtual bool Generate(const FileDescriptor* file,
88                         const string& parameter,
89                         OutputDirectory* output_directory,
90                         string* error) const;
91 
92  private:
93   string name_;
94 
95   static string GetOutputFileContent(const string& generator_name,
96                                      const string& parameter,
97                                      const FileDescriptor* file);
98   static string GetOutputFileContent(const string& generator_name,
99                                      const string& parameter,
100                                      const string& file,
101                                      const string& first_message_name);
102 };
103 
104 }  // namespace compiler
105 }  // namespace protobuf
106 
107 }  // namespace google
108 #endif  // GOOGLE_PROTOBUF_COMPILER_MOCK_CODE_GENERATOR_H__
109