mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-19 13:32:24 +02:00
Cleaned JsonObject unit tests
This commit is contained in:
@ -86,7 +86,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="EscapedStringTests.cpp" />
|
<ClCompile Include="EscapedStringTests.cpp" />
|
||||||
<ClCompile Include="JsonArrayTests.cpp" />
|
<ClCompile Include="JsonArrayTests.cpp" />
|
||||||
<ClCompile Include="JsonHashTableTests.cpp" />
|
<ClCompile Include="JsonObject_PrintTo_Tests.cpp" />
|
||||||
<ClCompile Include="JsonValueTests.cpp" />
|
<ClCompile Include="JsonValueTests.cpp" />
|
||||||
<ClCompile Include="StringBuilderTests.cpp" />
|
<ClCompile Include="StringBuilderTests.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
@ -18,9 +18,6 @@
|
|||||||
<ClCompile Include="JsonArrayTests.cpp">
|
<ClCompile Include="JsonArrayTests.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<ClCompile Include="JsonHashTableTests.cpp">
|
|
||||||
<Filter>Source Files</Filter>
|
|
||||||
</ClCompile>
|
|
||||||
<ClCompile Include="JsonValueTests.cpp">
|
<ClCompile Include="JsonValueTests.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
@ -30,5 +27,8 @@
|
|||||||
<ClCompile Include="EscapedStringTests.cpp">
|
<ClCompile Include="EscapedStringTests.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="JsonObject_PrintTo_Tests.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
@ -12,13 +12,12 @@ using namespace ArduinoJson::Generator;
|
|||||||
|
|
||||||
namespace JsonGeneratorTests
|
namespace JsonGeneratorTests
|
||||||
{
|
{
|
||||||
TEST_CLASS(JsonHashTableTests)
|
TEST_CLASS(JsonObject_PrintTo_Tests)
|
||||||
{
|
{
|
||||||
JsonHashTable<2> hash;
|
JsonObject<2> object;
|
||||||
char buffer[256];
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
TEST_METHOD(Empty)
|
TEST_METHOD(Empty)
|
||||||
{
|
{
|
||||||
outputMustBe("{}");
|
outputMustBe("{}");
|
||||||
@ -26,105 +25,101 @@ namespace JsonGeneratorTests
|
|||||||
|
|
||||||
TEST_METHOD(OneString)
|
TEST_METHOD(OneString)
|
||||||
{
|
{
|
||||||
add("key", "value");
|
object["key"] = "value";
|
||||||
|
|
||||||
outputMustBe("{\"key\":\"value\"}");
|
outputMustBe("{\"key\":\"value\"}");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_METHOD(TwoStrings)
|
TEST_METHOD(TwoStrings)
|
||||||
{
|
{
|
||||||
add("key1", "value1");
|
object["key1"] = "value1";
|
||||||
add("key2", "value2");
|
object["key2"] = "value2";
|
||||||
|
|
||||||
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_METHOD(ReplaceExistingKey)
|
TEST_METHOD(ReplaceExistingKey)
|
||||||
{
|
{
|
||||||
add("key", "value1");
|
object["key"] = "value1";
|
||||||
add("key", "value2");
|
object["key"] = "value2";
|
||||||
|
|
||||||
outputMustBe("{\"key\":\"value2\"}");
|
outputMustBe("{\"key\":\"value2\"}");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_METHOD(OneStringOverCapacity)
|
TEST_METHOD(OneStringOverCapacity)
|
||||||
{
|
{
|
||||||
add("key1", "value1");
|
object["key1"] = "value1";
|
||||||
add("key2", "value2");
|
object["key2"] = "value2";
|
||||||
add("key3", "value3");
|
object["key3"] = "value3";
|
||||||
|
|
||||||
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_METHOD(OneInteger)
|
TEST_METHOD(OneInteger)
|
||||||
{
|
{
|
||||||
add("key", 1);
|
object["key"] = 1;
|
||||||
outputMustBe("{\"key\":1}");
|
outputMustBe("{\"key\":1}");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_METHOD(OneDoubleFourDigits)
|
TEST_METHOD(OneDoubleFourDigits)
|
||||||
{
|
{
|
||||||
add<4>("key", 3.14159265358979323846);
|
object["key"].set<4>(3.14159265358979323846);
|
||||||
outputMustBe("{\"key\":3.1416}");
|
outputMustBe("{\"key\":3.1416}");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_METHOD(OneDoubleDefaultDigits)
|
TEST_METHOD(OneDoubleDefaultDigits)
|
||||||
{
|
{
|
||||||
add("key", 3.14159265358979323846);
|
object["key"] = 3.14159265358979323846;
|
||||||
outputMustBe("{\"key\":3.14}");
|
outputMustBe("{\"key\":3.14}");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_METHOD(OneNull)
|
TEST_METHOD(OneNull)
|
||||||
{
|
{
|
||||||
add("key", (char*) 0);
|
object["key"] = (char*) 0;
|
||||||
outputMustBe("{\"key\":null}");
|
outputMustBe("{\"key\":null}");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_METHOD(OneTrue)
|
TEST_METHOD(OneTrue)
|
||||||
{
|
{
|
||||||
add("key", true);
|
object["key"] = true;
|
||||||
outputMustBe("{\"key\":true}");
|
outputMustBe("{\"key\":true}");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_METHOD(OneFalse)
|
TEST_METHOD(OneFalse)
|
||||||
{
|
{
|
||||||
add("key", false);
|
object["key"] = false;
|
||||||
outputMustBe("{\"key\":false}");
|
outputMustBe("{\"key\":false}");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_METHOD(OneEmptyNestedArray)
|
TEST_METHOD(OneEmptyNestedArray)
|
||||||
{
|
{
|
||||||
addNested("key", JsonArray<1>());
|
auto nestedArray = JsonArray<1>();
|
||||||
|
|
||||||
|
object["key"] = nestedArray;
|
||||||
|
|
||||||
outputMustBe("{\"key\":[]}");
|
outputMustBe("{\"key\":[]}");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_METHOD(OneEmptyNestedHash)
|
TEST_METHOD(OneEmptyNestedObject)
|
||||||
{
|
{
|
||||||
addNested("key", JsonHashTable<1>());
|
auto nestedObject = JsonObject<1>();
|
||||||
|
|
||||||
|
object["key"] = nestedObject;
|
||||||
|
|
||||||
outputMustBe("{\"key\":{}}");
|
outputMustBe("{\"key\":{}}");
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
void addNested(const char* key, Printable& value)
|
|
||||||
{
|
|
||||||
hash[key] = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
void add(const char* key, T value)
|
|
||||||
{
|
|
||||||
hash[key] = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<int DIGITS>
|
|
||||||
void add(const char* key, double value)
|
|
||||||
{
|
|
||||||
hash[key].set<DIGITS>(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
void outputMustBe(const char* expected)
|
void outputMustBe(const char* expected)
|
||||||
{
|
{
|
||||||
size_t actual = hash.printTo(buffer, sizeof(buffer));
|
char buffer[256];
|
||||||
|
size_t result;
|
||||||
|
|
||||||
|
result = object.printTo(buffer, sizeof(buffer));
|
||||||
|
|
||||||
|
Assert::AreEqual(strlen(expected), result);
|
||||||
Assert::AreEqual(expected, buffer);
|
Assert::AreEqual(expected, buffer);
|
||||||
Assert::AreEqual(strlen(expected), actual);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
Reference in New Issue
Block a user