simpletest.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>, Viktor Kopp <vifactor@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 "stepchildenummessage.h"
  26. #include "simpleboolmessage.h"
  27. #include "simpleintmessage.h"
  28. #include "simplesintmessage.h"
  29. #include "simpleuintmessage.h"
  30. #include "simpleint64message.h"
  31. #include "simplesint64message.h"
  32. #include "simpleuint64message.h"
  33. #include "simplefixedint32message.h"
  34. #include "simplefixedint64message.h"
  35. #include "simplesfixedint32message.h"
  36. #include "simplesfixedint64message.h"
  37. #include "simplestringmessage.h"
  38. #include "simplefloatmessage.h"
  39. #include "simpledoublemessage.h"
  40. #include "simpleenummessage.h"
  41. #include "simpleenumlistmessage.h"
  42. #include "simplefileenummessage.h"
  43. #include "simpleexternalenummessage.h"
  44. #include "externalcomplexmessage.h"
  45. #include "complexmessage.h"
  46. #include "simplebytesmessage.h"
  47. #include "repeatedintmessage.h"
  48. #include "repeatedbytesmessage.h"
  49. #include "repeateddoublemessage.h"
  50. #include "repeatedcomplexmessage.h"
  51. #include "repeatedfloatmessage.h"
  52. #include "repeatedsintmessage.h"
  53. #include "repeatedstringmessage.h"
  54. #include "repeateduintmessage.h"
  55. #include "repeatedint64message.h"
  56. #include "repeatedsint64message.h"
  57. #include "repeateduint64message.h"
  58. #include "repeatedfixedintmessage.h"
  59. #include "repeatedsfixedintmessage.h"
  60. #include "repeatedfixedint64message.h"
  61. #include "repeatedsfixedint64message.h"
  62. #include "repeatedexternalcomplexmessage.h"
  63. #include "simplesint32stringmapmessage.h"
  64. #include "simplestringstringmapmessage.h"
  65. #include "emptymessage.h"
  66. #include "globalenums.h"
  67. #include <QVariantList>
  68. #include <QMetaProperty>
  69. #include <gtest/gtest.h>
  70. using namespace qtprotobufnamespace::tests;
  71. namespace QtProtobuf {
  72. namespace tests {
  73. class SimpleTest : public ::testing::Test
  74. {
  75. public:
  76. // see simpletest.proto for property names and their field indices
  77. SimpleTest()
  78. {
  79. }
  80. template<typename MessageType, typename PropertyType>
  81. static void assertMessagePropertyRegistered(int fieldIndex, const char *propertyTypeName, const char *propertyName)
  82. {
  83. // TODO: there should be(?) a mapping avaialble: PropertyType -> propertyTypeName
  84. const int propertyNumber = MessageType::propertyOrdering.at(fieldIndex);
  85. ASSERT_STREQ(MessageType::staticMetaObject.property(propertyNumber).typeName(), propertyTypeName);
  86. ASSERT_EQ(MessageType::staticMetaObject.property(propertyNumber).userType(), qMetaTypeId<PropertyType>());
  87. ASSERT_STREQ(MessageType::staticMetaObject.property(propertyNumber).name(), propertyName);
  88. }
  89. };
  90. TEST_F(SimpleTest, SimpleBoolMessageTest)
  91. {
  92. const char *propertyName = "testFieldBool";
  93. assertMessagePropertyRegistered<SimpleBoolMessage, bool>(1, "bool", propertyName);
  94. SimpleBoolMessage test;
  95. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue(true)));
  96. ASSERT_EQ(test.property(propertyName).value<bool>(), true);
  97. ASSERT_EQ(test.testFieldBool(), true);
  98. }
  99. TEST_F(SimpleTest, SimpleIntMessageTest)
  100. {
  101. const char *propertyName = "testFieldInt";
  102. assertMessagePropertyRegistered<SimpleIntMessage, int32>(1, "QtProtobuf::int32", propertyName);
  103. SimpleIntMessage test;
  104. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<int32>(1)));
  105. ASSERT_EQ(test.property(propertyName).value<int32>(), 1);
  106. ASSERT_EQ(test.testFieldInt(), 1);
  107. }
  108. TEST_F(SimpleTest, SimpleSIntMessageTest)
  109. {
  110. const char *propertyName = "testFieldInt";
  111. assertMessagePropertyRegistered<SimpleSIntMessage, sint32>(1, "QtProtobuf::sint32", propertyName);
  112. SimpleSIntMessage test;
  113. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<sint32>(1)));
  114. ASSERT_EQ(test.property(propertyName).value<sint32>(), 1);
  115. ASSERT_EQ(test.testFieldInt(), 1);
  116. }
  117. TEST_F(SimpleTest, SimpleUIntMessageTest)
  118. {
  119. const char *propertyName = "testFieldInt";
  120. assertMessagePropertyRegistered<SimpleUIntMessage, uint32>(1, "QtProtobuf::uint32", propertyName);
  121. SimpleUIntMessage test;
  122. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<uint32>(1)));
  123. ASSERT_EQ(test.property(propertyName).value<uint32>(), 1);
  124. ASSERT_EQ(test.testFieldInt(), 1);
  125. }
  126. TEST_F(SimpleTest, SimpleInt64MessageTest)
  127. {
  128. const char *propertyName = "testFieldInt";
  129. assertMessagePropertyRegistered<SimpleInt64Message, int64>(1, "QtProtobuf::int64", propertyName);
  130. SimpleInt64Message test;
  131. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<int64>(1)));
  132. ASSERT_EQ(test.property(propertyName).value<int64>(), 1);
  133. ASSERT_EQ(test.testFieldInt(), 1);
  134. }
  135. TEST_F(SimpleTest, SimpleSInt64MessageTest)
  136. {
  137. const char *propertyName = "testFieldInt";
  138. assertMessagePropertyRegistered<SimpleSInt64Message, sint64>(1, "QtProtobuf::sint64", propertyName);
  139. SimpleSInt64Message test;
  140. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<sint64>(1)));
  141. ASSERT_EQ(test.property(propertyName).value<sint64>(), 1);
  142. ASSERT_EQ(test.testFieldInt(), 1);
  143. }
  144. TEST_F(SimpleTest, SimpleUInt64MessageTest)
  145. {
  146. const char *propertyName = "testFieldInt";
  147. assertMessagePropertyRegistered<SimpleUInt64Message, uint64>(1, "QtProtobuf::uint64", propertyName);
  148. SimpleUInt64Message test;
  149. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<uint64>(1)));
  150. ASSERT_EQ(test.property(propertyName).value<uint64>(), 1);
  151. ASSERT_EQ(test.testFieldInt(), 1);
  152. }
  153. TEST_F(SimpleTest, SimpleFixedInt32MessageTest)
  154. {
  155. const char *propertyName = "testFieldFixedInt32";
  156. assertMessagePropertyRegistered<SimpleFixedInt32Message, fixed32>(1, "QtProtobuf::fixed32", propertyName);
  157. SimpleFixedInt32Message test;
  158. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<fixed32>(1)));
  159. ASSERT_EQ(test.property(propertyName).value<fixed32>(), 1);
  160. ASSERT_EQ(test.testFieldFixedInt32(), 1);
  161. }
  162. TEST_F(SimpleTest, SimpleFixedInt64MessageTest)
  163. {
  164. const char *propertyName = "testFieldFixedInt64";
  165. assertMessagePropertyRegistered<SimpleFixedInt64Message, fixed64>(1, "QtProtobuf::fixed64", propertyName);
  166. SimpleFixedInt64Message test;
  167. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<fixed64>(1)));
  168. ASSERT_EQ(test.property(propertyName).value<fixed64>(), 1);
  169. ASSERT_EQ(test.testFieldFixedInt64(), 1);
  170. }
  171. TEST_F(SimpleTest, SimpleSFixedInt32MessageTest)
  172. {
  173. const char *propertyName = "testFieldFixedInt32";
  174. assertMessagePropertyRegistered<SimpleSFixedInt32Message, sfixed32>(1, "QtProtobuf::sfixed32", propertyName);
  175. SimpleSFixedInt32Message test;
  176. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<sfixed32>(1)));
  177. ASSERT_EQ(test.property(propertyName).value<sfixed32>(), 1);
  178. ASSERT_EQ(test.testFieldFixedInt32(), 1);
  179. }
  180. TEST_F(SimpleTest, SimpleSFixedInt64MessageTest)
  181. {
  182. const char *propertyName = "testFieldFixedInt64";
  183. assertMessagePropertyRegistered<SimpleSFixedInt64Message, sfixed64>(1, "QtProtobuf::sfixed64", propertyName);
  184. SimpleSFixedInt64Message test;
  185. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<sfixed64>(1)));
  186. ASSERT_EQ(test.property(propertyName).value<sfixed64>(), 1);
  187. ASSERT_EQ(test.testFieldFixedInt64(), 1);
  188. }
  189. TEST_F(SimpleTest, SimpleStringMessageTest)
  190. {
  191. const char *propertyName = "testFieldString";
  192. SimpleStringMessage test;
  193. int propertyNumber = SimpleStringMessage::propertyOrdering.at(6); //See simpletest.proto
  194. ASSERT_EQ(SimpleStringMessage::staticMetaObject.property(propertyNumber).type(), QMetaType::QString);
  195. ASSERT_STREQ(SimpleStringMessage::staticMetaObject.property(propertyNumber).name(), propertyName);
  196. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue(QString("test1"))));
  197. ASSERT_STREQ(test.property(propertyName).toString().toStdString().c_str(), "test1");
  198. ASSERT_STREQ(test.testFieldString().toStdString().c_str(), "test1");
  199. }
  200. TEST_F(SimpleTest, SimpleFloatMessageTest)
  201. {
  202. const char *propertyName = "testFieldFloat";
  203. SimpleFloatMessage test;
  204. int propertyNumber = SimpleFloatMessage::propertyOrdering.at(7); //See simpletest.proto
  205. ASSERT_EQ(SimpleFloatMessage::staticMetaObject.property(propertyNumber).type(), QMetaType::Float);
  206. ASSERT_STREQ(SimpleFloatMessage::staticMetaObject.property(propertyNumber).name(), "testFieldFloat");
  207. float assignedValue = 1.55f;
  208. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<float>(assignedValue)));
  209. ASSERT_FLOAT_EQ(test.property(propertyName).toFloat(), assignedValue);
  210. ASSERT_FLOAT_EQ(test.testFieldFloat(), assignedValue);
  211. }
  212. TEST_F(SimpleTest, SimpleDoubleMessageTest)
  213. {
  214. const char *propertyName = "testFieldDouble";
  215. SimpleDoubleMessage test;
  216. int propertyNumber = SimpleDoubleMessage::propertyOrdering.at(8); //See simpletest.proto
  217. ASSERT_EQ(SimpleDoubleMessage::staticMetaObject.property(propertyNumber).type(), QMetaType::Double);
  218. ASSERT_STREQ(SimpleDoubleMessage::staticMetaObject.property(propertyNumber).name(), propertyName);
  219. double assignedValue = 0.55;
  220. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<double>(assignedValue)));
  221. ASSERT_DOUBLE_EQ(test.property(propertyName).toDouble(), assignedValue);
  222. ASSERT_DOUBLE_EQ(test.testFieldDouble(), assignedValue);
  223. }
  224. TEST_F(SimpleTest, SimpleLocalEnumTest)
  225. {
  226. ASSERT_GT(SimpleEnumMessage::staticMetaObject.enumeratorCount(), 0);
  227. QMetaEnum simpleEnum;
  228. for (int i = 0; i < SimpleEnumMessage::staticMetaObject.enumeratorCount(); i++) {
  229. QMetaEnum tmp = SimpleEnumMessage::staticMetaObject.enumerator(i);
  230. if (QString(tmp.name()) == QString("LocalEnum")) {
  231. simpleEnum = tmp;
  232. break;
  233. }
  234. }
  235. ASSERT_TRUE(simpleEnum.isValid());
  236. ASSERT_STREQ(simpleEnum.key(0), "LOCAL_ENUM_VALUE0");
  237. ASSERT_STREQ(simpleEnum.key(1), "LOCAL_ENUM_VALUE1");
  238. ASSERT_STREQ(simpleEnum.key(2), "LOCAL_ENUM_VALUE2");
  239. ASSERT_STREQ(simpleEnum.key(3), "LOCAL_ENUM_VALUE3");
  240. ASSERT_EQ(simpleEnum.value(0), 0);
  241. ASSERT_EQ(simpleEnum.value(1), 1);
  242. ASSERT_EQ(simpleEnum.value(2), 2);
  243. ASSERT_EQ(simpleEnum.value(3), 3);
  244. }
  245. TEST_F(SimpleTest, SimpleLocalEnumListTest)
  246. {
  247. ASSERT_GT(SimpleEnumListMessage::staticMetaObject.enumeratorCount(), 0);
  248. const char *propertyName = "localEnumList";
  249. assertMessagePropertyRegistered<SimpleEnumListMessage, SimpleEnumListMessage::LocalEnumRepeated>(1, "SimpleEnumListMessage::LocalEnumRepeated", propertyName);
  250. SimpleEnumListMessage::LocalEnumRepeated value({SimpleEnumListMessage::LOCAL_ENUM_VALUE2,
  251. SimpleEnumListMessage::LOCAL_ENUM_VALUE2,
  252. SimpleEnumListMessage::LOCAL_ENUM_VALUE1,
  253. SimpleEnumListMessage::LOCAL_ENUM_VALUE3});
  254. SimpleEnumListMessage test;
  255. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<SimpleEnumListMessage::LocalEnumRepeated>(value)));
  256. ASSERT_TRUE(test.property(propertyName).value<SimpleEnumListMessage::LocalEnumRepeated>() == value);
  257. ASSERT_TRUE(test.localEnumList() == value);
  258. }
  259. TEST_F(SimpleTest, SimpleExternalEnumMessageTest)
  260. {
  261. using ExternalGlobalEnums = qtprotobufnamespace1::externaltests::GlobalEnums;
  262. const char *propertyName = "externalEnum";
  263. assertMessagePropertyRegistered<SimpleExternalEnumMessage, ExternalGlobalEnums::ExternalTestEnum>(1, "qtprotobufnamespace1::externaltests::GlobalEnums::ExternalTestEnum", propertyName);
  264. SimpleExternalEnumMessage test;
  265. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<ExternalGlobalEnums::ExternalTestEnum>(ExternalGlobalEnums::EXTERNAL_TEST_ENUM_VALUE4)));
  266. ASSERT_TRUE(test.property(propertyName).value<ExternalGlobalEnums::ExternalTestEnum>() == QVariant::fromValue<ExternalGlobalEnums::ExternalTestEnum>(ExternalGlobalEnums::EXTERNAL_TEST_ENUM_VALUE4));
  267. ASSERT_TRUE(test.externalEnum() == QVariant::fromValue<ExternalGlobalEnums::ExternalTestEnum>(ExternalGlobalEnums::EXTERNAL_TEST_ENUM_VALUE4));
  268. }
  269. TEST_F(SimpleTest, SimpleEnumsTest)
  270. {
  271. EXPECT_GT(GlobalEnums::staticMetaObject.enumeratorCount(), 0);
  272. QMetaEnum testEnum;
  273. for (int i = 0; i < GlobalEnums::staticMetaObject.enumeratorCount(); i++) {
  274. QMetaEnum tmp = GlobalEnums::staticMetaObject.enumerator(i);
  275. if (QString(tmp.name()) == QString("TestEnum")) {
  276. testEnum = tmp;
  277. break;
  278. }
  279. }
  280. ASSERT_TRUE(testEnum.isValid());
  281. ASSERT_STREQ(testEnum.key(0), "TEST_ENUM_VALUE0");
  282. ASSERT_STREQ(testEnum.key(1), "TEST_ENUM_VALUE1");
  283. ASSERT_STREQ(testEnum.key(2), "TEST_ENUM_VALUE2");
  284. ASSERT_STREQ(testEnum.key(3), "TEST_ENUM_VALUE3");
  285. ASSERT_STREQ(testEnum.key(4), "TEST_ENUM_VALUE4");
  286. ASSERT_EQ(testEnum.value(0), 0);
  287. ASSERT_EQ(testEnum.value(1), 1);
  288. ASSERT_EQ(testEnum.value(2), 2);
  289. ASSERT_EQ(testEnum.value(3), 4);
  290. ASSERT_EQ(testEnum.value(4), 3);
  291. for (int i = 0; i < GlobalEnums::staticMetaObject.enumeratorCount(); i++) {
  292. QMetaEnum tmp = GlobalEnums::staticMetaObject.enumerator(i);
  293. if (QString(tmp.name()) == QString("TestEnumSecondInFile")) {
  294. testEnum = tmp;
  295. break;
  296. }
  297. }
  298. ASSERT_TRUE(testEnum.isValid());
  299. ASSERT_STREQ(testEnum.key(0), "TEST_ENUM_SIF_VALUE0");
  300. ASSERT_STREQ(testEnum.key(1), "TEST_ENUM_SIF_VALUE1");
  301. ASSERT_STREQ(testEnum.key(2), "TEST_ENUM_SIF_VALUE2");
  302. ASSERT_EQ(testEnum.value(0), 0);
  303. ASSERT_EQ(testEnum.value(1), 1);
  304. ASSERT_EQ(testEnum.value(2), 2);
  305. }
  306. TEST_F(SimpleTest, SimpleFileEnumsTest)
  307. {
  308. const char *propertyName = "globalEnumList";
  309. assertMessagePropertyRegistered<SimpleFileEnumMessage, GlobalEnums::TestEnumRepeated>(2, "qtprotobufnamespace::tests::GlobalEnums::TestEnumRepeated", propertyName);
  310. GlobalEnums::TestEnumRepeated value({GlobalEnums::TEST_ENUM_VALUE1,
  311. GlobalEnums::TEST_ENUM_VALUE3,
  312. GlobalEnums::TEST_ENUM_VALUE4,
  313. GlobalEnums::TEST_ENUM_VALUE2,
  314. GlobalEnums::TEST_ENUM_VALUE1});
  315. SimpleFileEnumMessage test;
  316. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<GlobalEnums::TestEnumRepeated>(value)));
  317. ASSERT_TRUE(test.property(propertyName).value<GlobalEnums::TestEnumRepeated>() == value);
  318. ASSERT_TRUE(test.globalEnumList() == value);
  319. }
  320. TEST_F(SimpleTest, ComplexMessageTest)
  321. {
  322. ComplexMessage msg;
  323. }
  324. TEST_F(SimpleTest, SimpleBytesMessageTest)
  325. {
  326. const char *propertyName = "testFieldBytes";
  327. SimpleBytesMessage test;
  328. int propertyNumber = SimpleBytesMessage::propertyOrdering.at(1); //See simpletest.proto
  329. ASSERT_EQ(SimpleBytesMessage::staticMetaObject.property(propertyNumber).type(), QMetaType::QByteArray);
  330. ASSERT_STREQ(SimpleBytesMessage::staticMetaObject.property(propertyNumber).name(), propertyName);
  331. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<QByteArray>("\x01\x02\x03\x04\x05")));
  332. ASSERT_TRUE(test.property(propertyName).toByteArray() == QByteArray("\x01\x02\x03\x04\x05"));
  333. ASSERT_TRUE(test.testFieldBytes() == QByteArray("\x01\x02\x03\x04\x05"));
  334. }
  335. TEST_F(SimpleTest, SimpleExternalComplexMessageTest)
  336. {
  337. const char *propertyName = "localList";
  338. assertMessagePropertyRegistered<qtprotobufnamespace1::externaltests::SimpleExternalMessage, int32List>(
  339. 1, "QtProtobuf::int32List", propertyName);
  340. qtprotobufnamespace1::externaltests::SimpleExternalMessage test;
  341. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<int32List>({1, 2, 3, 4, 5})));
  342. ASSERT_TRUE(test.property(propertyName).value<int32List>() == int32List({1, 2, 3, 4, 5}));
  343. ASSERT_TRUE(test.localList() == int32List({1, 2, 3, 4, 5}));
  344. }
  345. TEST_F(SimpleTest, RepeatedExternalComplexMessageTest)
  346. {
  347. const char *propertyName = "testExternalComplex";
  348. assertMessagePropertyRegistered<RepeatedExternalComplexMessage, qtprotobufnamespace1::externaltests::ExternalComplexMessageRepeated>(
  349. 1, "qtprotobufnamespace1::externaltests::ExternalComplexMessageRepeated", propertyName);
  350. qtprotobufnamespace1::externaltests::SimpleExternalMessage complexMessage;
  351. complexMessage.setLocalList({1, 2, 3, 4, 5});
  352. QSharedPointer<qtprotobufnamespace1::externaltests::ExternalComplexMessage> externalMessage(new qtprotobufnamespace1::externaltests::ExternalComplexMessage);
  353. externalMessage->setTestFieldInt(complexMessage);
  354. qtprotobufnamespace1::externaltests::ExternalComplexMessageRepeated complexMessageList;
  355. complexMessageList << externalMessage;
  356. RepeatedExternalComplexMessage test;
  357. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue(complexMessageList)));
  358. ASSERT_TRUE(test.property(propertyName).value<qtprotobufnamespace1::externaltests::ExternalComplexMessageRepeated>() == complexMessageList);
  359. ASSERT_TRUE(test.testExternalComplex() == complexMessageList);
  360. }
  361. TEST_F(SimpleTest, RepeatedStringMessageTest)
  362. {
  363. const char *propertyName = "testRepeatedString";
  364. assertMessagePropertyRegistered<RepeatedStringMessage, QStringList>(1, "QStringList", propertyName);
  365. RepeatedStringMessage test;
  366. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<QStringList>({"Text", "tryam"})));
  367. ASSERT_TRUE(test.property(propertyName).value<QStringList>() == QStringList({"Text", "tryam"}));
  368. ASSERT_TRUE(test.testRepeatedString() == QStringList({"Text", "tryam"}));
  369. }
  370. TEST_F(SimpleTest, RepeatedIntMessageTest)
  371. {
  372. const char *propertyName = "testRepeatedInt";
  373. assertMessagePropertyRegistered<RepeatedIntMessage, int32List>(1, "QtProtobuf::int32List", propertyName);
  374. RepeatedIntMessage test;
  375. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<int32List>({1, 2, 3, 4, 5})));
  376. ASSERT_TRUE(test.property(propertyName).value<int32List>() == int32List({1, 2, 3, 4, 5}));
  377. ASSERT_TRUE(test.testRepeatedInt() == int32List({1, 2, 3, 4, 5}));
  378. test.testRepeatedInt().append(66);
  379. ASSERT_TRUE(test.testRepeatedInt() == int32List({1, 2, 3, 4, 5, 66}));
  380. test.testRepeatedInt().pop_back();
  381. ASSERT_TRUE(test.testRepeatedInt() == int32List({1, 2, 3, 4, 5}));
  382. }
  383. TEST_F(SimpleTest, RepeatedDoubleMessageTest)
  384. {
  385. const char *propertyName = "testRepeatedDouble";
  386. assertMessagePropertyRegistered<RepeatedDoubleMessage, DoubleList>(1, "QtProtobuf::DoubleList", propertyName);
  387. RepeatedDoubleMessage test;
  388. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::DoubleList>({1.0, 2.3, 3, 4.7, 5.9})));
  389. ASSERT_TRUE(test.property(propertyName).value<QtProtobuf::DoubleList>() == QtProtobuf::DoubleList({1.0, 2.3, 3, 4.7, 5.9}));
  390. ASSERT_TRUE(test.testRepeatedDouble() == QtProtobuf::DoubleList({1.0, 2.3, 3, 4.7, 5.9}));
  391. test.testRepeatedDouble().append(6.6);
  392. ASSERT_TRUE(test.testRepeatedDouble() == QtProtobuf::DoubleList({1.0, 2.3, 3, 4.7, 5.9, 6.6}));
  393. test.testRepeatedDouble().pop_back();
  394. ASSERT_TRUE(test.testRepeatedDouble() == QtProtobuf::DoubleList({1.0, 2.3, 3, 4.7, 5.9}));
  395. }
  396. TEST_F(SimpleTest, RepeatedFloatMessageTest)
  397. {
  398. const char *propertyName = "testRepeatedFloat";
  399. assertMessagePropertyRegistered<RepeatedFloatMessage, FloatList>(1, "QtProtobuf::FloatList", propertyName);
  400. RepeatedFloatMessage test;
  401. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::FloatList>({1.0f, 2.3f, 3, 4.7f, 5.9f})));
  402. ASSERT_TRUE(test.property(propertyName).value<QtProtobuf::FloatList>() == QtProtobuf::FloatList({1.0f, 2.3f, 3, 4.7f, 5.9f}));
  403. ASSERT_TRUE(test.testRepeatedFloat() == QtProtobuf::FloatList({1.0f, 2.3f, 3, 4.7f, 5.9f}));
  404. test.testRepeatedFloat().append(6.6f);
  405. ASSERT_TRUE(test.testRepeatedFloat() == QtProtobuf::FloatList({1.0f, 2.3f, 3, 4.7f, 5.9f, 6.6f}));
  406. test.testRepeatedFloat().pop_back();
  407. ASSERT_TRUE(test.testRepeatedFloat() == QtProtobuf::FloatList({1.0f, 2.3f, 3, 4.7f, 5.9f}));
  408. }
  409. TEST_F(SimpleTest, RepeatedBytesMessageTest)
  410. {
  411. const char *propertyName = "testRepeatedBytes";
  412. assertMessagePropertyRegistered<RepeatedBytesMessage, QByteArrayList>(1, "QByteArrayList", propertyName);
  413. QByteArrayList bList;
  414. bList << "\x01\x02\x03\x04\x05";
  415. bList << "\x01\x05\x03\x04\x03";
  416. RepeatedBytesMessage test;
  417. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<QByteArrayList>(bList)));
  418. ASSERT_TRUE(test.property(propertyName).value<QByteArrayList>() == bList);
  419. ASSERT_TRUE(test.testRepeatedBytes() == bList);
  420. bList << "\x01\x05\x03\x03";
  421. test.testRepeatedBytes() << "\x01\x05\x03\x03";
  422. ASSERT_TRUE(test.testRepeatedBytes() == bList);
  423. bList.pop_back();
  424. test.testRepeatedBytes().pop_back();
  425. ASSERT_TRUE(test.testRepeatedBytes() == bList);
  426. }
  427. TEST_F(SimpleTest, RepeatedSIntMessageTest)
  428. {
  429. const char *propertyName = "testRepeatedInt";
  430. assertMessagePropertyRegistered<RepeatedSIntMessage, sint32List>(1, "QtProtobuf::sint32List", propertyName);
  431. RepeatedSIntMessage test;
  432. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<sint32List>({1, 2, 3, 4, 5})));
  433. ASSERT_TRUE(test.property(propertyName).value<sint32List>() == sint32List({1, 2, 3, 4, 5}));
  434. ASSERT_TRUE(test.testRepeatedInt() == sint32List({1, 2, 3, 4, 5}));
  435. test.testRepeatedInt() << 6;
  436. ASSERT_TRUE(test.testRepeatedInt() == sint32List({1, 2, 3, 4, 5, 6}));
  437. test.testRepeatedInt().pop_back();
  438. ASSERT_TRUE(test.testRepeatedInt() == sint32List({1, 2, 3, 4, 5}));
  439. }
  440. TEST_F(SimpleTest, RepeatedUIntMessageTest)
  441. {
  442. const char *propertyName = "testRepeatedInt";
  443. assertMessagePropertyRegistered<RepeatedUIntMessage, uint32List>(1, "QtProtobuf::uint32List", propertyName);
  444. RepeatedUIntMessage test;
  445. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<uint32List>({1, 2, 3, 4, 5})));
  446. ASSERT_TRUE(test.property(propertyName).value<uint32List>() == uint32List({1, 2, 3, 4, 5}));
  447. ASSERT_TRUE(test.testRepeatedInt() == uint32List({1, 2, 3, 4, 5}));
  448. test.testRepeatedInt().append(6);
  449. ASSERT_TRUE(test.testRepeatedInt() == uint32List({1, 2, 3, 4, 5,6}));
  450. test.testRepeatedInt().pop_back();
  451. ASSERT_TRUE(test.testRepeatedInt() == uint32List({1, 2, 3, 4, 5}));
  452. }
  453. TEST_F(SimpleTest, RepeatedInt64MessageTest)
  454. {
  455. const char *propertyName = "testRepeatedInt";
  456. assertMessagePropertyRegistered<RepeatedInt64Message, int64List>(1, "QtProtobuf::int64List", propertyName);
  457. RepeatedInt64Message test;
  458. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<int64List>({1, 2, 3, 4, 5})));
  459. ASSERT_TRUE(test.property(propertyName).value<int64List>() == int64List({1, 2, 3, 4, 5}));
  460. ASSERT_TRUE(test.testRepeatedInt() == int64List({1, 2, 3, 4, 5}));
  461. test.testRepeatedInt().append(69);
  462. ASSERT_TRUE(test.testRepeatedInt() == int64List({1, 2, 3, 4, 5, 69}));
  463. test.testRepeatedInt().pop_back();
  464. ASSERT_TRUE(test.testRepeatedInt() == int64List({1, 2, 3, 4, 5}));
  465. }
  466. TEST_F(SimpleTest, RepeatedSInt64MessageTest)
  467. {
  468. const char *propertyName = "testRepeatedInt";
  469. assertMessagePropertyRegistered<RepeatedSInt64Message, sint64List>(1, "QtProtobuf::sint64List", propertyName);
  470. RepeatedSInt64Message test;
  471. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<sint64List>({1, 2, 3, 4, 5})));
  472. ASSERT_TRUE(test.property(propertyName).value<sint64List>() == sint64List({1, 2, 3, 4, 5}));
  473. ASSERT_TRUE(test.testRepeatedInt() == sint64List({1, 2, 3, 4, 5}));
  474. test.testRepeatedInt() << 96;
  475. ASSERT_TRUE(test.testRepeatedInt() == sint64List({1, 2, 3, 4, 5, 96}));
  476. test.testRepeatedInt().pop_back();
  477. ASSERT_TRUE(test.testRepeatedInt() == sint64List({1, 2, 3, 4, 5}));
  478. }
  479. TEST_F(SimpleTest, RepeatedUInt64MessageTest)
  480. {
  481. const char *propertyName = "testRepeatedInt";
  482. assertMessagePropertyRegistered<RepeatedUInt64Message, uint64List>(1, "QtProtobuf::uint64List", propertyName);
  483. RepeatedUInt64Message test;
  484. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<uint64List>({1, 2, 3, 4, 5})));
  485. ASSERT_TRUE(test.property(propertyName).value<uint64List>() == uint64List({1, 2, 3, 4, 5}));
  486. ASSERT_TRUE(test.testRepeatedInt() == uint64List({1, 2, 3, 4, 5}));
  487. test.testRepeatedInt().append(96);
  488. ASSERT_TRUE(test.testRepeatedInt() == uint64List({1, 2, 3, 4, 5, 96}));
  489. test.testRepeatedInt().pop_back();
  490. ASSERT_TRUE(test.testRepeatedInt() == uint64List({1, 2, 3, 4, 5}));
  491. }
  492. TEST_F(SimpleTest, RepeatedFixedIntMessageTest)
  493. {
  494. const char *propertyName = "testRepeatedInt";
  495. assertMessagePropertyRegistered<RepeatedFixedIntMessage, fixed32List>(1, "QtProtobuf::fixed32List", propertyName);
  496. RepeatedFixedIntMessage test;
  497. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<fixed32List>({1, 2, 3, 4, 5})));
  498. ASSERT_TRUE(test.property(propertyName).value<fixed32List>() == fixed32List({1, 2, 3, 4, 5}));
  499. ASSERT_TRUE(test.testRepeatedInt() == fixed32List({1, 2, 3, 4, 5}));
  500. test.testRepeatedInt() << 0;
  501. ASSERT_TRUE(test.testRepeatedInt() == fixed32List({1, 2, 3, 4, 5, 0}));
  502. test.testRepeatedInt().pop_back();
  503. ASSERT_TRUE(test.testRepeatedInt() == fixed32List({1, 2, 3, 4, 5}));
  504. }
  505. TEST_F(SimpleTest, RepeatedFixedInt64MessageTest)
  506. {
  507. const char *propertyName = "testRepeatedInt";
  508. assertMessagePropertyRegistered<RepeatedFixedInt64Message, fixed64List>(1, "QtProtobuf::fixed64List", propertyName);
  509. RepeatedFixedInt64Message test;
  510. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<fixed64List>({1, 2, 3, 4, 5})));
  511. ASSERT_TRUE(test.property(propertyName).value<fixed64List>() == fixed64List({1, 2, 3, 4, 5}));
  512. ASSERT_TRUE(test.testRepeatedInt() == fixed64List({1, 2, 3, 4, 5}));
  513. test.testRepeatedInt() << 0;
  514. ASSERT_TRUE(test.testRepeatedInt() == fixed64List({1, 2, 3, 4, 5, 0}));
  515. test.testRepeatedInt().pop_back();
  516. ASSERT_TRUE(test.testRepeatedInt() == fixed64List({1, 2, 3, 4, 5}));
  517. }
  518. TEST_F(SimpleTest, RepeatedSFixedIntMessageTest)
  519. {
  520. const char *propertyName = "testRepeatedInt";
  521. assertMessagePropertyRegistered<RepeatedSFixedIntMessage, sfixed32List>(1, "QtProtobuf::sfixed32List", propertyName);
  522. RepeatedSFixedIntMessage test;
  523. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<sfixed32List>({1, 2, 3, 4, 5})));
  524. ASSERT_TRUE(test.property(propertyName).value<sfixed32List>() == sfixed32List({1, 2, 3, 4, 5}));
  525. ASSERT_TRUE(test.testRepeatedInt() == sfixed32List({1, 2, 3, 4, 5}));
  526. test.testRepeatedInt() << 0;
  527. ASSERT_TRUE(test.testRepeatedInt() == sfixed32List({1, 2, 3, 4, 5, 0}));
  528. test.testRepeatedInt().pop_back();
  529. ASSERT_TRUE(test.testRepeatedInt() == sfixed32List({1, 2, 3, 4, 5}));
  530. }
  531. TEST_F(SimpleTest, RepeatedSFixedInt64MessageTest)
  532. {
  533. const char *propertyName = "testRepeatedInt";
  534. assertMessagePropertyRegistered<RepeatedSFixedInt64Message, QtProtobuf::sfixed64List>(1, "QtProtobuf::sfixed64List", propertyName);
  535. RepeatedSFixedInt64Message test;
  536. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<sfixed64List>({1, 2, 3, 4, 5})));
  537. ASSERT_TRUE(test.property(propertyName).value<sfixed64List>() == sfixed64List({1, 2, 3, 4, 5}));
  538. ASSERT_TRUE(test.testRepeatedInt() == sfixed64List({1, 2, 3, 4, 5}));
  539. test.testRepeatedInt() << 0;
  540. ASSERT_TRUE(test.testRepeatedInt() == sfixed64List({1, 2, 3, 4, 5, 0}));
  541. test.testRepeatedInt().pop_back();
  542. ASSERT_TRUE(test.testRepeatedInt() == sfixed64List({1, 2, 3, 4, 5}));
  543. }
  544. TEST_F(SimpleTest, StepChildEnumMessageTest)
  545. {
  546. const char *propertyName = "localStepChildEnum";
  547. assertMessagePropertyRegistered<StepChildEnumMessage, SimpleEnumMessage::LocalEnum>(1, "qtprotobufnamespace::tests::SimpleEnumMessage::LocalEnum", propertyName);
  548. StepChildEnumMessage test;
  549. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<SimpleEnumMessage::LocalEnum>(SimpleEnumMessage::LocalEnum::LOCAL_ENUM_VALUE2)));
  550. ASSERT_TRUE(test.property(propertyName).value<SimpleEnumMessage::LocalEnum>() == SimpleEnumMessage::LocalEnum::LOCAL_ENUM_VALUE2);
  551. ASSERT_TRUE(test.localStepChildEnum() == SimpleEnumMessage::LocalEnum::LOCAL_ENUM_VALUE2);
  552. }
  553. TEST_F(SimpleTest, StepChildEnumListMessageTest)
  554. {
  555. const char *propertyName = "localStepChildList";
  556. assertMessagePropertyRegistered<StepChildEnumMessage, SimpleEnumMessage::LocalEnumRepeated>(2, "qtprotobufnamespace::tests::SimpleEnumMessage::LocalEnumRepeated", propertyName);
  557. SimpleEnumMessage::LocalEnumRepeated value({SimpleEnumMessage::LOCAL_ENUM_VALUE2,
  558. SimpleEnumMessage::LOCAL_ENUM_VALUE2,
  559. SimpleEnumMessage::LOCAL_ENUM_VALUE1,
  560. SimpleEnumMessage::LOCAL_ENUM_VALUE3});
  561. StepChildEnumMessage test;
  562. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<qtprotobufnamespace::tests::SimpleEnumMessage::LocalEnumRepeated>(value)));
  563. ASSERT_TRUE(test.property(propertyName).value<SimpleEnumMessage::LocalEnumRepeated>() == value);
  564. ASSERT_TRUE(test.localStepChildList() == value);
  565. }
  566. TEST_F(SimpleTest, SimpleSInt32StringMapMessageTest)
  567. {
  568. const char *propertyName = "mapField";
  569. assertMessagePropertyRegistered<SimpleSInt32StringMapMessage, SimpleSInt32StringMapMessage::MapFieldEntry>(1, "SimpleSInt32StringMapMessage::MapFieldEntry", propertyName);
  570. ASSERT_TRUE(QMetaType::isRegistered(qMetaTypeId<SimpleSInt32StringMapMessage::MapFieldEntry>()));
  571. SimpleSInt32StringMapMessage::MapFieldEntry testMap = {{10, {"Some 10"}}, {0, {"Some 0"}}, {44, {"Some 44"}}};
  572. SimpleSInt32StringMapMessage test;
  573. test.setMapField(testMap);
  574. ASSERT_TRUE(test.property(propertyName).value<SimpleSInt32StringMapMessage::MapFieldEntry>() == testMap);
  575. ASSERT_TRUE(test.mapField() == testMap);
  576. ASSERT_STREQ(test.mapField()[10].toStdString().c_str(), "Some 10");
  577. ASSERT_STREQ(test.mapField()[0].toStdString().c_str(), "Some 0");
  578. ASSERT_STREQ(test.mapField()[44].toStdString().c_str(), "Some 44");
  579. test.mapField()[66] = "Some 66";
  580. ASSERT_STREQ(test.mapField()[66].toStdString().c_str(), "Some 66");
  581. test.mapField()[66] = "Some default";
  582. ASSERT_STREQ(test.mapField()[66].toStdString().c_str(), "Some default");
  583. }
  584. TEST_F(SimpleTest, SimpleStringStringMapMessageTest)
  585. {
  586. const char *propertyName = "mapField";
  587. assertMessagePropertyRegistered<SimpleStringStringMapMessage, SimpleStringStringMapMessage::MapFieldEntry>(13, "SimpleStringStringMapMessage::MapFieldEntry", propertyName);
  588. ASSERT_TRUE(QMetaType::isRegistered(qMetaTypeId<SimpleStringStringMapMessage::MapFieldEntry>()));
  589. SimpleStringStringMapMessage::MapFieldEntry testMap = {{"key 10", "Some 10"}, {"key 0", "Some 0"}, {"key 44", "Some 44"}};
  590. SimpleStringStringMapMessage test;
  591. test.setMapField(testMap);
  592. ASSERT_TRUE(test.property(propertyName).value<SimpleStringStringMapMessage::MapFieldEntry>() == testMap);
  593. ASSERT_TRUE(test.mapField() == testMap);
  594. ASSERT_STREQ(test.mapField()["key 10"].toStdString().c_str(), "Some 10");
  595. ASSERT_STREQ(test.mapField()["key 0"].toStdString().c_str(), "Some 0");
  596. ASSERT_STREQ(test.mapField()["key 44"].toStdString().c_str(), "Some 44");
  597. test.mapField()["key 66"] = "Some 66";
  598. ASSERT_STREQ(test.mapField()["key 66"].toStdString().c_str(), "Some 66");
  599. test.mapField()["key 66"] = "Some default";
  600. ASSERT_STREQ(test.mapField()["key 66"].toStdString().c_str(), "Some default");
  601. }
  602. TEST_F(SimpleTest, EmptyMessageTest)
  603. {
  604. ASSERT_EQ(EmptyMessage::propertyOrdering.size(), 0);
  605. ASSERT_EQ(EmptyMessage::staticMetaObject.propertyCount(), 1);
  606. }
  607. TEST_F(SimpleTest, NullPointerMessageTest)
  608. {
  609. ComplexMessage msg(0, {QString("not null")});
  610. msg.setTestComplexField_p(nullptr);
  611. ASSERT_TRUE(msg.testComplexField().testFieldString().isEmpty());
  612. }
  613. } // tests
  614. } // qtprotobuf