simpletest.cpp 38 KB

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