mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-16 12:02:14 +02:00
Added tests of JsonObject::const_iterator
This commit is contained in:
@ -12,7 +12,7 @@ namespace Internals {
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
class NodeConstIterator {
|
class NodeConstIterator {
|
||||||
public:
|
public:
|
||||||
explicit NodeConstIterator(const Node<T> *node) : _node(node) {}
|
explicit NodeConstIterator(const Node<T> *node = NULL) : _node(node) {}
|
||||||
|
|
||||||
const T &operator*() const { return _node->content; }
|
const T &operator*() const { return _node->content; }
|
||||||
const T *operator->() { return &_node->content; }
|
const T *operator->() { return &_node->content; }
|
||||||
|
@ -14,7 +14,7 @@ namespace Internals {
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
class NodeIterator {
|
class NodeIterator {
|
||||||
public:
|
public:
|
||||||
explicit NodeIterator(Node<T> *node) : _node(node) {}
|
explicit NodeIterator(Node<T> *node = NULL) : _node(node) {}
|
||||||
|
|
||||||
T &operator*() const { return _node->content; }
|
T &operator*() const { return _node->content; }
|
||||||
T *operator->() { return &_node->content; }
|
T *operator->() { return &_node->content; }
|
||||||
|
@ -7,26 +7,54 @@
|
|||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
#include <ArduinoJson/JsonObject.hpp>
|
#include <ArduinoJson/JsonObject.hpp>
|
||||||
#include <ArduinoJson/StaticJsonBuffer.hpp>
|
#include <ArduinoJson/StaticJsonBuffer.hpp>
|
||||||
|
#include "Printers.hpp"
|
||||||
|
|
||||||
using namespace ArduinoJson;
|
using namespace ArduinoJson;
|
||||||
|
|
||||||
TEST(JsonObject_Iterator_Test, SimpleTest) {
|
class JsonObject_Iterator_Test : public testing::Test {
|
||||||
StaticJsonBuffer<256> jsonBuffer;
|
public:
|
||||||
|
JsonObject_Iterator_Test() : object(_buffer.createObject()) {
|
||||||
JsonObject &object = jsonBuffer.createObject();
|
|
||||||
object["ab"] = 12;
|
object["ab"] = 12;
|
||||||
object["cd"] = 34;
|
object["cd"] = 34;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
StaticJsonBuffer<256> _buffer;
|
||||||
|
JsonObject& object;
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST_F(JsonObject_Iterator_Test, NonConstIterator) {
|
||||||
JsonObject::iterator it = object.begin();
|
JsonObject::iterator it = object.begin();
|
||||||
JsonObject::iterator end = object.end();
|
ASSERT_NE(object.end(), it);
|
||||||
|
|
||||||
ASSERT_NE(end, it);
|
|
||||||
EXPECT_STREQ("ab", it->key);
|
EXPECT_STREQ("ab", it->key);
|
||||||
EXPECT_EQ(12, it->value.as<int>());
|
EXPECT_EQ(12, it->value);
|
||||||
|
it->key = "a.b";
|
||||||
|
it->value = 1.2;
|
||||||
++it;
|
++it;
|
||||||
ASSERT_NE(end, it);
|
ASSERT_NE(object.end(), it);
|
||||||
EXPECT_STREQ("cd", it->key);
|
EXPECT_STREQ("cd", it->key);
|
||||||
EXPECT_EQ(34, it->value.as<int>());
|
EXPECT_EQ(34, it->value);
|
||||||
|
it->key = "c.d";
|
||||||
|
it->value = 3.4;
|
||||||
++it;
|
++it;
|
||||||
EXPECT_EQ(object.end(), it);
|
ASSERT_EQ(object.end(), it);
|
||||||
|
|
||||||
|
ASSERT_EQ(2, object.size());
|
||||||
|
EXPECT_EQ(1.2, object["a.b"]);
|
||||||
|
EXPECT_EQ(3.4, object["c.d"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(JsonObject_Iterator_Test, ConstIterator) {
|
||||||
|
const JsonObject& const_object = object;
|
||||||
|
JsonObject::const_iterator it = const_object.begin();
|
||||||
|
|
||||||
|
ASSERT_NE(const_object.end(), it);
|
||||||
|
EXPECT_STREQ("ab", it->key);
|
||||||
|
EXPECT_EQ(12, it->value);
|
||||||
|
++it;
|
||||||
|
ASSERT_NE(const_object.end(), it);
|
||||||
|
EXPECT_STREQ("cd", it->key);
|
||||||
|
EXPECT_EQ(34, it->value);
|
||||||
|
++it;
|
||||||
|
ASSERT_EQ(const_object.end(), it);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user