simpletest.cpp 6.2 KB

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