simpletest.cpp 7.6 KB

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