simpletest.cpp 7.6 KB

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