mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-29 10:17:39 +02:00
Added JsonArray
This commit is contained in:
77
tests/JsonArray_Container_Tests.cpp
Normal file
77
tests/JsonArray_Container_Tests.cpp
Normal file
@ -0,0 +1,77 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <StaticJsonBuffer.h>
|
||||
#include <JsonValue.h>
|
||||
|
||||
class JsonArray_Container_Tests : public ::testing::Test
|
||||
{
|
||||
protected:
|
||||
virtual void SetUp()
|
||||
{
|
||||
array = json.createArray();
|
||||
}
|
||||
|
||||
StaticJsonBuffer<42> json;
|
||||
JsonArray array;
|
||||
};
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, InitialSizeIsZero)
|
||||
{
|
||||
EXPECT_EQ(0, array.size());
|
||||
}
|
||||
/*
|
||||
TEST_F(JsonArray_Container_Tests, Grow_WhenValuesAreAdded)
|
||||
{
|
||||
array.add("hello");
|
||||
EXPECT_EQ(1, array.size());
|
||||
|
||||
array.add("world");
|
||||
EXPECT_EQ(2, array.size());
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, CanStoreIntegers)
|
||||
{
|
||||
array.add(123);
|
||||
array.add(456);
|
||||
|
||||
EXPECT_EQ(123, (int) array[0]);
|
||||
EXPECT_EQ(456, (int) array[1]);
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, CanStoreDoubles)
|
||||
{
|
||||
array.add(123.45);
|
||||
array.add(456.78);
|
||||
|
||||
EXPECT_EQ(123.45, (double) array[0]);
|
||||
EXPECT_EQ(456.78, (double) array[1]);
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, CanStoreBooleans)
|
||||
{
|
||||
array.add(true);
|
||||
array.add(false);
|
||||
|
||||
EXPECT_TRUE((bool) array[0]);
|
||||
EXPECT_FALSE((bool) array[1]);
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, CanStoreStrings)
|
||||
{
|
||||
array.add("h3110");
|
||||
array.add("w0r1d");
|
||||
|
||||
EXPECT_STREQ("h3110", (const char*) array[0]);
|
||||
EXPECT_STREQ("w0r1d", (const char*) array[1]);
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, CanStoreInnerArrays)
|
||||
{
|
||||
JsonArray innerarray1 = json.createArray();
|
||||
JsonArray innerarray2 = json.createArray();
|
||||
|
||||
array.add(innerarray1);
|
||||
array.add(innerarray2);
|
||||
|
||||
EXPECT_EQ(innerarray1, (JsonArray) array[0]);
|
||||
EXPECT_EQ(innerarray2, (JsonArray) array[1]);
|
||||
}*/
|
162
tests/JsonArray_Serialization_Tests.cpp
Normal file
162
tests/JsonArray_Serialization_Tests.cpp
Normal file
@ -0,0 +1,162 @@
|
||||
/*
|
||||
* Arduino JSON library
|
||||
* Benoit Blanchon 2014 - MIT License
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <JsonArray.h>
|
||||
#include <JsonObject.h>
|
||||
#include <StaticJsonBuffer.h>
|
||||
|
||||
class JsonArray_Serialization_Tests : testing::Test
|
||||
{
|
||||
protected:
|
||||
virtual void SetUp()
|
||||
{
|
||||
array = json.createArray();
|
||||
}
|
||||
|
||||
void outputMustBe(const char* expected)
|
||||
{
|
||||
size_t n = array.printTo(buffer, sizeof(buffer));
|
||||
EXPECT_STREQ(expected, buffer);
|
||||
EXPECT_EQ(strlen(expected), n);
|
||||
}
|
||||
|
||||
private:
|
||||
JsonArray array;
|
||||
char buffer[256];
|
||||
StaticJsonBuffer<32> json;
|
||||
};
|
||||
/*
|
||||
TEST_F(JsonArray_Serialization_Tests, Empty)
|
||||
{
|
||||
outputMustBe("[]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Serialization_Tests, Null)
|
||||
{
|
||||
array.add((char*) 0);
|
||||
|
||||
outputMustBe("[null]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Serialization_Tests, OneString)
|
||||
{
|
||||
array.add("hello");
|
||||
|
||||
outputMustBe("[\"hello\"]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Serialization_Tests, TwoStrings)
|
||||
{
|
||||
array.add("hello");
|
||||
array.add("world");
|
||||
|
||||
outputMustBe("[\"hello\",\"world\"]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Serialization_Tests, OneStringOverCapacity)
|
||||
{
|
||||
array.add("hello");
|
||||
array.add("world");
|
||||
array.add("lost");
|
||||
|
||||
outputMustBe("[\"hello\",\"world\"]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Serialization_Tests, OneDoubleDefaultDigits)
|
||||
{
|
||||
array.add(3.14159265358979323846);
|
||||
outputMustBe("[3.14]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Serialization_Tests, OneDoubleFourDigits)
|
||||
{
|
||||
array.add<4>(3.14159265358979323846);
|
||||
outputMustBe("[3.1416]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Serialization_Tests, OneInteger)
|
||||
{
|
||||
array.add(1);
|
||||
|
||||
outputMustBe("[1]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Serialization_Tests, TwoIntegers)
|
||||
{
|
||||
array.add(1);
|
||||
array.add(2);
|
||||
|
||||
outputMustBe("[1,2]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Serialization_Tests, OneIntegerOverCapacity)
|
||||
{
|
||||
array.add(1);
|
||||
array.add(2);
|
||||
array.add(3);
|
||||
|
||||
outputMustBe("[1,2]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Serialization_Tests, OneTrue)
|
||||
{
|
||||
array.add(true);
|
||||
|
||||
outputMustBe("[true]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Serialization_Tests, OneFalse)
|
||||
{
|
||||
array.add(false);
|
||||
|
||||
outputMustBe("[false]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Serialization_Tests, TwoBooleans)
|
||||
{
|
||||
array.add(false);
|
||||
array.add(true);
|
||||
|
||||
outputMustBe("[false,true]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Serialization_Tests, OneBooleanOverCapacity)
|
||||
{
|
||||
array.add(false);
|
||||
array.add(true);
|
||||
array.add(false);
|
||||
|
||||
outputMustBe("[false,true]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Serialization_Tests, OneEmptyNestedArray)
|
||||
{
|
||||
JsonArray<1> nestedArray;
|
||||
|
||||
array.add(nestedArray);
|
||||
|
||||
outputMustBe("[[]]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Serialization_Tests, OneEmptyNestedHash)
|
||||
{
|
||||
JsonObject<1> nestedObject;
|
||||
|
||||
array.add(nestedObject);
|
||||
|
||||
outputMustBe("[{}]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Serialization_Tests, OneNestedArrayWithOneInteger)
|
||||
{
|
||||
JsonArray<1> nestedArray;
|
||||
nestedArray.add(1);
|
||||
|
||||
array.add(nestedArray);
|
||||
|
||||
outputMustBe("[[1]]");
|
||||
}
|
||||
*/
|
@ -2,7 +2,7 @@
|
||||
#include <StaticJsonBuffer.h>
|
||||
#include <JsonValue.h>
|
||||
|
||||
class JsonObjectTests : public ::testing::Test
|
||||
class JsonObject_Container_Tests : public ::testing::Test
|
||||
{
|
||||
protected:
|
||||
virtual void SetUp()
|
||||
@ -14,12 +14,12 @@ protected:
|
||||
JsonObject object;
|
||||
};
|
||||
|
||||
TEST_F(JsonObjectTests, InitialSizeIsZero)
|
||||
TEST_F(JsonObject_Container_Tests, InitialSizeIsZero)
|
||||
{
|
||||
EXPECT_EQ(0, object.size());
|
||||
}
|
||||
|
||||
TEST_F(JsonObjectTests, Grow_WhenValuesAreAdded)
|
||||
TEST_F(JsonObject_Container_Tests, Grow_WhenValuesAreAdded)
|
||||
{
|
||||
object["hello"];
|
||||
EXPECT_EQ(1, object.size());
|
||||
@ -28,7 +28,7 @@ TEST_F(JsonObjectTests, Grow_WhenValuesAreAdded)
|
||||
EXPECT_EQ(2, object.size());
|
||||
}
|
||||
|
||||
TEST_F(JsonObjectTests, DoNotGrow_WhenSameValueIsAdded)
|
||||
TEST_F(JsonObject_Container_Tests, DoNotGrow_WhenSameValueIsAdded)
|
||||
{
|
||||
object["hello"];
|
||||
EXPECT_EQ(1, object.size());
|
||||
@ -37,7 +37,7 @@ TEST_F(JsonObjectTests, DoNotGrow_WhenSameValueIsAdded)
|
||||
EXPECT_EQ(1, object.size());
|
||||
}
|
||||
|
||||
TEST_F(JsonObjectTests, Shrink_WhenValuesAreRemoved)
|
||||
TEST_F(JsonObject_Container_Tests, Shrink_WhenValuesAreRemoved)
|
||||
{
|
||||
object["hello"];
|
||||
object["world"];
|
||||
@ -49,7 +49,7 @@ TEST_F(JsonObjectTests, Shrink_WhenValuesAreRemoved)
|
||||
EXPECT_EQ(0, object.size());
|
||||
}
|
||||
|
||||
TEST_F(JsonObjectTests, DoNotShrink_WhenRemoveIsCalledWithAWrongKey)
|
||||
TEST_F(JsonObject_Container_Tests, DoNotShrink_WhenRemoveIsCalledWithAWrongKey)
|
||||
{
|
||||
object["hello"];
|
||||
object["world"];
|
||||
@ -59,7 +59,7 @@ TEST_F(JsonObjectTests, DoNotShrink_WhenRemoveIsCalledWithAWrongKey)
|
||||
EXPECT_EQ(2, object.size());
|
||||
}
|
||||
|
||||
TEST_F(JsonObjectTests, CanStoreIntegers)
|
||||
TEST_F(JsonObject_Container_Tests, CanStoreIntegers)
|
||||
{
|
||||
object["hello"] = 123;
|
||||
object["world"] = 456;
|
||||
@ -68,7 +68,7 @@ TEST_F(JsonObjectTests, CanStoreIntegers)
|
||||
EXPECT_EQ(456, (int) object["world"]);
|
||||
}
|
||||
|
||||
TEST_F(JsonObjectTests, CanStoreDoubles)
|
||||
TEST_F(JsonObject_Container_Tests, CanStoreDoubles)
|
||||
{
|
||||
object["hello"] = 123.45;
|
||||
object["world"] = 456.78;
|
||||
@ -77,7 +77,7 @@ TEST_F(JsonObjectTests, CanStoreDoubles)
|
||||
EXPECT_EQ(456.78, (double) object["world"]);
|
||||
}
|
||||
|
||||
TEST_F(JsonObjectTests, CanStoreBooleans)
|
||||
TEST_F(JsonObject_Container_Tests, CanStoreBooleans)
|
||||
{
|
||||
object["hello"] = true;
|
||||
object["world"] = false;
|
||||
@ -86,7 +86,7 @@ TEST_F(JsonObjectTests, CanStoreBooleans)
|
||||
EXPECT_FALSE((bool) object["world"]);
|
||||
}
|
||||
|
||||
TEST_F(JsonObjectTests, CanStoreStrings)
|
||||
TEST_F(JsonObject_Container_Tests, CanStoreStrings)
|
||||
{
|
||||
object["hello"] = "h3110";
|
||||
object["world"] = "w0r1d";
|
||||
@ -95,7 +95,7 @@ TEST_F(JsonObjectTests, CanStoreStrings)
|
||||
EXPECT_STREQ("w0r1d", (const char*) object["world"]);
|
||||
}
|
||||
|
||||
TEST_F(JsonObjectTests, CanStoreInnerObjects)
|
||||
TEST_F(JsonObject_Container_Tests, CanStoreInnerObjects)
|
||||
{
|
||||
JsonObject innerObject1 = json.createObject();
|
||||
JsonObject innerObject2 = json.createObject();
|
@ -3,7 +3,7 @@
|
||||
#include <JsonValue.h>
|
||||
#include <StaticJsonBuffer.h>
|
||||
|
||||
class JsonObjectSerializationTests : public testing::Test
|
||||
class JsonObject_Serialization_Tests : public testing::Test
|
||||
{
|
||||
protected:
|
||||
virtual void SetUp()
|
||||
@ -24,19 +24,19 @@ protected:
|
||||
StaticJsonBuffer<5> json;
|
||||
};
|
||||
|
||||
TEST_F(JsonObjectSerializationTests, EmptyObject)
|
||||
TEST_F(JsonObject_Serialization_Tests, EmptyObject)
|
||||
{
|
||||
outputMustBe("{}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObjectSerializationTests, OneString)
|
||||
TEST_F(JsonObject_Serialization_Tests, OneString)
|
||||
{
|
||||
object["key"] = "value";
|
||||
|
||||
outputMustBe("{\"key\":\"value\"}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObjectSerializationTests, TwoStrings)
|
||||
TEST_F(JsonObject_Serialization_Tests, TwoStrings)
|
||||
{
|
||||
object["key1"] = "value1";
|
||||
object["key2"] = "value2";
|
||||
@ -44,7 +44,7 @@ TEST_F(JsonObjectSerializationTests, TwoStrings)
|
||||
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObjectSerializationTests, RemoveFirst)
|
||||
TEST_F(JsonObject_Serialization_Tests, RemoveFirst)
|
||||
{
|
||||
object["key1"] = "value1";
|
||||
object["key2"] = "value2";
|
||||
@ -53,7 +53,7 @@ TEST_F(JsonObjectSerializationTests, RemoveFirst)
|
||||
outputMustBe("{\"key2\":\"value2\"}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObjectSerializationTests, RemoveLast)
|
||||
TEST_F(JsonObject_Serialization_Tests, RemoveLast)
|
||||
{
|
||||
object["key1"] = "value1";
|
||||
object["key2"] = "value2";
|
||||
@ -62,7 +62,7 @@ TEST_F(JsonObjectSerializationTests, RemoveLast)
|
||||
outputMustBe("{\"key1\":\"value1\"}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObjectSerializationTests, RemoveUnexistingKey)
|
||||
TEST_F(JsonObject_Serialization_Tests, RemoveUnexistingKey)
|
||||
{
|
||||
object["key1"] = "value1";
|
||||
object["key2"] = "value2";
|
||||
@ -71,7 +71,7 @@ TEST_F(JsonObjectSerializationTests, RemoveUnexistingKey)
|
||||
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObjectSerializationTests, ReplaceExistingKey)
|
||||
TEST_F(JsonObject_Serialization_Tests, ReplaceExistingKey)
|
||||
{
|
||||
object["key"] = "value1";
|
||||
object["key"] = "value2";
|
||||
@ -79,7 +79,7 @@ TEST_F(JsonObjectSerializationTests, ReplaceExistingKey)
|
||||
outputMustBe("{\"key\":\"value2\"}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObjectSerializationTests, OneStringOverCapacity)
|
||||
TEST_F(JsonObject_Serialization_Tests, OneStringOverCapacity)
|
||||
{
|
||||
object["key1"] = "value1";
|
||||
object["key2"] = "value2";
|
||||
@ -88,43 +88,43 @@ TEST_F(JsonObjectSerializationTests, OneStringOverCapacity)
|
||||
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObjectSerializationTests, OneInteger)
|
||||
TEST_F(JsonObject_Serialization_Tests, OneInteger)
|
||||
{
|
||||
object["key"] = 1;
|
||||
outputMustBe("{\"key\":1}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObjectSerializationTests, OneDoubleFourDigits)
|
||||
TEST_F(JsonObject_Serialization_Tests, OneDoubleFourDigits)
|
||||
{
|
||||
object["key"].set(3.14159265358979323846, 4);
|
||||
outputMustBe("{\"key\":3.1416}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObjectSerializationTests, OneDoubleDefaultDigits)
|
||||
TEST_F(JsonObject_Serialization_Tests, OneDoubleDefaultDigits)
|
||||
{
|
||||
object["key"] = 3.14159265358979323846;
|
||||
outputMustBe("{\"key\":3.14}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObjectSerializationTests, OneNull)
|
||||
TEST_F(JsonObject_Serialization_Tests, OneNull)
|
||||
{
|
||||
object["key"] = (char*) 0;
|
||||
outputMustBe("{\"key\":null}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObjectSerializationTests, OneTrue)
|
||||
TEST_F(JsonObject_Serialization_Tests, OneTrue)
|
||||
{
|
||||
object["key"] = true;
|
||||
outputMustBe("{\"key\":true}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObjectSerializationTests, OneFalse)
|
||||
TEST_F(JsonObject_Serialization_Tests, OneFalse)
|
||||
{
|
||||
object["key"] = false;
|
||||
outputMustBe("{\"key\":false}");
|
||||
}
|
||||
/*
|
||||
TEST_F(JsonObjectSerializationTests, OneEmptyNestedArray)
|
||||
TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedArray)
|
||||
{
|
||||
auto nestedArray = JsonArray<1>();
|
||||
|
||||
@ -133,7 +133,7 @@ TEST_F(JsonObjectSerializationTests, OneEmptyNestedArray)
|
||||
outputMustBe("{\"key\":[]}");
|
||||
}
|
||||
*/
|
||||
TEST_F(JsonObjectSerializationTests, OneEmptyNestedObject)
|
||||
TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedObject)
|
||||
{
|
||||
auto nestedObject = json.createObject();
|
||||
|
@ -86,8 +86,10 @@
|
||||
<ClCompile Include="..\third-party\gtest-1.7.0\src\gtest-all.cc" />
|
||||
<ClCompile Include="..\third-party\gtest-1.7.0\src\gtest_main.cc" />
|
||||
<ClCompile Include="EscapedStringTests.cpp" />
|
||||
<ClCompile Include="JsonObjectSerializationTests.cpp" />
|
||||
<ClCompile Include="JsonObjectTests.cpp" />
|
||||
<ClCompile Include="JsonArray_Container_Tests.cpp" />
|
||||
<ClCompile Include="JsonArray_Serialization_Tests.cpp" />
|
||||
<ClCompile Include="JsonObject_Serialization_Tests.cpp" />
|
||||
<ClCompile Include="JsonObject_Container_Tests.cpp" />
|
||||
<ClCompile Include="JsonValueTests.cpp" />
|
||||
<ClCompile Include="StaticJsonBufferTests.cpp" />
|
||||
<ClCompile Include="StringBuilderTests.cpp" />
|
||||
|
@ -27,20 +27,26 @@
|
||||
<ClCompile Include="StaticJsonBufferTests.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="JsonObjectTests.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="JsonValueTests.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="JsonObjectSerializationTests.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="EscapedStringTests.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="StringBuilderTests.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="JsonArray_Serialization_Tests.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="JsonObject_Serialization_Tests.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="JsonObject_Container_Tests.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="JsonArray_Container_Tests.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
Reference in New Issue
Block a user