simpletest.cpp 13 KB

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