2017-11-07 20:42:50 +01:00
|
|
|
// ArduinoJson - arduinojson.org
|
2018-01-05 09:20:01 +01:00
|
|
|
// Copyright Benoit Blanchon 2014-2018
|
2014-10-23 23:39:22 +02:00
|
|
|
// MIT License
|
|
|
|
|
2016-05-06 08:44:31 +02:00
|
|
|
#include <ArduinoJson.h>
|
2017-04-18 18:22:24 +02:00
|
|
|
#include <catch.hpp>
|
2014-10-16 16:23:24 +02:00
|
|
|
|
|
|
|
using namespace ArduinoJson::Internals;
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-12 09:08:20 +02:00
|
|
|
TEST_CASE("DynamicStringWriter") {
|
2017-04-22 11:33:40 +02:00
|
|
|
std::string output;
|
2018-07-12 09:08:20 +02:00
|
|
|
DynamicStringWriter<std::string> sb(output);
|
2017-04-22 11:33:40 +02:00
|
|
|
common_tests(sb, output);
|
|
|
|
}
|