simpletest.cpp 34 KB

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