/* * 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 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(); google::protobuf::qRegisterProtobufTypes(); } }; TEST_F(WellknowntypesTest, AnyTest) { ASSERT_GT(qMetaTypeId(), 0); assertMessagePropertyRegistered(1, "QString", "type_url"); 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*", "source_context", 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", "request_type_url"); assertMessagePropertyRegistered(3, "bool", "request_streaming"); assertMessagePropertyRegistered(4, "QString", "response_type_url"); assertMessagePropertyRegistered(5, "bool", "response_streaming"); 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"); } 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", "file_name"); } 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", "null_value"); assertMessagePropertyRegistered(2, "double", "number_value"); assertMessagePropertyRegistered(3, "QString", "string_value"); assertMessagePropertyRegistered(4, "bool", "bool_value"); assertMessagePropertyRegistered(5, "Struct*", "struct_value"); assertMessagePropertyRegistered(6, "ListValue*", "list_value"); } 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"); } 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*", "source_context", 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"); assertMessagePropertyRegistered(4, "QString", "name"); assertMessagePropertyRegistered(6, "QString", "type_url"); assertMessagePropertyRegistered(7, "QtProtobuf::int32", "oneof_index"); assertMessagePropertyRegistered(8, "bool", "packed"); assertMessagePropertyRegistered(9, "OptionRepeated", "options"); assertMessagePropertyRegistered(10, "QString", "json_name"); assertMessagePropertyRegistered(11, "QString", "default_value"); } TEST_F(WellknowntypesTest, EnumTest) { ASSERT_GT(qMetaTypeId(), 0); assertMessagePropertyRegistered(1, "QString", "name"); assertMessagePropertyRegistered(2, "EnumValueRepeated", "enumvalue"); assertMessagePropertyRegistered(3, "OptionRepeated", "options"); // assertMessagePropertyRegistered(4, "SourceContext*", "source_context"); assertMessagePropertyRegistered(5, "google::protobuf::SyntaxGadget::Syntax", "syntax"); } TEST_F(WellknowntypesTest, EnumValueTest) { ASSERT_GT(qMetaTypeId(), 0); assertMessagePropertyRegistered(1, "QString", "name"); assertMessagePropertyRegistered(2, "QtProtobuf::int32", "number"); assertMessagePropertyRegistered(3, "OptionRepeated", "options"); } TEST_F(WellknowntypesTest, OptionTest) { ASSERT_GT(qMetaTypeId