/* * MIT License * * Copyright (c) 2019 Alexey Edelev * * This file is part of QtProtobuf project https://git.semlanik.org/semlanik/qtprotobuf * * Permission is hereby granted, free of charge, to any person obtaining a copy of this * software and associated documentation files (the "Software"), to deal in the Software * without restriction, including without limitation the rights to use, copy, modify, * merge, publish, distribute, sublicense, and/or sell copies of the Software, and * to permit persons to whom the Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be included in all copies * or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "wellknowntypes.qpb.h" using namespace google::protobuf; namespace QtProtobuf { namespace tests { class WellknowntypesTest : public ::testing::Test { public: // see simpletest.proto for property names and their field indices WellknowntypesTest() { } template static void assertMessagePropertyRegistered(int fieldIndex, const char *propertyTypeName, const char *propertyName, bool skipMetatypeCheck = false) { // TODO: there should be(?) a mapping avaialble: PropertyType -> propertyTypeName const int propertyNumber = MessageType::propertyOrdering.at(fieldIndex); ASSERT_STREQ(MessageType::staticMetaObject.property(propertyNumber).typeName(), propertyTypeName); if(!skipMetatypeCheck) { ASSERT_EQ(MessageType::staticMetaObject.property(propertyNumber).userType(), qMetaTypeId()); } ASSERT_STREQ(MessageType::staticMetaObject.property(propertyNumber).name(), propertyName); } static void SetUpTestCase() { QtProtobuf::qRegisterProtobufTypes(); serializer.reset(new QProtobufSerializer); } static std::unique_ptr serializer; }; std::unique_ptr WellknowntypesTest::serializer; TEST_F(WellknowntypesTest, AnyTest) { ASSERT_GT(qMetaTypeId(), 0); assertMessagePropertyRegistered(1, "QString", "typeUrl"); assertMessagePropertyRegistered(2, "QByteArray", "value"); } TEST_F(WellknowntypesTest, ApiTest) { ASSERT_GT(qMetaTypeId(), 0); assertMessagePropertyRegistered(1, "QString", "name"); assertMessagePropertyRegistered(2, "MethodRepeated", "methods"); assertMessagePropertyRegistered(3, "OptionRepeated", "options"); assertMessagePropertyRegistered(4, "QString", "version"); assertMessagePropertyRegistered(5, "SourceContext*", "sourceContext", true); assertMessagePropertyRegistered(6, "MixinRepeated", "mixins"); assertMessagePropertyRegistered(7, "google::protobuf::SyntaxGadget::Syntax", "syntax"); } TEST_F(WellknowntypesTest, MethodTest) { ASSERT_GT(qMetaTypeId(), 0); assertMessagePropertyRegistered(1, "QString", "name"); assertMessagePropertyRegistered(2, "QString", "requestTypeUrl"); assertMessagePropertyRegistered(3, "bool", "requestStreaming"); assertMessagePropertyRegistered(4, "QString", "responseTypeUrl"); assertMessagePropertyRegistered(5, "bool", "responseStreaming"); assertMessagePropertyRegistered(6, "OptionRepeated", "options"); assertMessagePropertyRegistered(7, "google::protobuf::SyntaxGadget::Syntax", "syntax"); } TEST_F(WellknowntypesTest, MixinTest) { ASSERT_GT(qMetaTypeId(), 0); assertMessagePropertyRegistered(1, "QString", "name"); assertMessagePropertyRegistered(2, "QString", "root"); } TEST_F(WellknowntypesTest, DurationTest) { ASSERT_GT(qMetaTypeId(), 0); assertMessagePropertyRegistered(1, "QtProtobuf::int64", "seconds"); assertMessagePropertyRegistered(2, "QtProtobuf::int32", "nanos_p"); } TEST_F(WellknowntypesTest, EmptyTest) { ASSERT_GT(qMetaTypeId(), 0); } TEST_F(WellknowntypesTest, FieldMaskTest) { ASSERT_GT(qMetaTypeId(), 0); assertMessagePropertyRegistered(1, "QStringList", "paths"); } TEST_F(WellknowntypesTest, SourceContextTest) { ASSERT_GT(qMetaTypeId(), 0); assertMessagePropertyRegistered(1, "QString", "fileName"); } TEST_F(WellknowntypesTest, StructTest) { ASSERT_GT(qMetaTypeId(), 0); assertMessagePropertyRegistered(1, "Struct::FieldsEntry", "fields"); } TEST_F(WellknowntypesTest, ValueTest) { ASSERT_GT(qMetaTypeId(), 0); assertMessagePropertyRegistered(1, "google::protobuf::NullValueGadget::NullValue", "nullValue"); assertMessagePropertyRegistered(2, "double", "numberValue"); assertMessagePropertyRegistered(3, "QString", "stringValue"); assertMessagePropertyRegistered(4, "bool", "boolValue"); assertMessagePropertyRegistered(5, "Struct*", "structValue"); assertMessagePropertyRegistered(6, "ListValue*", "listValue"); } TEST_F(WellknowntypesTest, ListValueTest) { ASSERT_GT(qMetaTypeId(), 0); assertMessagePropertyRegistered(1, "ValueRepeated", "values"); } TEST_F(WellknowntypesTest, TimestampTest) { ASSERT_GT(qMetaTypeId(), 0); assertMessagePropertyRegistered(1, "QtProtobuf::int64", "seconds"); assertMessagePropertyRegistered(2, "QtProtobuf::int32", "nanos_p"); } TEST_F(WellknowntypesTest, TypeTest) { ASSERT_GT(qMetaTypeId(), 0); Q_PROPERTY(QStringList oneofs READ oneofs WRITE setOneofs NOTIFY oneofsChanged) assertMessagePropertyRegistered(1, "QString", "name"); assertMessagePropertyRegistered(2, "FieldRepeated", "fields"); assertMessagePropertyRegistered(3, "QStringList", "oneofs"); assertMessagePropertyRegistered(4, "OptionRepeated", "options"); assertMessagePropertyRegistered(5, "SourceContext*", "sourceContext", true); assertMessagePropertyRegistered(6, "google::protobuf::SyntaxGadget::Syntax", "syntax"); } TEST_F(WellknowntypesTest, FieldTest) { ASSERT_GT(qMetaTypeId(), 0); assertMessagePropertyRegistered(1, "Kind", "kind"); assertMessagePropertyRegistered(2, "Cardinality", "cardinality"); assertMessagePropertyRegistered(3, "QtProtobuf::int32", "number_p"); assertMessagePropertyRegistered(4, "QString", "name"); assertMessagePropertyRegistered(6, "QString", "typeUrl"); assertMessagePropertyRegistered(7, "QtProtobuf::int32", "oneofIndex_p"); assertMessagePropertyRegistered(8, "bool", "packed"); assertMessagePropertyRegistered(9, "OptionRepeated", "options"); assertMessagePropertyRegistered(10, "QString", "jsonName"); assertMessagePropertyRegistered(11, "QString", "defaultValue"); } TEST_F(WellknowntypesTest, EnumTest) { ASSERT_GT(qMetaTypeId(), 0); assertMessagePropertyRegistered(1, "QString", "name"); assertMessagePropertyRegistered(2, "EnumValueRepeated", "enumvalue"); assertMessagePropertyRegistered(3, "OptionRepeated", "options"); // assertMessagePropertyRegistered(4, "SourceContext*", "sourceContext"); assertMessagePropertyRegistered(5, "google::protobuf::SyntaxGadget::Syntax", "syntax"); } TEST_F(WellknowntypesTest, EnumValueTest) { ASSERT_GT(qMetaTypeId(), 0); assertMessagePropertyRegistered(1, "QString", "name"); assertMessagePropertyRegistered(2, "QtProtobuf::int32", "number_p"); assertMessagePropertyRegistered(3, "OptionRepeated", "options"); } TEST_F(WellknowntypesTest, OptionTest) { ASSERT_GT(qMetaTypeId