123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- /*
- * MIT License
- *
- * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
- *
- * 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 <QVariantList>
- #include <QMetaProperty>
- #include <QSignalSpy>
- #include <QProtobufSerializer>
- #include <QDateTime>
- #include <stdio.h>
- #include <iostream>
- #include <gtest/gtest.h>
- #include <google/protobuf/any.qpb.h>
- #include <google/protobuf/api.qpb.h>
- #include <google/protobuf/type.qpb.h>
- #include <google/protobuf/struct.qpb.h>
- #include <google/protobuf/empty.qpb.h>
- #include <google/protobuf/duration.qpb.h>
- #include <google/protobuf/wrappers.qpb.h>
- #include <google/protobuf/field_mask.qpb.h>
- #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<typename MessageType, typename PropertyType>
- 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<PropertyType>());
- }
- ASSERT_STREQ(MessageType::staticMetaObject.property(propertyNumber).name(), propertyName);
- }
- static void SetUpTestCase() {
- QtProtobuf::qRegisterProtobufTypes();
- serializer.reset(new QProtobufSerializer);
- }
- static std::unique_ptr<QProtobufSerializer> serializer;
- };
- std::unique_ptr<QProtobufSerializer> WellknowntypesTest::serializer;
- TEST_F(WellknowntypesTest, AnyTest)
- {
- ASSERT_GT(qMetaTypeId<Any>(), 0);
- assertMessagePropertyRegistered<Any, QString>(1, "QString", "typeUrl");
- assertMessagePropertyRegistered<Any, QByteArray>(2, "QByteArray", "value");
- }
- TEST_F(WellknowntypesTest, ApiTest)
- {
- ASSERT_GT(qMetaTypeId<Api>(), 0);
- assertMessagePropertyRegistered<Api, QString>(1, "QString", "name");
- assertMessagePropertyRegistered<Api, MethodRepeated>(2, "MethodRepeated", "methods");
- assertMessagePropertyRegistered<Api, OptionRepeated>(3, "OptionRepeated", "options");
- assertMessagePropertyRegistered<Api, QString>(4, "QString", "version");
- assertMessagePropertyRegistered<Api, SourceContext>(5, "SourceContext*", "sourceContext", true);
- assertMessagePropertyRegistered<Api, MixinRepeated>(6, "MixinRepeated", "mixins");
- assertMessagePropertyRegistered<Api, SyntaxGadget::Syntax>(7, "google::protobuf::SyntaxGadget::Syntax", "syntax");
- }
- TEST_F(WellknowntypesTest, MethodTest)
- {
- ASSERT_GT(qMetaTypeId<Method>(), 0);
- assertMessagePropertyRegistered<Method, QString>(1, "QString", "name");
- assertMessagePropertyRegistered<Method, QString>(2, "QString", "requestTypeUrl");
- assertMessagePropertyRegistered<Method, bool>(3, "bool", "requestStreaming");
- assertMessagePropertyRegistered<Method, QString>(4, "QString", "responseTypeUrl");
- assertMessagePropertyRegistered<Method, bool>(5, "bool", "responseStreaming");
- assertMessagePropertyRegistered<Method, OptionRepeated>(6, "OptionRepeated", "options");
- assertMessagePropertyRegistered<Method, SyntaxGadget::Syntax>(7, "google::protobuf::SyntaxGadget::Syntax", "syntax");
- }
- TEST_F(WellknowntypesTest, MixinTest)
- {
- ASSERT_GT(qMetaTypeId<Mixin>(), 0);
- assertMessagePropertyRegistered<Mixin, QString>(1, "QString", "name");
- assertMessagePropertyRegistered<Mixin, QString>(2, "QString", "root");
- }
- TEST_F(WellknowntypesTest, DurationTest)
- {
- ASSERT_GT(qMetaTypeId<Duration>(), 0);
- assertMessagePropertyRegistered<Duration, QtProtobuf::int64>(1, "QtProtobuf::int64", "seconds");
- assertMessagePropertyRegistered<Duration, QtProtobuf::int32>(2, "QtProtobuf::int32", "nanos_p");
- }
- TEST_F(WellknowntypesTest, EmptyTest)
- {
- ASSERT_GT(qMetaTypeId<Empty>(), 0);
- }
- TEST_F(WellknowntypesTest, FieldMaskTest)
- {
- ASSERT_GT(qMetaTypeId<FieldMask>(), 0);
- assertMessagePropertyRegistered<FieldMask, QStringList>(1, "QStringList", "paths");
- }
- TEST_F(WellknowntypesTest, SourceContextTest)
- {
- ASSERT_GT(qMetaTypeId<SourceContext>(), 0);
- assertMessagePropertyRegistered<SourceContext, QString>(1, "QString", "fileName");
- }
- TEST_F(WellknowntypesTest, StructTest)
- {
- ASSERT_GT(qMetaTypeId<Struct>(), 0);
- assertMessagePropertyRegistered<Struct, Struct::FieldsEntry>(1, "Struct::FieldsEntry", "fields");
- }
- TEST_F(WellknowntypesTest, ValueTest)
- {
- ASSERT_GT(qMetaTypeId<Value>(), 0);
- assertMessagePropertyRegistered<Value, NullValueGadget::NullValue>(1, "google::protobuf::NullValueGadget::NullValue", "nullValue");
- assertMessagePropertyRegistered<Value, double>(2, "double", "numberValue");
- assertMessagePropertyRegistered<Value, QString>(3, "QString", "stringValue");
- assertMessagePropertyRegistered<Value, bool>(4, "bool", "boolValue");
- assertMessagePropertyRegistered<Value, Struct *>(5, "Struct*", "structValue");
- assertMessagePropertyRegistered<Value, ListValue *>(6, "ListValue*", "listValue");
- }
- TEST_F(WellknowntypesTest, ListValueTest)
- {
- ASSERT_GT(qMetaTypeId<ListValue>(), 0);
- assertMessagePropertyRegistered<ListValue, ValueRepeated>(1, "ValueRepeated", "values");
- }
- TEST_F(WellknowntypesTest, TimestampTest)
- {
- ASSERT_GT(qMetaTypeId<Timestamp>(), 0);
- assertMessagePropertyRegistered<Timestamp, QtProtobuf::int64>(1, "QtProtobuf::int64", "seconds");
- assertMessagePropertyRegistered<Timestamp, QtProtobuf::int32>(2, "QtProtobuf::int32", "nanos_p");
- }
- TEST_F(WellknowntypesTest, TypeTest)
- {
- ASSERT_GT(qMetaTypeId<Type>(), 0);
- Q_PROPERTY(QStringList oneofs READ oneofs WRITE setOneofs NOTIFY oneofsChanged)
- assertMessagePropertyRegistered<Type, QString>(1, "QString", "name");
- assertMessagePropertyRegistered<Type, FieldRepeated>(2, "FieldRepeated", "fields");
- assertMessagePropertyRegistered<Type, QStringList>(3, "QStringList", "oneofs");
- assertMessagePropertyRegistered<Type, OptionRepeated>(4, "OptionRepeated", "options");
- assertMessagePropertyRegistered<Type, SourceContext *>(5, "SourceContext*", "sourceContext", true);
- assertMessagePropertyRegistered<Type, SyntaxGadget::Syntax>(6, "google::protobuf::SyntaxGadget::Syntax", "syntax");
- }
- TEST_F(WellknowntypesTest, FieldTest)
- {
- ASSERT_GT(qMetaTypeId<Field>(), 0);
- assertMessagePropertyRegistered<Field, Field::Kind>(1, "Kind", "kind");
- assertMessagePropertyRegistered<Field, Field::Cardinality>(2, "Cardinality", "cardinality");
- assertMessagePropertyRegistered<Field, QtProtobuf::int32>(3, "QtProtobuf::int32", "number_p");
- assertMessagePropertyRegistered<Field, QString>(4, "QString", "name");
- assertMessagePropertyRegistered<Field, QString>(6, "QString", "typeUrl");
- assertMessagePropertyRegistered<Field, QtProtobuf::int32>(7, "QtProtobuf::int32", "oneofIndex_p");
- assertMessagePropertyRegistered<Field, bool>(8, "bool", "packed");
- assertMessagePropertyRegistered<Field, OptionRepeated>(9, "OptionRepeated", "options");
- assertMessagePropertyRegistered<Field, QString>(10, "QString", "jsonName");
- assertMessagePropertyRegistered<Field, QString>(11, "QString", "defaultValue");
- }
- TEST_F(WellknowntypesTest, EnumTest)
- {
- ASSERT_GT(qMetaTypeId<Enum>(), 0);
- assertMessagePropertyRegistered<Enum, QString>(1, "QString", "name");
- assertMessagePropertyRegistered<Enum, EnumValueRepeated>(2, "EnumValueRepeated", "enumvalue");
- assertMessagePropertyRegistered<Enum, OptionRepeated>(3, "OptionRepeated", "options");
- // assertMessagePropertyRegistered<Enum, SourceContext *>(4, "SourceContext*", "sourceContext");
- assertMessagePropertyRegistered<Enum, SyntaxGadget::Syntax>(5, "google::protobuf::SyntaxGadget::Syntax", "syntax");
- }
- TEST_F(WellknowntypesTest, EnumValueTest)
- {
- ASSERT_GT(qMetaTypeId<EnumValue>(), 0);
- assertMessagePropertyRegistered<EnumValue, QString>(1, "QString", "name");
- assertMessagePropertyRegistered<EnumValue, QtProtobuf::int32>(2, "QtProtobuf::int32", "number_p");
- assertMessagePropertyRegistered<EnumValue, OptionRepeated>(3, "OptionRepeated", "options");
- }
- TEST_F(WellknowntypesTest, OptionTest)
- {
- ASSERT_GT(qMetaTypeId<Option>(), 0);
- assertMessagePropertyRegistered<Option, QString>(1, "QString", "name");
- assertMessagePropertyRegistered<Option, Any *>(2, "Any*", "value", true);
- }
- TEST_F(WellknowntypesTest, DoubleValueTest)
- {
- ASSERT_GT(qMetaTypeId<DoubleValue>(), 0);
- assertMessagePropertyRegistered<DoubleValue, double>(1, "double", "value");
- }
- TEST_F(WellknowntypesTest, FloatValueTest)
- {
- ASSERT_GT(qMetaTypeId<FloatValue>(), 0);
- assertMessagePropertyRegistered<FloatValue, float>(1, "float", "value");
- }
- TEST_F(WellknowntypesTest, Int64ValueTest)
- {
- ASSERT_GT(qMetaTypeId<Int64Value>(), 0);
- assertMessagePropertyRegistered<Int64Value, QtProtobuf::int64>(1, "QtProtobuf::int64", "value");
- }
- TEST_F(WellknowntypesTest, UInt64ValueTest)
- {
- ASSERT_GT(qMetaTypeId<UInt64Value>(), 0);
- assertMessagePropertyRegistered<UInt64Value, QtProtobuf::uint64>(1, "QtProtobuf::uint64", "value");
- }
- TEST_F(WellknowntypesTest, Int32ValueTest)
- {
- ASSERT_GT(qMetaTypeId<Int32Value>(), 0);
- assertMessagePropertyRegistered<Int32Value, QtProtobuf::int32>(1, "QtProtobuf::int32", "value_p");
- }
- TEST_F(WellknowntypesTest, UInt32ValueTest)
- {
- ASSERT_GT(qMetaTypeId<UInt32Value>(), 0);
- assertMessagePropertyRegistered<UInt32Value, QtProtobuf::uint32>(1, "QtProtobuf::uint32", "value");
- }
- TEST_F(WellknowntypesTest, StringValueTest)
- {
- ASSERT_GT(qMetaTypeId<StringValue>(), 0);
- assertMessagePropertyRegistered<StringValue, QString>(1, "QString", "value");
- }
- TEST_F(WellknowntypesTest, BytesValueTest)
- {
- ASSERT_GT(qMetaTypeId<BytesValue>(), 0);
- assertMessagePropertyRegistered<BytesValue, QByteArray>(1, "QByteArray", "value");
- }
- TEST_F(WellknowntypesTest, TimestampMessageTest)
- {
- assertMessagePropertyRegistered<qtprotobufnamespace::wellknowntypes::tests::TimestampMessage, google::protobuf::Timestamp*>(1, "google::protobuf::Timestamp*", "testField");
- }
- TEST_F(WellknowntypesTest, TimestampMessageSerializationTest) {
- qtprotobufnamespace::wellknowntypes::tests::TimestampMessage msg;
- QDateTime deserializedDateTime;
- QDateTime originalDateTime;
- qint64 secsSinceEpoch = QDateTime::currentSecsSinceEpoch();
- originalDateTime.setSecsSinceEpoch(secsSinceEpoch);
- msg.setTestField({secsSinceEpoch, 0, nullptr});
- ASSERT_EQ(msg.testField().seconds(), secsSinceEpoch);
- ASSERT_EQ(msg.testField().nanos(), 0);
- QByteArray val = msg.serialize(serializer.get());
- msg.setTestField({0, 0, nullptr});
- ASSERT_EQ(msg.testField().seconds(), 0);
- ASSERT_EQ(msg.testField().nanos(), 0);
- msg.deserialize(serializer.get(), val);
- deserializedDateTime.setSecsSinceEpoch(msg.testField().seconds());
- ASSERT_TRUE(deserializedDateTime == originalDateTime);
- }
- }
- }
|