simpletest.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 "globalenums.h"
  31. #include <QMetaProperty>
  32. using namespace qtprotobuf::tests;
  33. SimpleTest::SimpleTest()
  34. {
  35. }
  36. TEST_F(SimpleTest, SimpleIntMessageTest)
  37. {
  38. const char* propertyName = "testFieldInt";
  39. SimpleIntMessage test;
  40. int propertyNumber = SimpleIntMessage::propertyOrdering.at(1); //See simpletest.proto
  41. ASSERT_EQ(SimpleIntMessage::staticMetaObject.property(propertyNumber).type(), QMetaType::Int);
  42. ASSERT_STREQ(SimpleIntMessage::staticMetaObject.property(propertyNumber).name(), propertyName);
  43. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue(1)));
  44. ASSERT_EQ(test.property(propertyName).toInt(), 1);
  45. }
  46. TEST_F(SimpleTest, SimpleStringMessageTest)
  47. {
  48. const char* propertyName = "testFieldString";
  49. SimpleStringMessage test;
  50. int propertyNumber = SimpleStringMessage::propertyOrdering.at(6); //See simpletest.proto
  51. ASSERT_EQ(SimpleStringMessage::staticMetaObject.property(propertyNumber).type(), QMetaType::QString);
  52. ASSERT_STREQ(SimpleStringMessage::staticMetaObject.property(propertyNumber).name(), propertyName);
  53. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue(QString("test1"))));
  54. ASSERT_STREQ(test.property(propertyName).toString().toStdString().c_str(), "test1");
  55. }
  56. TEST_F(SimpleTest, SimpleFloatMessageTest)
  57. {
  58. const char* propertyName = "testFieldFloat";
  59. SimpleFloatMessage test;
  60. int propertyNumber = SimpleFloatMessage::propertyOrdering.at(7); //See simpletest.proto
  61. ASSERT_EQ(SimpleFloatMessage::staticMetaObject.property(propertyNumber).type(), QMetaType::Float);
  62. ASSERT_STREQ(SimpleFloatMessage::staticMetaObject.property(propertyNumber).name(), "testFieldFloat");
  63. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<float>(1.55)));
  64. ASSERT_TRUE(qFuzzyCompare(test.property(propertyName).toFloat(), 1.55f));
  65. }
  66. TEST_F(SimpleTest, SimpleDoubleMessageTest)
  67. {
  68. const char* propertyName = "testFieldDouble";
  69. SimpleDoubleMessage test;
  70. int propertyNumber = SimpleDoubleMessage::propertyOrdering.at(8); //See simpletest.proto
  71. ASSERT_EQ(SimpleDoubleMessage::staticMetaObject.property(propertyNumber).type(), QMetaType::Double);
  72. ASSERT_STREQ(SimpleDoubleMessage::staticMetaObject.property(propertyNumber).name(), propertyName);
  73. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<double>(0.55)));
  74. ASSERT_FLOAT_EQ(test.property(propertyName).toDouble(), 0.55);
  75. }
  76. TEST_F(SimpleTest, SimpleEnumsTest)
  77. {
  78. ASSERT_GT(GlobalEnums::staticMetaObject.enumeratorCount(), 0);
  79. QMetaEnum simpleEnum = GlobalEnums::staticMetaObject.enumerator(0);
  80. ASSERT_STREQ(simpleEnum.key(0), "TEST_ENUM_VALUE0");
  81. ASSERT_STREQ(simpleEnum.key(1), "TEST_ENUM_VALUE1");
  82. ASSERT_STREQ(simpleEnum.key(2), "TEST_ENUM_VALUE2");
  83. ASSERT_STREQ(simpleEnum.key(3), "TEST_ENUM_VALUE3");
  84. ASSERT_STREQ(simpleEnum.key(4), "TEST_ENUM_VALUE4");
  85. ASSERT_EQ(simpleEnum.value(0), 0);
  86. ASSERT_EQ(simpleEnum.value(1), 1);
  87. ASSERT_EQ(simpleEnum.value(2), 2);
  88. ASSERT_EQ(simpleEnum.value(3), 4);
  89. ASSERT_EQ(simpleEnum.value(4), 3);
  90. }