2021-03-29 17:14:01 +02:00
|
|
|
// ArduinoJson - https://arduinojson.org
|
2024-01-03 08:47:06 +01:00
|
|
|
// Copyright © 2014-2024, Benoit BLANCHON
|
2014-10-23 23:39:22 +02:00
|
|
|
// MIT License
|
|
|
|
|
2023-04-07 18:19:33 +02:00
|
|
|
#include <Arduino.h>
|
|
|
|
|
2020-01-14 14:50:44 +01:00
|
|
|
#define ARDUINOJSON_STRING_BUFFER_SIZE 5
|
2016-05-06 08:44:31 +02:00
|
|
|
#include <ArduinoJson.h>
|
2023-04-07 18:19:33 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
#include <catch.hpp>
|
2023-04-07 18:19:33 +02:00
|
|
|
|
2024-06-07 09:35:45 +02:00
|
|
|
#include "Literals.hpp"
|
2019-08-12 14:21:45 +02:00
|
|
|
#include "custom_string.hpp"
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2023-02-14 10:04:48 +01:00
|
|
|
using namespace ArduinoJson::detail;
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2018-07-12 09:08:20 +02:00
|
|
|
template <typename StringWriter>
|
2021-05-28 09:02:48 +02:00
|
|
|
static size_t print(StringWriter& writer, const char* s) {
|
|
|
|
return writer.write(reinterpret_cast<const uint8_t*>(s), strlen(s));
|
2018-07-12 09:08:20 +02:00
|
|
|
}
|
|
|
|
|
2021-09-10 08:40:02 +02:00
|
|
|
template <typename StringWriter>
|
|
|
|
static size_t print(StringWriter& writer, char c) {
|
|
|
|
return writer.write(static_cast<uint8_t>(c));
|
|
|
|
}
|
|
|
|
|
2018-07-12 09:08:20 +02:00
|
|
|
template <typename StringWriter, typename String>
|
2021-05-28 09:02:48 +02:00
|
|
|
void common_tests(StringWriter& writer, const String& output) {
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("InitialState") {
|
|
|
|
REQUIRE(std::string("") == output);
|
2017-01-06 21:07:34 +01:00
|
|
|
}
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("EmptyString") {
|
2021-05-28 09:02:48 +02:00
|
|
|
REQUIRE(0 == print(writer, ""));
|
2017-04-18 18:22:24 +02:00
|
|
|
REQUIRE(std::string("") == output);
|
2017-01-06 21:07:34 +01:00
|
|
|
}
|
2014-12-20 19:19:48 +01:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("OneString") {
|
2021-05-28 09:02:48 +02:00
|
|
|
REQUIRE(4 == print(writer, "ABCD"));
|
2024-06-07 09:35:45 +02:00
|
|
|
REQUIRE("ABCD"_s == output);
|
2017-01-06 21:07:34 +01:00
|
|
|
}
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("TwoStrings") {
|
2021-05-28 09:02:48 +02:00
|
|
|
REQUIRE(4 == print(writer, "ABCD"));
|
|
|
|
REQUIRE(4 == print(writer, "EFGH"));
|
2024-06-07 09:35:45 +02:00
|
|
|
REQUIRE("ABCDEFGH"_s == output);
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
2014-10-23 23:45:36 +02:00
|
|
|
}
|
2017-04-22 11:33:40 +02:00
|
|
|
|
2018-07-12 09:08:20 +02:00
|
|
|
TEST_CASE("StaticStringWriter") {
|
2021-04-29 20:59:15 +02:00
|
|
|
char output[20] = {0};
|
2021-05-28 09:02:48 +02:00
|
|
|
StaticStringWriter writer(output, sizeof(output));
|
2017-04-22 11:33:40 +02:00
|
|
|
|
2021-05-28 09:02:48 +02:00
|
|
|
common_tests(writer, static_cast<const char*>(output));
|
2017-04-22 11:33:40 +02:00
|
|
|
|
|
|
|
SECTION("OverCapacity") {
|
2021-05-28 09:02:48 +02:00
|
|
|
REQUIRE(20 == print(writer, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
|
|
|
|
REQUIRE(0 == print(writer, "ABC"));
|
2021-09-10 08:40:02 +02:00
|
|
|
REQUIRE(0 == print(writer, 'D'));
|
2021-04-29 20:59:15 +02:00
|
|
|
REQUIRE("ABCDEFGHIJKLMNOPQRST" == std::string(output, 20));
|
2017-04-22 11:33:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 19:27:23 +01:00
|
|
|
TEST_CASE("Writer<std::string>") {
|
2017-04-22 11:33:40 +02:00
|
|
|
std::string output;
|
2021-05-28 09:02:48 +02:00
|
|
|
Writer<std::string> writer(output);
|
|
|
|
common_tests(writer, output);
|
2017-04-22 11:33:40 +02:00
|
|
|
}
|
2019-08-12 14:21:45 +02:00
|
|
|
|
2020-01-14 10:32:52 +01:00
|
|
|
TEST_CASE("Writer<String>") {
|
|
|
|
::String output;
|
2023-02-16 11:51:02 +01:00
|
|
|
Writer<::String> writer(output);
|
2021-05-28 09:02:48 +02:00
|
|
|
|
|
|
|
SECTION("write(char)") {
|
|
|
|
SECTION("writes to temporary buffer") {
|
|
|
|
// accumulate in buffer
|
|
|
|
writer.write('a');
|
|
|
|
writer.write('b');
|
|
|
|
writer.write('c');
|
|
|
|
writer.write('d');
|
|
|
|
REQUIRE(output == "");
|
|
|
|
|
|
|
|
// flush when full
|
|
|
|
writer.write('e');
|
|
|
|
REQUIRE(output == "abcd");
|
|
|
|
|
|
|
|
// flush on destruction
|
|
|
|
writer.write('f');
|
|
|
|
writer.~Writer();
|
|
|
|
REQUIRE(output == "abcdef");
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("returns 1 on success") {
|
|
|
|
for (int i = 0; i < ARDUINOJSON_STRING_BUFFER_SIZE; i++) {
|
|
|
|
REQUIRE(writer.write('x') == 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("returns 0 on error") {
|
|
|
|
output.limitCapacityTo(1);
|
|
|
|
|
|
|
|
REQUIRE(writer.write('a') == 1);
|
|
|
|
REQUIRE(writer.write('b') == 1);
|
|
|
|
REQUIRE(writer.write('c') == 1);
|
|
|
|
REQUIRE(writer.write('d') == 1);
|
|
|
|
REQUIRE(writer.write('e') == 0);
|
|
|
|
REQUIRE(writer.write('f') == 0);
|
|
|
|
}
|
2020-01-14 14:50:44 +01:00
|
|
|
}
|
|
|
|
|
2021-05-28 09:02:48 +02:00
|
|
|
SECTION("write(char*, size_t)") {
|
|
|
|
SECTION("empty string") {
|
|
|
|
REQUIRE(0 == print(writer, ""));
|
|
|
|
writer.flush();
|
|
|
|
REQUIRE(output == "");
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("writes to temporary buffer") {
|
|
|
|
// accumulate in buffer
|
|
|
|
print(writer, "abc");
|
|
|
|
REQUIRE(output == "");
|
|
|
|
|
|
|
|
// flush when full, and continue to accumulate
|
|
|
|
print(writer, "de");
|
|
|
|
REQUIRE(output == "abcd");
|
|
|
|
|
|
|
|
// flush on destruction
|
|
|
|
writer.~Writer();
|
|
|
|
REQUIRE(output == "abcde");
|
|
|
|
}
|
2020-01-14 14:50:44 +01:00
|
|
|
}
|
2020-01-14 10:32:52 +01:00
|
|
|
}
|
|
|
|
|
2019-10-31 19:27:23 +01:00
|
|
|
TEST_CASE("Writer<custom_string>") {
|
2019-08-12 14:21:45 +02:00
|
|
|
custom_string output;
|
2021-05-28 09:02:48 +02:00
|
|
|
Writer<custom_string> writer(output);
|
2019-08-12 14:21:45 +02:00
|
|
|
|
2021-05-28 09:02:48 +02:00
|
|
|
REQUIRE(4 == print(writer, "ABCD"));
|
2019-08-12 14:21:45 +02:00
|
|
|
REQUIRE("ABCD" == output);
|
|
|
|
}
|
|
|
|
|
2021-05-28 09:02:48 +02:00
|
|
|
TEST_CASE("serializeJson(doc, String)") {
|
2023-07-17 18:15:13 +02:00
|
|
|
JsonDocument doc;
|
2021-05-28 09:02:48 +02:00
|
|
|
doc["hello"] = "world";
|
2023-07-31 18:37:35 +02:00
|
|
|
::String output = "erase me";
|
2021-05-28 09:02:48 +02:00
|
|
|
|
|
|
|
SECTION("sufficient capacity") {
|
|
|
|
serializeJson(doc, output);
|
|
|
|
REQUIRE(output == "{\"hello\":\"world\"}");
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("unsufficient capacity") { // issue #1561
|
|
|
|
output.limitCapacityTo(10);
|
|
|
|
serializeJson(doc, output);
|
|
|
|
REQUIRE(output == "{\"hello\"");
|
|
|
|
}
|
|
|
|
}
|