simpletest.cpp 32 KB

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