simpletest.cpp 38 KB

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