1d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// Protocol Buffers - Google's data interchange format
2d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// Copyright 2008 Google Inc.  All rights reserved.
3afb4b72037e3f13db208590fc782c4bc8e27f862Jeff Davidson// https://developers.google.com/protocol-buffers/
4d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville//
5d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// Redistribution and use in source and binary forms, with or without
6d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// modification, are permitted provided that the following conditions are
7d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// met:
8d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville//
9d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville//     * Redistributions of source code must retain the above copyright
10d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// notice, this list of conditions and the following disclaimer.
11d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville//     * Redistributions in binary form must reproduce the above
12d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// copyright notice, this list of conditions and the following disclaimer
13d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// in the documentation and/or other materials provided with the
14d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// distribution.
15d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville//     * Neither the name of Google Inc. nor the names of its
16d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// contributors may be used to endorse or promote products derived from
17d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// this software without specific prior written permission.
18d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville//
19d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville
31d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// Author: kenton@google.com (Kenton Varda)
32d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville
33d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville#ifndef GOOGLE_PROTOBUF_COMPILER_MOCK_CODE_GENERATOR_H__
34d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville#define GOOGLE_PROTOBUF_COMPILER_MOCK_CODE_GENERATOR_H__
35d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville
36d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville#include <string>
37d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville#include <google/protobuf/compiler/code_generator.h>
38d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville
39d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Savillenamespace google {
40d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Savillenamespace protobuf {
41d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Savillenamespace compiler {
42d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville
43d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// A mock CodeGenerator, used by command_line_interface_unittest.  This is in
44d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// its own file so that it can be used both directly and as a plugin.
45d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville//
46d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// Generate() produces some output which can be checked by ExpectCalled().  The
47d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// generator can run in a different process (e.g. a plugin).
48d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville//
49d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// If the parameter is "insert=NAMES", the MockCodeGenerator will insert lines
50d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// into the files generated by other MockCodeGenerators instead of creating
51d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// its own file.  NAMES is a comma-separated list of the names of those other
52d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// MockCodeGenerators.
53d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville//
54d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// MockCodeGenerator will also modify its behavior slightly if the input file
55d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// contains a message type with one of the following names:
56d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville//   MockCodeGenerator_Error:  Causes Generate() to return false and set the
57d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville//     error message to "Saw message type MockCodeGenerator_Error."
58d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville//   MockCodeGenerator_Exit:  Generate() prints "Saw message type
59d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville//     MockCodeGenerator_Exit." to stderr and then calls exit(123).
60d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville//   MockCodeGenerator_Abort:  Generate() prints "Saw message type
61d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville//     MockCodeGenerator_Abort." to stderr and then calls abort().
62a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson//   MockCodeGenerator_HasSourceCodeInfo:  Causes Generate() to abort after
63a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson//     printing "Saw message type MockCodeGenerator_HasSourceCodeInfo: FOO." to
64a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson//     stderr, where FOO is "1" if the supplied FileDescriptorProto has source
65a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson//     code info, and "0" otherwise.
66d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Savilleclass MockCodeGenerator : public CodeGenerator {
67d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville public:
68d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  MockCodeGenerator(const string& name);
69d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  virtual ~MockCodeGenerator();
70d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville
71d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  // Expect (via gTest) that a MockCodeGenerator with the given name was called
72d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  // with the given parameters by inspecting the output location.
73d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  //
74d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  // |insertions| is a comma-separated list of names of MockCodeGenerators which
75d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  // should have inserted lines into this file.
76a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson  // |parsed_file_list| is a comma-separated list of names of the files
77a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson  // that are being compiled together in this run.
78d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  static void ExpectGenerated(const string& name,
79d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville                              const string& parameter,
80d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville                              const string& insertions,
81d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville                              const string& file,
82d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville                              const string& first_message_name,
83a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson                              const string& parsed_file_list,
84d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville                              const string& output_directory);
85d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville
86d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  // Get the name of the file which would be written by the given generator.
87d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  static string GetOutputFileName(const string& generator_name,
88d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville                                  const FileDescriptor* file);
89d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  static string GetOutputFileName(const string& generator_name,
90d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville                                  const string& file);
91d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville
92d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  // implements CodeGenerator ----------------------------------------
93d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville
94d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  virtual bool Generate(const FileDescriptor* file,
95d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville                        const string& parameter,
96a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson                        GeneratorContext* context,
97d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville                        string* error) const;
98d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville
99d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville private:
100d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  string name_;
101d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville
102d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  static string GetOutputFileContent(const string& generator_name,
103d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville                                     const string& parameter,
104a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson                                     const FileDescriptor* file,
105a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson                                     GeneratorContext *context);
106d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  static string GetOutputFileContent(const string& generator_name,
107d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville                                     const string& parameter,
108d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville                                     const string& file,
109a3b2a6da25a76f17c73d31def3952feb0fd2296eJeff Davidson                                     const string& parsed_file_list,
110d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville                                     const string& first_message_name);
111d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville};
112d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville
113d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville}  // namespace compiler
114d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville}  // namespace protobuf
115d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville
116d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville}  // namespace google
117d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville#endif  // GOOGLE_PROTOBUF_COMPILER_MOCK_CODE_GENERATOR_H__
118