simpletest.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 <gtest/gtest.h>
  29. #include <qtprotobufwellknowntypes_global.qbp.h>
  30. using namespace google::protobuf;
  31. namespace QtProtobuf {
  32. namespace tests {
  33. class WellknowntypesTest : public ::testing::Test
  34. {
  35. public:
  36. // see simpletest.proto for property names and their field indices
  37. WellknowntypesTest() {}
  38. template<typename MessageType, typename PropertyType>
  39. static void assertMessagePropertyRegistered(int fieldIndex, const char *propertyTypeName, const char *propertyName, bool skipMetatypeCheck = false)
  40. {
  41. // TODO: there should be(?) a mapping avaialble: PropertyType -> propertyTypeName
  42. const int propertyNumber = MessageType::propertyOrdering.at(fieldIndex);
  43. ASSERT_STREQ(MessageType::staticMetaObject.property(propertyNumber).typeName(), propertyTypeName);
  44. if(!skipMetatypeCheck) {
  45. ASSERT_EQ(MessageType::staticMetaObject.property(propertyNumber).userType(), qMetaTypeId<PropertyType>());
  46. }
  47. ASSERT_STREQ(MessageType::staticMetaObject.property(propertyNumber).name(), propertyName);
  48. }
  49. static void SetUpTestCase() {
  50. QtProtobuf::qRegisterProtobufTypes();
  51. google::protobuf::qRegisterProtobufTypes();
  52. }
  53. };
  54. TEST_F(WellknowntypesTest, AnyTest)
  55. {
  56. ASSERT_GT(qMetaTypeId<Any>(), 0);
  57. assertMessagePropertyRegistered<Any, QString>(1, "QString", "type_url");
  58. assertMessagePropertyRegistered<Any, QByteArray>(2, "QByteArray", "value");
  59. }
  60. TEST_F(WellknowntypesTest, ApiTest)
  61. {
  62. ASSERT_GT(qMetaTypeId<Api>(), 0);
  63. assertMessagePropertyRegistered<Api, QString>(1, "QString", "name");
  64. assertMessagePropertyRegistered<Api, MethodRepeated>(2, "MethodRepeated", "methods");
  65. assertMessagePropertyRegistered<Api, OptionRepeated>(3, "OptionRepeated", "options");
  66. assertMessagePropertyRegistered<Api, QString>(4, "QString", "version");
  67. assertMessagePropertyRegistered<Api, SourceContext>(5, "SourceContext*", "source_context", true);
  68. assertMessagePropertyRegistered<Api, MixinRepeated>(6, "MixinRepeated", "mixins");
  69. assertMessagePropertyRegistered<Api, SyntaxGadget::Syntax>(7, "google::protobuf::SyntaxGadget::Syntax", "syntax");
  70. }
  71. TEST_F(WellknowntypesTest, MethodTest)
  72. {
  73. ASSERT_GT(qMetaTypeId<Method>(), 0);
  74. assertMessagePropertyRegistered<Method, QString>(1, "QString", "name");
  75. assertMessagePropertyRegistered<Method, QString>(2, "QString", "request_type_url");
  76. assertMessagePropertyRegistered<Method, bool>(3, "bool", "request_streaming");
  77. assertMessagePropertyRegistered<Method, QString>(4, "QString", "response_type_url");
  78. assertMessagePropertyRegistered<Method, bool>(5, "bool", "response_streaming");
  79. assertMessagePropertyRegistered<Method, OptionRepeated>(6, "OptionRepeated", "options");
  80. assertMessagePropertyRegistered<Method, SyntaxGadget::Syntax>(7, "google::protobuf::SyntaxGadget::Syntax", "syntax");
  81. }
  82. TEST_F(WellknowntypesTest, MixinTest)
  83. {
  84. ASSERT_GT(qMetaTypeId<Mixin>(), 0);
  85. assertMessagePropertyRegistered<Mixin, QString>(1, "QString", "name");
  86. assertMessagePropertyRegistered<Mixin, QString>(2, "QString", "root");
  87. }
  88. TEST_F(WellknowntypesTest, DurationTest)
  89. {
  90. ASSERT_GT(qMetaTypeId<Duration>(), 0);
  91. assertMessagePropertyRegistered<Duration, QtProtobuf::int64>(1, "QtProtobuf::int64", "seconds");
  92. assertMessagePropertyRegistered<Duration, QtProtobuf::int32>(2, "QtProtobuf::int32", "nanos");
  93. }
  94. TEST_F(WellknowntypesTest, EmptyTest)
  95. {
  96. ASSERT_GT(qMetaTypeId<Empty>(), 0);
  97. }
  98. TEST_F(WellknowntypesTest, FieldMaskTest)
  99. {
  100. ASSERT_GT(qMetaTypeId<FieldMask>(), 0);
  101. assertMessagePropertyRegistered<FieldMask, QStringList>(1, "QStringList", "paths");
  102. }
  103. TEST_F(WellknowntypesTest, SourceContextTest)
  104. {
  105. ASSERT_GT(qMetaTypeId<SourceContext>(), 0);
  106. assertMessagePropertyRegistered<SourceContext, QString>(1, "QString", "file_name");
  107. }
  108. TEST_F(WellknowntypesTest, StructTest)
  109. {
  110. ASSERT_GT(qMetaTypeId<Struct>(), 0);
  111. assertMessagePropertyRegistered<Struct, Struct::FieldsEntry>(1, "Struct::FieldsEntry", "fields");
  112. }
  113. TEST_F(WellknowntypesTest, ValueTest)
  114. {
  115. ASSERT_GT(qMetaTypeId<Value>(), 0);
  116. assertMessagePropertyRegistered<Value, NullValueGadget::NullValue>(1, "google::protobuf::NullValueGadget::NullValue", "null_value");
  117. assertMessagePropertyRegistered<Value, double>(2, "double", "number_value");
  118. assertMessagePropertyRegistered<Value, QString>(3, "QString", "string_value");
  119. assertMessagePropertyRegistered<Value, bool>(4, "bool", "bool_value");
  120. assertMessagePropertyRegistered<Value, Struct *>(5, "Struct*", "struct_value");
  121. assertMessagePropertyRegistered<Value, ListValue *>(6, "ListValue*", "list_value");
  122. }
  123. TEST_F(WellknowntypesTest, ListValueTest)
  124. {
  125. ASSERT_GT(qMetaTypeId<ListValue>(), 0);
  126. assertMessagePropertyRegistered<ListValue, ValueRepeated>(1, "ValueRepeated", "values");
  127. }
  128. TEST_F(WellknowntypesTest, TimestampTest)
  129. {
  130. ASSERT_GT(qMetaTypeId<Timestamp>(), 0);
  131. assertMessagePropertyRegistered<Timestamp, QtProtobuf::int64>(1, "QtProtobuf::int64", "seconds");
  132. assertMessagePropertyRegistered<Timestamp, QtProtobuf::int32>(2, "QtProtobuf::int32", "nanos");
  133. }
  134. TEST_F(WellknowntypesTest, TypeTest)
  135. {
  136. ASSERT_GT(qMetaTypeId<Type>(), 0);
  137. Q_PROPERTY(QStringList oneofs READ oneofs WRITE setOneofs NOTIFY oneofsChanged)
  138. assertMessagePropertyRegistered<Type, QString>(1, "QString", "name");
  139. assertMessagePropertyRegistered<Type, FieldRepeated>(2, "FieldRepeated", "fields");
  140. assertMessagePropertyRegistered<Type, QStringList>(3, "QStringList", "oneofs");
  141. assertMessagePropertyRegistered<Type, OptionRepeated>(4, "OptionRepeated", "options");
  142. assertMessagePropertyRegistered<Type, SourceContext *>(5, "SourceContext*", "source_context", true);
  143. assertMessagePropertyRegistered<Type, SyntaxGadget::Syntax>(6, "google::protobuf::SyntaxGadget::Syntax", "syntax");
  144. }
  145. TEST_F(WellknowntypesTest, FieldTest)
  146. {
  147. ASSERT_GT(qMetaTypeId<Field>(), 0);
  148. assertMessagePropertyRegistered<Field, Field::Kind>(1, "Kind", "kind");
  149. assertMessagePropertyRegistered<Field, Field::Cardinality>(2, "Cardinality", "cardinality");
  150. assertMessagePropertyRegistered<Field, QtProtobuf::int32>(3, "QtProtobuf::int32", "number");
  151. assertMessagePropertyRegistered<Field, QString>(4, "QString", "name");
  152. assertMessagePropertyRegistered<Field, QString>(6, "QString", "type_url");
  153. assertMessagePropertyRegistered<Field, QtProtobuf::int32>(7, "QtProtobuf::int32", "oneof_index");
  154. assertMessagePropertyRegistered<Field, bool>(8, "bool", "packed");
  155. assertMessagePropertyRegistered<Field, OptionRepeated>(9, "OptionRepeated", "options");
  156. assertMessagePropertyRegistered<Field, QString>(10, "QString", "json_name");
  157. assertMessagePropertyRegistered<Field, QString>(11, "QString", "default_value");
  158. }
  159. TEST_F(WellknowntypesTest, EnumTest)
  160. {
  161. ASSERT_GT(qMetaTypeId<Enum>(), 0);
  162. assertMessagePropertyRegistered<Enum, QString>(1, "QString", "name");
  163. assertMessagePropertyRegistered<Enum, EnumValueRepeated>(2, "EnumValueRepeated", "enumvalue");
  164. assertMessagePropertyRegistered<Enum, OptionRepeated>(3, "OptionRepeated", "options");
  165. // assertMessagePropertyRegistered<Enum, SourceContext *>(4, "SourceContext*", "source_context");
  166. assertMessagePropertyRegistered<Enum, SyntaxGadget::Syntax>(5, "google::protobuf::SyntaxGadget::Syntax", "syntax");
  167. }
  168. TEST_F(WellknowntypesTest, EnumValueTest)
  169. {
  170. ASSERT_GT(qMetaTypeId<EnumValue>(), 0);
  171. assertMessagePropertyRegistered<EnumValue, QString>(1, "QString", "name");
  172. assertMessagePropertyRegistered<EnumValue, QtProtobuf::int32>(2, "QtProtobuf::int32", "number");
  173. assertMessagePropertyRegistered<EnumValue, OptionRepeated>(3, "OptionRepeated", "options");
  174. }
  175. TEST_F(WellknowntypesTest, OptionTest)
  176. {
  177. ASSERT_GT(qMetaTypeId<Option>(), 0);
  178. assertMessagePropertyRegistered<Option, QString>(1, "QString", "name");
  179. assertMessagePropertyRegistered<Option, Any *>(2, "Any*", "value", true);
  180. }
  181. TEST_F(WellknowntypesTest, DoubleValueTest)
  182. {
  183. ASSERT_GT(qMetaTypeId<DoubleValue>(), 0);
  184. assertMessagePropertyRegistered<DoubleValue, double>(1, "double", "value");
  185. }
  186. TEST_F(WellknowntypesTest, FloatValueTest)
  187. {
  188. ASSERT_GT(qMetaTypeId<FloatValue>(), 0);
  189. assertMessagePropertyRegistered<FloatValue, float>(1, "float", "value");
  190. }
  191. TEST_F(WellknowntypesTest, Int64ValueTest)
  192. {
  193. ASSERT_GT(qMetaTypeId<Int64Value>(), 0);
  194. assertMessagePropertyRegistered<Int64Value, QtProtobuf::int64>(1, "QtProtobuf::int64", "value");
  195. }
  196. TEST_F(WellknowntypesTest, UInt64ValueTest)
  197. {
  198. ASSERT_GT(qMetaTypeId<UInt64Value>(), 0);
  199. assertMessagePropertyRegistered<UInt64Value, QtProtobuf::uint64>(1, "QtProtobuf::uint64", "value");
  200. }
  201. TEST_F(WellknowntypesTest, Int32ValueTest)
  202. {
  203. ASSERT_GT(qMetaTypeId<Int32Value>(), 0);
  204. assertMessagePropertyRegistered<Int32Value, QtProtobuf::int32>(1, "QtProtobuf::int32", "value");
  205. }
  206. TEST_F(WellknowntypesTest, UInt32ValueTest)
  207. {
  208. ASSERT_GT(qMetaTypeId<UInt32Value>(), 0);
  209. assertMessagePropertyRegistered<UInt32Value, QtProtobuf::uint32>(1, "QtProtobuf::uint32", "value");
  210. }
  211. TEST_F(WellknowntypesTest, StringValueTest)
  212. {
  213. ASSERT_GT(qMetaTypeId<StringValue>(), 0);
  214. assertMessagePropertyRegistered<StringValue, QString>(1, "QString", "value");
  215. }
  216. TEST_F(WellknowntypesTest, BytesValueTest)
  217. {
  218. ASSERT_GT(qMetaTypeId<BytesValue>(), 0);
  219. assertMessagePropertyRegistered<BytesValue, QByteArray>(1, "QByteArray", "value");
  220. }
  221. }
  222. }