StringWriter.cpp 1.8 KB

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