Tests: add user-defined literal ""_s for std::string

This commit is contained in:
Benoit Blanchon
2024-06-07 09:35:45 +02:00
parent 5b88b2c1f6
commit 45611924f3
55 changed files with 316 additions and 229 deletions

View File

@ -5,6 +5,8 @@
#include <ArduinoJson.h>
#include <catch.hpp>
#include "Literals.hpp"
static void eraseString(std::string& str) {
char* p = const_cast<char*>(str.c_str());
while (*p)
@ -20,7 +22,7 @@ TEST_CASE("std::string") {
deserializeJson(doc, json);
JsonObject obj = doc.as<JsonObject>();
REQUIRE(std::string("value") == obj[std::string("key")]);
REQUIRE("value"_s == obj["key"_s]);
}
SECTION("operator[] const") {
@ -29,21 +31,21 @@ TEST_CASE("std::string") {
deserializeJson(doc, json);
JsonObject obj = doc.as<JsonObject>();
REQUIRE(std::string("value") == obj[std::string("key")]);
REQUIRE("value"_s == obj["key"_s]);
}
SECTION("containsKey()") {
char json[] = "{\"key\":\"value\"}";
deserializeJson(doc, json);
JsonObject obj = doc.as<JsonObject>();
REQUIRE(true == obj.containsKey(std::string("key")));
REQUIRE(true == obj.containsKey("key"_s));
}
SECTION("remove()") {
JsonObject obj = doc.to<JsonObject>();
obj["key"] = "value";
obj.remove(std::string("key"));
obj.remove("key"_s);
REQUIRE(0 == obj.size());
}
@ -53,7 +55,7 @@ TEST_CASE("std::string") {
JsonObject obj = doc.to<JsonObject>();
obj[key] = "world";
eraseString(key);
REQUIRE(std::string("world") == obj["hello"]);
REQUIRE("world"_s == obj["hello"]);
}
SECTION("operator[], set value") {
@ -61,6 +63,6 @@ TEST_CASE("std::string") {
JsonObject obj = doc.to<JsonObject>();
obj["hello"] = value;
eraseString(value);
REQUIRE(std::string("world") == obj["hello"]);
REQUIRE("world"_s == obj["hello"]);
}
}