simpletest.cpp 6.2 KB

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