
【主 题】:添加库文件 【描 述】: [原因]: [过程]: [影响]: 【结 束】 # 类型 包含: # feat:新功能(feature) # fix:修补bug # docs:文档(documentation) # style: 格式(不影响代码运行的变动) # refactor:重构(即不是新增功能,也不是修改bug的代码变动) # test:增加测试 # chore:构建过程或辅助工具的变动
78 lines
1.9 KiB
C++
78 lines
1.9 KiB
C++
// ArduinoJson - https://arduinojson.org
|
|
// Copyright © 2014-2022, Benoit BLANCHON
|
|
// MIT License
|
|
|
|
#include <ArduinoJson.h>
|
|
#include <catch.hpp>
|
|
#include <string>
|
|
|
|
static void checkObjectPretty(const JsonObject obj,
|
|
const std::string expected) {
|
|
char json[256];
|
|
|
|
size_t actualLen = serializeJsonPretty(obj, json);
|
|
size_t measuredLen = measureJsonPretty(obj);
|
|
|
|
REQUIRE(json == expected);
|
|
REQUIRE(expected.size() == actualLen);
|
|
REQUIRE(expected.size() == measuredLen);
|
|
}
|
|
|
|
TEST_CASE("serializeJsonPretty(JsonObject)") {
|
|
DynamicJsonDocument doc(4096);
|
|
JsonObject obj = doc.to<JsonObject>();
|
|
|
|
SECTION("EmptyObject") {
|
|
checkObjectPretty(obj, "{}");
|
|
}
|
|
|
|
SECTION("OneMember") {
|
|
obj["key"] = "value";
|
|
|
|
checkObjectPretty(obj,
|
|
"{\r\n"
|
|
" \"key\": \"value\"\r\n"
|
|
"}");
|
|
}
|
|
|
|
SECTION("TwoMembers") {
|
|
obj["key1"] = "value1";
|
|
obj["key2"] = "value2";
|
|
|
|
checkObjectPretty(obj,
|
|
"{\r\n"
|
|
" \"key1\": \"value1\",\r\n"
|
|
" \"key2\": \"value2\"\r\n"
|
|
"}");
|
|
}
|
|
|
|
SECTION("EmptyNestedContainers") {
|
|
obj.createNestedObject("key1");
|
|
obj.createNestedArray("key2");
|
|
|
|
checkObjectPretty(obj,
|
|
"{\r\n"
|
|
" \"key1\": {},\r\n"
|
|
" \"key2\": []\r\n"
|
|
"}");
|
|
}
|
|
|
|
SECTION("NestedContainers") {
|
|
JsonObject nested1 = obj.createNestedObject("key1");
|
|
nested1["a"] = 1;
|
|
|
|
JsonArray nested2 = obj.createNestedArray("key2");
|
|
nested2.add(2);
|
|
|
|
checkObjectPretty(obj,
|
|
"{\r\n"
|
|
" \"key1\": {\r\n"
|
|
" \"a\": 1\r\n"
|
|
" },\r\n"
|
|
" \"key2\": [\r\n"
|
|
" 2\r\n"
|
|
" ]\r\n"
|
|
"}");
|
|
}
|
|
}
|