nestedtest.cpp.inc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2020 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 <gtest/gtest.h>
  26. #include <qprotobufserializer.h>
  27. #include "../testscommon.h"
  28. using namespace qtprotobufnamespace::tests::nested;
  29. namespace QtProtobuf {
  30. namespace tests {
  31. class NestedTest : public ::testing::Test
  32. {
  33. public:
  34. NestedTest() = default;
  35. void SetUp() override;
  36. static void SetUpTestCase();
  37. protected:
  38. std::unique_ptr<QProtobufSerializer> serializer;
  39. };
  40. void NestedTest::SetUpTestCase()
  41. {
  42. //Register all types
  43. QtProtobuf::qRegisterProtobufTypes();
  44. }
  45. void NestedTest::SetUp()
  46. {
  47. serializer.reset(new QProtobufSerializer);
  48. }
  49. TEST_F(NestedTest, NestedMessageTest)
  50. {
  51. assertMessagePropertyRegistered<NestedFieldMessage::NestedMessage, QtProtobuf::sint32>(1, "QtProtobuf::sint32", "testFieldInt");
  52. }
  53. TEST_F(NestedTest, SimpleTest)
  54. {
  55. const char *propertyName = "nested";
  56. assertMessagePropertyRegistered<NestedFieldMessage, NestedFieldMessage::NestedMessage*>(2, "qtprotobufnamespace::tests::nested::NestedFieldMessage::NestedMessage*", "nested");
  57. NestedFieldMessage test{10, {15}};
  58. EXPECT_EQ(test.nested().testFieldInt(), 15);
  59. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<NestedFieldMessage::NestedMessage*>(new NestedFieldMessage::NestedMessage{55})));
  60. ASSERT_TRUE(*(test.property(propertyName).value<NestedFieldMessage::NestedMessage*>()) == NestedFieldMessage::NestedMessage{55});
  61. ASSERT_TRUE(test.nested() == NestedFieldMessage::NestedMessage{55});
  62. assertMessagePropertyRegistered<NestedFieldMessage2::NestedMessageLevel1, NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2*>(1, "qtprotobufnamespace::tests::nested::NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2*", "nested");
  63. NestedFieldMessage2::NestedMessageLevel1 level1{{20}};
  64. EXPECT_EQ(level1.nested().testFieldInt(), 20);
  65. ASSERT_TRUE(level1.setProperty(propertyName, QVariant::fromValue<NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2*>(new NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2{55})));
  66. ASSERT_TRUE(*(level1.property(propertyName).value<NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2*>()) == NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2{55});
  67. ASSERT_TRUE(level1.nested() == NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2{55});
  68. assertMessagePropertyRegistered<NestedFieldMessage2, NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2*>(3, "qtprotobufnamespace::tests::nested::NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2*", "nested2");
  69. NestedFieldMessage2 test2{level1, {20}};
  70. EXPECT_EQ(test2.nested1().nested().testFieldInt(), 55);
  71. EXPECT_EQ(test2.nested2().testFieldInt(), 20);
  72. ASSERT_TRUE(test2.setProperty("nested1", QVariant::fromValue<NestedFieldMessage2::NestedMessageLevel1*>(new NestedFieldMessage2::NestedMessageLevel1{{65}})));
  73. ASSERT_TRUE(*(test2.property("nested1").value<NestedFieldMessage2::NestedMessageLevel1*>()) == NestedFieldMessage2::NestedMessageLevel1{{65}});
  74. ASSERT_TRUE(test2.nested1() == NestedFieldMessage2::NestedMessageLevel1{{65}});
  75. ASSERT_TRUE(test2.setProperty("nested2", QVariant::fromValue<NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2*>(new NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2{75})));
  76. ASSERT_TRUE(*(test2.property("nested2").value<NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2*>()) == NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2{75});
  77. ASSERT_TRUE(test2.nested2() == NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2{75});
  78. }
  79. TEST_F(NestedTest, SerializationTest)
  80. {
  81. NestedFieldMessage::NestedMessage nested{15};
  82. QByteArray result = nested.serialize(serializer.get());
  83. EXPECT_EQ(result.size(), 2);
  84. EXPECT_STREQ(result.toHex().toStdString().c_str(), "081e");
  85. NestedFieldMessage test{10, nested};
  86. result = test.serialize(serializer.get());
  87. EXPECT_TRUE(result == QByteArray::fromHex("1202081e0814")
  88. || result == QByteArray::fromHex("08141202081e"));
  89. NestedFieldMessage2::NestedMessageLevel1 level1{{10}};
  90. result = level1.serialize(serializer.get());
  91. EXPECT_STREQ(result.toHex().toStdString().c_str(), "0a020814");
  92. NestedFieldMessage2 test2{level1, {15}};
  93. result = test2.serialize(serializer.get());
  94. EXPECT_TRUE(result == QByteArray::fromHex("1a02081e12040a020814")
  95. || result == QByteArray::fromHex("12040a0208141a02081e"));
  96. NeighborNested neigbor{{15},{20}};
  97. result = neigbor.serialize(serializer.get());
  98. EXPECT_TRUE(result == QByteArray::fromHex("120208280a02081e")
  99. || result == QByteArray::fromHex("0a02081e12020828"));
  100. }
  101. TEST_F(NestedTest, DeserializationTest)
  102. {
  103. NestedFieldMessage::NestedMessage nested;
  104. nested.deserialize(serializer.get(), QByteArray::fromHex("081e"));
  105. EXPECT_EQ(nested.testFieldInt(), 15);
  106. NestedFieldMessage test;
  107. test.deserialize(serializer.get(), QByteArray::fromHex("1202081e0814"));
  108. EXPECT_EQ(test.nested().testFieldInt(), 15);
  109. NestedFieldMessage2::NestedMessageLevel1 level1;
  110. level1.deserialize(serializer.get(), QByteArray::fromHex("0a020814"));
  111. EXPECT_EQ(level1.nested().testFieldInt(), 10);
  112. NestedFieldMessage2 test2;
  113. test2.deserialize(serializer.get(), QByteArray::fromHex("1a02081e12040a020814"));
  114. EXPECT_EQ(test2.nested1().nested().testFieldInt(), 10);
  115. EXPECT_EQ(test2.nested2().testFieldInt(), 15);
  116. NeighborNested neigbor;
  117. neigbor.deserialize(serializer.get(), QByteArray::fromHex("120208280a02081e"));
  118. EXPECT_EQ(neigbor.neighborNested().testFieldInt(), 15);
  119. EXPECT_EQ(neigbor.neighborNested2().testFieldInt(), 20);
  120. }
  121. TEST_F(NestedTest, NeighborTest)
  122. {
  123. assertMessagePropertyRegistered<NeighborNested, NestedFieldMessage::NestedMessage*>(1, "qtprotobufnamespace::tests::nested::NestedFieldMessage::NestedMessage*", "neighborNested");
  124. assertMessagePropertyRegistered<NeighborNested, NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2*>(2, "qtprotobufnamespace::tests::nested::NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2*", "neighborNested2");
  125. NeighborNested test{{15},{20}};
  126. EXPECT_EQ(test.neighborNested().testFieldInt(), 15);
  127. EXPECT_EQ(test.neighborNested2().testFieldInt(), 20);
  128. const char *propertyName = "neighborNested";
  129. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<NestedFieldMessage::NestedMessage*>(new NestedFieldMessage::NestedMessage{55})));
  130. ASSERT_TRUE(*(test.property(propertyName).value<NestedFieldMessage::NestedMessage*>()) == NestedFieldMessage::NestedMessage{55});
  131. ASSERT_TRUE(test.neighborNested() == NestedFieldMessage::NestedMessage{55});
  132. propertyName = "neighborNested2";
  133. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2*>(new NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2{75})));
  134. ASSERT_TRUE(*(test.property(propertyName).value<NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2*>()) == NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2{75});
  135. ASSERT_TRUE(test.neighborNested2() == NestedFieldMessage2::NestedMessageLevel1::NestedMessageLevel2{75});
  136. }
  137. TEST_F(NestedTest, ExternalTest)
  138. {
  139. assertMessagePropertyRegistered<NestedExternal, qtprotobufnamespace1::externaltests::NestedFieldMessage::NestedMessage*>(1, "qtprotobufnamespace1::externaltests::NestedFieldMessage::NestedMessage*", "externalNested");
  140. NestedExternal test{{15}};
  141. EXPECT_EQ(test.externalNested().field(), 15);
  142. const char *propertyName = "externalNested";
  143. ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<qtprotobufnamespace1::externaltests::NestedFieldMessage::NestedMessage*>(new qtprotobufnamespace1::externaltests::NestedFieldMessage::NestedMessage{55})));
  144. ASSERT_TRUE(*(test.property(propertyName).value<qtprotobufnamespace1::externaltests::NestedFieldMessage::NestedMessage*>()) == qtprotobufnamespace1::externaltests::NestedFieldMessage::NestedMessage{55});
  145. ASSERT_TRUE(test.externalNested() == qtprotobufnamespace1::externaltests::NestedFieldMessage::NestedMessage{55});
  146. }
  147. }
  148. }