add.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. TEST_CASE("JsonArray::add()") {
  10. DynamicJsonBuffer _jsonBuffer;
  11. JsonArray& _array = _jsonBuffer.createArray();
  12. SECTION("SizeIncreased_WhenValuesAreAdded") {
  13. _array.add("hello");
  14. REQUIRE(1U == _array.size());
  15. }
  16. SECTION("StoreInteger") {
  17. _array.add(123);
  18. REQUIRE(123 == _array[0].as<int>());
  19. REQUIRE(_array[0].is<int>());
  20. REQUIRE_FALSE(_array[0].is<double>());
  21. }
  22. SECTION("StoreDouble") {
  23. _array.add(123.45);
  24. REQUIRE(123.45 == _array[0].as<double>());
  25. REQUIRE(_array[0].is<double>());
  26. REQUIRE_FALSE(_array[0].is<int>());
  27. }
  28. SECTION("StoreBoolean") {
  29. _array.add(true);
  30. REQUIRE(true == _array[0].as<bool>());
  31. REQUIRE(_array[0].is<bool>());
  32. REQUIRE_FALSE(_array[0].is<int>());
  33. }
  34. SECTION("StoreString") {
  35. const char* str = "hello";
  36. _array.add(str);
  37. REQUIRE(str == _array[0].as<const char*>());
  38. REQUIRE(_array[0].is<const char*>());
  39. REQUIRE_FALSE(_array[0].is<int>());
  40. }
  41. SECTION("StoreNestedArray") {
  42. JsonArray& arr = _jsonBuffer.createArray();
  43. _array.add(arr);
  44. REQUIRE(&arr == &_array[0].as<JsonArray&>());
  45. REQUIRE(_array[0].is<JsonArray&>());
  46. REQUIRE_FALSE(_array[0].is<int>());
  47. }
  48. SECTION("StoreNestedObject") {
  49. JsonObject& obj = _jsonBuffer.createObject();
  50. _array.add(obj);
  51. REQUIRE(&obj == &_array[0].as<JsonObject&>());
  52. REQUIRE(_array[0].is<JsonObject&>());
  53. REQUIRE_FALSE(_array[0].is<int>());
  54. }
  55. SECTION("StoreArraySubscript") {
  56. const char* str = "hello";
  57. JsonArray& arr = _jsonBuffer.createArray();
  58. arr.add(str);
  59. _array.add(arr[0]);
  60. REQUIRE(str == _array[0]);
  61. }
  62. SECTION("StoreObjectSubscript") {
  63. const char* str = "hello";
  64. JsonObject& obj = _jsonBuffer.createObject();
  65. obj["x"] = str;
  66. _array.add(obj["x"]);
  67. REQUIRE(str == _array[0]);
  68. }
  69. }