Add unit-tests for the C++ generator

PiperOrigin-RevId: 542674144
pull/13117/head
Mike Kruskal 2 years ago committed by Copybara-Service
parent b483eccef1
commit 1b162147d3
  1. 2
      src/google/protobuf/compiler/code_generator.h
  2. 12
      src/google/protobuf/compiler/cpp/BUILD.bazel
  3. 16
      src/google/protobuf/compiler/cpp/generator.cc
  4. 3
      src/google/protobuf/compiler/cpp/generator.h
  5. 92
      src/google/protobuf/compiler/cpp/generator_unittest.cc
  6. 1
      src/google/protobuf/descriptor.h

@ -43,6 +43,7 @@
#include <vector>
#include "absl/strings/string_view.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/port.h"
// Must be included last.
@ -124,6 +125,7 @@ class PROTOC_EXPORT CodeGenerator {
// version of the library. When protobufs does a api breaking change, the
// method can be removed.
virtual bool HasGenerateAll() const { return true; }
};
// CodeGenerators generate one or more files in a given directory. This

@ -184,6 +184,18 @@ cc_test(
],
)
cc_test(
name = "generator_unittest",
srcs = ["generator_unittest.cc"],
deps = [
":cpp",
"//:protobuf",
"//src/google/protobuf/compiler:command_line_interface_tester",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
)
cc_test(
name = "message_size_unittest",
srcs = ["message_size_unittest.cc"],

@ -40,12 +40,15 @@
#include <utility>
#include <vector>
#include "absl/status/status.h"
#include "absl/strings/match.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/compiler/cpp/file.h"
#include "google/protobuf/compiler/cpp/helpers.h"
#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/descriptor_visitor.h"
namespace google {
namespace protobuf {
@ -96,6 +99,7 @@ absl::flat_hash_map<absl::string_view, std::string> CommonVars(
"K"},
};
}
} // namespace
bool CppGenerator::Generate(const FileDescriptor* file,
@ -235,6 +239,12 @@ bool CppGenerator::Generate(const FileDescriptor* file,
return true;
}
absl::Status validation_result = ValidateFeatures(file);
if (!validation_result.ok()) {
*error = std::string(validation_result.message());
return false;
}
FileGenerator file_generator(file, file_options);
// Generate header(s).
@ -351,6 +361,12 @@ bool CppGenerator::Generate(const FileDescriptor* file,
return true;
}
absl::Status CppGenerator::ValidateFeatures(const FileDescriptor* file) const {
absl::Status status = absl::OkStatus();
return status;
}
} // namespace cpp
} // namespace compiler
} // namespace protobuf

@ -41,6 +41,7 @@
#include <utility>
#include "google/protobuf/compiler/code_generator.h"
#include "absl/status/status.h"
// Must be included last.
#include "google/protobuf/port_def.inc"
@ -94,6 +95,8 @@ class PROTOC_EXPORT CppGenerator : public CodeGenerator {
private:
bool opensource_runtime_ = PROTO2_IS_OSS;
std::string runtime_include_base_;
absl::Status ValidateFeatures(const FileDescriptor* file) const;
};
} // namespace cpp
} // namespace compiler

@ -0,0 +1,92 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "google/protobuf/compiler/cpp/generator.h"
#include <memory>
#include "google/protobuf/descriptor.pb.h"
#include <gtest/gtest.h>
#include "google/protobuf/compiler/command_line_interface_tester.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace cpp {
namespace {
class CppGeneratorTest : public CommandLineInterfaceTester {
protected:
CppGeneratorTest() {
RegisterGenerator("--cpp_out", "--cpp_opt",
std::make_unique<CppGenerator>(), "C++ test generator");
// Generate built-in protos.
CreateTempFile(
"net/proto2/proto/descriptor.proto",
google::protobuf::DescriptorProto::descriptor()->file()->DebugString());
}
};
TEST_F(CppGeneratorTest, Basic) {
CreateTempFile("foo.proto",
R"schema(
syntax = "proto2";
message Foo {
optional int32 bar = 1;
})schema");
RunProtoc(
"protocol_compiler --proto_path=$tmpdir --cpp_out=$tmpdir foo.proto");
ExpectNoErrors();
}
TEST_F(CppGeneratorTest, BasicError) {
CreateTempFile("foo.proto",
R"schema(
syntax = "proto2";
message Foo {
int32 bar = 1;
})schema");
RunProtoc(
"protocol_compiler --proto_path=$tmpdir --cpp_out=$tmpdir foo.proto");
ExpectErrorSubstring(
"foo.proto:4:7: Expected \"required\", \"optional\", or \"repeated\"");
}
} // namespace
} // namespace cpp
} // namespace compiler
} // namespace protobuf
} // namespace google

@ -137,6 +137,7 @@ class UnknownField;
// Defined in command_line_interface.cc
namespace compiler {
class CodeGenerator;
class CommandLineInterface;
namespace cpp {
// Defined in helpers.h

Loading…
Cancel
Save