StringBuilder.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2023
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. using namespace ArduinoJson::Internals;
  7. template <typename StringBuilder, typename String>
  8. void common_tests(StringBuilder& sb, const String& output) {
  9. SECTION("InitialState") {
  10. REQUIRE(std::string("") == output);
  11. }
  12. SECTION("EmptyString") {
  13. REQUIRE(0 == sb.print(""));
  14. REQUIRE(std::string("") == output);
  15. }
  16. SECTION("OneString") {
  17. REQUIRE(4 == sb.print("ABCD"));
  18. REQUIRE(std::string("ABCD") == output);
  19. }
  20. SECTION("TwoStrings") {
  21. REQUIRE(4 == sb.print("ABCD"));
  22. REQUIRE(4 == sb.print("EFGH"));
  23. REQUIRE(std::string("ABCDEFGH") == output);
  24. }
  25. }
  26. TEST_CASE("StaticStringBuilder") {
  27. char output[20];
  28. StaticStringBuilder sb(output, sizeof(output));
  29. common_tests(sb, static_cast<const char*>(output));
  30. SECTION("OverCapacity") {
  31. REQUIRE(19 == sb.print("ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
  32. REQUIRE(0 == sb.print("ABC"));
  33. REQUIRE(std::string("ABCDEFGHIJKLMNOPQRS") == output);
  34. }
  35. }
  36. TEST_CASE("DynamicStringBuilder") {
  37. std::string output;
  38. DynamicStringBuilder<std::string> sb(output);
  39. common_tests(sb, output);
  40. }