2017-11-07 20:42:50 +01:00
|
|
|
// ArduinoJson - arduinojson.org
|
2020-01-09 15:48:38 +01:00
|
|
|
// Copyright Benoit Blanchon 2014-2020
|
2014-10-23 23:39:22 +02:00
|
|
|
// MIT License
|
|
|
|
|
2020-01-14 10:32:52 +01:00
|
|
|
#define ARDUINOJSON_ENABLE_ARDUINO_STRING 1
|
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>
|
2017-04-18 18:22:24 +02:00
|
|
|
#include <catch.hpp>
|
2019-08-12 14:21:45 +02:00
|
|
|
#include "custom_string.hpp"
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2018-10-02 16:54:05 +02:00
|
|
|
using namespace ARDUINOJSON_NAMESPACE;
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2018-07-12 09:08:20 +02:00
|
|
|
template <typename StringWriter>
|
|
|
|
static size_t print(StringWriter& sb, const char* s) {
|
|
|
|
return sb.write(reinterpret_cast<const uint8_t*>(s), strlen(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename StringWriter, typename String>
|
|
|
|
void common_tests(StringWriter& sb, 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") {
|
2018-07-12 09:08:20 +02:00
|
|
|
REQUIRE(0 == print(sb, ""));
|
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") {
|
2018-07-12 09:08:20 +02:00
|
|
|
REQUIRE(4 == print(sb, "ABCD"));
|
2017-04-18 18:22:24 +02:00
|
|
|
REQUIRE(std::string("ABCD") == 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") {
|
2018-07-12 09:08:20 +02:00
|
|
|
REQUIRE(4 == print(sb, "ABCD"));
|
|
|
|
REQUIRE(4 == print(sb, "EFGH"));
|
2017-04-18 18:22:24 +02:00
|
|
|
REQUIRE(std::string("ABCDEFGH") == output);
|
|
|
|
}
|
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") {
|
2017-04-22 11:33:40 +02:00
|
|
|
char output[20];
|
2018-07-12 09:08:20 +02:00
|
|
|
StaticStringWriter sb(output, sizeof(output));
|
2017-04-22 11:33:40 +02:00
|
|
|
|
|
|
|
common_tests(sb, static_cast<const char*>(output));
|
|
|
|
|
|
|
|
SECTION("OverCapacity") {
|
2018-07-12 09:08:20 +02:00
|
|
|
REQUIRE(19 == print(sb, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
|
|
|
|
REQUIRE(0 == print(sb, "ABC"));
|
2017-04-22 11:33:40 +02:00
|
|
|
REQUIRE(std::string("ABCDEFGHIJKLMNOPQRS") == output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 19:27:23 +01:00
|
|
|
TEST_CASE("Writer<std::string>") {
|
2017-04-22 11:33:40 +02:00
|
|
|
std::string output;
|
2019-10-31 19:27:23 +01:00
|
|
|
Writer<std::string> sb(output);
|
2017-04-22 11:33:40 +02:00
|
|
|
common_tests(sb, output);
|
|
|
|
}
|
2019-08-12 14:21:45 +02:00
|
|
|
|
2020-01-14 10:32:52 +01:00
|
|
|
TEST_CASE("Writer<String>") {
|
|
|
|
::String output;
|
|
|
|
Writer< ::String> sb(output);
|
2020-01-14 14:50:44 +01:00
|
|
|
|
2020-01-14 10:32:52 +01:00
|
|
|
common_tests(sb, output);
|
2020-01-14 14:50:44 +01:00
|
|
|
|
|
|
|
SECTION("Writes characters to temporary buffer") {
|
|
|
|
// accumulate in buffer
|
|
|
|
sb.write('a');
|
|
|
|
sb.write('b');
|
|
|
|
sb.write('c');
|
|
|
|
REQUIRE(output == "");
|
|
|
|
|
|
|
|
// flush when full
|
|
|
|
sb.write('d');
|
|
|
|
REQUIRE(output == "abcd");
|
|
|
|
|
|
|
|
// flush on destruction
|
|
|
|
sb.write('e');
|
|
|
|
sb.~Writer();
|
|
|
|
REQUIRE(output == "abcde");
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Writes strings to temporary buffer") {
|
|
|
|
// accumulate in buffer
|
|
|
|
print(sb, "abc");
|
|
|
|
REQUIRE(output == "");
|
|
|
|
|
|
|
|
// flush when full, and continue to accumulate
|
|
|
|
print(sb, "de");
|
|
|
|
REQUIRE(output == "abcd");
|
|
|
|
|
|
|
|
// flush on destruction
|
|
|
|
sb.~Writer();
|
|
|
|
REQUIRE(output == "abcde");
|
|
|
|
}
|
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;
|
2019-10-31 19:27:23 +01:00
|
|
|
Writer<custom_string> sb(output);
|
2019-08-12 14:21:45 +02:00
|
|
|
|
|
|
|
REQUIRE(4 == print(sb, "ABCD"));
|
|
|
|
REQUIRE("ABCD" == output);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("IsWriteableString") {
|
|
|
|
SECTION("std::string") {
|
|
|
|
REQUIRE(IsWriteableString<std::string>::value == true);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("custom_string") {
|
|
|
|
REQUIRE(IsWriteableString<custom_string>::value == true);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("basic_string<wchar_t>") {
|
|
|
|
REQUIRE(IsWriteableString<std::basic_string<wchar_t> >::value == false);
|
|
|
|
}
|
|
|
|
}
|