blob: 5c7942bda6cefc7870be5e2439882b8e8ba799b3 [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.
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
39namespace google {
40namespace protobuf {
41namespace 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().
62class 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.
liujisi@google.com33165fe2010-11-02 13:14:58 +000072 // |parsed_file_list| is a comma-separated list of names of the files
73 // that are being compiled together in this run.
kenton@google.com5e744ff2009-12-18 04:51:42 +000074 static void ExpectGenerated(const string& name,
75 const string& parameter,
76 const string& insertions,
77 const string& file,
78 const string& first_message_name,
liujisi@google.com33165fe2010-11-02 13:14:58 +000079 const string& parsed_file_list,
kenton@google.com5e744ff2009-12-18 04:51:42 +000080 const string& output_directory);
81
82 // Get the name of the file which would be written by the given generator.
83 static string GetOutputFileName(const string& generator_name,
84 const FileDescriptor* file);
85 static string GetOutputFileName(const string& generator_name,
86 const string& file);
87
88 // implements CodeGenerator ----------------------------------------
89
90 virtual bool Generate(const FileDescriptor* file,
91 const string& parameter,
liujisi@google.com33165fe2010-11-02 13:14:58 +000092 GeneratorContext* context,
kenton@google.com5e744ff2009-12-18 04:51:42 +000093 string* error) const;
94
95 private:
96 string name_;
97
98 static string GetOutputFileContent(const string& generator_name,
99 const string& parameter,
liujisi@google.com33165fe2010-11-02 13:14:58 +0000100 const FileDescriptor* file,
101 GeneratorContext *context);
kenton@google.com5e744ff2009-12-18 04:51:42 +0000102 static string GetOutputFileContent(const string& generator_name,
103 const string& parameter,
104 const string& file,
liujisi@google.com33165fe2010-11-02 13:14:58 +0000105 const string& parsed_file_list,
kenton@google.com5e744ff2009-12-18 04:51:42 +0000106 const string& first_message_name);
107};
108
109} // namespace compiler
110} // namespace protobuf
111
112} // namespace google
113#endif // GOOGLE_PROTOBUF_COMPILER_MOCK_CODE_GENERATOR_H__