2014-10-23 23:39:22 +02:00
|
|
|
// Copyright Benoit Blanchon 2014
|
|
|
|
// MIT License
|
|
|
|
//
|
|
|
|
// Arduino JSON library
|
|
|
|
// https://github.com/bblanchon/ArduinoJson
|
|
|
|
|
2014-10-16 16:23:24 +02:00
|
|
|
#include <gtest/gtest.h>
|
2014-10-19 15:46:36 +02:00
|
|
|
#include <ArduinoJson/StaticJsonBuffer.hpp>
|
|
|
|
#include <ArduinoJson/JsonValue.hpp>
|
2014-10-24 18:31:50 +02:00
|
|
|
#include "Printers.hpp"
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-18 23:05:54 +02:00
|
|
|
using namespace ArduinoJson;
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
class JsonObject_Container_Tests : public ::testing::Test {
|
2014-10-23 23:13:13 +02:00
|
|
|
protected:
|
2014-10-23 19:54:00 +02:00
|
|
|
virtual void SetUp() { object = json.createObject(); }
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
StaticJsonBuffer<42> json;
|
|
|
|
JsonObject object;
|
2014-10-16 16:23:24 +02:00
|
|
|
};
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_Container_Tests, InitialSizeIsZero) {
|
|
|
|
EXPECT_EQ(0, object.size());
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_Container_Tests, Grow_WhenValuesAreAdded) {
|
|
|
|
object["hello"];
|
|
|
|
EXPECT_EQ(1, object.size());
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
object["world"];
|
|
|
|
EXPECT_EQ(2, object.size());
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_Container_Tests, DoNotGrow_WhenSameValueIsAdded) {
|
|
|
|
object["hello"];
|
|
|
|
EXPECT_EQ(1, object.size());
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
object["hello"];
|
|
|
|
EXPECT_EQ(1, object.size());
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_Container_Tests, Shrink_WhenValuesAreRemoved) {
|
|
|
|
object["hello"];
|
|
|
|
object["world"];
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
object.remove("hello");
|
|
|
|
EXPECT_EQ(1, object.size());
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
object.remove("world");
|
|
|
|
EXPECT_EQ(0, object.size());
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_Container_Tests,
|
|
|
|
DoNotShrink_WhenRemoveIsCalledWithAWrongKey) {
|
|
|
|
object["hello"];
|
|
|
|
object["world"];
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
object.remove(":-P");
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
EXPECT_EQ(2, object.size());
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_Container_Tests, CanStoreIntegers) {
|
|
|
|
object["hello"] = 123;
|
|
|
|
object["world"] = 456;
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-24 00:08:25 +02:00
|
|
|
EXPECT_EQ(123, object["hello"].as<int>());
|
|
|
|
EXPECT_EQ(456, object["world"].as<int>());
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_Container_Tests, CanStoreDoubles) {
|
|
|
|
object["hello"] = 123.45;
|
|
|
|
object["world"] = 456.78;
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-24 00:08:25 +02:00
|
|
|
EXPECT_EQ(123.45, object["hello"].as<double>());
|
|
|
|
EXPECT_EQ(456.78, object["world"].as<double>());
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_Container_Tests, CanStoreBooleans) {
|
|
|
|
object["hello"] = true;
|
|
|
|
object["world"] = false;
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-24 00:08:25 +02:00
|
|
|
EXPECT_TRUE(object["hello"].as<bool>());
|
|
|
|
EXPECT_FALSE(object["world"].as<bool>());
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_Container_Tests, CanStoreStrings) {
|
|
|
|
object["hello"] = "h3110";
|
|
|
|
object["world"] = "w0r1d";
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-24 00:08:25 +02:00
|
|
|
EXPECT_STREQ("h3110", object["hello"].as<const char*>());
|
|
|
|
EXPECT_STREQ("w0r1d", object["world"].as<const char*>());
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_Container_Tests, CanStoreInnerArrays) {
|
|
|
|
JsonArray innerarray1 = json.createArray();
|
|
|
|
JsonArray innerarray2 = json.createArray();
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
object["hello"] = innerarray1;
|
|
|
|
object["world"] = innerarray2;
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-24 00:08:25 +02:00
|
|
|
EXPECT_EQ(innerarray1, object["hello"].as<JsonArray>());
|
|
|
|
EXPECT_EQ(innerarray2, object["world"].as<JsonArray>());
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_Container_Tests, CanStoreInnerObjects) {
|
|
|
|
JsonObject innerObject1 = json.createObject();
|
|
|
|
JsonObject innerObject2 = json.createObject();
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
object["hello"] = innerObject1;
|
|
|
|
object["world"] = innerObject2;
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-24 00:08:25 +02:00
|
|
|
EXPECT_EQ(innerObject1, object["hello"].as<JsonObject>());
|
|
|
|
EXPECT_EQ(innerObject2, object["world"].as<JsonObject>());
|
2014-10-23 23:45:36 +02:00
|
|
|
}
|