blob: e1665f88cef19c132eb721fca545809491f3eff4 [file] [log] [blame]
kenton@google.com5e744ff2009-12-18 04:51:42 +00001// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc. All rights reserved.
Feng Xiaoe4288622014-10-01 16:26:23 -07003// https://developers.google.com/protocol-buffers/
kenton@google.com5e744ff2009-12-18 04:51:42 +00004//
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>
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070037
kenton@google.com5e744ff2009-12-18 04:51:42 +000038#include <google/protobuf/compiler/code_generator.h>
39
40namespace google {
41namespace protobuf {
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070042class FileDescriptor;
43} // namespace protobuf
44
45namespace protobuf {
kenton@google.com5e744ff2009-12-18 04:51:42 +000046namespace compiler {
47
48// A mock CodeGenerator, used by command_line_interface_unittest. This is in
49// its own file so that it can be used both directly and as a plugin.
50//
51// Generate() produces some output which can be checked by ExpectCalled(). The
52// generator can run in a different process (e.g. a plugin).
53//
54// If the parameter is "insert=NAMES", the MockCodeGenerator will insert lines
55// into the files generated by other MockCodeGenerators instead of creating
56// its own file. NAMES is a comma-separated list of the names of those other
57// MockCodeGenerators.
58//
59// MockCodeGenerator will also modify its behavior slightly if the input file
60// contains a message type with one of the following names:
61// MockCodeGenerator_Error: Causes Generate() to return false and set the
62// error message to "Saw message type MockCodeGenerator_Error."
63// MockCodeGenerator_Exit: Generate() prints "Saw message type
64// MockCodeGenerator_Exit." to stderr and then calls exit(123).
65// MockCodeGenerator_Abort: Generate() prints "Saw message type
66// MockCodeGenerator_Abort." to stderr and then calls abort().
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000067// MockCodeGenerator_HasSourceCodeInfo: Causes Generate() to abort after
68// printing "Saw message type MockCodeGenerator_HasSourceCodeInfo: FOO." to
69// stderr, where FOO is "1" if the supplied FileDescriptorProto has source
70// code info, and "0" otherwise.
kenton@google.com5e744ff2009-12-18 04:51:42 +000071class MockCodeGenerator : public CodeGenerator {
72 public:
73 MockCodeGenerator(const string& name);
74 virtual ~MockCodeGenerator();
75
76 // Expect (via gTest) that a MockCodeGenerator with the given name was called
77 // with the given parameters by inspecting the output location.
78 //
79 // |insertions| is a comma-separated list of names of MockCodeGenerators which
80 // should have inserted lines into this file.
liujisi@google.com33165fe2010-11-02 13:14:58 +000081 // |parsed_file_list| is a comma-separated list of names of the files
82 // that are being compiled together in this run.
kenton@google.com5e744ff2009-12-18 04:51:42 +000083 static void ExpectGenerated(const string& name,
84 const string& parameter,
85 const string& insertions,
86 const string& file,
87 const string& first_message_name,
liujisi@google.com33165fe2010-11-02 13:14:58 +000088 const string& parsed_file_list,
kenton@google.com5e744ff2009-12-18 04:51:42 +000089 const string& output_directory);
90
91 // Get the name of the file which would be written by the given generator.
92 static string GetOutputFileName(const string& generator_name,
93 const FileDescriptor* file);
94 static string GetOutputFileName(const string& generator_name,
95 const string& file);
96
97 // implements CodeGenerator ----------------------------------------
98
99 virtual bool Generate(const FileDescriptor* file,
100 const string& parameter,
liujisi@google.com33165fe2010-11-02 13:14:58 +0000101 GeneratorContext* context,
kenton@google.com5e744ff2009-12-18 04:51:42 +0000102 string* error) const;
103
104 private:
105 string name_;
106
107 static string GetOutputFileContent(const string& generator_name,
108 const string& parameter,
liujisi@google.com33165fe2010-11-02 13:14:58 +0000109 const FileDescriptor* file,
110 GeneratorContext *context);
kenton@google.com5e744ff2009-12-18 04:51:42 +0000111 static string GetOutputFileContent(const string& generator_name,
112 const string& parameter,
113 const string& file,
liujisi@google.com33165fe2010-11-02 13:14:58 +0000114 const string& parsed_file_list,
kenton@google.com5e744ff2009-12-18 04:51:42 +0000115 const string& first_message_name);
116};
117
118} // namespace compiler
119} // namespace protobuf
120
121} // namespace google
122#endif // GOOGLE_PROTOBUF_COMPILER_MOCK_CODE_GENERATOR_H__