set.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #define ARDUINOJSON_ENABLE_ARDUINO_STRING 1
  2. #define ARDUINOJSON_ENABLE_PROGMEM 1
  3. #include <ArduinoJson.h>
  4. #include <catch.hpp>
  5. #include "Allocators.hpp"
  6. #include "Literals.hpp"
  7. TEST_CASE("JsonDocument::set()") {
  8. SpyingAllocator spy;
  9. JsonDocument doc(&spy);
  10. SECTION("integer") {
  11. doc.set(42);
  12. REQUIRE(doc.as<std::string>() == "42");
  13. REQUIRE(spy.log() == AllocatorLog{});
  14. }
  15. SECTION("const char*") {
  16. doc.set("example");
  17. REQUIRE(doc.as<const char*>() == "example"_s);
  18. REQUIRE(spy.log() == AllocatorLog{});
  19. }
  20. SECTION("std::string") {
  21. doc.set("example"_s);
  22. REQUIRE(doc.as<const char*>() == "example"_s);
  23. REQUIRE(spy.log() == AllocatorLog{
  24. Allocate(sizeofString("example")),
  25. });
  26. }
  27. SECTION("char*") {
  28. char value[] = "example";
  29. doc.set(value);
  30. REQUIRE(doc.as<const char*>() == "example"_s);
  31. REQUIRE(spy.log() == AllocatorLog{
  32. Allocate(sizeofString("example")),
  33. });
  34. }
  35. SECTION("Arduino String") {
  36. doc.set(String("example"));
  37. REQUIRE(doc.as<const char*>() == "example"_s);
  38. REQUIRE(spy.log() == AllocatorLog{
  39. Allocate(sizeofString("example")),
  40. });
  41. }
  42. SECTION("Flash string") {
  43. doc.set(F("example"));
  44. REQUIRE(doc.as<const char*>() == "example"_s);
  45. REQUIRE(spy.log() == AllocatorLog{
  46. Allocate(sizeofString("example")),
  47. });
  48. }
  49. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  50. SECTION("VLA") {
  51. size_t i = 16;
  52. char vla[i];
  53. strcpy(vla, "example");
  54. doc.set(vla);
  55. REQUIRE(doc.as<const char*>() == "example"_s);
  56. REQUIRE(spy.log() == AllocatorLog{
  57. Allocate(sizeofString("example")),
  58. });
  59. }
  60. #endif
  61. }