forked from bblanchon/ArduinoJson
Added support for non zero-terminated strings (fixes #704)
This commit is contained in:
@ -8,6 +8,8 @@ add_executable(JsonSerializerTests
|
||||
JsonObject.cpp
|
||||
JsonObjectPretty.cpp
|
||||
JsonVariant.cpp
|
||||
std_stream.cpp
|
||||
std_string.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(JsonSerializerTests catch)
|
||||
|
64
test/JsonSerializer/std_stream.cpp
Normal file
64
test/JsonSerializer/std_stream.cpp
Normal file
@ -0,0 +1,64 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
#include <sstream>
|
||||
|
||||
TEST_CASE("operator<<(std::ostream)") {
|
||||
DynamicJsonDocument doc;
|
||||
std::ostringstream os;
|
||||
|
||||
SECTION("JsonVariant containing false") {
|
||||
JsonVariant variant = false;
|
||||
|
||||
os << variant;
|
||||
|
||||
REQUIRE("false" == os.str());
|
||||
}
|
||||
|
||||
SECTION("JsonVariant containing string") {
|
||||
JsonVariant variant = "coucou";
|
||||
|
||||
os << variant;
|
||||
|
||||
REQUIRE("\"coucou\"" == os.str());
|
||||
}
|
||||
|
||||
SECTION("JsonObject") {
|
||||
JsonObject& object = doc.to<JsonObject>();
|
||||
object["key"] = "value";
|
||||
|
||||
os << object;
|
||||
|
||||
REQUIRE("{\"key\":\"value\"}" == os.str());
|
||||
}
|
||||
|
||||
SECTION("JsonObjectSubscript") {
|
||||
JsonObject& object = doc.to<JsonObject>();
|
||||
object["key"] = "value";
|
||||
|
||||
os << object["key"];
|
||||
|
||||
REQUIRE("\"value\"" == os.str());
|
||||
}
|
||||
|
||||
SECTION("JsonArray") {
|
||||
JsonArray& array = doc.to<JsonArray>();
|
||||
array.add("value");
|
||||
|
||||
os << array;
|
||||
|
||||
REQUIRE("[\"value\"]" == os.str());
|
||||
}
|
||||
|
||||
SECTION("JsonArraySubscript") {
|
||||
JsonArray& array = doc.to<JsonArray>();
|
||||
array.add("value");
|
||||
|
||||
os << array[0];
|
||||
|
||||
REQUIRE("\"value\"" == os.str());
|
||||
}
|
||||
}
|
47
test/JsonSerializer/std_string.cpp
Normal file
47
test/JsonSerializer/std_string.cpp
Normal file
@ -0,0 +1,47 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("serialize JsonArray to std::string") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonArray &array = doc.to<JsonArray>();
|
||||
array.add(4);
|
||||
array.add(2);
|
||||
|
||||
SECTION("serializeJson()") {
|
||||
std::string json;
|
||||
serializeJson(array, json);
|
||||
|
||||
REQUIRE(std::string("[4,2]") == json);
|
||||
}
|
||||
|
||||
SECTION("serializeJsonPretty") {
|
||||
std::string json;
|
||||
serializeJsonPretty(array, json);
|
||||
|
||||
REQUIRE(std::string("[\r\n 4,\r\n 2\r\n]") == json);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("serialize JsonObject to std::string") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject &obj = doc.to<JsonObject>();
|
||||
obj["key"] = "value";
|
||||
|
||||
SECTION("object") {
|
||||
std::string json;
|
||||
serializeJson(doc, json);
|
||||
|
||||
REQUIRE(std::string("{\"key\":\"value\"}") == json);
|
||||
}
|
||||
|
||||
SECTION("serializeJsonPretty") {
|
||||
std::string json;
|
||||
serializeJsonPretty(doc, json);
|
||||
|
||||
REQUIRE(std::string("{\r\n \"key\": \"value\"\r\n}") == json);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user