Restructure syntax branches in binary conformance tests.

This doesn't change the tests at all, but refactors them to be more reusable in different contexts.  Specifically, this will make it easier to add corresponding editions-based tests.  This also splits the concept of "uses a proto3 message" and "runs proto3 tests", in preparation for that change.

PiperOrigin-RevId: 574299923
pull/14421/head
Mike Kruskal 1 year ago committed by Copybara-Service
parent 8908ea9aff
commit 8c80a3265d
  1. 1
      conformance/BUILD.bazel
  2. 1619
      conformance/binary_json_conformance_suite.cc
  3. 93
      conformance/binary_json_conformance_suite.h

@ -157,6 +157,7 @@ cc_library(
":conformance_test", ":conformance_test",
":test_messages_proto2_proto_cc", ":test_messages_proto2_proto_cc",
":test_messages_proto3_proto_cc", ":test_messages_proto3_proto_cc",
"@com_google_absl//absl/log:die_if_null",
"@com_google_absl//absl/status", "@com_google_absl//absl/status",
"@jsoncpp", "@jsoncpp",
], ],

File diff suppressed because it is too large Load Diff

@ -8,19 +8,53 @@
#ifndef CONFORMANCE_BINARY_JSON_CONFORMANCE_SUITE_H #ifndef CONFORMANCE_BINARY_JSON_CONFORMANCE_SUITE_H
#define CONFORMANCE_BINARY_JSON_CONFORMANCE_SUITE_H #define CONFORMANCE_BINARY_JSON_CONFORMANCE_SUITE_H
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "json/json.h" #include "json/json.h"
#include "conformance_test.h" #include "conformance_test.h"
#include "google/protobuf/descriptor.h" #include "google/protobuf/descriptor.h"
#include "google/protobuf/util/type_resolver.h"
namespace google { namespace google {
namespace protobuf { namespace protobuf {
class BinaryAndJsonConformanceSuite : public ConformanceTestSuite { class BinaryAndJsonConformanceSuite : public ConformanceTestSuite {
public: public:
BinaryAndJsonConformanceSuite() {} BinaryAndJsonConformanceSuite() = default;
private: private:
void RunSuiteImpl() override; void RunSuiteImpl() override;
bool ParseJsonResponse(const conformance::ConformanceResponse& response,
Message* test_message);
bool ParseResponse(const conformance::ConformanceResponse& response,
const ConformanceRequestSetting& setting,
Message* test_message) override;
template <typename MessageType>
friend class BinaryAndJsonConformanceSuiteImpl;
std::unique_ptr<google::protobuf::util::TypeResolver> type_resolver_;
std::string type_url_;
};
template <typename MessageType>
class BinaryAndJsonConformanceSuiteImpl {
public:
BinaryAndJsonConformanceSuiteImpl(BinaryAndJsonConformanceSuite* suite,
bool run_proto3_tests);
private:
using ConformanceRequestSetting =
BinaryAndJsonConformanceSuite::ConformanceRequestSetting;
using ConformanceLevel = BinaryAndJsonConformanceSuite::ConformanceLevel;
constexpr static ConformanceLevel RECOMMENDED = ConformanceLevel::RECOMMENDED;
constexpr static ConformanceLevel REQUIRED = ConformanceLevel::REQUIRED;
void RunAllTests();
void RunBinaryPerformanceTests(); void RunBinaryPerformanceTests();
void RunJsonPerformanceTests(); void RunJsonPerformanceTests();
void RunJsonTests(); void RunJsonTests();
@ -37,10 +71,6 @@ class BinaryAndJsonConformanceSuite : public ConformanceTestSuite {
void RunValidJsonTest(const std::string& test_name, ConformanceLevel level, void RunValidJsonTest(const std::string& test_name, ConformanceLevel level,
const std::string& input_json, const std::string& input_json,
const std::string& equivalent_text_format); const std::string& equivalent_text_format);
void RunValidJsonTest(const std::string& test_name, ConformanceLevel level,
const std::string& input_json,
const std::string& equivalent_text_format,
bool is_proto3);
void RunValidJsonTestWithMessage(const std::string& test_name, void RunValidJsonTestWithMessage(const std::string& test_name,
ConformanceLevel level, ConformanceLevel level,
const std::string& input_json, const std::string& input_json,
@ -57,38 +87,26 @@ class BinaryAndJsonConformanceSuite : public ConformanceTestSuite {
void RunValidProtobufTest(const std::string& test_name, void RunValidProtobufTest(const std::string& test_name,
ConformanceLevel level, ConformanceLevel level,
const std::string& input_protobuf, const std::string& input_protobuf,
const std::string& equivalent_text_format, const std::string& equivalent_text_format);
bool is_proto3);
void RunValidBinaryProtobufTest(const std::string& test_name, void RunValidBinaryProtobufTest(const std::string& test_name,
ConformanceLevel level, ConformanceLevel level,
const std::string& input_protobuf, const std::string& input_protobuf);
bool is_proto3);
void RunValidBinaryProtobufTest(const std::string& test_name, void RunValidBinaryProtobufTest(const std::string& test_name,
ConformanceLevel level, ConformanceLevel level,
const std::string& input_protobuf, const std::string& input_protobuf,
const std::string& expected_protobuf, const std::string& expected_protobuf);
bool is_proto3); void RunBinaryPerformanceMergeMessageWithField(
void RunBinaryPerformanceMergeMessageWithField(const std::string& test_name, const std::string& test_name, const std::string& field_proto);
const std::string& field_proto,
bool is_proto3);
void RunValidProtobufTestWithMessage( void RunValidProtobufTestWithMessage(
const std::string& test_name, ConformanceLevel level, const std::string& test_name, ConformanceLevel level,
const Message* input, const std::string& equivalent_text_format, const Message* input, const std::string& equivalent_text_format);
bool is_proto3);
bool ParseJsonResponse(const conformance::ConformanceResponse& response,
Message* test_message);
bool ParseResponse(const conformance::ConformanceResponse& response,
const ConformanceRequestSetting& setting,
Message* test_message) override;
typedef std::function<bool(const Json::Value&)> Validator; typedef std::function<bool(const Json::Value&)> Validator;
void RunValidJsonTestWithValidator(const std::string& test_name, void RunValidJsonTestWithValidator(const std::string& test_name,
ConformanceLevel level, ConformanceLevel level,
const std::string& input_json, const std::string& input_json,
const Validator& validator, const Validator& validator);
bool is_proto3);
void ExpectParseFailureForJson(const std::string& test_name, void ExpectParseFailureForJson(const std::string& test_name,
ConformanceLevel level, ConformanceLevel level,
const std::string& input_json); const std::string& input_json);
@ -97,8 +115,7 @@ class BinaryAndJsonConformanceSuite : public ConformanceTestSuite {
const std::string& text_format); const std::string& text_format);
void ExpectParseFailureForProtoWithProtoVersion(const std::string& proto, void ExpectParseFailureForProtoWithProtoVersion(const std::string& proto,
const std::string& test_name, const std::string& test_name,
ConformanceLevel level, ConformanceLevel level);
bool is_proto3);
void ExpectParseFailureForProto(const std::string& proto, void ExpectParseFailureForProto(const std::string& proto,
const std::string& test_name, const std::string& test_name,
ConformanceLevel level); ConformanceLevel level);
@ -107,10 +124,8 @@ class BinaryAndJsonConformanceSuite : public ConformanceTestSuite {
ConformanceLevel level); ConformanceLevel level);
void TestPrematureEOFForType(google::protobuf::FieldDescriptor::Type type); void TestPrematureEOFForType(google::protobuf::FieldDescriptor::Type type);
void TestIllegalTags(); void TestIllegalTags();
template <class MessageType> void TestOneofMessage();
void TestOneofMessage(MessageType& message, bool is_proto3); void TestUnknownMessage();
template <class MessageType>
void TestUnknownMessage(MessageType& message, bool is_proto3);
void TestValidDataForType( void TestValidDataForType(
google::protobuf::FieldDescriptor::Type, google::protobuf::FieldDescriptor::Type,
std::vector<std::pair<std::string, std::string>> values); std::vector<std::pair<std::string, std::string>> values);
@ -128,8 +143,22 @@ class BinaryAndJsonConformanceSuite : public ConformanceTestSuite {
void TestJsonPerformanceMergeMessageWithRepeatedFieldForType( void TestJsonPerformanceMergeMessageWithRepeatedFieldForType(
google::protobuf::FieldDescriptor::Type, std::string field_value); google::protobuf::FieldDescriptor::Type, std::string field_value);
std::unique_ptr<google::protobuf::util::TypeResolver> type_resolver_; enum class Packed {
std::string type_url_; kUnspecified = 0,
kTrue = 1,
kFalse = 2,
};
const FieldDescriptor* GetFieldForType(
FieldDescriptor::Type type, bool repeated,
Packed packed = Packed::kUnspecified) const;
const FieldDescriptor* GetFieldForMapType(
FieldDescriptor::Type key_type, FieldDescriptor::Type value_type) const;
const FieldDescriptor* GetFieldForOneofType(FieldDescriptor::Type type,
bool exclusive = false) const;
std::string SyntaxIdentifier() const;
BinaryAndJsonConformanceSuite& suite_;
bool run_proto3_tests_;
}; };
} // namespace protobuf } // namespace protobuf

Loading…
Cancel
Save