Use DynamicJsonBuffer instead of arbitrary sized StaticJsonBuffer

This commit is contained in:
Benoit Blanchon
2014-12-20 15:42:43 +01:00
parent d855b0f98c
commit aef7e43c48
16 changed files with 228 additions and 228 deletions

View File

@ -9,118 +9,118 @@
class JsonObject_Container_Tests : public ::testing::Test {
public:
JsonObject_Container_Tests() : object(json.createObject()) {}
JsonObject_Container_Tests() : _object(_jsonBuffer.createObject()) {}
protected:
StaticJsonBuffer<256> json;
JsonObject& object;
DynamicJsonBuffer _jsonBuffer;
JsonObject& _object;
};
TEST_F(JsonObject_Container_Tests, InitialSizeIsZero) {
EXPECT_EQ(0, object.size());
EXPECT_EQ(0, _object.size());
}
TEST_F(JsonObject_Container_Tests, Grow_WhenValuesAreAdded) {
object["hello"];
EXPECT_EQ(1, object.size());
_object["hello"];
EXPECT_EQ(1, _object.size());
object["world"];
EXPECT_EQ(2, object.size());
_object["world"];
EXPECT_EQ(2, _object.size());
}
TEST_F(JsonObject_Container_Tests, DoNotGrow_WhenSameValueIsAdded) {
object["hello"];
EXPECT_EQ(1, object.size());
_object["hello"];
EXPECT_EQ(1, _object.size());
object["hello"];
EXPECT_EQ(1, object.size());
_object["hello"];
EXPECT_EQ(1, _object.size());
}
TEST_F(JsonObject_Container_Tests, Shrink_WhenValuesAreRemoved) {
object["hello"];
object["world"];
_object["hello"];
_object["world"];
object.remove("hello");
EXPECT_EQ(1, object.size());
_object.remove("hello");
EXPECT_EQ(1, _object.size());
object.remove("world");
EXPECT_EQ(0, object.size());
_object.remove("world");
EXPECT_EQ(0, _object.size());
}
TEST_F(JsonObject_Container_Tests,
DoNotShrink_WhenRemoveIsCalledWithAWrongKey) {
object["hello"];
object["world"];
_object["hello"];
_object["world"];
object.remove(":-P");
_object.remove(":-P");
EXPECT_EQ(2, object.size());
EXPECT_EQ(2, _object.size());
}
TEST_F(JsonObject_Container_Tests, CanStoreIntegers) {
object["hello"] = 123;
object["world"] = 456;
_object["hello"] = 123;
_object["world"] = 456;
EXPECT_EQ(123, object["hello"].as<int>());
EXPECT_EQ(456, object["world"].as<int>());
EXPECT_EQ(123, _object["hello"].as<int>());
EXPECT_EQ(456, _object["world"].as<int>());
}
TEST_F(JsonObject_Container_Tests, CanStoreDoubles) {
object["hello"] = 123.45;
object["world"] = 456.78;
_object["hello"] = 123.45;
_object["world"] = 456.78;
EXPECT_EQ(123.45, object["hello"].as<double>());
EXPECT_EQ(456.78, object["world"].as<double>());
EXPECT_EQ(123.45, _object["hello"].as<double>());
EXPECT_EQ(456.78, _object["world"].as<double>());
}
TEST_F(JsonObject_Container_Tests, CanStoreBooleans) {
object["hello"] = true;
object["world"] = false;
_object["hello"] = true;
_object["world"] = false;
EXPECT_TRUE(object["hello"].as<bool>());
EXPECT_FALSE(object["world"].as<bool>());
EXPECT_TRUE(_object["hello"].as<bool>());
EXPECT_FALSE(_object["world"].as<bool>());
}
TEST_F(JsonObject_Container_Tests, CanStoreStrings) {
object["hello"] = "h3110";
object["world"] = "w0r1d";
_object["hello"] = "h3110";
_object["world"] = "w0r1d";
EXPECT_STREQ("h3110", object["hello"].as<const char*>());
EXPECT_STREQ("w0r1d", object["world"].as<const char*>());
EXPECT_STREQ("h3110", _object["hello"].as<const char*>());
EXPECT_STREQ("w0r1d", _object["world"].as<const char*>());
}
TEST_F(JsonObject_Container_Tests, CanStoreInnerArrays) {
JsonArray& innerarray1 = json.createArray();
JsonArray& innerarray2 = json.createArray();
JsonArray& innerarray1 = _jsonBuffer.createArray();
JsonArray& innerarray2 = _jsonBuffer.createArray();
object["hello"] = innerarray1;
object["world"] = innerarray2;
_object["hello"] = innerarray1;
_object["world"] = innerarray2;
EXPECT_EQ(&innerarray1, &object["hello"].asArray());
EXPECT_EQ(&innerarray2, &object["world"].asArray());
EXPECT_EQ(&innerarray1, &_object["hello"].asArray());
EXPECT_EQ(&innerarray2, &_object["world"].asArray());
}
TEST_F(JsonObject_Container_Tests, CanStoreInnerObjects) {
JsonObject& innerObject1 = json.createObject();
JsonObject& innerObject2 = json.createObject();
JsonObject& innerObject1 = _jsonBuffer.createObject();
JsonObject& innerObject2 = _jsonBuffer.createObject();
object["hello"] = innerObject1;
object["world"] = innerObject2;
_object["hello"] = innerObject1;
_object["world"] = innerObject2;
EXPECT_EQ(&innerObject1, &object["hello"].asObject());
EXPECT_EQ(&innerObject2, &object["world"].asObject());
EXPECT_EQ(&innerObject1, &_object["hello"].asObject());
EXPECT_EQ(&innerObject2, &_object["world"].asObject());
}
TEST_F(JsonObject_Container_Tests, ContainsKeyReturnFalseForNonExistingKey) {
EXPECT_FALSE(object.containsKey("hello"));
EXPECT_FALSE(_object.containsKey("hello"));
}
TEST_F(JsonObject_Container_Tests, ContainsKeyReturnTrueForDefinedValue) {
object.add("hello", 42);
EXPECT_TRUE(object.containsKey("hello"));
_object.add("hello", 42);
EXPECT_TRUE(_object.containsKey("hello"));
}
TEST_F(JsonObject_Container_Tests, ContainsKeyReturnFalseForUndefinedValue) {
object.add("hello");
EXPECT_FALSE(object.containsKey("hello"));
_object.add("hello");
EXPECT_FALSE(_object.containsKey("hello"));
}