set.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #include <string>
  10. TEST_CASE("JsonObject::set()") {
  11. DynamicJsonBuffer jb;
  12. JsonObject& _object = jb.createObject();
  13. SECTION("SizeIncreased_WhenValuesAreAdded") {
  14. _object.set("hello", 42);
  15. REQUIRE(1 == _object.size());
  16. }
  17. SECTION("SizeUntouched_WhenSameValueIsAdded") {
  18. _object["hello"] = 1;
  19. _object["hello"] = 2;
  20. REQUIRE(1 == _object.size());
  21. }
  22. SECTION("int") {
  23. _object.set("hello", 123);
  24. REQUIRE(123 == _object["hello"].as<int>());
  25. REQUIRE(_object["hello"].is<int>());
  26. REQUIRE_FALSE(_object["hello"].is<bool>());
  27. }
  28. SECTION("double") {
  29. _object.set("hello", 123.45);
  30. REQUIRE(123.45 == _object["hello"].as<double>());
  31. REQUIRE(_object["hello"].is<double>());
  32. REQUIRE_FALSE(_object["hello"].is<bool>());
  33. }
  34. SECTION("bool") {
  35. _object.set("hello", true);
  36. REQUIRE(_object["hello"].as<bool>());
  37. REQUIRE(_object["hello"].is<bool>());
  38. REQUIRE_FALSE(_object["hello"].is<long>());
  39. }
  40. SECTION("const char*") {
  41. _object.set("hello", "h3110");
  42. REQUIRE(std::string("h3110") == _object["hello"].as<const char*>());
  43. REQUIRE(_object["hello"].is<const char*>());
  44. REQUIRE_FALSE(_object["hello"].is<long>());
  45. }
  46. SECTION("nested array") {
  47. JsonArray& arr = jb.createArray();
  48. _object.set("hello", arr);
  49. REQUIRE(&arr == &_object["hello"].as<JsonArray>());
  50. REQUIRE(_object["hello"].is<JsonArray&>());
  51. REQUIRE_FALSE(_object["hello"].is<JsonObject&>());
  52. }
  53. SECTION("nested object") {
  54. JsonObject& obj = jb.createObject();
  55. _object.set("hello", obj);
  56. REQUIRE(&obj == &_object["hello"].as<JsonObject>());
  57. REQUIRE(_object["hello"].is<JsonObject&>());
  58. REQUIRE_FALSE(_object["hello"].is<JsonArray&>());
  59. }
  60. SECTION("array subscript") {
  61. JsonArray& arr = jb.createArray();
  62. arr.add(42);
  63. _object.set("a", arr[0]);
  64. REQUIRE(42 == _object["a"]);
  65. }
  66. SECTION("object subscript") {
  67. JsonObject& obj = jb.createObject();
  68. obj.set("x", 42);
  69. _object.set("a", obj["x"]);
  70. REQUIRE(42 == _object["a"]);
  71. }
  72. SECTION("ShouldReturnTrue_WhenAllocationSucceeds") {
  73. StaticJsonBuffer<JSON_OBJECT_SIZE(1) + 15> jsonBuffer;
  74. JsonObject& obj = jsonBuffer.createObject();
  75. REQUIRE(true == obj.set(std::string("hello"), std::string("world")));
  76. }
  77. SECTION("ShouldReturnFalse_WhenAllocationFails") {
  78. StaticJsonBuffer<JSON_OBJECT_SIZE(1) + 10> jsonBuffer;
  79. JsonObject& obj = jsonBuffer.createObject();
  80. REQUIRE(false == obj.set(std::string("hello"), std::string("world")));
  81. }
  82. }