set.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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("StoreInteger") {
  23. _object.set("hello", 123);
  24. REQUIRE(123 == _object["hello"].as<int>());
  25. REQUIRE(_object["hello"].is<int>());
  26. REQUIRE_FALSE(_object["hello"].is<double>());
  27. }
  28. SECTION("StoreDouble") {
  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<long>());
  33. }
  34. SECTION("StoreDoubleWithDigits") {
  35. _object.set("hello", 123.45, 2);
  36. REQUIRE(123.45 == _object["hello"].as<double>());
  37. REQUIRE(_object["hello"].is<double>());
  38. REQUIRE_FALSE(_object["hello"].is<long>());
  39. }
  40. SECTION("StoreBoolean") {
  41. _object.set("hello", true);
  42. REQUIRE(_object["hello"].as<bool>());
  43. REQUIRE(_object["hello"].is<bool>());
  44. REQUIRE_FALSE(_object["hello"].is<long>());
  45. }
  46. SECTION("StoreString") {
  47. _object.set("hello", "h3110");
  48. REQUIRE(std::string("h3110") == _object["hello"].as<const char*>());
  49. REQUIRE(_object["hello"].is<const char*>());
  50. REQUIRE_FALSE(_object["hello"].is<long>());
  51. }
  52. SECTION("StoreArray") {
  53. JsonArray& arr = jb.createArray();
  54. _object.set("hello", arr);
  55. REQUIRE(&arr == &_object["hello"].as<JsonArray>());
  56. REQUIRE(_object["hello"].is<JsonArray&>());
  57. REQUIRE_FALSE(_object["hello"].is<JsonObject&>());
  58. }
  59. SECTION("StoreObject") {
  60. JsonObject& obj = jb.createObject();
  61. _object.set("hello", obj);
  62. REQUIRE(&obj == &_object["hello"].as<JsonObject>());
  63. REQUIRE(_object["hello"].is<JsonObject&>());
  64. REQUIRE_FALSE(_object["hello"].is<JsonArray&>());
  65. }
  66. SECTION("StoreArraySubscript") {
  67. JsonArray& arr = jb.createArray();
  68. arr.add(42);
  69. _object.set("a", arr[0]);
  70. REQUIRE(42 == _object["a"]);
  71. }
  72. SECTION("StoreObjectSubscript") {
  73. JsonObject& obj = jb.createObject();
  74. obj.set("x", 42);
  75. _object.set("a", obj["x"]);
  76. REQUIRE(42 == _object["a"]);
  77. }
  78. SECTION("ShouldReturnTrue_WhenAllocationSucceeds") {
  79. StaticJsonBuffer<JSON_OBJECT_SIZE(1) + 15> jsonBuffer;
  80. JsonObject& obj = jsonBuffer.createObject();
  81. REQUIRE(true == obj.set(std::string("hello"), std::string("world")));
  82. }
  83. SECTION("ShouldReturnFalse_WhenAllocationFails") {
  84. StaticJsonBuffer<JSON_OBJECT_SIZE(1) + 10> jsonBuffer;
  85. JsonObject& obj = jsonBuffer.createObject();
  86. REQUIRE(false == obj.set(std::string("hello"), std::string("world")));
  87. }
  88. }