Files
ArduinoJson/test/JsonObject_Container_Tests.cpp

122 lines
3.0 KiB
C++
Raw Normal View History

2015-02-07 16:05:48 +01:00
// Copyright Benoit Blanchon 2014-2015
2014-10-23 23:39:22 +02:00
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
2014-10-16 16:23:24 +02:00
#include <gtest/gtest.h>
#include <ArduinoJson.h>
2014-10-18 23:05:54 +02:00
2014-10-23 19:54:00 +02:00
class JsonObject_Container_Tests : public ::testing::Test {
2014-10-30 10:49:02 +01:00
public:
JsonObject_Container_Tests() : _object(_jsonBuffer.createObject()) {}
2014-10-16 16:23:24 +02:00
2014-10-30 10:49:02 +01:00
protected:
DynamicJsonBuffer _jsonBuffer;
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"] = 1;
EXPECT_EQ(1, _object.size());
2014-10-16 16:23:24 +02:00
_object.set("world", 2);
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"] = 1;
EXPECT_EQ(1, _object.size());
2014-10-16 16:23:24 +02:00
_object["hello"] = 2;
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"] = 1;
_object["world"] = 2;
2014-10-16 16:23:24 +02:00
_object.remove("hello");
EXPECT_EQ(1, _object.size());
2014-10-16 16:23:24 +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"] = 1;
_object["world"] = 2;
2014-10-16 16:23:24 +02:00
_object.remove(":-P");
2014-10-16 16:23:24 +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.set("world", 456);
2014-10-16 16:23:24 +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.set("world", 456.78);
2014-10-16 16:23:24 +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.set("world", false);
2014-10-16 16:23:24 +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.set("world", "w0r1d");
2014-10-16 16:23:24 +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
}
TEST_F(JsonObject_Container_Tests, CanStoreArrays) {
JsonArray& array1 = _jsonBuffer.createArray();
JsonArray& array2 = _jsonBuffer.createArray();
2014-10-16 16:23:24 +02:00
_object["hello"] = array1;
_object.set("world", array2);
2014-10-16 16:23:24 +02:00
EXPECT_EQ(&array1, &_object["hello"].asArray());
EXPECT_EQ(&array2, &_object["world"].asArray());
2014-10-16 16:23:24 +02:00
}
TEST_F(JsonObject_Container_Tests, CanStoreObjects) {
JsonObject& object1 = _jsonBuffer.createObject();
JsonObject& object2 = _jsonBuffer.createObject();
2014-10-16 16:23:24 +02:00
_object["hello"] = object1;
_object.set("world", object2);
2014-10-16 16:23:24 +02:00
EXPECT_EQ(&object1, &_object["hello"].asObject());
EXPECT_EQ(&object2, &_object["world"].asObject());
2014-10-23 23:45:36 +02:00
}
2014-11-07 17:27:30 +01:00
TEST_F(JsonObject_Container_Tests, ContainsKeyReturnsFalseForNonExistingKey) {
EXPECT_FALSE(_object.containsKey("hello"));
2014-11-07 17:27:30 +01:00
}
TEST_F(JsonObject_Container_Tests, ContainsKeyReturnsTrueForDefinedValue) {
_object.set("hello", 42);
EXPECT_TRUE(_object.containsKey("hello"));
2014-11-07 17:27:30 +01:00
}