iterator.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. using namespace Catch::Matchers;
  7. TEST_CASE("JsonObject::begin()/end()") {
  8. StaticJsonDocument<JSON_OBJECT_SIZE(2)> doc;
  9. JsonObject obj = doc.to<JsonObject>();
  10. obj["ab"] = 12;
  11. obj["cd"] = 34;
  12. SECTION("NonConstIterator") {
  13. JsonObject::iterator it = obj.begin();
  14. REQUIRE(obj.end() != it);
  15. REQUIRE(it->key() == "ab");
  16. REQUIRE(12 == it->value());
  17. ++it;
  18. REQUIRE(obj.end() != it);
  19. REQUIRE(it->key() == "cd");
  20. REQUIRE(34 == it->value());
  21. ++it;
  22. REQUIRE(obj.end() == it);
  23. }
  24. SECTION("Dereferencing end() is safe") {
  25. REQUIRE(obj.end()->key().isNull());
  26. REQUIRE(obj.end()->value().isNull());
  27. }
  28. SECTION("null JsonObject") {
  29. JsonObject null;
  30. REQUIRE(null.begin() == null.end());
  31. }
  32. }
  33. TEST_CASE("JsonObjectConst::begin()/end()") {
  34. StaticJsonDocument<JSON_OBJECT_SIZE(2)> doc;
  35. JsonObject obj = doc.to<JsonObject>();
  36. obj["ab"] = 12;
  37. obj["cd"] = 34;
  38. JsonObjectConst cobj = obj;
  39. SECTION("Iteration") {
  40. JsonObjectConst::iterator it = cobj.begin();
  41. REQUIRE(cobj.end() != it);
  42. REQUIRE(it->key() == "ab");
  43. REQUIRE(12 == it->value());
  44. ++it;
  45. REQUIRE(cobj.end() != it);
  46. JsonPairConst pair = *it;
  47. REQUIRE(pair.key() == "cd");
  48. REQUIRE(34 == pair.value());
  49. ++it;
  50. REQUIRE(cobj.end() == it);
  51. }
  52. SECTION("Dereferencing end() is safe") {
  53. REQUIRE(cobj.end()->key().isNull());
  54. REQUIRE(cobj.end()->value().isNull());
  55. }
  56. SECTION("null JsonObjectConst") {
  57. JsonObjectConst null;
  58. REQUIRE(null.begin() == null.end());
  59. }
  60. }