jsonserializationtest.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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 <gtest/gtest.h>
  26. #include <QByteArray>
  27. #include <QString>
  28. #include <qprotobufjsonserializer.h>
  29. #include "simpletest.qpb.h"
  30. using namespace qtprotobufnamespace::tests;
  31. namespace QtProtobuf {
  32. namespace tests {
  33. class JsonSerializationTest : public ::testing::Test
  34. {
  35. public:
  36. JsonSerializationTest() = default;
  37. void SetUp() override;
  38. static void SetUpTestCase() {
  39. QtProtobuf::qRegisterProtobufTypes();
  40. }
  41. protected:
  42. std::unique_ptr<QProtobufJsonSerializer> serializer;
  43. };
  44. void JsonSerializationTest::SetUp() {
  45. serializer.reset(new QProtobufJsonSerializer);
  46. }
  47. TEST_F(JsonSerializationTest, FixedInt32MessageSerializeTest)
  48. {
  49. SimpleFixedInt32Message msg;
  50. msg.setTestFieldFixedInt32(555);
  51. QByteArray result = msg.serialize(serializer.get());
  52. ASSERT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testFieldFixedInt32\":555}");
  53. }
  54. TEST_F(JsonSerializationTest, SFixedInt32MessageSerializeTest)
  55. {
  56. SimpleSFixedInt32Message msg;
  57. msg.setTestFieldFixedInt32(555);
  58. QByteArray result = msg.serialize(serializer.get());
  59. ASSERT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testFieldFixedInt32\":555}");
  60. msg.setTestFieldFixedInt32(-555);
  61. result = msg.serialize(serializer.get());
  62. ASSERT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testFieldFixedInt32\":-555}");
  63. }
  64. TEST_F(JsonSerializationTest, Int32MessageSerializeTest)
  65. {
  66. qtprotobufnamespace::tests::SimpleIntMessage msg;
  67. msg.setTestFieldInt(555);
  68. QByteArray result = msg.serialize(serializer.get());
  69. ASSERT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testFieldInt\":555}");
  70. }
  71. TEST_F(JsonSerializationTest, SInt32MessageSerializeTest)
  72. {
  73. SimpleSIntMessage msg;
  74. msg.setTestFieldInt(555);
  75. QByteArray result = msg.serialize(serializer.get());
  76. ASSERT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testFieldInt\":555}");
  77. msg.setTestFieldInt(-555);
  78. result = msg.serialize(serializer.get());
  79. ASSERT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testFieldInt\":-555}");
  80. }
  81. TEST_F(JsonSerializationTest, UInt32MessageSerializeTest)
  82. {
  83. SimpleUIntMessage msg;
  84. msg.setTestFieldInt(555);
  85. QByteArray result = msg.serialize(serializer.get());
  86. ASSERT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testFieldInt\":555}");
  87. }
  88. TEST_F(JsonSerializationTest, FixedInt64MessageSerializeTest)
  89. {
  90. SimpleFixedInt64Message msg;
  91. msg.setTestFieldFixedInt64(555);
  92. QByteArray result = msg.serialize(serializer.get());
  93. ASSERT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testFieldFixedInt64\":555}");
  94. }
  95. TEST_F(JsonSerializationTest, Int64MessageSerializeTest)
  96. {
  97. SimpleInt64Message msg;
  98. msg.setTestFieldInt(555);
  99. QByteArray result = msg.serialize(serializer.get());
  100. ASSERT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testFieldInt\":555}");
  101. }
  102. TEST_F(JsonSerializationTest, SInt64MessageSerializeTest)
  103. {
  104. SimpleSInt64Message msg;
  105. msg.setTestFieldInt(555);
  106. QByteArray result = msg.serialize(serializer.get());
  107. ASSERT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testFieldInt\":555}");
  108. msg.setTestFieldInt(-555);
  109. result = msg.serialize(serializer.get());
  110. ASSERT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testFieldInt\":-555}");
  111. }
  112. TEST_F(JsonSerializationTest, UInt64MessageSerializeTest)
  113. {
  114. SimpleUInt64Message msg;
  115. msg.setTestFieldInt(555);
  116. QByteArray result = msg.serialize(serializer.get());
  117. ASSERT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testFieldInt\":555}");
  118. }
  119. TEST_F(JsonSerializationTest, FloatMessageSerializeTest)
  120. {
  121. SimpleFloatMessage test;
  122. test.setTestFieldFloat(0.1f);
  123. QByteArray result = test.serialize(serializer.get());
  124. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testFieldFloat\":0.1}");
  125. test.setTestFieldFloat(FLT_MIN);
  126. result = test.serialize(serializer.get());
  127. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testFieldFloat\":1.17549e-38}");
  128. test.setTestFieldFloat(FLT_MAX);
  129. result = test.serialize(serializer.get());
  130. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testFieldFloat\":3.40282e+38}");
  131. test.setTestFieldFloat(-4.2f);
  132. result = test.serialize(serializer.get());
  133. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testFieldFloat\":-4.2}");
  134. test.setTestFieldFloat(-0.0f);
  135. result = test.serialize(serializer.get());
  136. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testFieldFloat\":0}");
  137. }
  138. TEST_F(JsonSerializationTest, DoubleMessageSerializeTest)
  139. {
  140. SimpleDoubleMessage test;
  141. test.setTestFieldDouble(0.1);
  142. QByteArray result = test.serialize(serializer.get());
  143. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testFieldDouble\":0.1}");
  144. test.setTestFieldDouble(DBL_MIN);
  145. result = test.serialize(serializer.get());
  146. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testFieldDouble\":2.2250738585072014e-308}");
  147. test.setTestFieldDouble(DBL_MAX);
  148. result = test.serialize(serializer.get());
  149. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testFieldDouble\":1.7976931348623157e+308}");
  150. test.setTestFieldDouble(-4.2);
  151. result = test.serialize(serializer.get());
  152. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testFieldDouble\":-4.2}");
  153. test.setTestFieldDouble(-0.0);
  154. result = test.serialize(serializer.get());
  155. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testFieldDouble\":0}");
  156. }
  157. TEST_F(JsonSerializationTest, StringMessageSerializeTest)
  158. {
  159. SimpleStringMessage test;
  160. test.setTestFieldString("qwerty");
  161. QByteArray result = test.serialize(serializer.get());
  162. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testFieldString\":\"qwerty\"}");
  163. }
  164. TEST_F(JsonSerializationTest, BytesMessageSerializeTest)
  165. {
  166. SimpleBytesMessage test;
  167. test.setTestFieldBytes(QByteArray::fromHex("0012840432810459560052664a766a78716267764677533159764f5a5867746a356666474c53374001694e487a396f5a496f4b626d377a38480137397842757009506b70515876476f4f30394f5939785261777833654f417339786a6f544131784a68727732385441637131436562596c43395755665143366849616e74614e647948694b546f666669305a74376c613432535278585a53503447757862635a49703533704a6e79437766437931716446637a5430646d6e3768386670794164656d456176774665646134643050417047665355326a4c74333958386b595542784e4d325767414c524267486456646538377136506935553639546a684d6432385731534644314478796f67434372714f6374325a5049436f4c6e72716446334f644e7a6a52564c6665797651384c674c76524e4652395766574179417a37396e4b6742616d64384e746c7674344d6733354535675653326737415137726b6d37326342646e5739734345794761626558417548356a34475262754c543771425a574463464c463453734364533357664647644e48667761696a7a796b42796f3731507646566c54584832574a576f4676523546414c6a42546e37624364503070416953624c435938587a324d73633364426235466639474953506255704e6d557642644d5a4d485176714f6d544e584550704e306237344d444f4d5166574a53684f6f334e6b41764d6a73082a"));
  168. QByteArray result = test.serialize(serializer.get());
  169. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testFieldBytes\":\"ABKEBDKBBFlWAFJmSnZqeHFiZ3ZGd1MxWXZPWlhndGo1ZmZHTFM3QAFpTkh6OW9aSW9LYm03ejhIATc5eEJ1cAlQa3BRWHZHb08wOU9ZOXhSYXd4M2VPQXM5eGpvVEExeEpocncyOFRBY3ExQ2ViWWxDOVdVZlFDNmhJYW50YU5keUhpS1RvZmZpMFp0N2xhNDJTUnhYWlNQNEd1eGJjWklwNTNwSm55Q3dmQ3kxcWRGY3pUMGRtbjdoOGZweUFkZW1FYXZ3RmVkYTRkMFBBcEdmU1Uyakx0MzlYOGtZVUJ4Tk0yV2dBTFJCZ0hkVmRlODdxNlBpNVU2OVRqaE1kMjhXMVNGRDFEeHlvZ0NDcnFPY3QyWlBJQ29MbnJxZEYzT2ROempSVkxmZXl2UThMZ0x2Uk5GUjlXZldBeUF6NzluS2dCYW1kOE50bHZ0NE1nMzVFNWdWUzJnN0FRN3JrbTcyY0Jkblc5c0NFeUdhYmVYQXVINWo0R1JidUxUN3FCWldEY0ZMRjRTc0NkUzNXZkZHZE5IZndhaWp6eWtCeW83MVB2RlZsVFhIMldKV29GdlI1RkFMakJUbjdiQ2RQMHBBaVNiTENZOFh6Mk1zYzNkQmI1RmY5R0lTUGJVcE5tVXZCZE1aTUhRdnFPbVROWEVQcE4wYjc0TURPTVFmV0pTaE9vM05rQXZNanMIKg==\"}");
  170. }
  171. TEST_F(JsonSerializationTest, ComplexTypeSerializeTest)
  172. {
  173. SimpleStringMessage stringMsg;
  174. stringMsg.setTestFieldString("qwerty");
  175. ComplexMessage test;
  176. test.setTestFieldInt(42);
  177. test.setTestComplexField(stringMsg);
  178. QByteArray result = test.serialize(serializer.get());
  179. EXPECT_TRUE(QString::fromUtf8(result).toStdString() == std::string("{\"testComplexField\":{\"testFieldString\":\"qwerty\"},\"testFieldInt\":42}")
  180. || QString::fromUtf8(result).toStdString() == std::string("{\"testFieldInt\":42,\"testComplexField\":{\"testFieldString\":\"qwerty\"}}"));
  181. stringMsg.setTestFieldString("YVRfJvjxqbgvFwS1YvOZXgtj5ffGLS7AiNHz9oZIoKbm7z8H79xBuyPkpQXvGoO09OY9xRawx3eOAs9xjoTA1xJhrw28TAcq1CebYlC9WUfQC6hIantaNdyHiKToffi0Zt7la42SRxXZSP4GuxbcZIp53pJnyCwfCy1qdFczT0dmn7h8fpyAdemEavwFeda4d0PApGfSU2jLt39X8kYUBxNM2WgALRBgHdVde87q6Pi5U69TjhMd28W1SFD1DxyogCCrqOct2ZPICoLnrqdF3OdNzjRVLfeyvQ8LgLvRNFR9WfWAyAz79nKgBamd8Ntlvt4Mg35E5gVS2g7AQ7rkm72cBdnW9sCEyGabeXAuH5j4GRbuLT7qBZWDcFLF4SsCdS3WfFGdNHfwaijzykByo71PvFVlTXH2WJWoFvR5FALjBTn7bCdP0pAiSbLCY8Xz2Msc3dBb5Ff9GISPbUpNmUvBdMZMHQvqOmTNXEPpN0b74MDOMQfWJShOo3NkAvMjs");
  182. test.setTestFieldInt(42);
  183. test.setTestComplexField(stringMsg);
  184. result = test.serialize(serializer.get());
  185. EXPECT_TRUE(QString::fromUtf8(result).toStdString() == "{\"testComplexField\":{\"testFieldString\":\"YVRfJvjxqbgvFwS1YvOZXgtj5ffGLS7AiNHz9oZIoKbm7z8H79xBuyPkpQXvGoO09OY9xRawx3eOAs9xjoTA1xJhrw28TAcq1CebYlC9WUfQC6hIantaNdyHiKToffi0Zt7la42SRxXZSP4GuxbcZIp53pJnyCwfCy1qdFczT0dmn7h8fpyAdemEavwFeda4d0PApGfSU2jLt39X8kYUBxNM2WgALRBgHdVde87q6Pi5U69TjhMd28W1SFD1DxyogCCrqOct2ZPICoLnrqdF3OdNzjRVLfeyvQ8LgLvRNFR9WfWAyAz79nKgBamd8Ntlvt4Mg35E5gVS2g7AQ7rkm72cBdnW9sCEyGabeXAuH5j4GRbuLT7qBZWDcFLF4SsCdS3WfFGdNHfwaijzykByo71PvFVlTXH2WJWoFvR5FALjBTn7bCdP0pAiSbLCY8Xz2Msc3dBb5Ff9GISPbUpNmUvBdMZMHQvqOmTNXEPpN0b74MDOMQfWJShOo3NkAvMjs\"},\"testFieldInt\":42}"
  186. || QString::fromUtf8(result).toStdString() == "{\"testFieldInt\":42,\"testComplexField\":{\"testFieldString\":\"YVRfJvjxqbgvFwS1YvOZXgtj5ffGLS7AiNHz9oZIoKbm7z8H79xBuyPkpQXvGoO09OY9xRawx3eOAs9xjoTA1xJhrw28TAcq1CebYlC9WUfQC6hIantaNdyHiKToffi0Zt7la42SRxXZSP4GuxbcZIp53pJnyCwfCy1qdFczT0dmn7h8fpyAdemEavwFeda4d0PApGfSU2jLt39X8kYUBxNM2WgALRBgHdVde87q6Pi5U69TjhMd28W1SFD1DxyogCCrqOct2ZPICoLnrqdF3OdNzjRVLfeyvQ8LgLvRNFR9WfWAyAz79nKgBamd8Ntlvt4Mg35E5gVS2g7AQ7rkm72cBdnW9sCEyGabeXAuH5j4GRbuLT7qBZWDcFLF4SsCdS3WfFGdNHfwaijzykByo71PvFVlTXH2WJWoFvR5FALjBTn7bCdP0pAiSbLCY8Xz2Msc3dBb5Ff9GISPbUpNmUvBdMZMHQvqOmTNXEPpN0b74MDOMQfWJShOo3NkAvMjs\"}}");
  187. stringMsg.setTestFieldString("qwerty");
  188. test.setTestFieldInt(-45);
  189. test.setTestComplexField(stringMsg);
  190. result = test.serialize(serializer.get());
  191. EXPECT_TRUE(QString::fromUtf8(result).toStdString() == "{\"testComplexField\":{\"testFieldString\":\"qwerty\"},\"testFieldInt\":-45}"
  192. || QString::fromUtf8(result).toStdString() == "{\"testFieldInt\":-45,\"testComplexField\":{\"testFieldString\":\"qwerty\"}}");
  193. }
  194. TEST_F(JsonSerializationTest, RepeatedIntMessageTest)
  195. {
  196. RepeatedIntMessage test;
  197. test.setTestRepeatedInt({0, 1, 321, -65999, 123245, -3, 3});
  198. QByteArray result = test.serialize(serializer.get());
  199. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[0,1,321,-65999,123245,-3,3]}");
  200. test.setTestRepeatedInt(int32List());
  201. result = test.serialize(serializer.get());
  202. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[]}");
  203. }
  204. TEST_F(JsonSerializationTest, RepeatedSIntMessageTest)
  205. {
  206. RepeatedSIntMessage test;
  207. test.setTestRepeatedInt({1, 321, -65999, 123245, -3, 3, 0});
  208. QByteArray result = test.serialize(serializer.get());
  209. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[1,321,-65999,123245,-3,3,0]}");
  210. test.setTestRepeatedInt(sint32List());
  211. result = test.serialize(serializer.get());
  212. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[]}");
  213. }
  214. TEST_F(JsonSerializationTest, RepeatedUIntMessageTest)
  215. {
  216. RepeatedUIntMessage test;
  217. test.setTestRepeatedInt({1, 0, 321, 65999, 123245, 3});
  218. QByteArray result = test.serialize(serializer.get());
  219. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[1,0,321,65999,123245,3]}");
  220. test.setTestRepeatedInt(uint32List());
  221. result = test.serialize(serializer.get());
  222. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[]}");
  223. }
  224. TEST_F(JsonSerializationTest, RepeatedInt64MessageTest)
  225. {
  226. RepeatedInt64Message test;
  227. test.setTestRepeatedInt({1, 321, -65999, 12324523123123, -3, 0, 3});
  228. QByteArray result = test.serialize(serializer.get());
  229. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[1,321,-65999,12324523123123,-3,0,3]}");
  230. test.setTestRepeatedInt(int64List());
  231. result = test.serialize(serializer.get());
  232. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[]}");
  233. }
  234. TEST_F(JsonSerializationTest, RepeatedSInt64MessageTest)
  235. {
  236. RepeatedSInt64Message test;
  237. test.setTestRepeatedInt({1, 321, -65999, 12324523123123, 0, -3, 3});
  238. QByteArray result = test.serialize(serializer.get());
  239. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[1,321,-65999,12324523123123,0,-3,3]}");
  240. test.setTestRepeatedInt(sint64List());
  241. result = test.serialize(serializer.get());
  242. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[]}");
  243. }
  244. TEST_F(JsonSerializationTest, RepeatedUInt64MessageTest)
  245. {
  246. RepeatedUInt64Message test;
  247. test.setTestRepeatedInt({1, 321, 0, 65999, 123245, 123245324235425234, 3});
  248. QByteArray result = test.serialize(serializer.get());
  249. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[1,321,0,65999,123245,123245324235425234,3]}");
  250. test.setTestRepeatedInt(uint64List());
  251. result = test.serialize(serializer.get());
  252. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[]}");
  253. }
  254. TEST_F(JsonSerializationTest, RepeatedFixedIntMessageTest)
  255. {
  256. RepeatedFixedIntMessage test;
  257. test.setTestRepeatedInt({1, 321, 65999, 12324523, 3, 3, 0});
  258. QByteArray result = test.serialize(serializer.get());
  259. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[1,321,65999,12324523,3,3,0]}");
  260. test.setTestRepeatedInt(fixed32List());
  261. result = test.serialize(serializer.get());
  262. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[]}");
  263. }
  264. TEST_F(JsonSerializationTest, RepeatedSFixedIntMessageTest)
  265. {
  266. RepeatedSFixedIntMessage test;
  267. test.setTestRepeatedInt({0, 1, 321, -65999, 12324523, -3, 3});
  268. QByteArray result = test.serialize(serializer.get());
  269. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[0,1,321,-65999,12324523,-3,3]}");
  270. test.setTestRepeatedInt(sfixed32List());
  271. result = test.serialize(serializer.get());
  272. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[]}");
  273. }
  274. TEST_F(JsonSerializationTest, RepeatedFixedInt64MessageTest)
  275. {
  276. RepeatedFixedInt64Message test;
  277. test.setTestRepeatedInt({1, 321, 65999, 123245324235425234, 3, 3, 0});
  278. QByteArray result = test.serialize(serializer.get());
  279. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[1,321,65999,123245324235425234,3,3,0]}");
  280. test.setTestRepeatedInt(fixed64List());
  281. result = test.serialize(serializer.get());
  282. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[]}");
  283. }
  284. TEST_F(JsonSerializationTest, RepeatedSFixedInt64MessageTest)
  285. {
  286. RepeatedSFixedInt64Message test;
  287. test.setTestRepeatedInt({1, 321, -65999, 123245324235425234, -3, 3, 0});
  288. QByteArray result = test.serialize(serializer.get());
  289. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[1,321,-65999,123245324235425234,-3,3,0]}");
  290. test.setTestRepeatedInt(sfixed64List());
  291. result = test.serialize(serializer.get());
  292. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedInt\":[]}");
  293. }
  294. TEST_F(JsonSerializationTest, RepeatedStringMessageTest)
  295. {
  296. RepeatedStringMessage test;
  297. test.setTestRepeatedString({"aaaa","bbbbb","ccc","dddddd","eeeee",""});
  298. QByteArray result = test.serialize(serializer.get());
  299. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedString\":[\"aaaa\",\"bbbbb\",\"ccc\",\"dddddd\",\"eeeee\",\"\"]}");
  300. test.setTestRepeatedString(QStringList());
  301. result = test.serialize(serializer.get());
  302. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedString\":[]}");
  303. }
  304. TEST_F(JsonSerializationTest, RepeatedDoubleMessageTest)
  305. {
  306. RepeatedDoubleMessage test;
  307. test.setTestRepeatedDouble({0.1, 0.2, 0.3, 0.4, 0.5});
  308. QByteArray result = test.serialize(serializer.get());
  309. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedDouble\":[0.1,0.2,0.3,0.4,0.5]}");
  310. test.setTestRepeatedDouble(DoubleList());
  311. result = test.serialize(serializer.get());
  312. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedDouble\":[]}");
  313. }
  314. TEST_F(JsonSerializationTest, RepeatedBytesMessageTest)
  315. {
  316. RepeatedBytesMessage test;
  317. test.setTestRepeatedBytes({QByteArray::fromHex("010203040506"),
  318. QByteArray::fromHex("ffffffff"),
  319. QByteArray::fromHex("eaeaeaeaea"),
  320. QByteArray::fromHex("010203040506")});
  321. QByteArray result = test.serialize(serializer.get());
  322. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedBytes\":[\"AQIDBAUG\",\"/////w==\",\"6urq6uo=\",\"AQIDBAUG\"]}");
  323. test.setTestRepeatedBytes(QByteArrayList());
  324. result = test.serialize(serializer.get());
  325. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedBytes\":[]}");
  326. test.setTestRepeatedBytes({QByteArray::fromHex("010203040506"),
  327. QByteArray::fromHex(""),
  328. QByteArray::fromHex("eaeaeaeaea"),
  329. QByteArray::fromHex("010203040506")});
  330. result = test.serialize(serializer.get());
  331. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedBytes\":[\"AQIDBAUG\",\"\",\"6urq6uo=\",\"AQIDBAUG\"]}");
  332. }
  333. TEST_F(JsonSerializationTest, RepeatedFloatMessageTest)
  334. {
  335. RepeatedFloatMessage test;
  336. test.setTestRepeatedFloat({0.4f, 1.2f, 0.5f, 1.4f, 0.6f});
  337. QByteArray result = test.serialize(serializer.get());
  338. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedFloat\":[0.4,1.2,0.5,1.4,0.6]}");
  339. test.setTestRepeatedFloat(FloatList());
  340. result = test.serialize(serializer.get());
  341. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedFloat\":[]}");
  342. }
  343. TEST_F(JsonSerializationTest, RepeatedComplexMessageTest)
  344. {
  345. SimpleStringMessage stringMsg;
  346. stringMsg.setTestFieldString("qwerty");
  347. QSharedPointer<ComplexMessage> msg(new ComplexMessage);
  348. msg->setTestFieldInt(25);
  349. msg->setTestComplexField(stringMsg);
  350. RepeatedComplexMessage test;
  351. test.setTestRepeatedComplex({msg, msg, msg});
  352. QByteArray result = test.serialize(serializer.get());
  353. EXPECT_TRUE(QString::fromUtf8(result).toStdString() == "{\"testRepeatedComplex\":[{\"testComplexField\":{\"testFieldString\":\"qwerty\"},\"testFieldInt\":25},{\"testComplexField\":{\"testFieldString\":\"qwerty\"},\"testFieldInt\":25},{\"testComplexField\":{\"testFieldString\":\"qwerty\"},\"testFieldInt\":25}]}"
  354. || QString::fromUtf8(result).toStdString() == "{\"testRepeatedComplex\":[{\"testFieldInt\":25,\"testComplexField\":{\"testFieldString\":\"qwerty\"}},{\"testFieldInt\":25,\"testComplexField\":{\"testFieldString\":\"qwerty\"}},{\"testFieldInt\":25,\"testComplexField\":{\"testFieldString\":\"qwerty\"}}]}");
  355. test.setTestRepeatedComplex({});
  356. result = test.serialize(serializer.get());
  357. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testRepeatedComplex\":[]}");
  358. }
  359. TEST_F(JsonSerializationTest, BoolMessageSerializeTest)
  360. {
  361. SimpleBoolMessage test;
  362. test.setTestFieldBool(true);
  363. QByteArray result = test.serialize(serializer.get());
  364. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testFieldBool\":true}");
  365. test.setTestFieldBool(false);
  366. result = test.serialize(serializer.get());
  367. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"testFieldBool\":false}");
  368. }
  369. TEST_F(JsonSerializationTest, SimpleEnumMessageSerializeTest)
  370. {
  371. SimpleEnumMessage test;
  372. test.setLocalEnum(SimpleEnumMessage::LOCAL_ENUM_VALUE2);
  373. QByteArray result = test.serialize(serializer.get());
  374. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"localEnum\":\"LOCAL_ENUM_VALUE2\"}");
  375. }
  376. TEST_F(JsonSerializationTest, SimpleEnumListMessageTest)
  377. {
  378. SimpleEnumListMessage msg;
  379. msg.setLocalEnumList({SimpleEnumListMessage::LOCAL_ENUM_VALUE0,
  380. SimpleEnumListMessage::LOCAL_ENUM_VALUE1,
  381. SimpleEnumListMessage::LOCAL_ENUM_VALUE2,
  382. SimpleEnumListMessage::LOCAL_ENUM_VALUE1,
  383. SimpleEnumListMessage::LOCAL_ENUM_VALUE2,
  384. SimpleEnumListMessage::LOCAL_ENUM_VALUE3});
  385. QByteArray result = msg.serialize(serializer.get());
  386. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"localEnumList\":[\"LOCAL_ENUM_VALUE0\",\"LOCAL_ENUM_VALUE1\",\"LOCAL_ENUM_VALUE2\",\"LOCAL_ENUM_VALUE1\",\"LOCAL_ENUM_VALUE2\",\"LOCAL_ENUM_VALUE3\"]}");
  387. msg.setLocalEnumList({});
  388. result = msg.serialize(serializer.get());
  389. EXPECT_STREQ(QString::fromUtf8(result).toStdString().c_str(), "{\"localEnumList\":[]}");
  390. }
  391. TEST_F(JsonSerializationTest, SimpleFixed32StringMapSerializeTest)
  392. {
  393. SimpleFixed32StringMapMessage test;
  394. test.setMapField({{10, {"ten"}}, {42, {"fourty two"}}, {15, {"fifteen"}}});
  395. QByteArray result = test.serialize(serializer.get());
  396. ASSERT_STREQ(result.toStdString().c_str(),
  397. "{\"mapField\":{\"10\":\"ten\",\"15\":\"fifteen\",\"42\":\"fourty two\"}}");
  398. }
  399. TEST_F(JsonSerializationTest, SimpleSFixed32StringMapSerializeTest)
  400. {
  401. SimpleSFixed32StringMapMessage test;
  402. test.setMapField({{10, {"ten"}}, {-42, {"minus fourty two"}}, {15, {"fifteen"}}});
  403. QByteArray result = test.serialize(serializer.get());
  404. ASSERT_STREQ(result.toStdString().c_str(),
  405. "{\"mapField\":{\"-42\":\"minus fourty two\",\"10\":\"ten\",\"15\":\"fifteen\"}}");
  406. }
  407. TEST_F(JsonSerializationTest, SimpleInt32StringMapSerializeTest)
  408. {
  409. SimpleInt32StringMapMessage test;
  410. test.setMapField({{-10, {"minus ten"}}, {42, {"fourty two"}}, {15, {"fifteen"}}});
  411. QByteArray result = test.serialize(serializer.get());
  412. ASSERT_STREQ(result.toStdString().c_str(),
  413. "{\"mapField\":{\"-10\":\"minus ten\",\"15\":\"fifteen\",\"42\":\"fourty two\"}}");
  414. }
  415. TEST_F(JsonSerializationTest, SimpleSInt32StringMapSerializeTest)
  416. {
  417. SimpleSInt32StringMapMessage test;
  418. test.setMapField({{10, {"ten"}}, {-42, {"minus fourty two"}}, {15, {"fifteen"}}});
  419. QByteArray result = test.serialize(serializer.get());
  420. ASSERT_STREQ(result.toStdString().c_str(),
  421. "{\"mapField\":{\"-42\":\"minus fourty two\",\"10\":\"ten\",\"15\":\"fifteen\"}}");
  422. }
  423. TEST_F(JsonSerializationTest, SimpleUInt32StringMapSerializeTest)
  424. {
  425. SimpleUInt32StringMapMessage test;
  426. test.setMapField({{10, {"ten"}}, {42, {"fourty two"}}, {15, {"fifteen"}}});
  427. QByteArray result = test.serialize(serializer.get());
  428. ASSERT_STREQ(result.toStdString().c_str(),
  429. "{\"mapField\":{\"10\":\"ten\",\"15\":\"fifteen\",\"42\":\"fourty two\"}}");
  430. }
  431. TEST_F(JsonSerializationTest, SimpleFixed64StringMapSerializeTest)
  432. {
  433. SimpleFixed64StringMapMessage test;
  434. test.setMapField({{10, {"ten"}}, {42, {"fourty two"}}, {15, {"fifteen"}}});
  435. QByteArray result = test.serialize(serializer.get());
  436. ASSERT_STREQ(result.toStdString().c_str(),
  437. "{\"mapField\":{\"10\":\"ten\",\"15\":\"fifteen\",\"42\":\"fourty two\"}}");
  438. }
  439. TEST_F(JsonSerializationTest, SimpleSFixed64StringMapSerializeTest)
  440. {
  441. SimpleSFixed64StringMapMessage test;
  442. test.setMapField({{10, {"ten"}}, {-42, {"minus fourty two"}}, {15, {"fifteen"}}});
  443. QByteArray result = test.serialize(serializer.get());
  444. ASSERT_STREQ(result.toStdString().c_str(),
  445. "{\"mapField\":{\"-42\":\"minus fourty two\",\"10\":\"ten\",\"15\":\"fifteen\"}}");
  446. }
  447. TEST_F(JsonSerializationTest, SimpleInt64StringMapSerializeTest)
  448. {
  449. SimpleInt64StringMapMessage test;
  450. test.setMapField({{-10, {"minus ten"}}, {42, {"fourty two"}}, {15, {"fifteen"}}});
  451. QByteArray result = test.serialize(serializer.get());
  452. ASSERT_STREQ(result.toStdString().c_str(),
  453. "{\"mapField\":{\"-10\":\"minus ten\",\"15\":\"fifteen\",\"42\":\"fourty two\"}}");
  454. }
  455. TEST_F(JsonSerializationTest, SimpleSInt64StringMapSerializeTest)
  456. {
  457. SimpleSInt64StringMapMessage test;
  458. test.setMapField({{10, {"ten"}}, {-42, {"minus fourty two"}}, {15, {"fifteen"}}});
  459. QByteArray result = test.serialize(serializer.get());
  460. ASSERT_STREQ(result.toStdString().c_str(),
  461. "{\"mapField\":{\"-42\":\"minus fourty two\",\"10\":\"ten\",\"15\":\"fifteen\"}}");
  462. }
  463. TEST_F(JsonSerializationTest, SimpleUInt64StringMapSerializeTest)
  464. {
  465. SimpleUInt64StringMapMessage test;
  466. test.setMapField({{10, {"ten"}}, {42, {"fourty two"}}, {15, {"fifteen"}}});
  467. QByteArray result = test.serialize(serializer.get());
  468. ASSERT_STREQ(result.toStdString().c_str(),
  469. "{\"mapField\":{\"10\":\"ten\",\"15\":\"fifteen\",\"42\":\"fourty two\"}}");
  470. }
  471. TEST_F(JsonSerializationTest, SimpleStringStringMapSerializeTest)
  472. {
  473. SimpleStringStringMapMessage test;
  474. test.setMapField({{"ben", "ten"}, {"what is the answer?", "fourty two"}, {"sweet", "fifteen"}});
  475. QByteArray result = test.serialize(serializer.get());
  476. ASSERT_STREQ(result.toStdString().c_str(),
  477. "{\"mapField\":{\"ben\":\"ten\",\"sweet\":\"fifteen\",\"what is the answer?\":\"fourty two\"}}");
  478. }
  479. }
  480. }