StringBuilder.cpp 1.3 KB

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