simpletest.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of QtProtobuf project https://git.semlanik.org/semlanik/qtprotobuf
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #include <QVariantList>
  26. #include <QMetaProperty>
  27. #include <QSignalSpy>
  28. #include <QProtobufSerializer>
  29. #include <QDateTime>
  30. #include <stdio.h>
  31. #include <iostream>
  32. #include <gtest/gtest.h>
  33. #include "wellknowntypes.qpb.h"
  34. #include <qtprotobufwellknowntypes_global.qbp.h>
  35. using namespace google::protobuf;
  36. namespace QtProtobuf {
  37. namespace tests {
  38. class WellknowntypesTest : public ::testing::Test
  39. {
  40. public:
  41. // see simpletest.proto for property names and their field indices
  42. WellknowntypesTest() {
  43. }
  44. template<typename MessageType, typename PropertyType>
  45. static void assertMessagePropertyRegistered(int fieldIndex, const char *propertyTypeName, const char *propertyName, bool skipMetatypeCheck = false)
  46. {
  47. // TODO: there should be(?) a mapping avaialble: PropertyType -> propertyTypeName
  48. const int propertyNumber = MessageType::propertyOrdering.at(fieldIndex);
  49. ASSERT_STREQ(MessageType::staticMetaObject.property(propertyNumber).typeName(), propertyTypeName);
  50. if(!skipMetatypeCheck) {
  51. ASSERT_EQ(MessageType::staticMetaObject.property(propertyNumber).userType(), qMetaTypeId<PropertyType>());
  52. }
  53. ASSERT_STREQ(MessageType::staticMetaObject.property(propertyNumber).name(), propertyName);
  54. }
  55. static void SetUpTestCase() {
  56. QtProtobuf::qRegisterProtobufTypes();
  57. google::protobuf::qRegisterProtobufTypes();
  58. serializer.reset(new QProtobufSerializer);
  59. }
  60. static std::unique_ptr<QProtobufSerializer> serializer;
  61. };
  62. std::unique_ptr<QProtobufSerializer> WellknowntypesTest::serializer;
  63. TEST_F(WellknowntypesTest, AnyTest)
  64. {
  65. ASSERT_GT(qMetaTypeId<Any>(), 0);
  66. assertMessagePropertyRegistered<Any, QString>(1, "QString", "type_url");
  67. assertMessagePropertyRegistered<Any, QByteArray>(2, "QByteArray", "value");
  68. }
  69. TEST_F(WellknowntypesTest, ApiTest)
  70. {
  71. ASSERT_GT(qMetaTypeId<Api>(), 0);
  72. assertMessagePropertyRegistered<Api, QString>(1, "QString", "name");
  73. assertMessagePropertyRegistered<Api, MethodRepeated>(2, "MethodRepeated", "methods");
  74. assertMessagePropertyRegistered<Api, OptionRepeated>(3, "OptionRepeated", "options");
  75. assertMessagePropertyRegistered<Api, QString>(4, "QString", "version");
  76. assertMessagePropertyRegistered<Api, SourceContext>(5, "SourceContext*", "source_context", true);
  77. assertMessagePropertyRegistered<Api, MixinRepeated>(6, "MixinRepeated", "mixins");
  78. assertMessagePropertyRegistered<Api, SyntaxGadget::Syntax>(7, "google::protobuf::SyntaxGadget::Syntax", "syntax");
  79. }
  80. TEST_F(WellknowntypesTest, MethodTest)
  81. {
  82. ASSERT_GT(qMetaTypeId<Method>(), 0);
  83. assertMessagePropertyRegistered<Method, QString>(1, "QString", "name");
  84. assertMessagePropertyRegistered<Method, QString>(2, "QString", "request_type_url");
  85. assertMessagePropertyRegistered<Method, bool>(3, "bool", "request_streaming");
  86. assertMessagePropertyRegistered<Method, QString>(4, "QString", "response_type_url");
  87. assertMessagePropertyRegistered<Method, bool>(5, "bool", "response_streaming");
  88. assertMessagePropertyRegistered<Method, OptionRepeated>(6, "OptionRepeated", "options");
  89. assertMessagePropertyRegistered<Method, SyntaxGadget::Syntax>(7, "google::protobuf::SyntaxGadget::Syntax", "syntax");
  90. }
  91. TEST_F(WellknowntypesTest, MixinTest)
  92. {
  93. ASSERT_GT(qMetaTypeId<Mixin>(), 0);
  94. assertMessagePropertyRegistered<Mixin, QString>(1, "QString", "name");
  95. assertMessagePropertyRegistered<Mixin, QString>(2, "QString", "root");
  96. }
  97. TEST_F(WellknowntypesTest, DurationTest)
  98. {
  99. ASSERT_GT(qMetaTypeId<Duration>(), 0);
  100. assertMessagePropertyRegistered<Duration, QtProtobuf::int64>(1, "QtProtobuf::int64", "seconds");
  101. assertMessagePropertyRegistered<Duration, QtProtobuf::int32>(2, "QtProtobuf::int32", "nanos_p");
  102. }
  103. TEST_F(WellknowntypesTest, EmptyTest)
  104. {
  105. ASSERT_GT(qMetaTypeId<Empty>(), 0);
  106. }
  107. TEST_F(WellknowntypesTest, FieldMaskTest)
  108. {
  109. ASSERT_GT(qMetaTypeId<FieldMask>(), 0);
  110. assertMessagePropertyRegistered<FieldMask, QStringList>(1, "QStringList", "paths");
  111. }
  112. TEST_F(WellknowntypesTest, SourceContextTest)
  113. {
  114. ASSERT_GT(qMetaTypeId<SourceContext>(), 0);
  115. assertMessagePropertyRegistered<SourceContext, QString>(1, "QString", "file_name");
  116. }
  117. TEST_F(WellknowntypesTest, StructTest)
  118. {
  119. ASSERT_GT(qMetaTypeId<Struct>(), 0);
  120. assertMessagePropertyRegistered<Struct, Struct::FieldsEntry>(1, "Struct::FieldsEntry", "fields");
  121. }
  122. TEST_F(WellknowntypesTest, ValueTest)
  123. {
  124. ASSERT_GT(qMetaTypeId<Value>(), 0);
  125. assertMessagePropertyRegistered<Value, NullValueGadget::NullValue>(1, "google::protobuf::NullValueGadget::NullValue", "null_value");
  126. assertMessagePropertyRegistered<Value, double>(2, "double", "number_value");
  127. assertMessagePropertyRegistered<Value, QString>(3, "QString", "string_value");
  128. assertMessagePropertyRegistered<Value, bool>(4, "bool", "bool_value");
  129. assertMessagePropertyRegistered<Value, Struct *>(5, "Struct*", "struct_value");
  130. assertMessagePropertyRegistered<Value, ListValue *>(6, "ListValue*", "list_value");
  131. }
  132. TEST_F(WellknowntypesTest, ListValueTest)
  133. {
  134. ASSERT_GT(qMetaTypeId<ListValue>(), 0);
  135. assertMessagePropertyRegistered<ListValue, ValueRepeated>(1, "ValueRepeated", "values");
  136. }
  137. TEST_F(WellknowntypesTest, TimestampTest)
  138. {
  139. ASSERT_GT(qMetaTypeId<Timestamp>(), 0);
  140. assertMessagePropertyRegistered<Timestamp, QtProtobuf::int64>(1, "QtProtobuf::int64", "seconds");
  141. assertMessagePropertyRegistered<Timestamp, QtProtobuf::int32>(2, "QtProtobuf::int32", "nanos_p");
  142. }
  143. TEST_F(WellknowntypesTest, TypeTest)
  144. {
  145. ASSERT_GT(qMetaTypeId<Type>(), 0);
  146. Q_PROPERTY(QStringList oneofs READ oneofs WRITE setOneofs NOTIFY oneofsChanged)
  147. assertMessagePropertyRegistered<Type, QString>(1, "QString", "name");
  148. assertMessagePropertyRegistered<Type, FieldRepeated>(2, "FieldRepeated", "fields");
  149. assertMessagePropertyRegistered<Type, QStringList>(3, "QStringList", "oneofs");
  150. assertMessagePropertyRegistered<Type, OptionRepeated>(4, "OptionRepeated", "options");
  151. assertMessagePropertyRegistered<Type, SourceContext *>(5, "SourceContext*", "source_context", true);
  152. assertMessagePropertyRegistered<Type, SyntaxGadget::Syntax>(6, "google::protobuf::SyntaxGadget::Syntax", "syntax");
  153. }
  154. TEST_F(WellknowntypesTest, FieldTest)
  155. {
  156. ASSERT_GT(qMetaTypeId<Field>(), 0);
  157. assertMessagePropertyRegistered<Field, Field::Kind>(1, "Kind", "kind");
  158. assertMessagePropertyRegistered<Field, Field::Cardinality>(2, "Cardinality", "cardinality");
  159. assertMessagePropertyRegistered<Field, QtProtobuf::int32>(3, "QtProtobuf::int32", "number_p");
  160. assertMessagePropertyRegistered<Field, QString>(4, "QString", "name");
  161. assertMessagePropertyRegistered<Field, QString>(6, "QString", "type_url");
  162. assertMessagePropertyRegistered<Field, QtProtobuf::int32>(7, "QtProtobuf::int32", "oneof_index_p");
  163. assertMessagePropertyRegistered<Field, bool>(8, "bool", "packed");
  164. assertMessagePropertyRegistered<Field, OptionRepeated>(9, "OptionRepeated", "options");
  165. assertMessagePropertyRegistered<Field, QString>(10, "QString", "json_name");
  166. assertMessagePropertyRegistered<Field, QString>(11, "QString", "default_value");
  167. }
  168. TEST_F(WellknowntypesTest, EnumTest)
  169. {
  170. ASSERT_GT(qMetaTypeId<Enum>(), 0);
  171. assertMessagePropertyRegistered<Enum, QString>(1, "QString", "name");
  172. assertMessagePropertyRegistered<Enum, EnumValueRepeated>(2, "EnumValueRepeated", "enumvalue");
  173. assertMessagePropertyRegistered<Enum, OptionRepeated>(3, "OptionRepeated", "options");
  174. // assertMessagePropertyRegistered<Enum, SourceContext *>(4, "SourceContext*", "source_context");
  175. assertMessagePropertyRegistered<Enum, SyntaxGadget::Syntax>(5, "google::protobuf::SyntaxGadget::Syntax", "syntax");
  176. }
  177. TEST_F(WellknowntypesTest, EnumValueTest)
  178. {
  179. ASSERT_GT(qMetaTypeId<EnumValue>(), 0);
  180. assertMessagePropertyRegistered<EnumValue, QString>(1, "QString", "name");
  181. assertMessagePropertyRegistered<EnumValue, QtProtobuf::int32>(2, "QtProtobuf::int32", "number_p");
  182. assertMessagePropertyRegistered<EnumValue, OptionRepeated>(3, "OptionRepeated", "options");
  183. }
  184. TEST_F(WellknowntypesTest, OptionTest)
  185. {
  186. ASSERT_GT(qMetaTypeId<Option>(), 0);
  187. assertMessagePropertyRegistered<Option, QString>(1, "QString", "name");
  188. assertMessagePropertyRegistered<Option, Any *>(2, "Any*", "value", true);
  189. }
  190. TEST_F(WellknowntypesTest, DoubleValueTest)
  191. {
  192. ASSERT_GT(qMetaTypeId<DoubleValue>(), 0);
  193. assertMessagePropertyRegistered<DoubleValue, double>(1, "double", "value");
  194. }
  195. TEST_F(WellknowntypesTest, FloatValueTest)
  196. {
  197. ASSERT_GT(qMetaTypeId<FloatValue>(), 0);
  198. assertMessagePropertyRegistered<FloatValue, float>(1, "float", "value");
  199. }
  200. TEST_F(WellknowntypesTest, Int64ValueTest)
  201. {
  202. ASSERT_GT(qMetaTypeId<Int64Value>(), 0);
  203. assertMessagePropertyRegistered<Int64Value, QtProtobuf::int64>(1, "QtProtobuf::int64", "value");
  204. }
  205. TEST_F(WellknowntypesTest, UInt64ValueTest)
  206. {
  207. ASSERT_GT(qMetaTypeId<UInt64Value>(), 0);
  208. assertMessagePropertyRegistered<UInt64Value, QtProtobuf::uint64>(1, "QtProtobuf::uint64", "value");
  209. }
  210. TEST_F(WellknowntypesTest, Int32ValueTest)
  211. {
  212. ASSERT_GT(qMetaTypeId<Int32Value>(), 0);
  213. assertMessagePropertyRegistered<Int32Value, QtProtobuf::int32>(1, "QtProtobuf::int32", "value_p");
  214. }
  215. TEST_F(WellknowntypesTest, UInt32ValueTest)
  216. {
  217. ASSERT_GT(qMetaTypeId<UInt32Value>(), 0);
  218. assertMessagePropertyRegistered<UInt32Value, QtProtobuf::uint32>(1, "QtProtobuf::uint32", "value");
  219. }
  220. TEST_F(WellknowntypesTest, StringValueTest)
  221. {
  222. ASSERT_GT(qMetaTypeId<StringValue>(), 0);
  223. assertMessagePropertyRegistered<StringValue, QString>(1, "QString", "value");
  224. }
  225. TEST_F(WellknowntypesTest, BytesValueTest)
  226. {
  227. ASSERT_GT(qMetaTypeId<BytesValue>(), 0);
  228. assertMessagePropertyRegistered<BytesValue, QByteArray>(1, "QByteArray", "value");
  229. }
  230. TEST_F(WellknowntypesTest, TimestampMessageTest)
  231. {
  232. assertMessagePropertyRegistered<qtprotobufnamespace::wellknowntypes::tests::TimestampMessage, google::protobuf::Timestamp*>(1, "google::protobuf::Timestamp*", "testField");
  233. }
  234. TEST_F(WellknowntypesTest, TimestampMessageSerializationTest) {
  235. qtprotobufnamespace::wellknowntypes::tests::TimestampMessage msg;
  236. QDateTime deserializedDateTime;
  237. QDateTime originalDateTime;
  238. qint64 secsSinceEpoch = QDateTime::currentSecsSinceEpoch();
  239. originalDateTime.setSecsSinceEpoch(secsSinceEpoch);
  240. msg.setTestField({secsSinceEpoch, 0, nullptr});
  241. ASSERT_EQ(msg.testField().seconds(), secsSinceEpoch);
  242. ASSERT_EQ(msg.testField().nanos(), 0);
  243. QByteArray val = msg.serialize(serializer.get());
  244. msg.setTestField({0, 0, nullptr});
  245. ASSERT_EQ(msg.testField().seconds(), 0);
  246. ASSERT_EQ(msg.testField().nanos(), 0);
  247. msg.deserialize(serializer.get(), val);
  248. deserializedDateTime.setSecsSinceEpoch(msg.testField().seconds());
  249. ASSERT_TRUE(deserializedDateTime == originalDateTime);
  250. }
  251. }
  252. }