StringWriter.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2020
  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& sb, const char* s) {
  12. return sb.write(reinterpret_cast<const uint8_t*>(s), strlen(s));
  13. }
  14. template <typename StringWriter, typename String>
  15. void common_tests(StringWriter& sb, const String& output) {
  16. SECTION("InitialState") {
  17. REQUIRE(std::string("") == output);
  18. }
  19. SECTION("EmptyString") {
  20. REQUIRE(0 == print(sb, ""));
  21. REQUIRE(std::string("") == output);
  22. }
  23. SECTION("OneString") {
  24. REQUIRE(4 == print(sb, "ABCD"));
  25. REQUIRE(std::string("ABCD") == output);
  26. }
  27. SECTION("TwoStrings") {
  28. REQUIRE(4 == print(sb, "ABCD"));
  29. REQUIRE(4 == print(sb, "EFGH"));
  30. REQUIRE(std::string("ABCDEFGH") == output);
  31. }
  32. }
  33. TEST_CASE("StaticStringWriter") {
  34. char output[20];
  35. StaticStringWriter sb(output, sizeof(output));
  36. common_tests(sb, static_cast<const char*>(output));
  37. SECTION("OverCapacity") {
  38. REQUIRE(19 == print(sb, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
  39. REQUIRE(0 == print(sb, "ABC"));
  40. REQUIRE(std::string("ABCDEFGHIJKLMNOPQRS") == output);
  41. }
  42. }
  43. TEST_CASE("Writer<std::string>") {
  44. std::string output;
  45. Writer<std::string> sb(output);
  46. common_tests(sb, output);
  47. }
  48. TEST_CASE("Writer<String>") {
  49. ::String output;
  50. Writer< ::String> sb(output);
  51. common_tests(sb, output);
  52. SECTION("Writes characters to temporary buffer") {
  53. // accumulate in buffer
  54. sb.write('a');
  55. sb.write('b');
  56. sb.write('c');
  57. REQUIRE(output == "");
  58. // flush when full
  59. sb.write('d');
  60. REQUIRE(output == "abcd");
  61. // flush on destruction
  62. sb.write('e');
  63. sb.~Writer();
  64. REQUIRE(output == "abcde");
  65. }
  66. SECTION("Writes strings to temporary buffer") {
  67. // accumulate in buffer
  68. print(sb, "abc");
  69. REQUIRE(output == "");
  70. // flush when full, and continue to accumulate
  71. print(sb, "de");
  72. REQUIRE(output == "abcd");
  73. // flush on destruction
  74. sb.~Writer();
  75. REQUIRE(output == "abcde");
  76. }
  77. }
  78. TEST_CASE("Writer<custom_string>") {
  79. custom_string output;
  80. Writer<custom_string> sb(output);
  81. REQUIRE(4 == print(sb, "ABCD"));
  82. REQUIRE("ABCD" == output);
  83. }
  84. TEST_CASE("IsWriteableString") {
  85. SECTION("std::string") {
  86. REQUIRE(IsWriteableString<std::string>::value == true);
  87. }
  88. SECTION("custom_string") {
  89. REQUIRE(IsWriteableString<custom_string>::value == true);
  90. }
  91. SECTION("basic_string<wchar_t>") {
  92. REQUIRE(IsWriteableString<std::basic_string<wchar_t> >::value == false);
  93. }
  94. }