StringWriter.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2021
  3. // MIT License
  4. #define ARDUINOJSON_ENABLE_ARDUINO_STRING 1
  5. #define ARDUINOJSON_STRING_BUFFER_SIZE 5
  6. #include <ArduinoJson.h>
  7. #include <catch.hpp>
  8. #include "custom_string.hpp"
  9. using namespace ARDUINOJSON_NAMESPACE;
  10. template <typename StringWriter>
  11. static size_t print(StringWriter& writer, const char* s) {
  12. return writer.write(reinterpret_cast<const uint8_t*>(s), strlen(s));
  13. }
  14. template <typename StringWriter, typename String>
  15. void common_tests(StringWriter& writer, const String& output) {
  16. SECTION("InitialState") {
  17. REQUIRE(std::string("") == output);
  18. }
  19. SECTION("EmptyString") {
  20. REQUIRE(0 == print(writer, ""));
  21. REQUIRE(std::string("") == output);
  22. }
  23. SECTION("OneString") {
  24. REQUIRE(4 == print(writer, "ABCD"));
  25. REQUIRE(std::string("ABCD") == output);
  26. }
  27. SECTION("TwoStrings") {
  28. REQUIRE(4 == print(writer, "ABCD"));
  29. REQUIRE(4 == print(writer, "EFGH"));
  30. REQUIRE(std::string("ABCDEFGH") == output);
  31. }
  32. }
  33. TEST_CASE("StaticStringWriter") {
  34. char output[20] = {0};
  35. StaticStringWriter writer(output, sizeof(output));
  36. common_tests(writer, static_cast<const char*>(output));
  37. SECTION("OverCapacity") {
  38. REQUIRE(20 == print(writer, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
  39. REQUIRE(0 == print(writer, "ABC"));
  40. REQUIRE("ABCDEFGHIJKLMNOPQRST" == std::string(output, 20));
  41. }
  42. }
  43. TEST_CASE("Writer<std::string>") {
  44. std::string output;
  45. Writer<std::string> writer(output);
  46. common_tests(writer, output);
  47. }
  48. TEST_CASE("Writer<String>") {
  49. ::String output;
  50. Writer< ::String> writer(output);
  51. SECTION("write(char)") {
  52. SECTION("writes to temporary buffer") {
  53. // accumulate in buffer
  54. writer.write('a');
  55. writer.write('b');
  56. writer.write('c');
  57. writer.write('d');
  58. REQUIRE(output == "");
  59. // flush when full
  60. writer.write('e');
  61. REQUIRE(output == "abcd");
  62. // flush on destruction
  63. writer.write('f');
  64. writer.~Writer();
  65. REQUIRE(output == "abcdef");
  66. }
  67. SECTION("returns 1 on success") {
  68. for (int i = 0; i < ARDUINOJSON_STRING_BUFFER_SIZE; i++) {
  69. REQUIRE(writer.write('x') == 1);
  70. }
  71. }
  72. SECTION("returns 0 on error") {
  73. output.limitCapacityTo(1);
  74. REQUIRE(writer.write('a') == 1);
  75. REQUIRE(writer.write('b') == 1);
  76. REQUIRE(writer.write('c') == 1);
  77. REQUIRE(writer.write('d') == 1);
  78. REQUIRE(writer.write('e') == 0);
  79. REQUIRE(writer.write('f') == 0);
  80. }
  81. }
  82. SECTION("write(char*, size_t)") {
  83. SECTION("empty string") {
  84. REQUIRE(0 == print(writer, ""));
  85. writer.flush();
  86. REQUIRE(output == "");
  87. }
  88. SECTION("writes to temporary buffer") {
  89. // accumulate in buffer
  90. print(writer, "abc");
  91. REQUIRE(output == "");
  92. // flush when full, and continue to accumulate
  93. print(writer, "de");
  94. REQUIRE(output == "abcd");
  95. // flush on destruction
  96. writer.~Writer();
  97. REQUIRE(output == "abcde");
  98. }
  99. }
  100. }
  101. TEST_CASE("Writer<custom_string>") {
  102. custom_string output;
  103. Writer<custom_string> writer(output);
  104. REQUIRE(4 == print(writer, "ABCD"));
  105. REQUIRE("ABCD" == output);
  106. }
  107. TEST_CASE("IsWriteableString") {
  108. SECTION("std::string") {
  109. REQUIRE(IsWriteableString<std::string>::value == true);
  110. }
  111. SECTION("custom_string") {
  112. REQUIRE(IsWriteableString<custom_string>::value == true);
  113. }
  114. SECTION("basic_string<wchar_t>") {
  115. REQUIRE(IsWriteableString<std::basic_string<wchar_t> >::value == false);
  116. }
  117. }
  118. TEST_CASE("serializeJson(doc, String)") {
  119. StaticJsonDocument<1024> doc;
  120. doc["hello"] = "world";
  121. ::String output;
  122. SECTION("sufficient capacity") {
  123. serializeJson(doc, output);
  124. REQUIRE(output == "{\"hello\":\"world\"}");
  125. }
  126. SECTION("unsufficient capacity") { // issue #1561
  127. output.limitCapacityTo(10);
  128. serializeJson(doc, output);
  129. REQUIRE(output == "{\"hello\"");
  130. }
  131. }