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

@ -10,16 +10,16 @@
class GbathreeBug : public testing::Test { class GbathreeBug : public testing::Test {
public: public:
GbathreeBug() : object(buffer.parseObject(getJson())) {} GbathreeBug() : _object(_buffer.parseObject(getJson())) {}
protected: protected:
char json[1024]; char _json[1024];
StaticJsonBuffer<10000> buffer; DynamicJsonBuffer _buffer;
const JsonObject& object; const JsonObject& _object;
private: private:
char* getJson() { char* getJson() {
strcpy(json, strcpy(_json,
"{\"protocol_name\":\"fluorescence\",\"repeats\":1,\"wait\":0," "{\"protocol_name\":\"fluorescence\",\"repeats\":1,\"wait\":0,"
"\"averages\":1,\"measurements\":3,\"meas2_light\":15,\"meas1_" "\"averages\":1,\"measurements\":3,\"meas2_light\":15,\"meas1_"
"baseline\":0,\"act_light\":20,\"pulsesize\":25,\"pulsedistance\":" "baseline\":0,\"act_light\":20,\"pulsesize\":25,\"pulsedistance\":"
@ -30,46 +30,46 @@ class GbathreeBug : public testing::Test {
"\"measlights\":[[15,15,15,15],[15,15,15,15],[15,15,15,15],[15,15," "\"measlights\":[[15,15,15,15],[15,15,15,15],[15,15,15,15],[15,15,"
"15,15]],\"measlights2\":[[15,15,15,15],[15,15,15,15],[15,15,15,15]," "15,15]],\"measlights2\":[[15,15,15,15],[15,15,15,15],[15,15,15,15],"
"[15,15,15,15]],\"altc\":[2,2,2,2],\"altd\":[2,2,2,2]}"); "[15,15,15,15]],\"altc\":[2,2,2,2],\"altd\":[2,2,2,2]}");
return json; return _json;
} }
}; };
TEST_F(GbathreeBug, Success) { EXPECT_TRUE(object.success()); } TEST_F(GbathreeBug, Success) { EXPECT_TRUE(_object.success()); }
TEST_F(GbathreeBug, ProtocolName) { TEST_F(GbathreeBug, ProtocolName) {
EXPECT_STREQ("fluorescence", object.at("protocol_name").asString()); EXPECT_STREQ("fluorescence", _object.at("protocol_name").asString());
} }
TEST_F(GbathreeBug, Repeats) { EXPECT_EQ(1, object["repeats"]); } TEST_F(GbathreeBug, Repeats) { EXPECT_EQ(1, _object["repeats"]); }
TEST_F(GbathreeBug, Wait) { EXPECT_EQ(0, object["wait"]); } TEST_F(GbathreeBug, Wait) { EXPECT_EQ(0, _object["wait"]); }
TEST_F(GbathreeBug, Measurements) { EXPECT_EQ(3, object["measurements"]); } TEST_F(GbathreeBug, Measurements) { EXPECT_EQ(3, _object["measurements"]); }
TEST_F(GbathreeBug, Meas2_Light) { EXPECT_EQ(15, object["meas2_light"]); } TEST_F(GbathreeBug, Meas2_Light) { EXPECT_EQ(15, _object["meas2_light"]); }
TEST_F(GbathreeBug, Meas1_Baseline) { EXPECT_EQ(0, object["meas1_baseline"]); } TEST_F(GbathreeBug, Meas1_Baseline) { EXPECT_EQ(0, _object["meas1_baseline"]); }
TEST_F(GbathreeBug, Act_Light) { EXPECT_EQ(20, object["act_light"]); } TEST_F(GbathreeBug, Act_Light) { EXPECT_EQ(20, _object["act_light"]); }
TEST_F(GbathreeBug, Pulsesize) { EXPECT_EQ(25, object["pulsesize"]); } TEST_F(GbathreeBug, Pulsesize) { EXPECT_EQ(25, _object["pulsesize"]); }
TEST_F(GbathreeBug, Pulsedistance) { TEST_F(GbathreeBug, Pulsedistance) {
EXPECT_EQ(10000, object["pulsedistance"]); EXPECT_EQ(10000, _object["pulsedistance"]);
} }
TEST_F(GbathreeBug, Actintensity1) { EXPECT_EQ(50, object["actintensity1"]); } TEST_F(GbathreeBug, Actintensity1) { EXPECT_EQ(50, _object["actintensity1"]); }
TEST_F(GbathreeBug, Actintensity2) { EXPECT_EQ(255, object["actintensity2"]); } TEST_F(GbathreeBug, Actintensity2) { EXPECT_EQ(255, _object["actintensity2"]); }
TEST_F(GbathreeBug, Measintensity) { EXPECT_EQ(255, object["measintensity"]); } TEST_F(GbathreeBug, Measintensity) { EXPECT_EQ(255, _object["measintensity"]); }
TEST_F(GbathreeBug, Calintensity) { EXPECT_EQ(255, object["calintensity"]); } TEST_F(GbathreeBug, Calintensity) { EXPECT_EQ(255, _object["calintensity"]); }
TEST_F(GbathreeBug, Pulses) { TEST_F(GbathreeBug, Pulses) {
// "pulses":[50,50,50] // "pulses":[50,50,50]
JsonArray& array = object.at("pulses"); JsonArray& array = _object.at("pulses");
EXPECT_TRUE(array.success()); EXPECT_TRUE(array.success());
EXPECT_EQ(3, array.size()); EXPECT_EQ(3, array.size());
@ -82,7 +82,7 @@ TEST_F(GbathreeBug, Pulses) {
TEST_F(GbathreeBug, Act) { TEST_F(GbathreeBug, Act) {
// "act":[2,1,2,2] // "act":[2,1,2,2]
JsonArray& array = object.at("act"); JsonArray& array = _object.at("act");
EXPECT_TRUE(array.success()); EXPECT_TRUE(array.success());
EXPECT_EQ(4, array.size()); EXPECT_EQ(4, array.size());
@ -95,7 +95,7 @@ TEST_F(GbathreeBug, Act) {
TEST_F(GbathreeBug, Detectors) { TEST_F(GbathreeBug, Detectors) {
// "detectors":[[34,34,34,34],[34,34,34,34],[34,34,34,34],[34,34,34,34]] // "detectors":[[34,34,34,34],[34,34,34,34],[34,34,34,34],[34,34,34,34]]
JsonArray& array = object.at("detectors"); JsonArray& array = _object.at("detectors");
EXPECT_TRUE(array.success()); EXPECT_TRUE(array.success());
EXPECT_EQ(4, array.size()); EXPECT_EQ(4, array.size());
@ -110,7 +110,7 @@ TEST_F(GbathreeBug, Detectors) {
TEST_F(GbathreeBug, Alta) { TEST_F(GbathreeBug, Alta) {
// alta:[2,2,2,2] // alta:[2,2,2,2]
JsonArray& array = object.at("alta"); JsonArray& array = _object.at("alta");
EXPECT_TRUE(array.success()); EXPECT_TRUE(array.success());
EXPECT_EQ(4, array.size()); EXPECT_EQ(4, array.size());
@ -123,7 +123,7 @@ TEST_F(GbathreeBug, Alta) {
TEST_F(GbathreeBug, Altb) { TEST_F(GbathreeBug, Altb) {
// altb:[2,2,2,2] // altb:[2,2,2,2]
JsonArray& array = object.at("altb"); JsonArray& array = _object.at("altb");
EXPECT_TRUE(array.success()); EXPECT_TRUE(array.success());
EXPECT_EQ(4, array.size()); EXPECT_EQ(4, array.size());
@ -136,7 +136,7 @@ TEST_F(GbathreeBug, Altb) {
TEST_F(GbathreeBug, Measlights) { TEST_F(GbathreeBug, Measlights) {
// "measlights":[[15,15,15,15],[15,15,15,15],[15,15,15,15],[15,15,15,15]] // "measlights":[[15,15,15,15],[15,15,15,15],[15,15,15,15],[15,15,15,15]]
JsonArray& array = object.at("measlights"); JsonArray& array = _object.at("measlights");
EXPECT_TRUE(array.success()); EXPECT_TRUE(array.success());
EXPECT_EQ(4, array.size()); EXPECT_EQ(4, array.size());
@ -152,7 +152,7 @@ TEST_F(GbathreeBug, Measlights) {
TEST_F(GbathreeBug, Measlights2) { TEST_F(GbathreeBug, Measlights2) {
// "measlights2":[[15,15,15,15],[15,15,15,15],[15,15,15,15],[15,15,15,15]] // "measlights2":[[15,15,15,15],[15,15,15,15],[15,15,15,15],[15,15,15,15]]
JsonArray& array = object.at("measlights2"); JsonArray& array = _object.at("measlights2");
EXPECT_TRUE(array.success()); EXPECT_TRUE(array.success());
EXPECT_EQ(4, array.size()); EXPECT_EQ(4, array.size());
@ -167,7 +167,7 @@ TEST_F(GbathreeBug, Measlights2) {
TEST_F(GbathreeBug, Altc) { TEST_F(GbathreeBug, Altc) {
// altc:[2,2,2,2] // altc:[2,2,2,2]
JsonArray& array = object.at("altc"); JsonArray& array = _object.at("altc");
EXPECT_TRUE(array.success()); EXPECT_TRUE(array.success());
EXPECT_EQ(4, array.size()); EXPECT_EQ(4, array.size());
@ -180,7 +180,7 @@ TEST_F(GbathreeBug, Altc) {
TEST_F(GbathreeBug, Altd) { TEST_F(GbathreeBug, Altd) {
// altd:[2,2,2,2] // altd:[2,2,2,2]
JsonArray& array = object.at("altd"); JsonArray& array = _object.at("altd");
EXPECT_TRUE(array.success()); EXPECT_TRUE(array.success());
EXPECT_EQ(4, array.size()); EXPECT_EQ(4, array.size());

View File

@ -13,34 +13,34 @@ class IntegrationTests : public testing::TestWithParam<const char*> {
protected: protected:
virtual void SetUp() { virtual void SetUp() {
_input = GetParam(); _input = GetParam();
strcpy(inputBuffer, _input); strcpy(_inputBuffer, _input);
} }
void parseThenPrint(char* input, char* output) { void parseThenPrint(char* input, char* output) {
StaticJsonBuffer<10000> json; DynamicJsonBuffer buffer;
json.parseObject(input).printTo(output, MAX_JSON_SIZE); buffer.parseObject(input).printTo(output, MAX_JSON_SIZE);
} }
void parseThenPrettyPrint(char* input, char* output) { void parseThenPrettyPrint(char* input, char* output) {
StaticJsonBuffer<10000> json; DynamicJsonBuffer buffer;
json.parseObject(input).prettyPrintTo(output, MAX_JSON_SIZE); buffer.parseObject(input).prettyPrintTo(output, MAX_JSON_SIZE);
} }
const char* _input; const char* _input;
char inputBuffer[MAX_JSON_SIZE]; char _inputBuffer[MAX_JSON_SIZE];
char outputBuffer[MAX_JSON_SIZE]; char _outputBuffer[MAX_JSON_SIZE];
char intermediateBuffer[MAX_JSON_SIZE]; char _intermediateBuffer[MAX_JSON_SIZE];
}; };
TEST_P(IntegrationTests, ParseThenPrint) { TEST_P(IntegrationTests, ParseThenPrint) {
parseThenPrint(inputBuffer, outputBuffer); parseThenPrint(_inputBuffer, _outputBuffer);
ASSERT_STREQ(_input, outputBuffer); ASSERT_STREQ(_input, _outputBuffer);
} }
TEST_P(IntegrationTests, ParseThenPrettyPrintThenParseThenPrint) { TEST_P(IntegrationTests, ParseThenPrettyPrintThenParseThenPrint) {
parseThenPrettyPrint(inputBuffer, intermediateBuffer); parseThenPrettyPrint(_inputBuffer, _intermediateBuffer);
parseThenPrint(intermediateBuffer, outputBuffer); parseThenPrint(_intermediateBuffer, _outputBuffer);
ASSERT_STREQ(_input, outputBuffer); ASSERT_STREQ(_input, _outputBuffer);
} }
INSTANTIATE_TEST_CASE_P( INSTANTIATE_TEST_CASE_P(

View File

@ -11,7 +11,7 @@ class Issue34 : public testing::Test {
protected: protected:
template <typename T> template <typename T>
void test_with_value(T expected) { void test_with_value(T expected) {
StaticJsonBuffer<200> jsonBuffer; StaticJsonBuffer<JSON_OBJECT_SIZE(1)> jsonBuffer;
JsonObject& jsonObject = jsonBuffer.createObject(); JsonObject& jsonObject = jsonBuffer.createObject();

View File

@ -9,7 +9,7 @@
class JsonArray_Container_Tests : public ::testing::Test { class JsonArray_Container_Tests : public ::testing::Test {
protected: protected:
JsonArray_Container_Tests() : array(json.createArray()) {} JsonArray_Container_Tests() : _array(_jsonBuffer.createArray()) {}
template <typename T> template <typename T>
void firstMustEqual(T expected) { void firstMustEqual(T expected) {
@ -31,56 +31,56 @@ class JsonArray_Container_Tests : public ::testing::Test {
itemMustReference(1, expected); itemMustReference(1, expected);
} }
void sizeMustBe(int expected) { EXPECT_EQ(expected, array.size()); } void sizeMustBe(int expected) { EXPECT_EQ(expected, _array.size()); }
StaticJsonBuffer<256> json; DynamicJsonBuffer _jsonBuffer;
JsonArray& array; JsonArray& _array;
private: private:
template <typename T> template <typename T>
void itemMustEqual(int index, T expected) { void itemMustEqual(int index, T expected) {
EXPECT_EQ(expected, array[index].as<T>()); EXPECT_EQ(expected, _array[index].as<T>());
} }
template <typename T> template <typename T>
void itemMustReference(int index, const T& expected) { void itemMustReference(int index, const T& expected) {
EXPECT_EQ(&expected, &array[index].as<T&>()); EXPECT_EQ(&expected, &_array[index].as<T&>());
} }
}; };
TEST_F(JsonArray_Container_Tests, SuccessIsTrue) { TEST_F(JsonArray_Container_Tests, SuccessIsTrue) {
EXPECT_TRUE(array.success()); EXPECT_TRUE(_array.success());
} }
TEST_F(JsonArray_Container_Tests, InitialSizeIsZero) { sizeMustBe(0); } TEST_F(JsonArray_Container_Tests, InitialSizeIsZero) { sizeMustBe(0); }
TEST_F(JsonArray_Container_Tests, Grow_WhenValuesAreAdded) { TEST_F(JsonArray_Container_Tests, Grow_WhenValuesAreAdded) {
array.add("hello"); _array.add("hello");
sizeMustBe(1); sizeMustBe(1);
array.add("world"); _array.add("world");
sizeMustBe(2); sizeMustBe(2);
} }
TEST_F(JsonArray_Container_Tests, CanStoreIntegers) { TEST_F(JsonArray_Container_Tests, CanStoreIntegers) {
array.add(123); _array.add(123);
array.add(456); _array.add(456);
firstMustEqual(123); firstMustEqual(123);
secondMustEqual(456); secondMustEqual(456);
} }
TEST_F(JsonArray_Container_Tests, CanStoreDoubles) { TEST_F(JsonArray_Container_Tests, CanStoreDoubles) {
array.add(123.45); _array.add(123.45);
array.add(456.78); _array.add(456.78);
firstMustEqual(123.45); firstMustEqual(123.45);
secondMustEqual(456.78); secondMustEqual(456.78);
} }
TEST_F(JsonArray_Container_Tests, CanStoreBooleans) { TEST_F(JsonArray_Container_Tests, CanStoreBooleans) {
array.add(true); _array.add(true);
array.add(false); _array.add(false);
firstMustEqual(true); firstMustEqual(true);
secondMustEqual(false); secondMustEqual(false);
@ -90,46 +90,46 @@ TEST_F(JsonArray_Container_Tests, CanStoreStrings) {
const char* firstString = "h3110"; const char* firstString = "h3110";
const char* secondString = "w0r1d"; const char* secondString = "w0r1d";
array.add(firstString); _array.add(firstString);
array.add(secondString); _array.add(secondString);
firstMustEqual(firstString); firstMustEqual(firstString);
secondMustEqual(secondString); secondMustEqual(secondString);
} }
TEST_F(JsonArray_Container_Tests, CanStoreNestedArrays) { TEST_F(JsonArray_Container_Tests, CanStoreNestedArrays) {
JsonArray& innerarray1 = json.createArray(); JsonArray& inner_array1 = _jsonBuffer.createArray();
JsonArray& innerarray2 = json.createArray(); JsonArray& inner_array2 = _jsonBuffer.createArray();
array.add(innerarray1); _array.add(inner_array1);
array.add(innerarray2); _array.add(inner_array2);
firstMustReference(innerarray1); firstMustReference(inner_array1);
secondMustReference(innerarray2); secondMustReference(inner_array2);
} }
TEST_F(JsonArray_Container_Tests, CanStoreNestedObjects) { TEST_F(JsonArray_Container_Tests, CanStoreNestedObjects) {
JsonObject& innerObject1 = json.createObject(); JsonObject& innerObject1 = _jsonBuffer.createObject();
JsonObject& innerObject2 = json.createObject(); JsonObject& innerObject2 = _jsonBuffer.createObject();
array.add(innerObject1); _array.add(innerObject1);
array.add(innerObject2); _array.add(innerObject2);
firstMustReference(innerObject1); firstMustReference(innerObject1);
secondMustReference(innerObject2); secondMustReference(innerObject2);
} }
TEST_F(JsonArray_Container_Tests, CanCreateNestedArrays) { TEST_F(JsonArray_Container_Tests, CanCreateNestedArrays) {
JsonArray& innerarray1 = array.createNestedArray(); JsonArray& inner_array1 = _array.createNestedArray();
JsonArray& innerarray2 = array.createNestedArray(); JsonArray& inner_array2 = _array.createNestedArray();
firstMustReference(innerarray1); firstMustReference(inner_array1);
secondMustReference(innerarray2); secondMustReference(inner_array2);
} }
TEST_F(JsonArray_Container_Tests, CanCreateNestedObjects) { TEST_F(JsonArray_Container_Tests, CanCreateNestedObjects) {
JsonObject& innerObject1 = array.createNestedObject(); JsonObject& innerObject1 = _array.createNestedObject();
JsonObject& innerObject2 = array.createNestedObject(); JsonObject& innerObject2 = _array.createNestedObject();
firstMustReference(innerObject1); firstMustReference(innerObject1);
secondMustReference(innerObject2); secondMustReference(innerObject2);

View File

@ -9,7 +9,7 @@
template <typename TIterator> template <typename TIterator>
static void run_iterator_test() { static void run_iterator_test() {
StaticJsonBuffer<100> jsonBuffer; StaticJsonBuffer<JSON_ARRAY_SIZE(2)> jsonBuffer;
JsonArray &array = jsonBuffer.createArray(); JsonArray &array = jsonBuffer.createArray();
array.add(12); array.add(12);

View File

@ -9,20 +9,20 @@
class JsonArray_PrettyPrintTo_Tests : public testing::Test { class JsonArray_PrettyPrintTo_Tests : public testing::Test {
public: public:
JsonArray_PrettyPrintTo_Tests() : array(json.createArray()) {} JsonArray_PrettyPrintTo_Tests() : array(jsonBuffer.createArray()) {}
protected: protected:
StaticJsonBuffer<200> json; DynamicJsonBuffer jsonBuffer;
JsonArray& array; JsonArray& array;
void outputMustBe(const char* expected) { void outputMustBe(const char* expected) {
size_t n = array.prettyPrintTo(buffer, sizeof(buffer)); size_t n = array.prettyPrintTo(_buffer, sizeof(_buffer));
EXPECT_STREQ(expected, buffer); EXPECT_STREQ(expected, _buffer);
EXPECT_EQ(strlen(expected), n); EXPECT_EQ(strlen(expected), n);
} }
private: private:
char buffer[256]; char _buffer[256];
}; };
TEST_F(JsonArray_PrettyPrintTo_Tests, Empty) { outputMustBe("[]"); } TEST_F(JsonArray_PrettyPrintTo_Tests, Empty) { outputMustBe("[]"); }

View File

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

View File

@ -10,39 +10,39 @@
class JsonObject_Iterator_Test : public testing::Test { class JsonObject_Iterator_Test : public testing::Test {
public: public:
JsonObject_Iterator_Test() : object(_buffer.createObject()) { JsonObject_Iterator_Test() : _object(_buffer.createObject()) {
object["ab"] = 12; _object["ab"] = 12;
object["cd"] = 34; _object["cd"] = 34;
} }
protected: protected:
StaticJsonBuffer<256> _buffer; StaticJsonBuffer<JSON_OBJECT_SIZE(2)> _buffer;
JsonObject& object; JsonObject& _object;
}; };
TEST_F(JsonObject_Iterator_Test, NonConstIterator) { TEST_F(JsonObject_Iterator_Test, NonConstIterator) {
JsonObject::iterator it = object.begin(); JsonObject::iterator it = _object.begin();
ASSERT_NE(object.end(), it); ASSERT_NE(_object.end(), it);
EXPECT_STREQ("ab", it->key); EXPECT_STREQ("ab", it->key);
EXPECT_EQ(12, it->value); EXPECT_EQ(12, it->value);
it->key = "a.b"; it->key = "a.b";
it->value = 1.2; it->value = 1.2;
++it; ++it;
ASSERT_NE(object.end(), it); ASSERT_NE(_object.end(), it);
EXPECT_STREQ("cd", it->key); EXPECT_STREQ("cd", it->key);
EXPECT_EQ(34, it->value); EXPECT_EQ(34, it->value);
it->key = "c.d"; it->key = "c.d";
it->value = 3.4; it->value = 3.4;
++it; ++it;
ASSERT_EQ(object.end(), it); ASSERT_EQ(_object.end(), it);
ASSERT_EQ(2, object.size()); ASSERT_EQ(2, _object.size());
EXPECT_EQ(1.2, object["a.b"]); EXPECT_EQ(1.2, _object["a.b"]);
EXPECT_EQ(3.4, object["c.d"]); EXPECT_EQ(3.4, _object["c.d"]);
} }
TEST_F(JsonObject_Iterator_Test, ConstIterator) { TEST_F(JsonObject_Iterator_Test, ConstIterator) {
const JsonObject& const_object = object; const JsonObject& const_object = _object;
JsonObject::const_iterator it = const_object.begin(); JsonObject::const_iterator it = const_object.begin();
ASSERT_NE(const_object.end(), it); ASSERT_NE(const_object.end(), it);

View File

@ -9,14 +9,14 @@
class JsonObject_PrettyPrintTo_Tests : public testing::Test { class JsonObject_PrettyPrintTo_Tests : public testing::Test {
public: public:
JsonObject_PrettyPrintTo_Tests() : object(json.createObject()) {} JsonObject_PrettyPrintTo_Tests() : _object(_jsonBuffer.createObject()) {}
protected: protected:
StaticJsonBuffer<300> json; DynamicJsonBuffer _jsonBuffer;
JsonObject &object; JsonObject &_object;
void outputMustBe(const char *expected) { void outputMustBe(const char *expected) {
size_t n = object.prettyPrintTo(buffer, sizeof(buffer)); size_t n = _object.prettyPrintTo(buffer, sizeof(buffer));
EXPECT_STREQ(expected, buffer); EXPECT_STREQ(expected, buffer);
EXPECT_EQ(strlen(expected), n); EXPECT_EQ(strlen(expected), n);
} }
@ -28,7 +28,7 @@ class JsonObject_PrettyPrintTo_Tests : public testing::Test {
TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyObject) { outputMustBe("{}"); } TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyObject) { outputMustBe("{}"); }
TEST_F(JsonObject_PrettyPrintTo_Tests, OneMember) { TEST_F(JsonObject_PrettyPrintTo_Tests, OneMember) {
object["key"] = "value"; _object["key"] = "value";
outputMustBe( outputMustBe(
"{\r\n" "{\r\n"
@ -37,8 +37,8 @@ TEST_F(JsonObject_PrettyPrintTo_Tests, OneMember) {
} }
TEST_F(JsonObject_PrettyPrintTo_Tests, TwoMembers) { TEST_F(JsonObject_PrettyPrintTo_Tests, TwoMembers) {
object["key1"] = "value1"; _object["key1"] = "value1";
object["key2"] = "value2"; _object["key2"] = "value2";
outputMustBe( outputMustBe(
"{\r\n" "{\r\n"
@ -48,8 +48,8 @@ TEST_F(JsonObject_PrettyPrintTo_Tests, TwoMembers) {
} }
TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyNestedContainers) { TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyNestedContainers) {
object.createNestedObject("key1"); _object.createNestedObject("key1");
object.createNestedArray("key2"); _object.createNestedArray("key2");
outputMustBe( outputMustBe(
"{\r\n" "{\r\n"
@ -59,10 +59,10 @@ TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyNestedContainers) {
} }
TEST_F(JsonObject_PrettyPrintTo_Tests, NestedContainers) { TEST_F(JsonObject_PrettyPrintTo_Tests, NestedContainers) {
JsonObject &nested1 = object.createNestedObject("key1"); JsonObject &nested1 = _object.createNestedObject("key1");
nested1["a"] = 1; nested1["a"] = 1;
JsonArray &nested2 = object.createNestedArray("key2"); JsonArray &nested2 = _object.createNestedArray("key2");
nested2.add(2); nested2.add(2);
outputMustBe( outputMustBe(

View File

@ -42,7 +42,7 @@ class JsonParser_Array_Tests : public testing::Test {
EXPECT_STREQ(expected, _array->at(index).as<const char *>()); EXPECT_STREQ(expected, _array->at(index).as<const char *>());
} }
StaticJsonBuffer<256> _jsonBuffer; DynamicJsonBuffer _jsonBuffer;
JsonArray *_array; JsonArray *_array;
char _jsonString[256]; char _jsonString[256];
}; };

View File

@ -8,7 +8,7 @@
#include <ArduinoJson.h> #include <ArduinoJson.h>
TEST(JsonParser_Nested_Tests, ArrayNestedInObject) { TEST(JsonParser_Nested_Tests, ArrayNestedInObject) {
StaticJsonBuffer<256> jsonBuffer; DynamicJsonBuffer jsonBuffer;
char jsonString[] = " { \"ab\" : [ 1 , 2 ] , \"cd\" : [ 3 , 4 ] } "; char jsonString[] = " { \"ab\" : [ 1 , 2 ] , \"cd\" : [ 3 , 4 ] } ";
JsonObject &object = jsonBuffer.parseObject(jsonString); JsonObject &object = jsonBuffer.parseObject(jsonString);
@ -31,7 +31,7 @@ TEST(JsonParser_Nested_Tests, ArrayNestedInObject) {
} }
TEST(JsonParser_Nested_Tests, ObjectNestedInArray) { TEST(JsonParser_Nested_Tests, ObjectNestedInArray) {
StaticJsonBuffer<256> jsonBuffer; DynamicJsonBuffer jsonBuffer;
char jsonString[] = char jsonString[] =
" [ { \"a\" : 1 , \"b\" : 2 } , { \"c\" : 3 , \"d\" : 4 } ] "; " [ { \"a\" : 1 , \"b\" : 2 } , { \"c\" : 3 , \"d\" : 4 } ] ";

View File

@ -31,14 +31,14 @@ class JsonParser_NestingLimit_Tests : public testing::Test {
private: private:
bool tryParseArray(const char *json) { bool tryParseArray(const char *json) {
StaticJsonBuffer<256> buffer; DynamicJsonBuffer buffer;
char s[256]; char s[256];
strcpy(s, json); strcpy(s, json);
return buffer.parseArray(s, _nestingLimit).success(); return buffer.parseArray(s, _nestingLimit).success();
} }
bool tryParseObject(const char *json) { bool tryParseObject(const char *json) {
StaticJsonBuffer<256> buffer; DynamicJsonBuffer buffer;
char s[256]; char s[256];
strcpy(s, json); strcpy(s, json);
return buffer.parseObject(s, _nestingLimit).success(); return buffer.parseObject(s, _nestingLimit).success();

View File

@ -30,7 +30,7 @@ class JsonParser_Object_Test : public testing::Test {
} }
private: private:
StaticJsonBuffer<256> _jsonBuffer; DynamicJsonBuffer _jsonBuffer;
JsonObject *_object; JsonObject *_object;
char _jsonString[256]; char _jsonString[256];
}; };

View File

@ -9,59 +9,59 @@
class JsonVariant_Copy_Tests : public ::testing::Test { class JsonVariant_Copy_Tests : public ::testing::Test {
protected: protected:
StaticJsonBuffer<200> json; DynamicJsonBuffer _jsonBuffer;
JsonVariant variant1; JsonVariant _variant1;
JsonVariant variant2; JsonVariant _variant2;
}; };
TEST_F(JsonVariant_Copy_Tests, IntegersAreCopiedByValue) { TEST_F(JsonVariant_Copy_Tests, IntegersAreCopiedByValue) {
variant1 = 123; _variant1 = 123;
variant2 = variant1; _variant2 = _variant1;
variant1 = 456; _variant1 = 456;
EXPECT_EQ(123, variant2.as<int>()); EXPECT_EQ(123, _variant2.as<int>());
} }
TEST_F(JsonVariant_Copy_Tests, DoublesAreCopiedByValue) { TEST_F(JsonVariant_Copy_Tests, DoublesAreCopiedByValue) {
variant1 = 123.45; _variant1 = 123.45;
variant2 = variant1; _variant2 = _variant1;
variant1 = 456.78; _variant1 = 456.78;
EXPECT_EQ(123.45, variant2.as<double>()); EXPECT_EQ(123.45, _variant2.as<double>());
} }
TEST_F(JsonVariant_Copy_Tests, BooleansAreCopiedByValue) { TEST_F(JsonVariant_Copy_Tests, BooleansAreCopiedByValue) {
variant1 = true; _variant1 = true;
variant2 = variant1; _variant2 = _variant1;
variant1 = false; _variant1 = false;
EXPECT_TRUE(variant2.as<bool>()); EXPECT_TRUE(_variant2.as<bool>());
} }
TEST_F(JsonVariant_Copy_Tests, StringsAreCopiedByValue) { TEST_F(JsonVariant_Copy_Tests, StringsAreCopiedByValue) {
variant1 = "hello"; _variant1 = "hello";
variant2 = variant1; _variant2 = _variant1;
variant1 = "world"; _variant1 = "world";
EXPECT_STREQ("hello", variant2.as<const char *>()); EXPECT_STREQ("hello", _variant2.as<const char *>());
} }
TEST_F(JsonVariant_Copy_Tests, ObjectsAreCopiedByReference) { TEST_F(JsonVariant_Copy_Tests, ObjectsAreCopiedByReference) {
JsonObject &object = json.createObject(); JsonObject &object = _jsonBuffer.createObject();
variant1 = object; _variant1 = object;
object["hello"] = "world"; object["hello"] = "world";
EXPECT_EQ(1, variant1.asObject().size()); EXPECT_EQ(1, _variant1.asObject().size());
} }
TEST_F(JsonVariant_Copy_Tests, ArraysAreCopiedByReference) { TEST_F(JsonVariant_Copy_Tests, ArraysAreCopiedByReference) {
JsonArray &array = json.createArray(); JsonArray &array = _jsonBuffer.createArray();
variant1 = array; _variant1 = array;
array.add("world"); array.add("world");
EXPECT_EQ(1, variant1.asArray().size()); EXPECT_EQ(1, _variant1.asArray().size());
} }

View File

@ -11,18 +11,18 @@ class JsonVariant_Storage_Tests : public ::testing::Test {
protected: protected:
template <typename T> template <typename T>
void testValue(T expected) { void testValue(T expected) {
actual.set(expected); _actual.set(expected);
EXPECT_EQ(expected, actual.as<T>()); EXPECT_EQ(expected, _actual.as<T>());
} }
template <typename T> template <typename T>
void testReference(T &expected) { void testReference(T &expected) {
actual.set(expected); _actual.set(expected);
EXPECT_EQ(expected, actual.as<T &>()); EXPECT_EQ(expected, _actual.as<T &>());
} }
private: private:
JsonVariant actual; JsonVariant _actual;
}; };
TEST_F(JsonVariant_Storage_Tests, Double) { testValue<double>(123.45); } TEST_F(JsonVariant_Storage_Tests, Double) { testValue<double>(123.45); }
@ -41,8 +41,8 @@ TEST_F(JsonVariant_Storage_Tests, ULong) { testValue<unsigned long>(123UL); }
TEST_F(JsonVariant_Storage_Tests, UShort) { testValue<unsigned short>(123); } TEST_F(JsonVariant_Storage_Tests, UShort) { testValue<unsigned short>(123); }
TEST_F(JsonVariant_Storage_Tests, CanStoreObject) { TEST_F(JsonVariant_Storage_Tests, CanStoreObject) {
StaticJsonBuffer<200> json; DynamicJsonBuffer jsonBuffer;
JsonObject &object = json.createObject(); JsonObject &object = jsonBuffer.createObject();
testReference(object); testReference(object);
} }

View File

@ -9,56 +9,56 @@
class JsonVariant_Subscript_Tests : public ::testing::Test { class JsonVariant_Subscript_Tests : public ::testing::Test {
protected: protected:
StaticJsonBuffer<200> buffer; DynamicJsonBuffer _jsonBuffer;
JsonVariant variant; JsonVariant _variant;
}; };
TEST_F(JsonVariant_Subscript_Tests, Array) { TEST_F(JsonVariant_Subscript_Tests, Array) {
JsonArray &array = buffer.createArray(); JsonArray &array = _jsonBuffer.createArray();
array.add("element at index 0"); array.add("element at index 0");
array.add("element at index 1"); array.add("element at index 1");
variant = array; _variant = array;
EXPECT_EQ(2, variant.size()); EXPECT_EQ(2, _variant.size());
EXPECT_STREQ("element at index 0", variant[0].asString()); EXPECT_STREQ("element at index 0", _variant[0].asString());
EXPECT_STREQ("element at index 1", variant[1].asString()); EXPECT_STREQ("element at index 1", _variant[1].asString());
EXPECT_FALSE(variant[-1].success()); EXPECT_FALSE(_variant[-1].success());
EXPECT_FALSE(variant[3].success()); EXPECT_FALSE(_variant[3].success());
EXPECT_FALSE(variant["0"].success()); EXPECT_FALSE(_variant["0"].success());
} }
TEST_F(JsonVariant_Subscript_Tests, Object) { TEST_F(JsonVariant_Subscript_Tests, Object) {
JsonObject &object = buffer.createObject(); JsonObject &object = _jsonBuffer.createObject();
object["a"] = "element at key \"a\""; object["a"] = "element at key \"a\"";
object["b"] = "element at key \"b\""; object["b"] = "element at key \"b\"";
variant = object; _variant = object;
EXPECT_EQ(2, variant.size()); EXPECT_EQ(2, _variant.size());
EXPECT_STREQ("element at key \"a\"", variant["a"].asString()); EXPECT_STREQ("element at key \"a\"", _variant["a"].asString());
EXPECT_STREQ("element at key \"b\"", variant["b"].asString()); EXPECT_STREQ("element at key \"b\"", _variant["b"].asString());
EXPECT_FALSE(variant["c"].success()); EXPECT_FALSE(_variant["c"].success());
EXPECT_FALSE(variant[0].success()); EXPECT_FALSE(_variant[0].success());
} }
TEST_F(JsonVariant_Subscript_Tests, Undefined) { TEST_F(JsonVariant_Subscript_Tests, Undefined) {
variant = JsonVariant(); _variant = JsonVariant();
EXPECT_EQ(0, variant.size()); EXPECT_EQ(0, _variant.size());
EXPECT_FALSE(variant["0"].success()); EXPECT_FALSE(_variant["0"].success());
EXPECT_FALSE(variant[0].success()); EXPECT_FALSE(_variant[0].success());
} }
TEST_F(JsonVariant_Subscript_Tests, Invalid) { TEST_F(JsonVariant_Subscript_Tests, Invalid) {
variant = JsonVariant::invalid(); _variant = JsonVariant::invalid();
EXPECT_EQ(0, variant.size()); EXPECT_EQ(0, _variant.size());
EXPECT_FALSE(variant["0"].success()); EXPECT_FALSE(_variant["0"].success());
EXPECT_FALSE(variant[0].success()); EXPECT_FALSE(_variant[0].success());
} }
TEST_F(JsonVariant_Subscript_Tests, String) { TEST_F(JsonVariant_Subscript_Tests, String) {
variant = "hello world"; _variant = "hello world";
EXPECT_EQ(0, variant.size()); EXPECT_EQ(0, _variant.size());
EXPECT_FALSE(variant["0"].success()); EXPECT_FALSE(_variant["0"].success());
EXPECT_FALSE(variant[0].success()); EXPECT_FALSE(_variant[0].success());
} }