simpletest.cpp 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 <QMetaProperty>
  31. using namespace qtprotobuf::tests;
  32. SimpleTest::SimpleTest()
  33. {
  34. }
  35. TEST_F(SimpleTest, SimpleIntMessageTest)
  36. {
  37. const char* propertyName = "testFieldInt";
  38. SimpleIntMessage test;
  39. int propertyNumber = SimpleIntMessage::propertyOrdering.at(5); //See simpletest.proto
  40. ASSERT_EQ(SimpleIntMessage::staticMetaObject.property(propertyNumber).type(), QVariant::Int);
  41. ASSERT_STREQ(SimpleIntMessage::staticMetaObject.property(propertyNumber).name(), propertyName);
  42. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue(1)));
  43. ASSERT_EQ(test.property(propertyName).toInt(), 1);
  44. }
  45. TEST_F(SimpleTest, SimpleStringMessageTest)
  46. {
  47. const char* propertyName = "testFieldString";
  48. SimpleStringMessage test;
  49. int propertyNumber = SimpleStringMessage::propertyOrdering.at(6); //See simpletest.proto
  50. ASSERT_EQ(SimpleStringMessage::staticMetaObject.property(propertyNumber).type(), QVariant::String);
  51. ASSERT_STREQ(SimpleStringMessage::staticMetaObject.property(propertyNumber).name(), propertyName);
  52. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue(QString("test1"))));
  53. ASSERT_STREQ(test.property(propertyName).toString().toStdString().c_str(), "test1");
  54. }
  55. TEST_F(SimpleTest, SimpleFloatMessageTest)
  56. {
  57. const char* propertyName = "testFieldFloat";
  58. SimpleFloatMessage test;
  59. int propertyNumber = SimpleFloatMessage::propertyOrdering.at(7); //See simpletest.proto
  60. ASSERT_EQ(SimpleFloatMessage::staticMetaObject.property(propertyNumber).type(), QVariant::Double);
  61. ASSERT_STREQ(SimpleFloatMessage::staticMetaObject.property(propertyNumber).name(), "testFieldFloat");
  62. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue(1.55)));
  63. ASSERT_EQ(test.property(propertyName).toDouble(), 1.55);
  64. }
  65. TEST_F(SimpleTest, SimpleDoubleMessageTest)
  66. {
  67. const char* propertyName = "testFieldDouble";
  68. SimpleDoubleMessage test;
  69. int propertyNumber = SimpleDoubleMessage::propertyOrdering.at(8); //See simpletest.proto
  70. ASSERT_EQ(SimpleDoubleMessage::staticMetaObject.property(propertyNumber).type(), QVariant::Double);
  71. ASSERT_STREQ(SimpleDoubleMessage::staticMetaObject.property(propertyNumber).name(), propertyName);
  72. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue(0.55)));
  73. ASSERT_EQ(test.property(propertyName).toDouble(), 0.55);
  74. }