simpletest.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 "simpletest.h"
  26. #include "simpleintmessage.h"
  27. #include "simplesintmessage.h"
  28. #include "simpleuintmessage.h"
  29. #include "simpleint64message.h"
  30. #include "simplesint64message.h"
  31. #include "simpleuint64message.h"
  32. #include "simplefixedint32message.h"
  33. #include "simplefixedint64message.h"
  34. #include "simplesfixedint32message.h"
  35. #include "simplesfixedint64message.h"
  36. #include "simplestringmessage.h"
  37. #include "simplefloatmessage.h"
  38. #include "simpledoublemessage.h"
  39. #include "simpleenummessage.h"
  40. #include "complexmessage.h"
  41. #include "repeatedintmessage.h"
  42. #include "simplebytesmessage.h"
  43. #include "globalenums.h"
  44. #include "qtprotobuf.h"
  45. #include <QVariantList>
  46. #include <QMetaProperty>
  47. using namespace qtprotobufnamespace::tests;
  48. using namespace qtprotobuf::tests;
  49. using namespace qtprotobuf;
  50. SimpleTest::SimpleTest()
  51. {
  52. QtProtobuf::init();
  53. }
  54. TEST_F(SimpleTest, SimpleIntMessageTest)
  55. {
  56. const char* propertyName = "testFieldInt";
  57. SimpleIntMessage test;
  58. int propertyNumber = SimpleIntMessage::propertyOrdering.at(1); //See simpletest.proto
  59. ASSERT_STREQ(SimpleIntMessage::staticMetaObject.property(propertyNumber).typeName(), "qtprotobuf::int32");
  60. ASSERT_EQ(SimpleIntMessage::staticMetaObject.property(propertyNumber).type(), qMetaTypeId<int32>());
  61. ASSERT_STREQ(SimpleIntMessage::staticMetaObject.property(propertyNumber).name(), propertyName);
  62. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue(1)));
  63. ASSERT_EQ(test.property(propertyName).toInt(), 1);
  64. ASSERT_EQ(test.testFieldInt(), 1);
  65. }
  66. TEST_F(SimpleTest, SimpleSIntMessageTest)
  67. {
  68. const char* propertyName = "testFieldInt";
  69. SimpleSIntMessage test;
  70. int propertyNumber = SimpleSIntMessage::propertyOrdering.at(1); //See simpletest.proto
  71. ASSERT_STREQ(SimpleSIntMessage::staticMetaObject.property(propertyNumber).typeName(), "qtprotobuf::sint32");
  72. ASSERT_EQ(SimpleSIntMessage::staticMetaObject.property(propertyNumber).type(), qMetaTypeId<sint32>());
  73. ASSERT_STREQ(SimpleSIntMessage::staticMetaObject.property(propertyNumber).name(), propertyName);
  74. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue(1)));
  75. ASSERT_EQ(test.property(propertyName).toInt(), 1);
  76. ASSERT_EQ(test.testFieldInt(), 1);
  77. }
  78. TEST_F(SimpleTest, SimpleUIntMessageTest)
  79. {
  80. const char* propertyName = "testFieldInt";
  81. SimpleUIntMessage test;
  82. int propertyNumber = SimpleUIntMessage::propertyOrdering.at(1); //See simpletest.proto
  83. ASSERT_STREQ(SimpleUIntMessage::staticMetaObject.property(propertyNumber).typeName(), "qtprotobuf::uint32");
  84. ASSERT_EQ(SimpleUIntMessage::staticMetaObject.property(propertyNumber).type(), qMetaTypeId<uint32>());
  85. ASSERT_STREQ(SimpleUIntMessage::staticMetaObject.property(propertyNumber).name(), propertyName);
  86. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue(1)));
  87. ASSERT_EQ(test.property(propertyName).toInt(), 1);
  88. ASSERT_EQ(test.testFieldInt(), 1);
  89. }
  90. TEST_F(SimpleTest, SimpleInt64MessageTest)
  91. {
  92. const char* propertyName = "testFieldInt";
  93. SimpleInt64Message test;
  94. int propertyNumber = SimpleInt64Message::propertyOrdering.at(1); //See simpletest.proto
  95. ASSERT_STREQ(SimpleInt64Message::staticMetaObject.property(propertyNumber).typeName(), "qtprotobuf::int64");
  96. ASSERT_EQ(SimpleInt64Message::staticMetaObject.property(propertyNumber).type(), qMetaTypeId<int64>());
  97. ASSERT_STREQ(SimpleInt64Message::staticMetaObject.property(propertyNumber).name(), propertyName);
  98. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue(1)));
  99. ASSERT_EQ(test.property(propertyName).toInt(), 1);
  100. ASSERT_EQ(test.testFieldInt(), 1);
  101. }
  102. TEST_F(SimpleTest, SimpleSInt64MessageTest)
  103. {
  104. const char* propertyName = "testFieldInt";
  105. SimpleSInt64Message test;
  106. int propertyNumber = SimpleSInt64Message::propertyOrdering.at(1); //See simpletest.proto
  107. ASSERT_STREQ(SimpleSInt64Message::staticMetaObject.property(propertyNumber).typeName(), "qtprotobuf::sint64");
  108. ASSERT_EQ(SimpleSInt64Message::staticMetaObject.property(propertyNumber).type(), qMetaTypeId<sint64>());
  109. ASSERT_STREQ(SimpleSInt64Message::staticMetaObject.property(propertyNumber).name(), propertyName);
  110. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue(1)));
  111. ASSERT_EQ(test.property(propertyName).toInt(), 1);
  112. ASSERT_EQ(test.testFieldInt(), 1);
  113. }
  114. TEST_F(SimpleTest, SimpleUInt64MessageTest)
  115. {
  116. const char* propertyName = "testFieldInt";
  117. SimpleUInt64Message test;
  118. int propertyNumber = SimpleUInt64Message::propertyOrdering.at(1); //See simpletest.proto
  119. ASSERT_STREQ(SimpleUInt64Message::staticMetaObject.property(propertyNumber).typeName(), "qtprotobuf::uint64");
  120. ASSERT_EQ(SimpleUInt64Message::staticMetaObject.property(propertyNumber).type(), qMetaTypeId<uint64>());
  121. ASSERT_STREQ(SimpleUInt64Message::staticMetaObject.property(propertyNumber).name(), propertyName);
  122. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue(1)));
  123. ASSERT_EQ(test.property(propertyName).toInt(), 1);
  124. ASSERT_EQ(test.testFieldInt(), 1);
  125. }
  126. TEST_F(SimpleTest, SimpleFixedInt32MessageTest)
  127. {
  128. const char* propertyName = "testFieldFixedInt32";
  129. SimpleFixedInt32Message test;
  130. int propertyNumber = SimpleFixedInt32Message::propertyOrdering.at(1); //See simpletest.proto
  131. ASSERT_EQ(SimpleFixedInt32Message::staticMetaObject.property(propertyNumber).type(), qMetaTypeId<fint32>());
  132. ASSERT_STREQ(SimpleFixedInt32Message::staticMetaObject.property(propertyNumber).typeName(), "qtprotobuf::fint32");
  133. ASSERT_STREQ(SimpleFixedInt32Message::staticMetaObject.property(propertyNumber).name(), propertyName);
  134. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue(1)));
  135. ASSERT_EQ(test.property(propertyName).value<fint32>(), 1);
  136. ASSERT_EQ(test.testFieldFixedInt32(), 1);
  137. }
  138. TEST_F(SimpleTest, SimpleFixedInt64MessageTest)
  139. {
  140. const char* propertyName = "testFieldFixedInt64";
  141. SimpleFixedInt64Message test;
  142. int propertyNumber = SimpleFixedInt64Message::propertyOrdering.at(1); //See simpletest.proto
  143. ASSERT_EQ(SimpleFixedInt64Message::staticMetaObject.property(propertyNumber).type(), qMetaTypeId<fint64>());
  144. ASSERT_STREQ(SimpleFixedInt64Message::staticMetaObject.property(propertyNumber).typeName(), "qtprotobuf::fint64");
  145. ASSERT_STREQ(SimpleFixedInt64Message::staticMetaObject.property(propertyNumber).name(), propertyName);
  146. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue(1)));
  147. ASSERT_EQ(test.property(propertyName).value<fint64>(), 1);
  148. ASSERT_EQ(test.testFieldFixedInt64(), 1);
  149. }
  150. TEST_F(SimpleTest, SimpleSFixedInt32MessageTest)
  151. {
  152. const char* propertyName = "testFieldFixedInt32";
  153. SimpleSFixedInt32Message test;
  154. int propertyNumber = SimpleSFixedInt32Message::propertyOrdering.at(1); //See simpletest.proto
  155. ASSERT_EQ(SimpleSFixedInt32Message::staticMetaObject.property(propertyNumber).type(), qMetaTypeId<sfint32>());
  156. ASSERT_STREQ(SimpleSFixedInt32Message::staticMetaObject.property(propertyNumber).typeName(), "qtprotobuf::sfint32");
  157. ASSERT_STREQ(SimpleSFixedInt32Message::staticMetaObject.property(propertyNumber).name(), propertyName);
  158. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue(1)));
  159. ASSERT_EQ(test.property(propertyName).value<fint32>(), 1);
  160. ASSERT_EQ(test.testFieldFixedInt32(), 1);
  161. }
  162. TEST_F(SimpleTest, SimpleSFixedInt64MessageTest)
  163. {
  164. const char* propertyName = "testFieldFixedInt64";
  165. SimpleSFixedInt64Message test;
  166. int propertyNumber = SimpleSFixedInt64Message::propertyOrdering.at(1); //See simpletest.proto
  167. ASSERT_EQ(SimpleSFixedInt64Message::staticMetaObject.property(propertyNumber).type(), qMetaTypeId<sfint64>());
  168. ASSERT_STREQ(SimpleSFixedInt64Message::staticMetaObject.property(propertyNumber).typeName(), "qtprotobuf::sfint64");
  169. ASSERT_STREQ(SimpleSFixedInt64Message::staticMetaObject.property(propertyNumber).name(), propertyName);
  170. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue(1)));
  171. ASSERT_EQ(test.property(propertyName).value<fint64>(), 1);
  172. ASSERT_EQ(test.testFieldFixedInt64(), 1);
  173. }
  174. TEST_F(SimpleTest, SimpleStringMessageTest)
  175. {
  176. const char* propertyName = "testFieldString";
  177. SimpleStringMessage test;
  178. int propertyNumber = SimpleStringMessage::propertyOrdering.at(6); //See simpletest.proto
  179. ASSERT_EQ(SimpleStringMessage::staticMetaObject.property(propertyNumber).type(), QMetaType::QString);
  180. ASSERT_STREQ(SimpleStringMessage::staticMetaObject.property(propertyNumber).name(), propertyName);
  181. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue(QString("test1"))));
  182. ASSERT_STREQ(test.property(propertyName).toString().toStdString().c_str(), "test1");
  183. ASSERT_STREQ(test.testFieldString().toStdString().c_str(), "test1");
  184. }
  185. TEST_F(SimpleTest, SimpleFloatMessageTest)
  186. {
  187. const char* propertyName = "testFieldFloat";
  188. SimpleFloatMessage test;
  189. int propertyNumber = SimpleFloatMessage::propertyOrdering.at(7); //See simpletest.proto
  190. ASSERT_EQ(SimpleFloatMessage::staticMetaObject.property(propertyNumber).type(), QMetaType::Float);
  191. ASSERT_STREQ(SimpleFloatMessage::staticMetaObject.property(propertyNumber).name(), "testFieldFloat");
  192. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<float>(1.55f)));
  193. ASSERT_TRUE(qFuzzyCompare(test.property(propertyName).toFloat(), 1.55f));
  194. ASSERT_TRUE(qFuzzyCompare(test.testFieldFloat(), 1.55f));
  195. }
  196. TEST_F(SimpleTest, SimpleDoubleMessageTest)
  197. {
  198. const char* propertyName = "testFieldDouble";
  199. SimpleDoubleMessage test;
  200. int propertyNumber = SimpleDoubleMessage::propertyOrdering.at(8); //See simpletest.proto
  201. ASSERT_EQ(SimpleDoubleMessage::staticMetaObject.property(propertyNumber).type(), QMetaType::Double);
  202. ASSERT_STREQ(SimpleDoubleMessage::staticMetaObject.property(propertyNumber).name(), propertyName);
  203. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<double>(0.55)));
  204. ASSERT_FLOAT_EQ(test.property(propertyName).toDouble(), 0.55);
  205. ASSERT_FLOAT_EQ(test.testFieldDouble(), 0.55);
  206. }
  207. TEST_F(SimpleTest, SimpleLocalEnumsTest)
  208. {
  209. ASSERT_GT(SimpleEnumMessage::staticMetaObject.enumeratorCount(), 0);
  210. QMetaEnum simpleEnum;
  211. for (int i = 0; i < SimpleEnumMessage::staticMetaObject.enumeratorCount(); i++) {
  212. QMetaEnum tmp = SimpleEnumMessage::staticMetaObject.enumerator(i);
  213. if (QString(tmp.name()) == QString("LocalEnum")) {
  214. simpleEnum = tmp;
  215. break;
  216. }
  217. }
  218. ASSERT_TRUE(simpleEnum.isValid());
  219. ASSERT_STREQ(simpleEnum.key(0), "LOCAL_ENUM_VALUE0");
  220. ASSERT_STREQ(simpleEnum.key(1), "LOCAL_ENUM_VALUE1");
  221. ASSERT_STREQ(simpleEnum.key(2), "LOCAL_ENUM_VALUE2");
  222. ASSERT_STREQ(simpleEnum.key(3), "LOCAL_ENUM_VALUE3");
  223. ASSERT_EQ(simpleEnum.value(0), 0);
  224. ASSERT_EQ(simpleEnum.value(1), 1);
  225. ASSERT_EQ(simpleEnum.value(2), 2);
  226. ASSERT_EQ(simpleEnum.value(3), 3);
  227. }
  228. TEST_F(SimpleTest, SimpleEnumsTest)
  229. {
  230. ASSERT_GT(GlobalEnums::staticMetaObject.enumeratorCount(), 0);
  231. QMetaEnum simpleEnum;
  232. for (int i = 0; i < GlobalEnums::staticMetaObject.enumeratorCount(); i++) {
  233. QMetaEnum tmp = GlobalEnums::staticMetaObject.enumerator(i);
  234. if (QString(tmp.name()) == QString("TestEnum")) {
  235. simpleEnum = tmp;
  236. break;
  237. }
  238. }
  239. ASSERT_TRUE(simpleEnum.isValid());
  240. ASSERT_STREQ(simpleEnum.key(0), "TEST_ENUM_VALUE0");
  241. ASSERT_STREQ(simpleEnum.key(1), "TEST_ENUM_VALUE1");
  242. ASSERT_STREQ(simpleEnum.key(2), "TEST_ENUM_VALUE2");
  243. ASSERT_STREQ(simpleEnum.key(3), "TEST_ENUM_VALUE3");
  244. ASSERT_STREQ(simpleEnum.key(4), "TEST_ENUM_VALUE4");
  245. ASSERT_EQ(simpleEnum.value(0), 0);
  246. ASSERT_EQ(simpleEnum.value(1), 1);
  247. ASSERT_EQ(simpleEnum.value(2), 2);
  248. ASSERT_EQ(simpleEnum.value(3), 4);
  249. ASSERT_EQ(simpleEnum.value(4), 3);
  250. }
  251. TEST_F(SimpleTest, ComplexMessageTest)
  252. {
  253. ComplexMessage msg;
  254. }
  255. TEST_F(SimpleTest, SimpleBytesMessageTest)
  256. {
  257. const char* propertyName = "testFieldBytes";
  258. SimpleBytesMessage test;
  259. int propertyNumber = SimpleBytesMessage::propertyOrdering.at(1); //See simpletest.proto
  260. ASSERT_EQ(SimpleBytesMessage::staticMetaObject.property(propertyNumber).type(), QMetaType::QByteArray);
  261. ASSERT_STREQ(SimpleBytesMessage::staticMetaObject.property(propertyNumber).name(), propertyName);
  262. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<QByteArray>("\x01\x02\x03\x04\x05")));
  263. ASSERT_TRUE(test.property(propertyName).toByteArray() == QByteArray("\x01\x02\x03\x04\x05"));
  264. ASSERT_TRUE(test.testFieldBytes() == QByteArray("\x01\x02\x03\x04\x05"));
  265. }
  266. TEST_F(SimpleTest, RepeatedIntMessageTest)
  267. {
  268. const char* propertyName = "testRepeatedInt";
  269. RepeatedIntMessage test;
  270. int propertyNumber = RepeatedIntMessage::propertyOrdering.at(1); //See simpletest.proto
  271. ASSERT_STREQ(RepeatedIntMessage::staticMetaObject.property(propertyNumber).typeName(), "qtprotobuf::sint32List");
  272. ASSERT_EQ(RepeatedIntMessage::staticMetaObject.property(propertyNumber).userType(), qMetaTypeId<qtprotobuf::sint32List>());
  273. ASSERT_STREQ(RepeatedIntMessage::staticMetaObject.property(propertyNumber).name(), propertyName);
  274. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<sint32List>({1, 2, 3, 4, 5})));
  275. ASSERT_TRUE(test.property(propertyName).value<sint32List>() == sint32List({1, 2, 3, 4, 5}));
  276. ASSERT_TRUE(test.testRepeatedInt() == sint32List({1, 2, 3, 4, 5}));
  277. }