simpletest.cpp 32 KB

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