StringWriter.cpp 3.7 KB

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