iterator.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  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. }
  29. TEST_CASE("JsonObjectConst::begin()/end()") {
  30. StaticJsonDocument<JSON_OBJECT_SIZE(2)> doc;
  31. JsonObject obj = doc.to<JsonObject>();
  32. obj["ab"] = 12;
  33. obj["cd"] = 34;
  34. JsonObjectConst cobj = obj;
  35. SECTION("NonConstIterator") {
  36. JsonObjectConst::iterator it = cobj.begin();
  37. REQUIRE(cobj.end() != it);
  38. REQUIRE(it->key() == "ab");
  39. REQUIRE(12 == it->value());
  40. ++it;
  41. REQUIRE(cobj.end() != it);
  42. REQUIRE(it->key() == "cd");
  43. REQUIRE(34 == it->value());
  44. ++it;
  45. REQUIRE(cobj.end() == it);
  46. }
  47. SECTION("Dereferencing end() is safe") {
  48. REQUIRE(cobj.end()->key().isNull());
  49. REQUIRE(cobj.end()->value().isNull());
  50. }
  51. }