simpletest.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 "simplefixedint32message.h"
  28. #include "simplefixedint64message.h"
  29. #include "simplestringmessage.h"
  30. #include "simplefloatmessage.h"
  31. #include "simpledoublemessage.h"
  32. #include "simpleenummessage.h"
  33. #include "complexmessage.h"
  34. #include "repeatedintmessage.h"
  35. #include "simplebytesmessage.h"
  36. #include "globalenums.h"
  37. #include "qtprotobuf.h"
  38. #include <QVariantList>
  39. #include <QMetaProperty>
  40. using namespace qtprotobufnamespace::tests;
  41. using namespace qtprotobuf::tests;
  42. using namespace qtprotobuf;
  43. SimpleTest::SimpleTest()
  44. {
  45. QtProtobuf::init();
  46. }
  47. TEST_F(SimpleTest, SimpleIntMessageTest)
  48. {
  49. const char* propertyName = "testFieldInt";
  50. SimpleIntMessage test;
  51. int propertyNumber = SimpleIntMessage::propertyOrdering.at(1); //See simpletest.proto
  52. ASSERT_EQ(SimpleIntMessage::staticMetaObject.property(propertyNumber).type(), QMetaType::Int);
  53. ASSERT_STREQ(SimpleIntMessage::staticMetaObject.property(propertyNumber).name(), propertyName);
  54. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue(1)));
  55. ASSERT_EQ(test.property(propertyName).toInt(), 1);
  56. ASSERT_EQ(test.testFieldInt(), 1);
  57. }
  58. TEST_F(SimpleTest, SimpleFixedInt32MessageTest)
  59. {
  60. const char* propertyName = "testFieldFixedInt32";
  61. SimpleFixedInt32Message test;
  62. int propertyNumber = SimpleFixedInt32Message::propertyOrdering.at(1); //See simpletest.proto
  63. ASSERT_EQ(SimpleFixedInt32Message::staticMetaObject.property(propertyNumber).type(), qMetaTypeId<FixedInt32>());
  64. ASSERT_STREQ(SimpleFixedInt32Message::staticMetaObject.property(propertyNumber).name(), propertyName);
  65. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue(1)));
  66. ASSERT_EQ(test.property(propertyName).value<FixedInt32>(), 1);
  67. ASSERT_EQ(test.testFieldFixedInt32(), 1);
  68. }
  69. TEST_F(SimpleTest, SimpleFixedInt64MessageTest)
  70. {
  71. const char* propertyName = "testFieldFixedInt64";
  72. SimpleFixedInt64Message test;
  73. int propertyNumber = SimpleFixedInt64Message::propertyOrdering.at(1); //See simpletest.proto
  74. ASSERT_EQ(SimpleFixedInt64Message::staticMetaObject.property(propertyNumber).type(), qMetaTypeId<FixedInt64>());
  75. ASSERT_STREQ(SimpleFixedInt64Message::staticMetaObject.property(propertyNumber).name(), propertyName);
  76. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue(1)));
  77. ASSERT_EQ(test.property(propertyName).value<FixedInt64>(), 1);
  78. ASSERT_EQ(test.testFieldFixedInt64(), 1);
  79. }
  80. TEST_F(SimpleTest, SimpleStringMessageTest)
  81. {
  82. const char* propertyName = "testFieldString";
  83. SimpleStringMessage test;
  84. int propertyNumber = SimpleStringMessage::propertyOrdering.at(6); //See simpletest.proto
  85. ASSERT_EQ(SimpleStringMessage::staticMetaObject.property(propertyNumber).type(), QMetaType::QString);
  86. ASSERT_STREQ(SimpleStringMessage::staticMetaObject.property(propertyNumber).name(), propertyName);
  87. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue(QString("test1"))));
  88. ASSERT_STREQ(test.property(propertyName).toString().toStdString().c_str(), "test1");
  89. ASSERT_STREQ(test.testFieldString().toStdString().c_str(), "test1");
  90. }
  91. TEST_F(SimpleTest, SimpleFloatMessageTest)
  92. {
  93. const char* propertyName = "testFieldFloat";
  94. SimpleFloatMessage test;
  95. int propertyNumber = SimpleFloatMessage::propertyOrdering.at(7); //See simpletest.proto
  96. ASSERT_EQ(SimpleFloatMessage::staticMetaObject.property(propertyNumber).type(), QMetaType::Float);
  97. ASSERT_STREQ(SimpleFloatMessage::staticMetaObject.property(propertyNumber).name(), "testFieldFloat");
  98. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<float>(1.55f)));
  99. ASSERT_TRUE(qFuzzyCompare(test.property(propertyName).toFloat(), 1.55f));
  100. ASSERT_TRUE(qFuzzyCompare(test.testFieldFloat(), 1.55f));
  101. }
  102. TEST_F(SimpleTest, SimpleDoubleMessageTest)
  103. {
  104. const char* propertyName = "testFieldDouble";
  105. SimpleDoubleMessage test;
  106. int propertyNumber = SimpleDoubleMessage::propertyOrdering.at(8); //See simpletest.proto
  107. ASSERT_EQ(SimpleDoubleMessage::staticMetaObject.property(propertyNumber).type(), QMetaType::Double);
  108. ASSERT_STREQ(SimpleDoubleMessage::staticMetaObject.property(propertyNumber).name(), propertyName);
  109. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<double>(0.55)));
  110. ASSERT_FLOAT_EQ(test.property(propertyName).toDouble(), 0.55);
  111. ASSERT_FLOAT_EQ(test.testFieldDouble(), 0.55);
  112. }
  113. TEST_F(SimpleTest, SimpleLocalEnumsTest)
  114. {
  115. ASSERT_GT(SimpleEnumMessage::staticMetaObject.enumeratorCount(), 0);
  116. QMetaEnum simpleEnum;
  117. for (int i = 0; i < SimpleEnumMessage::staticMetaObject.enumeratorCount(); i++) {
  118. QMetaEnum tmp = SimpleEnumMessage::staticMetaObject.enumerator(i);
  119. if (QString(tmp.name()) == QString("LocalEnum")) {
  120. simpleEnum = tmp;
  121. break;
  122. }
  123. }
  124. ASSERT_TRUE(simpleEnum.isValid());
  125. ASSERT_STREQ(simpleEnum.key(0), "LOCAL_ENUM_VALUE0");
  126. ASSERT_STREQ(simpleEnum.key(1), "LOCAL_ENUM_VALUE1");
  127. ASSERT_STREQ(simpleEnum.key(2), "LOCAL_ENUM_VALUE2");
  128. ASSERT_STREQ(simpleEnum.key(3), "LOCAL_ENUM_VALUE3");
  129. ASSERT_EQ(simpleEnum.value(0), 0);
  130. ASSERT_EQ(simpleEnum.value(1), 1);
  131. ASSERT_EQ(simpleEnum.value(2), 2);
  132. ASSERT_EQ(simpleEnum.value(3), 3);
  133. }
  134. TEST_F(SimpleTest, SimpleEnumsTest)
  135. {
  136. ASSERT_GT(GlobalEnums::staticMetaObject.enumeratorCount(), 0);
  137. QMetaEnum simpleEnum;
  138. for (int i = 0; i < GlobalEnums::staticMetaObject.enumeratorCount(); i++) {
  139. QMetaEnum tmp = GlobalEnums::staticMetaObject.enumerator(i);
  140. if (QString(tmp.name()) == QString("TestEnum")) {
  141. simpleEnum = tmp;
  142. break;
  143. }
  144. }
  145. ASSERT_TRUE(simpleEnum.isValid());
  146. ASSERT_STREQ(simpleEnum.key(0), "TEST_ENUM_VALUE0");
  147. ASSERT_STREQ(simpleEnum.key(1), "TEST_ENUM_VALUE1");
  148. ASSERT_STREQ(simpleEnum.key(2), "TEST_ENUM_VALUE2");
  149. ASSERT_STREQ(simpleEnum.key(3), "TEST_ENUM_VALUE3");
  150. ASSERT_STREQ(simpleEnum.key(4), "TEST_ENUM_VALUE4");
  151. ASSERT_EQ(simpleEnum.value(0), 0);
  152. ASSERT_EQ(simpleEnum.value(1), 1);
  153. ASSERT_EQ(simpleEnum.value(2), 2);
  154. ASSERT_EQ(simpleEnum.value(3), 4);
  155. ASSERT_EQ(simpleEnum.value(4), 3);
  156. }
  157. TEST_F(SimpleTest, ComplexMessageTest)
  158. {
  159. ComplexMessage msg;
  160. }
  161. TEST_F(SimpleTest, SimpleBytesMessageTest)
  162. {
  163. const char* propertyName = "testFieldBytes";
  164. SimpleBytesMessage test;
  165. int propertyNumber = SimpleBytesMessage::propertyOrdering.at(1); //See simpletest.proto
  166. ASSERT_EQ(SimpleBytesMessage::staticMetaObject.property(propertyNumber).type(), QMetaType::QByteArray);
  167. ASSERT_STREQ(SimpleBytesMessage::staticMetaObject.property(propertyNumber).name(), propertyName);
  168. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<QByteArray>("\x01\x02\x03\x04\x05")));
  169. ASSERT_TRUE(test.property(propertyName).toByteArray() == QByteArray("\x01\x02\x03\x04\x05"));
  170. ASSERT_TRUE(test.testFieldBytes() == QByteArray("\x01\x02\x03\x04\x05"));
  171. }
  172. TEST_F(SimpleTest, RepeatedIntMessageTest)
  173. {
  174. const char* propertyName = "testRepeatedInt";
  175. RepeatedIntMessage test;
  176. int propertyNumber = RepeatedIntMessage::propertyOrdering.at(1); //See simpletest.proto
  177. ASSERT_EQ(RepeatedIntMessage::staticMetaObject.property(propertyNumber).type(), qMetaTypeId<IntList>());
  178. ASSERT_STREQ(RepeatedIntMessage::staticMetaObject.property(propertyNumber).name(), propertyName);
  179. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<IntList>({1,2,3,4,5})));
  180. ASSERT_TRUE(test.property(propertyName).value<IntList>() == IntList({1, 2, 3, 4, 5}));
  181. ASSERT_TRUE(test.testRepeatedInt() == IntList({1, 2, 3, 4, 5}));
  182. }