Files
ArduinoJson/extras/tests/Misc/StringWriter.cpp

79 lines
1.8 KiB
C++
Raw Normal View History

// ArduinoJson - arduinojson.org
2019-02-15 13:31:46 +01:00
// Copyright Benoit Blanchon 2014-2019
2014-10-23 23:39:22 +02:00
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include "custom_string.hpp"
2014-10-16 16:23:24 +02:00
using namespace ARDUINOJSON_NAMESPACE;
2014-10-16 16:23:24 +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) {
SECTION("InitialState") {
REQUIRE(std::string("") == output);
}
2014-10-16 16:23:24 +02:00
SECTION("EmptyString") {
REQUIRE(0 == print(sb, ""));
REQUIRE(std::string("") == output);
}
2014-12-20 19:19:48 +01:00
SECTION("OneString") {
REQUIRE(4 == print(sb, "ABCD"));
REQUIRE(std::string("ABCD") == output);
}
2014-10-16 16:23:24 +02:00
SECTION("TwoStrings") {
REQUIRE(4 == print(sb, "ABCD"));
REQUIRE(4 == print(sb, "EFGH"));
REQUIRE(std::string("ABCDEFGH") == output);
}
2014-10-23 23:45:36 +02:00
}
TEST_CASE("StaticStringWriter") {
char output[20];
StaticStringWriter sb(output, sizeof(output));
common_tests(sb, static_cast<const char*>(output));
SECTION("OverCapacity") {
REQUIRE(19 == print(sb, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
REQUIRE(0 == print(sb, "ABC"));
REQUIRE(std::string("ABCDEFGHIJKLMNOPQRS") == output);
}
}
2019-10-31 19:27:23 +01:00
TEST_CASE("Writer<std::string>") {
std::string output;
2019-10-31 19:27:23 +01:00
Writer<std::string> sb(output);
common_tests(sb, output);
}
2019-10-31 19:27:23 +01:00
TEST_CASE("Writer<custom_string>") {
custom_string output;
2019-10-31 19:27:23 +01:00
Writer<custom_string> sb(output);
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);
}
}