forked from bblanchon/ArduinoJson
Use DynamicJsonBuffer instead of arbitrary sized StaticJsonBuffer
This commit is contained in:
@ -10,16 +10,16 @@
|
||||
|
||||
class GbathreeBug : public testing::Test {
|
||||
public:
|
||||
GbathreeBug() : object(buffer.parseObject(getJson())) {}
|
||||
GbathreeBug() : _object(_buffer.parseObject(getJson())) {}
|
||||
|
||||
protected:
|
||||
char json[1024];
|
||||
StaticJsonBuffer<10000> buffer;
|
||||
const JsonObject& object;
|
||||
char _json[1024];
|
||||
DynamicJsonBuffer _buffer;
|
||||
const JsonObject& _object;
|
||||
|
||||
private:
|
||||
char* getJson() {
|
||||
strcpy(json,
|
||||
strcpy(_json,
|
||||
"{\"protocol_name\":\"fluorescence\",\"repeats\":1,\"wait\":0,"
|
||||
"\"averages\":1,\"measurements\":3,\"meas2_light\":15,\"meas1_"
|
||||
"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,"
|
||||
"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]}");
|
||||
return json;
|
||||
return _json;
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(GbathreeBug, Success) { EXPECT_TRUE(object.success()); }
|
||||
TEST_F(GbathreeBug, Success) { EXPECT_TRUE(_object.success()); }
|
||||
|
||||
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) {
|
||||
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) {
|
||||
// "pulses":[50,50,50]
|
||||
|
||||
JsonArray& array = object.at("pulses");
|
||||
JsonArray& array = _object.at("pulses");
|
||||
EXPECT_TRUE(array.success());
|
||||
|
||||
EXPECT_EQ(3, array.size());
|
||||
@ -82,7 +82,7 @@ TEST_F(GbathreeBug, Pulses) {
|
||||
TEST_F(GbathreeBug, Act) {
|
||||
// "act":[2,1,2,2]
|
||||
|
||||
JsonArray& array = object.at("act");
|
||||
JsonArray& array = _object.at("act");
|
||||
EXPECT_TRUE(array.success());
|
||||
|
||||
EXPECT_EQ(4, array.size());
|
||||
@ -95,7 +95,7 @@ TEST_F(GbathreeBug, Act) {
|
||||
TEST_F(GbathreeBug, Detectors) {
|
||||
// "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_EQ(4, array.size());
|
||||
|
||||
@ -110,7 +110,7 @@ TEST_F(GbathreeBug, Detectors) {
|
||||
TEST_F(GbathreeBug, Alta) {
|
||||
// alta:[2,2,2,2]
|
||||
|
||||
JsonArray& array = object.at("alta");
|
||||
JsonArray& array = _object.at("alta");
|
||||
EXPECT_TRUE(array.success());
|
||||
|
||||
EXPECT_EQ(4, array.size());
|
||||
@ -123,7 +123,7 @@ TEST_F(GbathreeBug, Alta) {
|
||||
TEST_F(GbathreeBug, Altb) {
|
||||
// altb:[2,2,2,2]
|
||||
|
||||
JsonArray& array = object.at("altb");
|
||||
JsonArray& array = _object.at("altb");
|
||||
EXPECT_TRUE(array.success());
|
||||
|
||||
EXPECT_EQ(4, array.size());
|
||||
@ -136,7 +136,7 @@ TEST_F(GbathreeBug, Altb) {
|
||||
TEST_F(GbathreeBug, Measlights) {
|
||||
// "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_EQ(4, array.size());
|
||||
|
||||
@ -152,7 +152,7 @@ TEST_F(GbathreeBug, Measlights) {
|
||||
TEST_F(GbathreeBug, Measlights2) {
|
||||
// "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_EQ(4, array.size());
|
||||
|
||||
@ -167,7 +167,7 @@ TEST_F(GbathreeBug, Measlights2) {
|
||||
TEST_F(GbathreeBug, Altc) {
|
||||
// altc:[2,2,2,2]
|
||||
|
||||
JsonArray& array = object.at("altc");
|
||||
JsonArray& array = _object.at("altc");
|
||||
EXPECT_TRUE(array.success());
|
||||
|
||||
EXPECT_EQ(4, array.size());
|
||||
@ -180,7 +180,7 @@ TEST_F(GbathreeBug, Altc) {
|
||||
TEST_F(GbathreeBug, Altd) {
|
||||
// altd:[2,2,2,2]
|
||||
|
||||
JsonArray& array = object.at("altd");
|
||||
JsonArray& array = _object.at("altd");
|
||||
EXPECT_TRUE(array.success());
|
||||
|
||||
EXPECT_EQ(4, array.size());
|
||||
|
@ -13,34 +13,34 @@ class IntegrationTests : public testing::TestWithParam<const char*> {
|
||||
protected:
|
||||
virtual void SetUp() {
|
||||
_input = GetParam();
|
||||
strcpy(inputBuffer, _input);
|
||||
strcpy(_inputBuffer, _input);
|
||||
}
|
||||
|
||||
void parseThenPrint(char* input, char* output) {
|
||||
StaticJsonBuffer<10000> json;
|
||||
json.parseObject(input).printTo(output, MAX_JSON_SIZE);
|
||||
DynamicJsonBuffer buffer;
|
||||
buffer.parseObject(input).printTo(output, MAX_JSON_SIZE);
|
||||
}
|
||||
|
||||
void parseThenPrettyPrint(char* input, char* output) {
|
||||
StaticJsonBuffer<10000> json;
|
||||
json.parseObject(input).prettyPrintTo(output, MAX_JSON_SIZE);
|
||||
DynamicJsonBuffer buffer;
|
||||
buffer.parseObject(input).prettyPrintTo(output, MAX_JSON_SIZE);
|
||||
}
|
||||
|
||||
const char* _input;
|
||||
char inputBuffer[MAX_JSON_SIZE];
|
||||
char outputBuffer[MAX_JSON_SIZE];
|
||||
char intermediateBuffer[MAX_JSON_SIZE];
|
||||
char _inputBuffer[MAX_JSON_SIZE];
|
||||
char _outputBuffer[MAX_JSON_SIZE];
|
||||
char _intermediateBuffer[MAX_JSON_SIZE];
|
||||
};
|
||||
|
||||
TEST_P(IntegrationTests, ParseThenPrint) {
|
||||
parseThenPrint(inputBuffer, outputBuffer);
|
||||
ASSERT_STREQ(_input, outputBuffer);
|
||||
parseThenPrint(_inputBuffer, _outputBuffer);
|
||||
ASSERT_STREQ(_input, _outputBuffer);
|
||||
}
|
||||
|
||||
TEST_P(IntegrationTests, ParseThenPrettyPrintThenParseThenPrint) {
|
||||
parseThenPrettyPrint(inputBuffer, intermediateBuffer);
|
||||
parseThenPrint(intermediateBuffer, outputBuffer);
|
||||
ASSERT_STREQ(_input, outputBuffer);
|
||||
parseThenPrettyPrint(_inputBuffer, _intermediateBuffer);
|
||||
parseThenPrint(_intermediateBuffer, _outputBuffer);
|
||||
ASSERT_STREQ(_input, _outputBuffer);
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(
|
||||
|
@ -11,7 +11,7 @@ class Issue34 : public testing::Test {
|
||||
protected:
|
||||
template <typename T>
|
||||
void test_with_value(T expected) {
|
||||
StaticJsonBuffer<200> jsonBuffer;
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1)> jsonBuffer;
|
||||
|
||||
JsonObject& jsonObject = jsonBuffer.createObject();
|
||||
|
||||
|
@ -9,7 +9,7 @@
|
||||
|
||||
class JsonArray_Container_Tests : public ::testing::Test {
|
||||
protected:
|
||||
JsonArray_Container_Tests() : array(json.createArray()) {}
|
||||
JsonArray_Container_Tests() : _array(_jsonBuffer.createArray()) {}
|
||||
|
||||
template <typename T>
|
||||
void firstMustEqual(T expected) {
|
||||
@ -31,56 +31,56 @@ class JsonArray_Container_Tests : public ::testing::Test {
|
||||
itemMustReference(1, expected);
|
||||
}
|
||||
|
||||
void sizeMustBe(int expected) { EXPECT_EQ(expected, array.size()); }
|
||||
void sizeMustBe(int expected) { EXPECT_EQ(expected, _array.size()); }
|
||||
|
||||
StaticJsonBuffer<256> json;
|
||||
JsonArray& array;
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonArray& _array;
|
||||
|
||||
private:
|
||||
template <typename T>
|
||||
void itemMustEqual(int index, T expected) {
|
||||
EXPECT_EQ(expected, array[index].as<T>());
|
||||
EXPECT_EQ(expected, _array[index].as<T>());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
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) {
|
||||
EXPECT_TRUE(array.success());
|
||||
EXPECT_TRUE(_array.success());
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, InitialSizeIsZero) { sizeMustBe(0); }
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, Grow_WhenValuesAreAdded) {
|
||||
array.add("hello");
|
||||
_array.add("hello");
|
||||
sizeMustBe(1);
|
||||
|
||||
array.add("world");
|
||||
_array.add("world");
|
||||
sizeMustBe(2);
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, CanStoreIntegers) {
|
||||
array.add(123);
|
||||
array.add(456);
|
||||
_array.add(123);
|
||||
_array.add(456);
|
||||
|
||||
firstMustEqual(123);
|
||||
secondMustEqual(456);
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, CanStoreDoubles) {
|
||||
array.add(123.45);
|
||||
array.add(456.78);
|
||||
_array.add(123.45);
|
||||
_array.add(456.78);
|
||||
|
||||
firstMustEqual(123.45);
|
||||
secondMustEqual(456.78);
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, CanStoreBooleans) {
|
||||
array.add(true);
|
||||
array.add(false);
|
||||
_array.add(true);
|
||||
_array.add(false);
|
||||
|
||||
firstMustEqual(true);
|
||||
secondMustEqual(false);
|
||||
@ -90,46 +90,46 @@ TEST_F(JsonArray_Container_Tests, CanStoreStrings) {
|
||||
const char* firstString = "h3110";
|
||||
const char* secondString = "w0r1d";
|
||||
|
||||
array.add(firstString);
|
||||
array.add(secondString);
|
||||
_array.add(firstString);
|
||||
_array.add(secondString);
|
||||
|
||||
firstMustEqual(firstString);
|
||||
secondMustEqual(secondString);
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, CanStoreNestedArrays) {
|
||||
JsonArray& innerarray1 = json.createArray();
|
||||
JsonArray& innerarray2 = json.createArray();
|
||||
JsonArray& inner_array1 = _jsonBuffer.createArray();
|
||||
JsonArray& inner_array2 = _jsonBuffer.createArray();
|
||||
|
||||
array.add(innerarray1);
|
||||
array.add(innerarray2);
|
||||
_array.add(inner_array1);
|
||||
_array.add(inner_array2);
|
||||
|
||||
firstMustReference(innerarray1);
|
||||
secondMustReference(innerarray2);
|
||||
firstMustReference(inner_array1);
|
||||
secondMustReference(inner_array2);
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, CanStoreNestedObjects) {
|
||||
JsonObject& innerObject1 = json.createObject();
|
||||
JsonObject& innerObject2 = json.createObject();
|
||||
JsonObject& innerObject1 = _jsonBuffer.createObject();
|
||||
JsonObject& innerObject2 = _jsonBuffer.createObject();
|
||||
|
||||
array.add(innerObject1);
|
||||
array.add(innerObject2);
|
||||
_array.add(innerObject1);
|
||||
_array.add(innerObject2);
|
||||
|
||||
firstMustReference(innerObject1);
|
||||
secondMustReference(innerObject2);
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, CanCreateNestedArrays) {
|
||||
JsonArray& innerarray1 = array.createNestedArray();
|
||||
JsonArray& innerarray2 = array.createNestedArray();
|
||||
JsonArray& inner_array1 = _array.createNestedArray();
|
||||
JsonArray& inner_array2 = _array.createNestedArray();
|
||||
|
||||
firstMustReference(innerarray1);
|
||||
secondMustReference(innerarray2);
|
||||
firstMustReference(inner_array1);
|
||||
secondMustReference(inner_array2);
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, CanCreateNestedObjects) {
|
||||
JsonObject& innerObject1 = array.createNestedObject();
|
||||
JsonObject& innerObject2 = array.createNestedObject();
|
||||
JsonObject& innerObject1 = _array.createNestedObject();
|
||||
JsonObject& innerObject2 = _array.createNestedObject();
|
||||
|
||||
firstMustReference(innerObject1);
|
||||
secondMustReference(innerObject2);
|
||||
|
@ -9,7 +9,7 @@
|
||||
|
||||
template <typename TIterator>
|
||||
static void run_iterator_test() {
|
||||
StaticJsonBuffer<100> jsonBuffer;
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(2)> jsonBuffer;
|
||||
|
||||
JsonArray &array = jsonBuffer.createArray();
|
||||
array.add(12);
|
||||
|
@ -9,20 +9,20 @@
|
||||
|
||||
class JsonArray_PrettyPrintTo_Tests : public testing::Test {
|
||||
public:
|
||||
JsonArray_PrettyPrintTo_Tests() : array(json.createArray()) {}
|
||||
JsonArray_PrettyPrintTo_Tests() : array(jsonBuffer.createArray()) {}
|
||||
|
||||
protected:
|
||||
StaticJsonBuffer<200> json;
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& array;
|
||||
|
||||
void outputMustBe(const char* expected) {
|
||||
size_t n = array.prettyPrintTo(buffer, sizeof(buffer));
|
||||
EXPECT_STREQ(expected, buffer);
|
||||
size_t n = array.prettyPrintTo(_buffer, sizeof(_buffer));
|
||||
EXPECT_STREQ(expected, _buffer);
|
||||
EXPECT_EQ(strlen(expected), n);
|
||||
}
|
||||
|
||||
private:
|
||||
char buffer[256];
|
||||
char _buffer[256];
|
||||
};
|
||||
|
||||
TEST_F(JsonArray_PrettyPrintTo_Tests, Empty) { outputMustBe("[]"); }
|
||||
|
@ -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"));
|
||||
}
|
||||
|
@ -10,39 +10,39 @@
|
||||
|
||||
class JsonObject_Iterator_Test : public testing::Test {
|
||||
public:
|
||||
JsonObject_Iterator_Test() : object(_buffer.createObject()) {
|
||||
object["ab"] = 12;
|
||||
object["cd"] = 34;
|
||||
JsonObject_Iterator_Test() : _object(_buffer.createObject()) {
|
||||
_object["ab"] = 12;
|
||||
_object["cd"] = 34;
|
||||
}
|
||||
|
||||
protected:
|
||||
StaticJsonBuffer<256> _buffer;
|
||||
JsonObject& object;
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(2)> _buffer;
|
||||
JsonObject& _object;
|
||||
};
|
||||
|
||||
TEST_F(JsonObject_Iterator_Test, NonConstIterator) {
|
||||
JsonObject::iterator it = object.begin();
|
||||
ASSERT_NE(object.end(), it);
|
||||
JsonObject::iterator it = _object.begin();
|
||||
ASSERT_NE(_object.end(), it);
|
||||
EXPECT_STREQ("ab", it->key);
|
||||
EXPECT_EQ(12, it->value);
|
||||
it->key = "a.b";
|
||||
it->value = 1.2;
|
||||
++it;
|
||||
ASSERT_NE(object.end(), it);
|
||||
ASSERT_NE(_object.end(), it);
|
||||
EXPECT_STREQ("cd", it->key);
|
||||
EXPECT_EQ(34, it->value);
|
||||
it->key = "c.d";
|
||||
it->value = 3.4;
|
||||
++it;
|
||||
ASSERT_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"]);
|
||||
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;
|
||||
const JsonObject& const_object = _object;
|
||||
JsonObject::const_iterator it = const_object.begin();
|
||||
|
||||
ASSERT_NE(const_object.end(), it);
|
||||
|
@ -9,14 +9,14 @@
|
||||
|
||||
class JsonObject_PrettyPrintTo_Tests : public testing::Test {
|
||||
public:
|
||||
JsonObject_PrettyPrintTo_Tests() : object(json.createObject()) {}
|
||||
JsonObject_PrettyPrintTo_Tests() : _object(_jsonBuffer.createObject()) {}
|
||||
|
||||
protected:
|
||||
StaticJsonBuffer<300> json;
|
||||
JsonObject &object;
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonObject &_object;
|
||||
|
||||
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_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, OneMember) {
|
||||
object["key"] = "value";
|
||||
_object["key"] = "value";
|
||||
|
||||
outputMustBe(
|
||||
"{\r\n"
|
||||
@ -37,8 +37,8 @@ TEST_F(JsonObject_PrettyPrintTo_Tests, OneMember) {
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_PrettyPrintTo_Tests, TwoMembers) {
|
||||
object["key1"] = "value1";
|
||||
object["key2"] = "value2";
|
||||
_object["key1"] = "value1";
|
||||
_object["key2"] = "value2";
|
||||
|
||||
outputMustBe(
|
||||
"{\r\n"
|
||||
@ -48,8 +48,8 @@ TEST_F(JsonObject_PrettyPrintTo_Tests, TwoMembers) {
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyNestedContainers) {
|
||||
object.createNestedObject("key1");
|
||||
object.createNestedArray("key2");
|
||||
_object.createNestedObject("key1");
|
||||
_object.createNestedArray("key2");
|
||||
|
||||
outputMustBe(
|
||||
"{\r\n"
|
||||
@ -59,10 +59,10 @@ TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyNestedContainers) {
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_PrettyPrintTo_Tests, NestedContainers) {
|
||||
JsonObject &nested1 = object.createNestedObject("key1");
|
||||
JsonObject &nested1 = _object.createNestedObject("key1");
|
||||
nested1["a"] = 1;
|
||||
|
||||
JsonArray &nested2 = object.createNestedArray("key2");
|
||||
JsonArray &nested2 = _object.createNestedArray("key2");
|
||||
nested2.add(2);
|
||||
|
||||
outputMustBe(
|
||||
|
@ -42,7 +42,7 @@ class JsonParser_Array_Tests : public testing::Test {
|
||||
EXPECT_STREQ(expected, _array->at(index).as<const char *>());
|
||||
}
|
||||
|
||||
StaticJsonBuffer<256> _jsonBuffer;
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonArray *_array;
|
||||
char _jsonString[256];
|
||||
};
|
||||
|
@ -8,7 +8,7 @@
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
TEST(JsonParser_Nested_Tests, ArrayNestedInObject) {
|
||||
StaticJsonBuffer<256> jsonBuffer;
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
char jsonString[] = " { \"ab\" : [ 1 , 2 ] , \"cd\" : [ 3 , 4 ] } ";
|
||||
|
||||
JsonObject &object = jsonBuffer.parseObject(jsonString);
|
||||
@ -31,7 +31,7 @@ TEST(JsonParser_Nested_Tests, ArrayNestedInObject) {
|
||||
}
|
||||
|
||||
TEST(JsonParser_Nested_Tests, ObjectNestedInArray) {
|
||||
StaticJsonBuffer<256> jsonBuffer;
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
char jsonString[] =
|
||||
" [ { \"a\" : 1 , \"b\" : 2 } , { \"c\" : 3 , \"d\" : 4 } ] ";
|
||||
|
||||
|
@ -31,14 +31,14 @@ class JsonParser_NestingLimit_Tests : public testing::Test {
|
||||
|
||||
private:
|
||||
bool tryParseArray(const char *json) {
|
||||
StaticJsonBuffer<256> buffer;
|
||||
DynamicJsonBuffer buffer;
|
||||
char s[256];
|
||||
strcpy(s, json);
|
||||
return buffer.parseArray(s, _nestingLimit).success();
|
||||
}
|
||||
|
||||
bool tryParseObject(const char *json) {
|
||||
StaticJsonBuffer<256> buffer;
|
||||
DynamicJsonBuffer buffer;
|
||||
char s[256];
|
||||
strcpy(s, json);
|
||||
return buffer.parseObject(s, _nestingLimit).success();
|
||||
|
@ -30,7 +30,7 @@ class JsonParser_Object_Test : public testing::Test {
|
||||
}
|
||||
|
||||
private:
|
||||
StaticJsonBuffer<256> _jsonBuffer;
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonObject *_object;
|
||||
char _jsonString[256];
|
||||
};
|
||||
|
@ -9,59 +9,59 @@
|
||||
|
||||
class JsonVariant_Copy_Tests : public ::testing::Test {
|
||||
protected:
|
||||
StaticJsonBuffer<200> json;
|
||||
JsonVariant variant1;
|
||||
JsonVariant variant2;
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonVariant _variant1;
|
||||
JsonVariant _variant2;
|
||||
};
|
||||
|
||||
TEST_F(JsonVariant_Copy_Tests, IntegersAreCopiedByValue) {
|
||||
variant1 = 123;
|
||||
variant2 = variant1;
|
||||
variant1 = 456;
|
||||
_variant1 = 123;
|
||||
_variant2 = _variant1;
|
||||
_variant1 = 456;
|
||||
|
||||
EXPECT_EQ(123, variant2.as<int>());
|
||||
EXPECT_EQ(123, _variant2.as<int>());
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Copy_Tests, DoublesAreCopiedByValue) {
|
||||
variant1 = 123.45;
|
||||
variant2 = variant1;
|
||||
variant1 = 456.78;
|
||||
_variant1 = 123.45;
|
||||
_variant2 = _variant1;
|
||||
_variant1 = 456.78;
|
||||
|
||||
EXPECT_EQ(123.45, variant2.as<double>());
|
||||
EXPECT_EQ(123.45, _variant2.as<double>());
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Copy_Tests, BooleansAreCopiedByValue) {
|
||||
variant1 = true;
|
||||
variant2 = variant1;
|
||||
variant1 = false;
|
||||
_variant1 = true;
|
||||
_variant2 = _variant1;
|
||||
_variant1 = false;
|
||||
|
||||
EXPECT_TRUE(variant2.as<bool>());
|
||||
EXPECT_TRUE(_variant2.as<bool>());
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Copy_Tests, StringsAreCopiedByValue) {
|
||||
variant1 = "hello";
|
||||
variant2 = variant1;
|
||||
variant1 = "world";
|
||||
_variant1 = "hello";
|
||||
_variant2 = _variant1;
|
||||
_variant1 = "world";
|
||||
|
||||
EXPECT_STREQ("hello", variant2.as<const char *>());
|
||||
EXPECT_STREQ("hello", _variant2.as<const char *>());
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Copy_Tests, ObjectsAreCopiedByReference) {
|
||||
JsonObject &object = json.createObject();
|
||||
JsonObject &object = _jsonBuffer.createObject();
|
||||
|
||||
variant1 = object;
|
||||
_variant1 = object;
|
||||
|
||||
object["hello"] = "world";
|
||||
|
||||
EXPECT_EQ(1, variant1.asObject().size());
|
||||
EXPECT_EQ(1, _variant1.asObject().size());
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Copy_Tests, ArraysAreCopiedByReference) {
|
||||
JsonArray &array = json.createArray();
|
||||
JsonArray &array = _jsonBuffer.createArray();
|
||||
|
||||
variant1 = array;
|
||||
_variant1 = array;
|
||||
|
||||
array.add("world");
|
||||
|
||||
EXPECT_EQ(1, variant1.asArray().size());
|
||||
EXPECT_EQ(1, _variant1.asArray().size());
|
||||
}
|
||||
|
@ -11,18 +11,18 @@ class JsonVariant_Storage_Tests : public ::testing::Test {
|
||||
protected:
|
||||
template <typename T>
|
||||
void testValue(T expected) {
|
||||
actual.set(expected);
|
||||
EXPECT_EQ(expected, actual.as<T>());
|
||||
_actual.set(expected);
|
||||
EXPECT_EQ(expected, _actual.as<T>());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void testReference(T &expected) {
|
||||
actual.set(expected);
|
||||
EXPECT_EQ(expected, actual.as<T &>());
|
||||
_actual.set(expected);
|
||||
EXPECT_EQ(expected, _actual.as<T &>());
|
||||
}
|
||||
|
||||
private:
|
||||
JsonVariant actual;
|
||||
JsonVariant _actual;
|
||||
};
|
||||
|
||||
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, CanStoreObject) {
|
||||
StaticJsonBuffer<200> json;
|
||||
JsonObject &object = json.createObject();
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject &object = jsonBuffer.createObject();
|
||||
|
||||
testReference(object);
|
||||
}
|
||||
|
@ -9,56 +9,56 @@
|
||||
|
||||
class JsonVariant_Subscript_Tests : public ::testing::Test {
|
||||
protected:
|
||||
StaticJsonBuffer<200> buffer;
|
||||
JsonVariant variant;
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonVariant _variant;
|
||||
};
|
||||
|
||||
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 1");
|
||||
|
||||
variant = array;
|
||||
_variant = array;
|
||||
|
||||
EXPECT_EQ(2, variant.size());
|
||||
EXPECT_STREQ("element at index 0", variant[0].asString());
|
||||
EXPECT_STREQ("element at index 1", variant[1].asString());
|
||||
EXPECT_FALSE(variant[-1].success());
|
||||
EXPECT_FALSE(variant[3].success());
|
||||
EXPECT_FALSE(variant["0"].success());
|
||||
EXPECT_EQ(2, _variant.size());
|
||||
EXPECT_STREQ("element at index 0", _variant[0].asString());
|
||||
EXPECT_STREQ("element at index 1", _variant[1].asString());
|
||||
EXPECT_FALSE(_variant[-1].success());
|
||||
EXPECT_FALSE(_variant[3].success());
|
||||
EXPECT_FALSE(_variant["0"].success());
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Subscript_Tests, Object) {
|
||||
JsonObject &object = buffer.createObject();
|
||||
JsonObject &object = _jsonBuffer.createObject();
|
||||
object["a"] = "element at key \"a\"";
|
||||
object["b"] = "element at key \"b\"";
|
||||
|
||||
variant = object;
|
||||
_variant = object;
|
||||
|
||||
EXPECT_EQ(2, variant.size());
|
||||
EXPECT_STREQ("element at key \"a\"", variant["a"].asString());
|
||||
EXPECT_STREQ("element at key \"b\"", variant["b"].asString());
|
||||
EXPECT_FALSE(variant["c"].success());
|
||||
EXPECT_FALSE(variant[0].success());
|
||||
EXPECT_EQ(2, _variant.size());
|
||||
EXPECT_STREQ("element at key \"a\"", _variant["a"].asString());
|
||||
EXPECT_STREQ("element at key \"b\"", _variant["b"].asString());
|
||||
EXPECT_FALSE(_variant["c"].success());
|
||||
EXPECT_FALSE(_variant[0].success());
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Subscript_Tests, Undefined) {
|
||||
variant = JsonVariant();
|
||||
EXPECT_EQ(0, variant.size());
|
||||
EXPECT_FALSE(variant["0"].success());
|
||||
EXPECT_FALSE(variant[0].success());
|
||||
_variant = JsonVariant();
|
||||
EXPECT_EQ(0, _variant.size());
|
||||
EXPECT_FALSE(_variant["0"].success());
|
||||
EXPECT_FALSE(_variant[0].success());
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Subscript_Tests, Invalid) {
|
||||
variant = JsonVariant::invalid();
|
||||
EXPECT_EQ(0, variant.size());
|
||||
EXPECT_FALSE(variant["0"].success());
|
||||
EXPECT_FALSE(variant[0].success());
|
||||
_variant = JsonVariant::invalid();
|
||||
EXPECT_EQ(0, _variant.size());
|
||||
EXPECT_FALSE(_variant["0"].success());
|
||||
EXPECT_FALSE(_variant[0].success());
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Subscript_Tests, String) {
|
||||
variant = "hello world";
|
||||
EXPECT_EQ(0, variant.size());
|
||||
EXPECT_FALSE(variant["0"].success());
|
||||
EXPECT_FALSE(variant[0].success());
|
||||
_variant = "hello world";
|
||||
EXPECT_EQ(0, _variant.size());
|
||||
EXPECT_FALSE(_variant["0"].success());
|
||||
EXPECT_FALSE(_variant[0].success());
|
||||
}
|
||||
|
Reference in New Issue
Block a user