JsonObject_Iterator_Tests.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright Benoit Blanchon 2014
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://github.com/bblanchon/ArduinoJson
  6. #include <gtest/gtest.h>
  7. #include <ArduinoJson/JsonObject.hpp>
  8. #include <ArduinoJson/StaticJsonBuffer.hpp>
  9. #include "Printers.hpp"
  10. using namespace ArduinoJson;
  11. class JsonObject_Iterator_Test : public testing::Test {
  12. public:
  13. JsonObject_Iterator_Test() : object(_buffer.createObject()) {
  14. object["ab"] = 12;
  15. object["cd"] = 34;
  16. }
  17. protected:
  18. StaticJsonBuffer<256> _buffer;
  19. JsonObject& object;
  20. };
  21. TEST_F(JsonObject_Iterator_Test, NonConstIterator) {
  22. JsonObject::iterator it = object.begin();
  23. ASSERT_NE(object.end(), it);
  24. EXPECT_STREQ("ab", it->key);
  25. EXPECT_EQ(12, it->value);
  26. it->key = "a.b";
  27. it->value = 1.2;
  28. ++it;
  29. ASSERT_NE(object.end(), it);
  30. EXPECT_STREQ("cd", it->key);
  31. EXPECT_EQ(34, it->value);
  32. it->key = "c.d";
  33. it->value = 3.4;
  34. ++it;
  35. ASSERT_EQ(object.end(), it);
  36. ASSERT_EQ(2, object.size());
  37. EXPECT_EQ(1.2, object["a.b"]);
  38. EXPECT_EQ(3.4, object["c.d"]);
  39. }
  40. TEST_F(JsonObject_Iterator_Test, ConstIterator) {
  41. const JsonObject& const_object = object;
  42. JsonObject::const_iterator it = const_object.begin();
  43. ASSERT_NE(const_object.end(), it);
  44. EXPECT_STREQ("ab", it->key);
  45. EXPECT_EQ(12, it->value);
  46. ++it;
  47. ASSERT_NE(const_object.end(), it);
  48. EXPECT_STREQ("cd", it->key);
  49. EXPECT_EQ(34, it->value);
  50. ++it;
  51. ASSERT_EQ(const_object.end(), it);
  52. }