set.cpp 2.6 KB

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