simpletest.cpp 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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. #ifndef QT_PROTOBUF_BASIC_MULTI_TEST
  26. #include "basicmessages.qpb.h"
  27. #include "repeatedmessages.qpb.h"
  28. #include "mapmessages.qpb.h"
  29. #include "simpletest.qpb.h"
  30. #include "sequencetest.qpb.h"
  31. #include "externalpackagetest.qpb.h"
  32. #include "globalenums.qpb.h"
  33. #include "globalenumssamenamespace.qpb.h"
  34. #include "nopackage.qpb.h"
  35. #endif
  36. #include <QVariantList>
  37. #include <QMetaProperty>
  38. #include <QSignalSpy>
  39. #include <gtest/gtest.h>
  40. #include <qtprotobuftestscommon.h>
  41. using namespace qtprotobufnamespace::tests;
  42. class SimpleTest : public ::testing::Test
  43. {
  44. public:
  45. // see simpletest.proto for property names and their field indices
  46. SimpleTest()
  47. {
  48. }
  49. static void SetUpTestCase();
  50. };
  51. void SimpleTest::SetUpTestCase()
  52. {
  53. qRegisterProtobufTypes();
  54. }
  55. TEST_F(SimpleTest, SimpleBoolMessageTest)
  56. {
  57. const char *propertyName = "testFieldBool";
  58. qProtobufAssertMessagePropertyRegistered<SimpleBoolMessage, bool>(1, "bool", propertyName);
  59. SimpleBoolMessage test;
  60. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue(true)));
  61. ASSERT_EQ(test.property(propertyName).value<bool>(), true);
  62. ASSERT_EQ(test.testFieldBool(), true);
  63. EXPECT_EQ(SimpleBoolMessage::TestFieldBoolProtoFieldNumber, 1);
  64. }
  65. TEST_F(SimpleTest, SimpleIntMessageTest)
  66. {
  67. const char *propertyName = "testFieldInt" QT_PROTOBUF_PROPERTY_SUFFIX;
  68. qProtobufAssertMessagePropertyRegistered<qtprotobufnamespace::tests::SimpleIntMessage, QtProtobuf::int32>(1, "QtProtobuf::int32", propertyName);
  69. qtprotobufnamespace::tests::SimpleIntMessage test;
  70. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::int32>(1)));
  71. ASSERT_EQ(test.property(propertyName).value<QtProtobuf::int32>(), 1);
  72. ASSERT_EQ(test.testFieldInt(), 1);
  73. EXPECT_EQ(qtprotobufnamespace::tests::SimpleIntMessage::TestFieldIntProtoFieldNumber, 1);
  74. }
  75. TEST_F(SimpleTest, SimpleSIntMessageTest)
  76. {
  77. const char *propertyName = "testFieldInt";
  78. qProtobufAssertMessagePropertyRegistered<SimpleSIntMessage, QtProtobuf::sint32>(1, "QtProtobuf::sint32", propertyName);
  79. SimpleSIntMessage test;
  80. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::sint32>(1)));
  81. ASSERT_EQ(test.property(propertyName).value<QtProtobuf::sint32>(), 1);
  82. ASSERT_EQ(test.testFieldInt(), 1);
  83. EXPECT_EQ(SimpleSIntMessage::TestFieldIntProtoFieldNumber, 1);
  84. }
  85. TEST_F(SimpleTest, SimpleUIntMessageTest)
  86. {
  87. const char *propertyName = "testFieldInt";
  88. qProtobufAssertMessagePropertyRegistered<SimpleUIntMessage, QtProtobuf::uint32>(1, "QtProtobuf::uint32", propertyName);
  89. SimpleUIntMessage test;
  90. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::uint32>(1)));
  91. ASSERT_EQ(test.property(propertyName).value<QtProtobuf::uint32>(), 1);
  92. ASSERT_EQ(test.testFieldInt(), 1);
  93. EXPECT_EQ(SimpleUIntMessage::TestFieldIntProtoFieldNumber, 1);
  94. }
  95. TEST_F(SimpleTest, SimpleInt64MessageTest)
  96. {
  97. const char *propertyName = "testFieldInt";
  98. qProtobufAssertMessagePropertyRegistered<SimpleInt64Message, QtProtobuf::int64>(1, "QtProtobuf::int64", propertyName);
  99. SimpleInt64Message test;
  100. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::int64>(1)));
  101. ASSERT_EQ(test.property(propertyName).value<QtProtobuf::int64>(), 1);
  102. ASSERT_EQ(test.testFieldInt(), 1);
  103. EXPECT_EQ(SimpleInt64Message::TestFieldIntProtoFieldNumber, 1);
  104. }
  105. TEST_F(SimpleTest, SimpleSInt64MessageTest)
  106. {
  107. const char *propertyName = "testFieldInt";
  108. qProtobufAssertMessagePropertyRegistered<SimpleSInt64Message, QtProtobuf::sint64>(1, "QtProtobuf::sint64", propertyName);
  109. SimpleSInt64Message test;
  110. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::sint64>(1)));
  111. ASSERT_EQ(test.property(propertyName).value<QtProtobuf::sint64>(), 1);
  112. ASSERT_EQ(test.testFieldInt(), 1);
  113. EXPECT_EQ(SimpleSInt64Message::TestFieldIntProtoFieldNumber, 1);
  114. }
  115. TEST_F(SimpleTest, SimpleUInt64MessageTest)
  116. {
  117. const char *propertyName = "testFieldInt";
  118. qProtobufAssertMessagePropertyRegistered<SimpleUInt64Message, QtProtobuf::uint64>(1, "QtProtobuf::uint64", propertyName);
  119. SimpleUInt64Message test;
  120. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::uint64>(1)));
  121. ASSERT_EQ(test.property(propertyName).value<QtProtobuf::uint64>(), 1);
  122. ASSERT_EQ(test.testFieldInt(), 1);
  123. EXPECT_EQ(SimpleUInt64Message::TestFieldIntProtoFieldNumber, 1);
  124. }
  125. TEST_F(SimpleTest, SimpleFixedInt32MessageTest)
  126. {
  127. const char *propertyName = "testFieldFixedInt32" QT_PROTOBUF_PROPERTY_SUFFIX;
  128. qProtobufAssertMessagePropertyRegistered<SimpleFixedInt32Message, QtProtobuf::fixed32>(1, "QtProtobuf::fixed32", propertyName);
  129. SimpleFixedInt32Message test;
  130. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::fixed32>(1)));
  131. ASSERT_EQ(test.property(propertyName).value<QtProtobuf::fixed32>(), 1);
  132. ASSERT_EQ(test.testFieldFixedInt32(), 1);
  133. EXPECT_EQ(SimpleFixedInt32Message::TestFieldFixedInt32ProtoFieldNumber, 1);
  134. }
  135. TEST_F(SimpleTest, SimpleFixedInt64MessageTest)
  136. {
  137. const char *propertyName = "testFieldFixedInt64";
  138. qProtobufAssertMessagePropertyRegistered<SimpleFixedInt64Message, QtProtobuf::fixed64>(1, "QtProtobuf::fixed64", propertyName);
  139. SimpleFixedInt64Message test;
  140. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::fixed64>(1)));
  141. ASSERT_EQ(test.property(propertyName).value<QtProtobuf::fixed64>(), 1);
  142. ASSERT_EQ(test.testFieldFixedInt64(), 1);
  143. EXPECT_EQ(SimpleFixedInt64Message::TestFieldFixedInt64ProtoFieldNumber, 1);
  144. }
  145. TEST_F(SimpleTest, SimpleSFixedInt32MessageTest)
  146. {
  147. const char *propertyName = "testFieldFixedInt32" QT_PROTOBUF_PROPERTY_SUFFIX;
  148. qProtobufAssertMessagePropertyRegistered<SimpleSFixedInt32Message, QtProtobuf::sfixed32>(1, "QtProtobuf::sfixed32", propertyName);
  149. SimpleSFixedInt32Message test;
  150. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::sfixed32>(1)));
  151. ASSERT_EQ(test.property(propertyName).value<QtProtobuf::sfixed32>(), 1);
  152. ASSERT_EQ(test.testFieldFixedInt32(), 1);
  153. EXPECT_EQ(SimpleSFixedInt32Message::TestFieldFixedInt32ProtoFieldNumber, 1);
  154. }
  155. TEST_F(SimpleTest, SimpleSFixedInt64MessageTest)
  156. {
  157. const char *propertyName = "testFieldFixedInt64";
  158. qProtobufAssertMessagePropertyRegistered<SimpleSFixedInt64Message, QtProtobuf::sfixed64>(1, "QtProtobuf::sfixed64", propertyName);
  159. SimpleSFixedInt64Message test;
  160. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::sfixed64>(1)));
  161. ASSERT_EQ(test.property(propertyName).value<QtProtobuf::sfixed64>(), 1);
  162. ASSERT_EQ(test.testFieldFixedInt64(), 1);
  163. EXPECT_EQ(SimpleSFixedInt64Message::TestFieldFixedInt64ProtoFieldNumber, 1);
  164. }
  165. TEST_F(SimpleTest, SimpleStringMessageTest)
  166. {
  167. const char *propertyName = "testFieldString";
  168. SimpleStringMessage test;
  169. int propertyNumber = SimpleStringMessage::propertyOrdering.at(6); //See simpletest.proto
  170. ASSERT_EQ(SimpleStringMessage::staticMetaObject.property(propertyNumber).typeId(), QMetaType::QString);
  171. ASSERT_STREQ(SimpleStringMessage::staticMetaObject.property(propertyNumber).name(), propertyName);
  172. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue(QString("test1"))));
  173. ASSERT_STREQ(test.property(propertyName).toString().toStdString().c_str(), "test1");
  174. ASSERT_STREQ(test.testFieldString().toStdString().c_str(), "test1");
  175. EXPECT_EQ(SimpleStringMessage::TestFieldStringProtoFieldNumber, 6);
  176. }
  177. TEST_F(SimpleTest, SimpleFloatMessageTest)
  178. {
  179. const char *propertyName = "testFieldFloat";
  180. SimpleFloatMessage test;
  181. int propertyNumber = SimpleFloatMessage::propertyOrdering.at(7); //See simpletest.proto
  182. ASSERT_EQ(SimpleFloatMessage::staticMetaObject.property(propertyNumber).typeId(), QMetaType::Float);
  183. ASSERT_STREQ(SimpleFloatMessage::staticMetaObject.property(propertyNumber).name(), "testFieldFloat");
  184. float assignedValue = 1.55f;
  185. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<float>(assignedValue)));
  186. ASSERT_FLOAT_EQ(test.property(propertyName).toFloat(), assignedValue);
  187. ASSERT_FLOAT_EQ(test.testFieldFloat(), assignedValue);
  188. EXPECT_EQ(SimpleFloatMessage::TestFieldFloatProtoFieldNumber, 7);
  189. }
  190. TEST_F(SimpleTest, SimpleDoubleMessageTest)
  191. {
  192. const char *propertyName = "testFieldDouble";
  193. SimpleDoubleMessage test;
  194. int propertyNumber = SimpleDoubleMessage::propertyOrdering.at(8); //See simpletest.proto
  195. ASSERT_EQ(SimpleDoubleMessage::staticMetaObject.property(propertyNumber).typeId(), QMetaType::Double);
  196. ASSERT_STREQ(SimpleDoubleMessage::staticMetaObject.property(propertyNumber).name(), propertyName);
  197. double assignedValue = 0.55;
  198. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<double>(assignedValue)));
  199. ASSERT_DOUBLE_EQ(test.property(propertyName).toDouble(), assignedValue);
  200. ASSERT_DOUBLE_EQ(test.testFieldDouble(), assignedValue);
  201. EXPECT_EQ(SimpleDoubleMessage::TestFieldDoubleProtoFieldNumber, 8);
  202. }
  203. TEST_F(SimpleTest, SimpleLocalEnumTest)
  204. {
  205. ASSERT_GT(SimpleEnumMessage::staticMetaObject.enumeratorCount(), 0);
  206. QMetaEnum simpleEnum;
  207. for (int i = 0; i < SimpleEnumMessage::staticMetaObject.enumeratorCount(); i++) {
  208. QMetaEnum tmp = SimpleEnumMessage::staticMetaObject.enumerator(i);
  209. if (QString(tmp.name()) == QString("LocalEnum")) {
  210. simpleEnum = tmp;
  211. break;
  212. }
  213. }
  214. ASSERT_TRUE(simpleEnum.isValid());
  215. ASSERT_STREQ(simpleEnum.key(0), "LOCAL_ENUM_VALUE0");
  216. ASSERT_STREQ(simpleEnum.key(1), "LOCAL_ENUM_VALUE1");
  217. ASSERT_STREQ(simpleEnum.key(2), "LOCAL_ENUM_VALUE2");
  218. ASSERT_STREQ(simpleEnum.key(3), "LOCAL_ENUM_VALUE3");
  219. ASSERT_EQ(simpleEnum.value(0), 0);
  220. ASSERT_EQ(simpleEnum.value(1), 1);
  221. ASSERT_EQ(simpleEnum.value(2), 2);
  222. ASSERT_EQ(simpleEnum.value(3), 3);
  223. }
  224. TEST_F(SimpleTest, SimpleLocalEnumListTest)
  225. {
  226. ASSERT_GT(SimpleEnumListMessage::staticMetaObject.enumeratorCount(), 0);
  227. const char *propertyName = "localEnumList";
  228. qProtobufAssertMessagePropertyRegistered<SimpleEnumListMessage, SimpleEnumListMessage::LocalEnumRepeated>(1, "qtprotobufnamespace::tests::SimpleEnumListMessage::LocalEnumRepeated", propertyName);
  229. SimpleEnumListMessage::LocalEnumRepeated value({SimpleEnumListMessage::LOCAL_ENUM_VALUE2,
  230. SimpleEnumListMessage::LOCAL_ENUM_VALUE2,
  231. SimpleEnumListMessage::LOCAL_ENUM_VALUE1,
  232. SimpleEnumListMessage::LOCAL_ENUM_VALUE3});
  233. SimpleEnumListMessage test;
  234. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<SimpleEnumListMessage::LocalEnumRepeated>(value)));
  235. ASSERT_TRUE(test.property(propertyName).value<SimpleEnumListMessage::LocalEnumRepeated>() == value);
  236. ASSERT_TRUE(test.localEnumList() == value);
  237. }
  238. TEST_F(SimpleTest, SimpleExternalEnumMessageTest)
  239. {
  240. using ExternalGlobalEnums = qtprotobufnamespace1::externaltests::ExternalTestEnumGadget;
  241. const char *propertyName = "externalEnum";
  242. qProtobufAssertMessagePropertyRegistered<SimpleExternalEnumMessage, ExternalGlobalEnums::ExternalTestEnum>(1, "qtprotobufnamespace1::externaltests::ExternalTestEnumGadget::ExternalTestEnum", propertyName);
  243. SimpleExternalEnumMessage test;
  244. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<ExternalGlobalEnums::ExternalTestEnum>(ExternalGlobalEnums::EXTERNAL_TEST_ENUM_VALUE4)));
  245. ASSERT_EQ(test.property(propertyName).value<ExternalGlobalEnums::ExternalTestEnum>(), ExternalGlobalEnums::EXTERNAL_TEST_ENUM_VALUE4);
  246. ASSERT_EQ(test.externalEnum(), ExternalGlobalEnums::EXTERNAL_TEST_ENUM_VALUE4);
  247. }
  248. TEST_F(SimpleTest, SimpleEnumsTest)
  249. {
  250. EXPECT_GT(qtprotobufnamespace::tests::TestEnumGadget::staticMetaObject.enumeratorCount(), 0);
  251. QMetaEnum testEnum;
  252. for (int i = 0; i < qtprotobufnamespace::tests::TestEnumGadget::staticMetaObject.enumeratorCount(); i++) {
  253. QMetaEnum tmp = qtprotobufnamespace::tests::TestEnumGadget::staticMetaObject.enumerator(i);
  254. if (QString(tmp.name()) == QString("TestEnum")) {
  255. testEnum = tmp;
  256. break;
  257. }
  258. }
  259. ASSERT_TRUE(testEnum.isValid());
  260. ASSERT_STREQ(testEnum.key(0), "TEST_ENUM_VALUE0");
  261. ASSERT_STREQ(testEnum.key(1), "TEST_ENUM_VALUE1");
  262. ASSERT_STREQ(testEnum.key(2), "TEST_ENUM_VALUE2");
  263. ASSERT_STREQ(testEnum.key(3), "TEST_ENUM_VALUE3");
  264. ASSERT_STREQ(testEnum.key(4), "TEST_ENUM_VALUE4");
  265. ASSERT_EQ(testEnum.value(0), 0);
  266. ASSERT_EQ(testEnum.value(1), 1);
  267. ASSERT_EQ(testEnum.value(2), 2);
  268. ASSERT_EQ(testEnum.value(3), 4);
  269. ASSERT_EQ(testEnum.value(4), 3);
  270. for (int i = 0; i < TestEnumSecondInFileGadget::staticMetaObject.enumeratorCount(); i++) {
  271. QMetaEnum tmp = TestEnumSecondInFileGadget::staticMetaObject.enumerator(i);
  272. if (QString(tmp.name()) == QString("TestEnumSecondInFile")) {
  273. testEnum = tmp;
  274. break;
  275. }
  276. }
  277. ASSERT_TRUE(testEnum.isValid());
  278. ASSERT_STREQ(testEnum.key(0), "TEST_ENUM_SIF_VALUE0");
  279. ASSERT_STREQ(testEnum.key(1), "TEST_ENUM_SIF_VALUE1");
  280. ASSERT_STREQ(testEnum.key(2), "TEST_ENUM_SIF_VALUE2");
  281. ASSERT_EQ(testEnum.value(0), 0);
  282. ASSERT_EQ(testEnum.value(1), 1);
  283. ASSERT_EQ(testEnum.value(2), 2);
  284. }
  285. TEST_F(SimpleTest, SimpleFileEnumsTest)
  286. {
  287. const char *propertyName = "globalEnumList";
  288. qProtobufAssertMessagePropertyRegistered<SimpleFileEnumMessage, qtprotobufnamespace::tests::TestEnumGadget::TestEnumRepeated>(2, "qtprotobufnamespace::tests::TestEnumGadget::TestEnumRepeated", propertyName);
  289. qtprotobufnamespace::tests::TestEnumGadget::TestEnumRepeated value{qtprotobufnamespace::tests::TestEnumGadget::TEST_ENUM_VALUE1,
  290. qtprotobufnamespace::tests::TestEnumGadget::TEST_ENUM_VALUE3,
  291. qtprotobufnamespace::tests::TestEnumGadget::TEST_ENUM_VALUE4,
  292. qtprotobufnamespace::tests::TestEnumGadget::TEST_ENUM_VALUE2,
  293. qtprotobufnamespace::tests::TestEnumGadget::TEST_ENUM_VALUE1};
  294. SimpleFileEnumMessage test;
  295. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<qtprotobufnamespace::tests::TestEnumGadget::TestEnumRepeated>(value)));
  296. ASSERT_TRUE(test.property(propertyName).value<qtprotobufnamespace::tests::TestEnumGadget::TestEnumRepeated>() == value);
  297. ASSERT_TRUE(test.globalEnumList() == value);
  298. }
  299. TEST_F(SimpleTest, ComplexMessageTest)
  300. {
  301. const char *propertyName = "testComplexField";
  302. qProtobufAssertMessagePropertyRegistered<qtprotobufnamespace::tests::ComplexMessage, qtprotobufnamespace::tests::SimpleStringMessage*>(
  303. 2, "qtprotobufnamespace::tests::SimpleStringMessage*", propertyName);
  304. ComplexMessage test;
  305. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<qtprotobufnamespace::tests::SimpleStringMessage*>(new qtprotobufnamespace::tests::SimpleStringMessage{"test qwerty"})));
  306. ASSERT_TRUE(*(test.property(propertyName).value<qtprotobufnamespace::tests::SimpleStringMessage*>()) == qtprotobufnamespace::tests::SimpleStringMessage{"test qwerty"});
  307. ASSERT_TRUE(test.testComplexField() == qtprotobufnamespace::tests::SimpleStringMessage{"test qwerty"});
  308. }
  309. TEST_F(SimpleTest, SimpleBytesMessageTest)
  310. {
  311. const char *propertyName = "testFieldBytes";
  312. SimpleBytesMessage test;
  313. int propertyNumber = SimpleBytesMessage::propertyOrdering.at(1); //See simpletest.proto
  314. ASSERT_EQ(SimpleBytesMessage::staticMetaObject.property(propertyNumber).typeId(), QMetaType::QByteArray);
  315. ASSERT_STREQ(SimpleBytesMessage::staticMetaObject.property(propertyNumber).name(), propertyName);
  316. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<QByteArray>("\x01\x02\x03\x04\x05")));
  317. ASSERT_TRUE(test.property(propertyName).toByteArray() == QByteArray("\x01\x02\x03\x04\x05"));
  318. ASSERT_TRUE(test.testFieldBytes() == QByteArray("\x01\x02\x03\x04\x05"));
  319. }
  320. TEST_F(SimpleTest, SimpleExternalComplexMessageTest)
  321. {
  322. const char *propertyName = "localList";
  323. qProtobufAssertMessagePropertyRegistered<qtprotobufnamespace1::externaltests::SimpleExternalMessage, QtProtobuf::int32List>(
  324. 1, "QtProtobuf::int32List", propertyName);
  325. qtprotobufnamespace1::externaltests::SimpleExternalMessage test;
  326. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::int32List>({1, 2, 3, 4, 5})));
  327. ASSERT_TRUE(test.property(propertyName).value<QtProtobuf::int32List>() == QtProtobuf::int32List({1, 2, 3, 4, 5}));
  328. ASSERT_TRUE(test.localList() == QtProtobuf::int32List({1, 2, 3, 4, 5}));
  329. }
  330. TEST_F(SimpleTest, RepeatedExternalComplexMessageTest)
  331. {
  332. const char *propertyName = "testExternalComplexData";
  333. qProtobufAssertMessagePropertyRegistered<RepeatedExternalComplexMessage, qtprotobufnamespace1::externaltests::ExternalComplexMessageRepeated>(
  334. 1, "qtprotobufnamespace1::externaltests::ExternalComplexMessageRepeated", propertyName);
  335. qtprotobufnamespace1::externaltests::SimpleExternalMessage complexMessage;
  336. complexMessage.setLocalList({1, 2, 3, 4, 5});
  337. QSharedPointer<qtprotobufnamespace1::externaltests::ExternalComplexMessage> externalMessage(new qtprotobufnamespace1::externaltests::ExternalComplexMessage);
  338. externalMessage->setTestFieldInt(complexMessage);
  339. qtprotobufnamespace1::externaltests::ExternalComplexMessageRepeated complexMessageList;
  340. complexMessageList << externalMessage;
  341. RepeatedExternalComplexMessage test;
  342. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue(complexMessageList)));
  343. ASSERT_TRUE(test.property(propertyName).value<qtprotobufnamespace1::externaltests::ExternalComplexMessageRepeated>() == complexMessageList);
  344. ASSERT_TRUE(test.testExternalComplex() == complexMessageList);
  345. }
  346. TEST_F(SimpleTest, RepeatedStringMessageTest)
  347. {
  348. const char *propertyName = "testRepeatedString";
  349. qProtobufAssertMessagePropertyRegistered<RepeatedStringMessage, QStringList>(1, "QStringList", propertyName);
  350. RepeatedStringMessage test;
  351. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<QStringList>({"Text", "tryam"})));
  352. ASSERT_TRUE(test.property(propertyName).value<QStringList>() == QStringList({"Text", "tryam"}));
  353. ASSERT_TRUE(test.testRepeatedString() == QStringList({"Text", "tryam"}));
  354. }
  355. TEST_F(SimpleTest, RepeatedIntMessageTest)
  356. {
  357. const char *propertyName = "testRepeatedInt";
  358. qProtobufAssertMessagePropertyRegistered<RepeatedIntMessage, QtProtobuf::int32List>(1, "QtProtobuf::int32List", propertyName);
  359. RepeatedIntMessage test;
  360. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::int32List>({1, 2, 3, 4, 5})));
  361. ASSERT_TRUE(test.property(propertyName).value<QtProtobuf::int32List>() == QtProtobuf::int32List({1, 2, 3, 4, 5}));
  362. ASSERT_TRUE(test.testRepeatedInt() == QtProtobuf::int32List({1, 2, 3, 4, 5}));
  363. test.testRepeatedInt().append(66);
  364. ASSERT_TRUE(test.testRepeatedInt() == QtProtobuf::int32List({1, 2, 3, 4, 5, 66}));
  365. test.testRepeatedInt().pop_back();
  366. ASSERT_TRUE(test.testRepeatedInt() == QtProtobuf::int32List({1, 2, 3, 4, 5}));
  367. }
  368. TEST_F(SimpleTest, RepeatedDoubleMessageTest)
  369. {
  370. const char *propertyName = "testRepeatedDouble";
  371. qProtobufAssertMessagePropertyRegistered<RepeatedDoubleMessage, QtProtobuf::DoubleList>(1, "QtProtobuf::DoubleList", propertyName);
  372. RepeatedDoubleMessage test;
  373. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::DoubleList>({1.0, 2.3, 3, 4.7, 5.9})));
  374. ASSERT_TRUE(test.property(propertyName).value<QtProtobuf::DoubleList>() == QtProtobuf::DoubleList({1.0, 2.3, 3, 4.7, 5.9}));
  375. ASSERT_TRUE(test.testRepeatedDouble() == QtProtobuf::DoubleList({1.0, 2.3, 3, 4.7, 5.9}));
  376. test.testRepeatedDouble().append(6.6);
  377. ASSERT_TRUE(test.testRepeatedDouble() == QtProtobuf::DoubleList({1.0, 2.3, 3, 4.7, 5.9, 6.6}));
  378. test.testRepeatedDouble().pop_back();
  379. ASSERT_TRUE(test.testRepeatedDouble() == QtProtobuf::DoubleList({1.0, 2.3, 3, 4.7, 5.9}));
  380. }
  381. TEST_F(SimpleTest, RepeatedFloatMessageTest)
  382. {
  383. const char *propertyName = "testRepeatedFloat";
  384. qProtobufAssertMessagePropertyRegistered<RepeatedFloatMessage, QtProtobuf::FloatList>(1, "QtProtobuf::FloatList", propertyName);
  385. RepeatedFloatMessage test;
  386. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::FloatList>({1.0f, 2.3f, 3, 4.7f, 5.9f})));
  387. ASSERT_TRUE(test.property(propertyName).value<QtProtobuf::FloatList>() == QtProtobuf::FloatList({1.0f, 2.3f, 3, 4.7f, 5.9f}));
  388. ASSERT_TRUE(test.testRepeatedFloat() == QtProtobuf::FloatList({1.0f, 2.3f, 3, 4.7f, 5.9f}));
  389. test.testRepeatedFloat().append(6.6f);
  390. ASSERT_TRUE(test.testRepeatedFloat() == QtProtobuf::FloatList({1.0f, 2.3f, 3, 4.7f, 5.9f, 6.6f}));
  391. test.testRepeatedFloat().pop_back();
  392. ASSERT_TRUE(test.testRepeatedFloat() == QtProtobuf::FloatList({1.0f, 2.3f, 3, 4.7f, 5.9f}));
  393. }
  394. TEST_F(SimpleTest, RepeatedBytesMessageTest)
  395. {
  396. const char *propertyName = "testRepeatedBytes";
  397. qProtobufAssertMessagePropertyRegistered<RepeatedBytesMessage, QByteArrayList>(1, "QByteArrayList", propertyName);
  398. QByteArrayList bList;
  399. bList << "\x01\x02\x03\x04\x05";
  400. bList << "\x01\x05\x03\x04\x03";
  401. RepeatedBytesMessage test;
  402. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<QByteArrayList>(bList)));
  403. ASSERT_TRUE(test.property(propertyName).value<QByteArrayList>() == bList);
  404. ASSERT_TRUE(test.testRepeatedBytes() == bList);
  405. bList << "\x01\x05\x03\x03";
  406. test.testRepeatedBytes() << "\x01\x05\x03\x03";
  407. ASSERT_TRUE(test.testRepeatedBytes() == bList);
  408. bList.pop_back();
  409. test.testRepeatedBytes().pop_back();
  410. ASSERT_TRUE(test.testRepeatedBytes() == bList);
  411. }
  412. TEST_F(SimpleTest, RepeatedSIntMessageTest)
  413. {
  414. const char *propertyName = "testRepeatedInt";
  415. qProtobufAssertMessagePropertyRegistered<RepeatedSIntMessage, QtProtobuf::sint32List>(1, "QtProtobuf::sint32List", propertyName);
  416. RepeatedSIntMessage test;
  417. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::sint32List>({1, 2, 3, 4, 5})));
  418. ASSERT_TRUE(test.property(propertyName).value<QtProtobuf::sint32List>() == QtProtobuf::sint32List({1, 2, 3, 4, 5}));
  419. ASSERT_TRUE(test.testRepeatedInt() == QtProtobuf::sint32List({1, 2, 3, 4, 5}));
  420. test.testRepeatedInt() << 6;
  421. ASSERT_TRUE(test.testRepeatedInt() == QtProtobuf::sint32List({1, 2, 3, 4, 5, 6}));
  422. test.testRepeatedInt().pop_back();
  423. ASSERT_TRUE(test.testRepeatedInt() == QtProtobuf::sint32List({1, 2, 3, 4, 5}));
  424. }
  425. TEST_F(SimpleTest, RepeatedUIntMessageTest)
  426. {
  427. const char *propertyName = "testRepeatedInt";
  428. qProtobufAssertMessagePropertyRegistered<RepeatedUIntMessage, QtProtobuf::uint32List>(1, "QtProtobuf::uint32List", propertyName);
  429. RepeatedUIntMessage test;
  430. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::uint32List>({1, 2, 3, 4, 5})));
  431. ASSERT_TRUE(test.property(propertyName).value<QtProtobuf::uint32List>() == QtProtobuf::uint32List({1, 2, 3, 4, 5}));
  432. ASSERT_TRUE(test.testRepeatedInt() == QtProtobuf::uint32List({1, 2, 3, 4, 5}));
  433. test.testRepeatedInt().append(6);
  434. ASSERT_TRUE(test.testRepeatedInt() == QtProtobuf::uint32List({1, 2, 3, 4, 5,6}));
  435. test.testRepeatedInt().pop_back();
  436. ASSERT_TRUE(test.testRepeatedInt() == QtProtobuf::uint32List({1, 2, 3, 4, 5}));
  437. }
  438. TEST_F(SimpleTest, RepeatedInt64MessageTest)
  439. {
  440. const char *propertyName = "testRepeatedInt";
  441. qProtobufAssertMessagePropertyRegistered<RepeatedInt64Message, QtProtobuf::int64List>(1, "QtProtobuf::int64List", propertyName);
  442. RepeatedInt64Message test;
  443. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::int64List>({1, 2, 3, 4, 5})));
  444. ASSERT_TRUE(test.property(propertyName).value<QtProtobuf::int64List>() == QtProtobuf::int64List({1, 2, 3, 4, 5}));
  445. ASSERT_TRUE(test.testRepeatedInt() == QtProtobuf::int64List({1, 2, 3, 4, 5}));
  446. test.testRepeatedInt().append(69);
  447. ASSERT_TRUE(test.testRepeatedInt() == QtProtobuf::int64List({1, 2, 3, 4, 5, 69}));
  448. test.testRepeatedInt().pop_back();
  449. ASSERT_TRUE(test.testRepeatedInt() == QtProtobuf::int64List({1, 2, 3, 4, 5}));
  450. }
  451. TEST_F(SimpleTest, RepeatedSInt64MessageTest)
  452. {
  453. const char *propertyName = "testRepeatedInt";
  454. qProtobufAssertMessagePropertyRegistered<RepeatedSInt64Message, QtProtobuf::sint64List>(1, "QtProtobuf::sint64List", propertyName);
  455. RepeatedSInt64Message test;
  456. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::sint64List>({1, 2, 3, 4, 5})));
  457. ASSERT_TRUE(test.property(propertyName).value<QtProtobuf::sint64List>() == QtProtobuf::sint64List({1, 2, 3, 4, 5}));
  458. ASSERT_TRUE(test.testRepeatedInt() == QtProtobuf::sint64List({1, 2, 3, 4, 5}));
  459. test.testRepeatedInt() << 96;
  460. ASSERT_TRUE(test.testRepeatedInt() == QtProtobuf::sint64List({1, 2, 3, 4, 5, 96}));
  461. test.testRepeatedInt().pop_back();
  462. ASSERT_TRUE(test.testRepeatedInt() == QtProtobuf::sint64List({1, 2, 3, 4, 5}));
  463. }
  464. TEST_F(SimpleTest, RepeatedUInt64MessageTest)
  465. {
  466. const char *propertyName = "testRepeatedInt";
  467. qProtobufAssertMessagePropertyRegistered<RepeatedUInt64Message, QtProtobuf::uint64List>(1, "QtProtobuf::uint64List", propertyName);
  468. RepeatedUInt64Message test;
  469. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::uint64List>({1, 2, 3, 4, 5})));
  470. ASSERT_TRUE(test.property(propertyName).value<QtProtobuf::uint64List>() == QtProtobuf::uint64List({1, 2, 3, 4, 5}));
  471. ASSERT_TRUE(test.testRepeatedInt() == QtProtobuf::uint64List({1, 2, 3, 4, 5}));
  472. test.testRepeatedInt().append(96);
  473. ASSERT_TRUE(test.testRepeatedInt() == QtProtobuf::uint64List({1, 2, 3, 4, 5, 96}));
  474. test.testRepeatedInt().pop_back();
  475. ASSERT_TRUE(test.testRepeatedInt() == QtProtobuf::uint64List({1, 2, 3, 4, 5}));
  476. }
  477. TEST_F(SimpleTest, RepeatedFixedIntMessageTest)
  478. {
  479. const char *propertyName = "testRepeatedInt";
  480. qProtobufAssertMessagePropertyRegistered<RepeatedFixedIntMessage, QtProtobuf::fixed32List>(1, "QtProtobuf::fixed32List", propertyName);
  481. RepeatedFixedIntMessage test;
  482. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::fixed32List>({1, 2, 3, 4, 5})));
  483. ASSERT_TRUE(test.property(propertyName).value<QtProtobuf::fixed32List>() == QtProtobuf::fixed32List({1, 2, 3, 4, 5}));
  484. ASSERT_TRUE(test.testRepeatedInt() == QtProtobuf::fixed32List({1, 2, 3, 4, 5}));
  485. test.testRepeatedInt() << 0;
  486. ASSERT_TRUE(test.testRepeatedInt() == QtProtobuf::fixed32List({1, 2, 3, 4, 5, 0}));
  487. test.testRepeatedInt().pop_back();
  488. ASSERT_TRUE(test.testRepeatedInt() == QtProtobuf::fixed32List({1, 2, 3, 4, 5}));
  489. }
  490. TEST_F(SimpleTest, RepeatedFixedInt64MessageTest)
  491. {
  492. const char *propertyName = "testRepeatedInt";
  493. qProtobufAssertMessagePropertyRegistered<RepeatedFixedInt64Message, QtProtobuf::fixed64List>(1, "QtProtobuf::fixed64List", propertyName);
  494. RepeatedFixedInt64Message test;
  495. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::fixed64List>({1, 2, 3, 4, 5})));
  496. ASSERT_TRUE(test.property(propertyName).value<QtProtobuf::fixed64List>() == QtProtobuf::fixed64List({1, 2, 3, 4, 5}));
  497. ASSERT_TRUE(test.testRepeatedInt() == QtProtobuf::fixed64List({1, 2, 3, 4, 5}));
  498. test.testRepeatedInt() << 0;
  499. ASSERT_TRUE(test.testRepeatedInt() == QtProtobuf::fixed64List({1, 2, 3, 4, 5, 0}));
  500. test.testRepeatedInt().pop_back();
  501. ASSERT_TRUE(test.testRepeatedInt() == QtProtobuf::fixed64List({1, 2, 3, 4, 5}));
  502. }
  503. TEST_F(SimpleTest, RepeatedSFixedIntMessageTest)
  504. {
  505. const char *propertyName = "testRepeatedInt";
  506. qProtobufAssertMessagePropertyRegistered<RepeatedSFixedIntMessage, QtProtobuf::sfixed32List>(1, "QtProtobuf::sfixed32List", propertyName);
  507. RepeatedSFixedIntMessage test;
  508. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::sfixed32List>({1, 2, 3, 4, 5})));
  509. ASSERT_TRUE(test.property(propertyName).value<QtProtobuf::sfixed32List>() == QtProtobuf::sfixed32List({1, 2, 3, 4, 5}));
  510. ASSERT_TRUE(test.testRepeatedInt() == QtProtobuf::sfixed32List({1, 2, 3, 4, 5}));
  511. test.testRepeatedInt() << 0;
  512. ASSERT_TRUE(test.testRepeatedInt() == QtProtobuf::sfixed32List({1, 2, 3, 4, 5, 0}));
  513. test.testRepeatedInt().pop_back();
  514. ASSERT_TRUE(test.testRepeatedInt() == QtProtobuf::sfixed32List({1, 2, 3, 4, 5}));
  515. }
  516. TEST_F(SimpleTest, RepeatedSFixedInt64MessageTest)
  517. {
  518. const char *propertyName = "testRepeatedInt";
  519. qProtobufAssertMessagePropertyRegistered<RepeatedSFixedInt64Message, QtProtobuf::sfixed64List>(1, "QtProtobuf::sfixed64List", propertyName);
  520. RepeatedSFixedInt64Message test;
  521. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::sfixed64List>({1, 2, 3, 4, 5})));
  522. ASSERT_TRUE(test.property(propertyName).value<QtProtobuf::sfixed64List>() == QtProtobuf::sfixed64List({1, 2, 3, 4, 5}));
  523. ASSERT_TRUE(test.testRepeatedInt() == QtProtobuf::sfixed64List({1, 2, 3, 4, 5}));
  524. test.testRepeatedInt() << 0;
  525. ASSERT_TRUE(test.testRepeatedInt() == QtProtobuf::sfixed64List({1, 2, 3, 4, 5, 0}));
  526. test.testRepeatedInt().pop_back();
  527. ASSERT_TRUE(test.testRepeatedInt() == QtProtobuf::sfixed64List({1, 2, 3, 4, 5}));
  528. }
  529. TEST_F(SimpleTest, StepChildEnumMessageTest)
  530. {
  531. const char *propertyName = "localStepChildEnum";
  532. qProtobufAssertMessagePropertyRegistered<StepChildEnumMessage, SimpleEnumMessage::LocalEnum>(1, "qtprotobufnamespace::tests::SimpleEnumMessage::LocalEnum", propertyName);
  533. StepChildEnumMessage test;
  534. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<SimpleEnumMessage::LocalEnum>(SimpleEnumMessage::LocalEnum::LOCAL_ENUM_VALUE2)));
  535. ASSERT_TRUE(test.property(propertyName).value<SimpleEnumMessage::LocalEnum>() == SimpleEnumMessage::LocalEnum::LOCAL_ENUM_VALUE2);
  536. ASSERT_TRUE(test.localStepChildEnum() == SimpleEnumMessage::LocalEnum::LOCAL_ENUM_VALUE2);
  537. }
  538. TEST_F(SimpleTest, StepChildEnumListMessageTest)
  539. {
  540. const char *propertyName = "localStepChildList";
  541. qProtobufAssertMessagePropertyRegistered<StepChildEnumMessage, SimpleEnumMessage::LocalEnumRepeated>(2, "qtprotobufnamespace::tests::SimpleEnumMessage::LocalEnumRepeated", propertyName);
  542. SimpleEnumMessage::LocalEnumRepeated value({SimpleEnumMessage::LOCAL_ENUM_VALUE2,
  543. SimpleEnumMessage::LOCAL_ENUM_VALUE2,
  544. SimpleEnumMessage::LOCAL_ENUM_VALUE1,
  545. SimpleEnumMessage::LOCAL_ENUM_VALUE3});
  546. StepChildEnumMessage test;
  547. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<qtprotobufnamespace::tests::SimpleEnumMessage::LocalEnumRepeated>(value)));
  548. ASSERT_TRUE(test.property(propertyName).value<SimpleEnumMessage::LocalEnumRepeated>() == value);
  549. ASSERT_TRUE(test.localStepChildList() == value);
  550. }
  551. TEST_F(SimpleTest, SimpleSInt32StringMapMessageTest)
  552. {
  553. const char *propertyName = "mapField";
  554. qProtobufAssertMessagePropertyRegistered<SimpleSInt32StringMapMessage, SimpleSInt32StringMapMessage::MapFieldEntry>(1, "qtprotobufnamespace::tests::SimpleSInt32StringMapMessage::MapFieldEntry", propertyName);
  555. ASSERT_TRUE(QMetaType::isRegistered(qMetaTypeId<SimpleSInt32StringMapMessage::MapFieldEntry>()));
  556. SimpleSInt32StringMapMessage::MapFieldEntry testMap = {{10, {"Some 10"}}, {0, {"Some 0"}}, {44, {"Some 44"}}};
  557. SimpleSInt32StringMapMessage test;
  558. test.setMapField(testMap);
  559. ASSERT_TRUE(test.property(propertyName).value<SimpleSInt32StringMapMessage::MapFieldEntry>() == testMap);
  560. ASSERT_TRUE(test.mapField() == testMap);
  561. ASSERT_STREQ(test.mapField()[10].toStdString().c_str(), "Some 10");
  562. ASSERT_STREQ(test.mapField()[0].toStdString().c_str(), "Some 0");
  563. ASSERT_STREQ(test.mapField()[44].toStdString().c_str(), "Some 44");
  564. test.mapField()[66] = "Some 66";
  565. ASSERT_STREQ(test.mapField()[66].toStdString().c_str(), "Some 66");
  566. test.mapField()[66] = "Some default";
  567. ASSERT_STREQ(test.mapField()[66].toStdString().c_str(), "Some default");
  568. }
  569. TEST_F(SimpleTest, SimpleStringStringMapMessageTest)
  570. {
  571. const char *propertyName = "mapField";
  572. qProtobufAssertMessagePropertyRegistered<SimpleStringStringMapMessage, SimpleStringStringMapMessage::MapFieldEntry>(13, "qtprotobufnamespace::tests::SimpleStringStringMapMessage::MapFieldEntry", propertyName);
  573. ASSERT_TRUE(QMetaType::isRegistered(qMetaTypeId<SimpleStringStringMapMessage::MapFieldEntry>()));
  574. SimpleStringStringMapMessage::MapFieldEntry testMap = {{"key 10", "Some 10"}, {"key 0", "Some 0"}, {"key 44", "Some 44"}};
  575. SimpleStringStringMapMessage test;
  576. test.setMapField(testMap);
  577. ASSERT_TRUE(test.property(propertyName).value<SimpleStringStringMapMessage::MapFieldEntry>() == testMap);
  578. ASSERT_TRUE(test.mapField() == testMap);
  579. ASSERT_STREQ(test.mapField()["key 10"].toStdString().c_str(), "Some 10");
  580. ASSERT_STREQ(test.mapField()["key 0"].toStdString().c_str(), "Some 0");
  581. ASSERT_STREQ(test.mapField()["key 44"].toStdString().c_str(), "Some 44");
  582. test.mapField()["key 66"] = "Some 66";
  583. ASSERT_STREQ(test.mapField()["key 66"].toStdString().c_str(), "Some 66");
  584. test.mapField()["key 66"] = "Some default";
  585. ASSERT_STREQ(test.mapField()["key 66"].toStdString().c_str(), "Some default");
  586. }
  587. TEST_F(SimpleTest, EmptyMessageTest)
  588. {
  589. ASSERT_EQ(qtprotobufnamespace::tests::EmptyMessage::propertyOrdering.size(), 0);
  590. ASSERT_EQ(qtprotobufnamespace::tests::EmptyMessage::staticMetaObject.propertyCount(), 1);
  591. }
  592. TEST_F(SimpleTest, AssignmentOperatorTest)
  593. {
  594. const char *propertyName = "testFieldInt";
  595. qtprotobufnamespace::tests::SimpleIntMessage test;
  596. qtprotobufnamespace::tests::SimpleIntMessage test2{35};
  597. QSignalSpy updateSpy(&test, &qtprotobufnamespace::tests::SimpleIntMessage::testFieldIntChanged);
  598. test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::int32>(15));
  599. test.setTestFieldInt(25);
  600. test = test2;
  601. test = test;
  602. test = test2;
  603. ASSERT_EQ(test2.testFieldInt(), test.testFieldInt());
  604. ASSERT_EQ(3, updateSpy.count());
  605. }
  606. TEST_F(SimpleTest, MoveOperatorTest)
  607. {
  608. const char *propertyName = "testFieldInt";
  609. qtprotobufnamespace::tests::SimpleIntMessage test;
  610. qtprotobufnamespace::tests::SimpleIntMessage test2{35};
  611. QSignalSpy updateSpy(&test, &qtprotobufnamespace::tests::SimpleIntMessage::testFieldIntChanged);
  612. QSignalSpy movedUpdateSpy(&test2, &qtprotobufnamespace::tests::SimpleIntMessage::testFieldIntChanged);
  613. qtprotobufnamespace::tests::SimpleIntMessage test3(std::move(test2));
  614. test2.setTestFieldInt(35);
  615. test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::int32>(15));
  616. test.setTestFieldInt(25);
  617. test = std::move(test2);
  618. ASSERT_EQ(35, test.testFieldInt());
  619. ASSERT_EQ(0, test2.testFieldInt());
  620. ASSERT_EQ(3, updateSpy.count());
  621. ASSERT_EQ(3, movedUpdateSpy.count());
  622. }
  623. TEST_F(SimpleTest, MoveOperatorRepeatedTest)
  624. {
  625. const char *propertyName = "testRepeatedInt";
  626. RepeatedIntMessage test;
  627. RepeatedIntMessage test2{{55,44,11,33}};
  628. QSignalSpy updateSpy(&test, &RepeatedIntMessage::testRepeatedIntChanged);
  629. QSignalSpy movedUpdateSpy(&test2, &RepeatedIntMessage::testRepeatedIntChanged);
  630. RepeatedIntMessage test3(std::move(test2));
  631. test2.setTestRepeatedInt({55,44,11,35});
  632. test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::int32List>({55}));
  633. test.setTestRepeatedInt({44});
  634. test = std::move(test2);
  635. ASSERT_EQ(QtProtobuf::int32List({55,44,11,35}), test.testRepeatedInt());
  636. ASSERT_TRUE(test2.testRepeatedInt().isEmpty());
  637. ASSERT_EQ(3, updateSpy.count());
  638. ASSERT_EQ(3, movedUpdateSpy.count());
  639. }
  640. TEST_F(SimpleTest, UnderscoresTest)
  641. {
  642. //Sanity compilation checks
  643. Message_Uderscore_name msg1;
  644. MessageUderscorename msg2;
  645. MessageUnderscoreField msg3;
  646. PriorMessageUnderscoreField msg4;
  647. FollowingMessageUnderscoreField msg5;
  648. CombinedMessageUnderscoreField msg6;
  649. qProtobufAssertMessagePropertyRegistered<MessageUnderscoreField, QtProtobuf::sint32>(1, "QtProtobuf::sint32", "underScoreMessageField");
  650. qProtobufAssertMessagePropertyRegistered<PriorMessageUnderscoreField, QtProtobuf::sint32>(1, "QtProtobuf::sint32", "underScoreMessageField");
  651. qProtobufAssertMessagePropertyRegistered<PriorMessageUnderscoreField, QtProtobuf::sint32>(1, "QtProtobuf::sint32", "underScoreMessageField");
  652. qProtobufAssertMessagePropertyRegistered<FollowingMessageUnderscoreField , QtProtobuf::sint32>(1, "QtProtobuf::sint32", "underScoreMessageField");
  653. qProtobufAssertMessagePropertyRegistered<CombinedMessageUnderscoreField , QtProtobuf::sint32>(1, "QtProtobuf::sint32", "underScoreMessageField");
  654. }
  655. TEST_F(SimpleTest, SequenceTest)
  656. {
  657. qProtobufAssertMessagePropertyRegistered<sequence::TestMessageSequence, sequence::TestMessageSequence2*>(1, "qtprotobufnamespace::tests::sequence::TestMessageSequence2*", "testField");
  658. qProtobufAssertMessagePropertyRegistered<sequence::TestMessageSequence2, bool>(1, "bool", "testField");
  659. }
  660. TEST_F(SimpleTest, CyclingTest)
  661. {
  662. qProtobufAssertMessagePropertyRegistered<sequence::CyclingFirstDependency, sequence::CyclingSecondDependency*>(1, "qtprotobufnamespace::tests::sequence::CyclingSecondDependency*", "testField");
  663. qProtobufAssertMessagePropertyRegistered<sequence::CyclingSecondDependency, sequence::CyclingFirstDependency*>(1, "qtprotobufnamespace::tests::sequence::CyclingFirstDependency*", "testField");
  664. sequence::CyclingFirstDependency test;
  665. sequence::CyclingSecondDependency test2;
  666. test.setTestField(test2);
  667. test2.setTestField(test);
  668. }
  669. TEST_F(SimpleTest, UpperCaseTest)
  670. {
  671. qProtobufAssertMessagePropertyRegistered<MessageUpperCase, QtProtobuf::sint32>(1, "QtProtobuf::sint32", "testField");
  672. }
  673. TEST_F(SimpleTest, ReservedTest)
  674. {
  675. qProtobufAssertMessagePropertyRegistered<MessageReserved, QtProtobuf::sint32>(1, "QtProtobuf::sint32", "import_proto");
  676. qProtobufAssertMessagePropertyRegistered<MessageReserved, QtProtobuf::sint32>(2, "QtProtobuf::sint32", "property_proto");
  677. qProtobufAssertMessagePropertyRegistered<MessageReserved, QtProtobuf::sint32>(3, "QtProtobuf::sint32", "id_proto");
  678. }
  679. TEST_F(SimpleTest, ReservedUpperCaseTest)
  680. {
  681. qProtobufAssertMessagePropertyRegistered<MessageUpperCaseReserved, QtProtobuf::sint32>(1, "QtProtobuf::sint32", "import_proto");
  682. qProtobufAssertMessagePropertyRegistered<MessageUpperCaseReserved, QtProtobuf::sint32>(2, "QtProtobuf::sint32", "property_proto");
  683. qProtobufAssertMessagePropertyRegistered<MessageUpperCaseReserved, QtProtobuf::sint32>(3, "QtProtobuf::sint32", "id_proto");
  684. }
  685. TEST_F(SimpleTest, ReservedEnumTest)
  686. {
  687. ASSERT_GT(MessageEnumReserved::staticMetaObject.enumeratorCount(), 0);
  688. QMetaEnum simpleEnum;
  689. for (int i = 0; i < MessageEnumReserved::staticMetaObject.enumeratorCount(); i++) {
  690. QMetaEnum tmp = MessageEnumReserved::staticMetaObject.enumerator(i);
  691. if (QString(tmp.name()) == QString("ReservedEnum")) {
  692. simpleEnum = tmp;
  693. break;
  694. }
  695. }
  696. ASSERT_TRUE(simpleEnum.isValid());
  697. ASSERT_STREQ(simpleEnum.key(0), "Import");
  698. ASSERT_STREQ(simpleEnum.key(1), "Property");
  699. ASSERT_STREQ(simpleEnum.key(2), "Id");
  700. ASSERT_EQ(simpleEnum.value(0), 0);
  701. ASSERT_EQ(simpleEnum.value(1), 1);
  702. ASSERT_EQ(simpleEnum.value(2), 2);
  703. }
  704. TEST_F(SimpleTest, LowerCaseEnumTest)
  705. {
  706. ASSERT_GT(MessageEnumReserved::staticMetaObject.enumeratorCount(), 0);
  707. QMetaEnum simpleEnum;
  708. for (int i = 0; i < MessageEnumReserved::staticMetaObject.enumeratorCount(); i++) {
  709. QMetaEnum tmp = MessageEnumReserved::staticMetaObject.enumerator(i);
  710. if (QString(tmp.name()) == QString("LowerCaseEnum")) {
  711. simpleEnum = tmp;
  712. break;
  713. }
  714. }
  715. ASSERT_TRUE(simpleEnum.isValid());
  716. ASSERT_STREQ(simpleEnum.key(0), "EnumValue0");
  717. ASSERT_STREQ(simpleEnum.key(1), "EnumValue1");
  718. ASSERT_STREQ(simpleEnum.key(2), "EnumValue2");
  719. }
  720. TEST_F(SimpleTest, MapRepeatedFieldSequenceTest)
  721. {
  722. qProtobufAssertMessagePropertyRegistered<sequence::RepeatedFieldSequence, sequence::RepeatedFieldSequence2Repeated>(1, "qtprotobufnamespace::tests::sequence::RepeatedFieldSequence2Repeated", "testFieldData");
  723. qProtobufAssertMessagePropertyRegistered<sequence::MapFieldSequence, sequence::MapFieldSequence::TestFieldEntry>(1, "qtprotobufnamespace::tests::sequence::MapFieldSequence::TestFieldEntry", "testField");
  724. }
  725. TEST_F(SimpleTest, NoPackageEmptyMessageMessageTest)
  726. {
  727. ASSERT_EQ(::EmptyMessage::propertyOrdering.size(), 0);
  728. ASSERT_EQ(::EmptyMessage::staticMetaObject.propertyCount(), 1);
  729. }
  730. TEST_F(SimpleTest, NoPackageSimpleIntMessageTest)
  731. {
  732. const char *propertyName = "testFieldInt" QT_PROTOBUF_PROPERTY_SUFFIX;
  733. qProtobufAssertMessagePropertyRegistered<::SimpleIntMessage, QtProtobuf::int32>(1, "QtProtobuf::int32", propertyName);
  734. ::SimpleIntMessage test;
  735. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<QtProtobuf::int32>(1)));
  736. ASSERT_EQ(test.property(propertyName).value<QtProtobuf::int32>(), 1);
  737. ASSERT_EQ(test.testFieldInt(), 1);
  738. EXPECT_EQ(::SimpleIntMessage::TestFieldIntProtoFieldNumber, 1);
  739. }
  740. TEST_F(SimpleTest, NoPackageEnumTest)
  741. {
  742. EXPECT_GT(::TestEnumGadget::staticMetaObject.enumeratorCount(), 0);
  743. QMetaEnum testEnum;
  744. for (int i = 0; i < ::TestEnumGadget::staticMetaObject.enumeratorCount(); i++) {
  745. QMetaEnum tmp = ::TestEnumGadget::staticMetaObject.enumerator(i);
  746. if (QString(tmp.name()) == QString("TestEnum")) {
  747. testEnum = tmp;
  748. break;
  749. }
  750. }
  751. ASSERT_EQ(testEnum.keyCount(), 4);
  752. EXPECT_TRUE(testEnum.isValid());
  753. EXPECT_STREQ(testEnum.key(0), "LOCAL_ENUM_VALUE0");
  754. EXPECT_STREQ(testEnum.key(1), "LOCAL_ENUM_VALUE1");
  755. EXPECT_STREQ(testEnum.key(2), "LOCAL_ENUM_VALUE2");
  756. EXPECT_STREQ(testEnum.key(3), "LOCAL_ENUM_VALUE3");
  757. EXPECT_EQ(testEnum.value(0), 0);
  758. EXPECT_EQ(testEnum.value(1), 1);
  759. EXPECT_EQ(testEnum.value(2), 2);
  760. EXPECT_EQ(testEnum.value(3), 5);
  761. }
  762. TEST_F(SimpleTest, NoPackageExternalTest)
  763. {
  764. const char *propertyName = "testField";
  765. qProtobufAssertMessagePropertyRegistered<NoPackageExternalMessage, SimpleIntMessageExt*>(1, "SimpleIntMessageExt*", propertyName);
  766. NoPackageExternalMessage test;
  767. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<SimpleIntMessageExt*>(new SimpleIntMessageExt{42})));
  768. ASSERT_EQ(test.property(propertyName).value<SimpleIntMessageExt*>()->testFieldInt(), 42);
  769. ASSERT_EQ(test.testField().testFieldInt(), 42);
  770. }
  771. TEST_F(SimpleTest, NoPackageMessageTest)
  772. {
  773. const char *propertyName = "testField";
  774. qProtobufAssertMessagePropertyRegistered<NoPackageMessage, SimpleIntMessageExt*>(1, "SimpleIntMessageExt*", propertyName);
  775. NoPackageMessage test;
  776. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<SimpleIntMessageExt*>(new SimpleIntMessageExt{42})));
  777. ASSERT_EQ(test.property(propertyName).value<SimpleIntMessageExt*>()->testFieldInt(), 42);
  778. ASSERT_EQ(test.testField().testFieldInt(), 42);
  779. }
  780. TEST_F(SimpleTest, RepeatedComplexMessageCompareTest)
  781. {
  782. QSharedPointer<ComplexMessage> msg1(new ComplexMessage(10, {"qwerty"}));
  783. QSharedPointer<ComplexMessage> msg2(new ComplexMessage(20, {"ytrewq"}));
  784. QSharedPointer<ComplexMessage> msg3(new ComplexMessage(10, {"qwerty"}));
  785. QSharedPointer<ComplexMessage> msg4(new ComplexMessage(20, {"ytrewq"}));
  786. ASSERT_TRUE(*msg1 == *msg3);
  787. ASSERT_TRUE(*msg2 == *msg4);
  788. RepeatedComplexMessage test1 = RepeatedComplexMessage({msg1, msg2});
  789. RepeatedComplexMessage test2 = RepeatedComplexMessage({msg3, msg4});
  790. RepeatedComplexMessage test3 = RepeatedComplexMessage({msg4, msg3});
  791. ASSERT_TRUE(test1 == test2);
  792. ASSERT_FALSE(test3 == test2);
  793. }
  794. TEST_F(SimpleTest, SimpleInt32ComplexMessageMapMessageCompareTest)
  795. {
  796. QSharedPointer<ComplexMessage> msg1(new ComplexMessage(10, {"qwerty"}));
  797. QSharedPointer<ComplexMessage> msg2(new ComplexMessage(20, {"ytrewq"}));
  798. QSharedPointer<ComplexMessage> msg3(new ComplexMessage(10, {"qwerty"}));
  799. QSharedPointer<ComplexMessage> msg4(new ComplexMessage(20, {"ytrewq"}));
  800. SimpleInt32ComplexMessageMapMessage test1 = SimpleInt32ComplexMessageMapMessage({{20, msg1}, {30, msg2}});
  801. SimpleInt32ComplexMessageMapMessage test2 = SimpleInt32ComplexMessageMapMessage({{20, msg3}, {30, msg4}});
  802. ASSERT_TRUE(test1 == test2);
  803. }
  804. TEST_F(SimpleTest, AccessMessageFieldsFromGetter)
  805. {
  806. ComplexMessage msg1;
  807. msg1.testComplexField().setTestFieldString("AccessMessageFieldsFromGetter");
  808. ASSERT_TRUE(msg1 == ComplexMessage(0, {"AccessMessageFieldsFromGetter"}));
  809. }