StringWriter.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2020
  3. // MIT License
  4. #define ARDUINOJSON_ENABLE_ARDUINO_STRING 1
  5. #include <ArduinoJson.h>
  6. #include <catch.hpp>
  7. #include "custom_string.hpp"
  8. using namespace ARDUINOJSON_NAMESPACE;
  9. template <typename StringWriter>
  10. static size_t print(StringWriter& sb, const char* s) {
  11. return sb.write(reinterpret_cast<const uint8_t*>(s), strlen(s));
  12. }
  13. template <typename StringWriter, typename String>
  14. void common_tests(StringWriter& sb, const String& output) {
  15. SECTION("InitialState") {
  16. REQUIRE(std::string("") == output);
  17. }
  18. SECTION("EmptyString") {
  19. REQUIRE(0 == print(sb, ""));
  20. REQUIRE(std::string("") == output);
  21. }
  22. SECTION("OneString") {
  23. REQUIRE(4 == print(sb, "ABCD"));
  24. REQUIRE(std::string("ABCD") == output);
  25. }
  26. SECTION("TwoStrings") {
  27. REQUIRE(4 == print(sb, "ABCD"));
  28. REQUIRE(4 == print(sb, "EFGH"));
  29. REQUIRE(std::string("ABCDEFGH") == output);
  30. }
  31. }
  32. TEST_CASE("StaticStringWriter") {
  33. char output[20];
  34. StaticStringWriter sb(output, sizeof(output));
  35. common_tests(sb, static_cast<const char*>(output));
  36. SECTION("OverCapacity") {
  37. REQUIRE(19 == print(sb, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
  38. REQUIRE(0 == print(sb, "ABC"));
  39. REQUIRE(std::string("ABCDEFGHIJKLMNOPQRS") == output);
  40. }
  41. }
  42. TEST_CASE("Writer<std::string>") {
  43. std::string output;
  44. Writer<std::string> sb(output);
  45. common_tests(sb, output);
  46. }
  47. TEST_CASE("Writer<String>") {
  48. ::String output;
  49. Writer< ::String> sb(output);
  50. common_tests(sb, output);
  51. }
  52. TEST_CASE("Writer<custom_string>") {
  53. custom_string output;
  54. Writer<custom_string> sb(output);
  55. REQUIRE(4 == print(sb, "ABCD"));
  56. REQUIRE("ABCD" == output);
  57. }
  58. TEST_CASE("IsWriteableString") {
  59. SECTION("std::string") {
  60. REQUIRE(IsWriteableString<std::string>::value == true);
  61. }
  62. SECTION("custom_string") {
  63. REQUIRE(IsWriteableString<custom_string>::value == true);
  64. }
  65. SECTION("basic_string<wchar_t>") {
  66. REQUIRE(IsWriteableString<std::basic_string<wchar_t> >::value == false);
  67. }
  68. }