mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-23 15:27:30 +02:00
Tests: add user-defined literal ""_s
for std::string
This commit is contained in:
@ -5,6 +5,8 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Literals.hpp"
|
||||
|
||||
typedef ArduinoJson::detail::ElementProxy<JsonDocument&> ElementProxy;
|
||||
|
||||
TEST_CASE("ElementProxy::add()") {
|
||||
@ -121,7 +123,7 @@ TEST_CASE("ElementProxy::remove()") {
|
||||
ep["a"] = 1;
|
||||
ep["b"] = 2;
|
||||
|
||||
ep.remove(std::string("b"));
|
||||
ep.remove("b"_s);
|
||||
|
||||
REQUIRE(ep.as<std::string>() == "{\"a\":1}");
|
||||
}
|
||||
|
@ -9,6 +9,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
#include "Literals.hpp"
|
||||
|
||||
using ArduinoJson::detail::sizeofArray;
|
||||
using ArduinoJson::detail::sizeofObject;
|
||||
@ -106,8 +107,8 @@ TEST_CASE("MemberProxy::containsKey()") {
|
||||
SECTION("containsKey(std::string)") {
|
||||
mp["key"] = "value";
|
||||
|
||||
REQUIRE(mp.containsKey(std::string("key")) == true);
|
||||
REQUIRE(mp.containsKey(std::string("key")) == true);
|
||||
REQUIRE(mp.containsKey("key"_s) == true);
|
||||
REQUIRE(mp.containsKey("key"_s) == true);
|
||||
}
|
||||
}
|
||||
|
||||
@ -117,8 +118,8 @@ TEST_CASE("MemberProxy::operator|()") {
|
||||
SECTION("const char*") {
|
||||
doc["a"] = "hello";
|
||||
|
||||
REQUIRE((doc["a"] | "world") == std::string("hello"));
|
||||
REQUIRE((doc["b"] | "world") == std::string("world"));
|
||||
REQUIRE((doc["a"] | "world") == "hello"_s);
|
||||
REQUIRE((doc["b"] | "world") == "world"_s);
|
||||
}
|
||||
|
||||
SECTION("Issue #1411") {
|
||||
@ -128,7 +129,7 @@ TEST_CASE("MemberProxy::operator|()") {
|
||||
// to trigger the bug
|
||||
const char* sensor = doc["sensor"] | test; // "gps"
|
||||
|
||||
REQUIRE(sensor == std::string("gps"));
|
||||
REQUIRE(sensor == "gps"_s);
|
||||
}
|
||||
|
||||
SECTION("Issue #1415") {
|
||||
@ -170,7 +171,7 @@ TEST_CASE("MemberProxy::remove()") {
|
||||
mp["a"] = 1;
|
||||
mp["b"] = 2;
|
||||
|
||||
mp.remove(std::string("b"));
|
||||
mp.remove("b"_s);
|
||||
|
||||
REQUIRE(mp.as<std::string>() == "{\"a\":1}");
|
||||
}
|
||||
@ -286,8 +287,8 @@ TEST_CASE("Deduplicate keys") {
|
||||
JsonDocument doc(&spy);
|
||||
|
||||
SECTION("std::string") {
|
||||
doc[0][std::string("example")] = 1;
|
||||
doc[1][std::string("example")] = 2;
|
||||
doc[0]["example"_s] = 1;
|
||||
doc[1]["example"_s] = 2;
|
||||
|
||||
const char* key1 = doc[0].as<JsonObject>().begin()->key().c_str();
|
||||
const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str();
|
||||
@ -351,7 +352,7 @@ TEST_CASE("MemberProxy under memory constraints") {
|
||||
SECTION("key allocation fails") {
|
||||
killswitch.on();
|
||||
|
||||
doc[std::string("hello")] = "world";
|
||||
doc["hello"_s] = "world";
|
||||
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
REQUIRE(doc.size() == 0);
|
||||
|
@ -9,6 +9,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
#include "Literals.hpp"
|
||||
|
||||
using ArduinoJson::detail::sizeofArray;
|
||||
|
||||
@ -35,8 +36,8 @@ TEST_CASE("JsonDocument::add(T)") {
|
||||
}
|
||||
|
||||
SECTION("std::string") {
|
||||
doc.add(std::string("example"));
|
||||
doc.add(std::string("example"));
|
||||
doc.add("example"_s);
|
||||
doc.add("example"_s);
|
||||
|
||||
CHECK(doc[0].as<const char*>() == doc[1].as<const char*>());
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
@ -99,7 +100,7 @@ TEST_CASE("JsonDocument::add<T>()") {
|
||||
|
||||
TEST_CASE("JsonObject::add(JsonObject) ") {
|
||||
JsonDocument doc1;
|
||||
doc1[std::string("hello")] = std::string("world");
|
||||
doc1["hello"_s] = "world"_s;
|
||||
|
||||
TimebombAllocator allocator(10);
|
||||
SpyingAllocator spy(&allocator);
|
||||
|
@ -6,6 +6,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
#include "Literals.hpp"
|
||||
|
||||
TEST_CASE("JsonDocument assignment") {
|
||||
SpyingAllocator spyingAllocator;
|
||||
@ -62,7 +63,7 @@ TEST_CASE("JsonDocument assignment") {
|
||||
SECTION("Move assign") {
|
||||
{
|
||||
JsonDocument doc1(&spyingAllocator);
|
||||
doc1[std::string("hello")] = std::string("world");
|
||||
doc1["hello"_s] = "world"_s;
|
||||
JsonDocument doc2(&spyingAllocator);
|
||||
|
||||
doc2 = std::move(doc1);
|
||||
|
@ -9,6 +9,7 @@
|
||||
#include <string>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
#include "Literals.hpp"
|
||||
|
||||
TEST_CASE("JsonDocument::clear()") {
|
||||
SpyingAllocator spy;
|
||||
@ -22,7 +23,7 @@ TEST_CASE("JsonDocument::clear()") {
|
||||
}
|
||||
|
||||
SECTION("releases resources") {
|
||||
doc[std::string("hello")] = std::string("world");
|
||||
doc["hello"_s] = "world"_s;
|
||||
spy.clearLog();
|
||||
|
||||
doc.clear();
|
||||
|
@ -6,6 +6,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
#include "Literals.hpp"
|
||||
|
||||
using ArduinoJson::detail::addPadding;
|
||||
|
||||
@ -20,7 +21,7 @@ TEST_CASE("JsonDocument constructor") {
|
||||
SECTION("JsonDocument(const JsonDocument&)") {
|
||||
{
|
||||
JsonDocument doc1(&spyingAllocator);
|
||||
doc1.set(std::string("The size of this string is 32!!"));
|
||||
doc1.set("The size of this string is 32!!"_s);
|
||||
|
||||
JsonDocument doc2(doc1);
|
||||
|
||||
@ -38,7 +39,7 @@ TEST_CASE("JsonDocument constructor") {
|
||||
SECTION("JsonDocument(JsonDocument&&)") {
|
||||
{
|
||||
JsonDocument doc1(&spyingAllocator);
|
||||
doc1.set(std::string("The size of this string is 32!!"));
|
||||
doc1.set("The size of this string is 32!!"_s);
|
||||
|
||||
JsonDocument doc2(std::move(doc1));
|
||||
|
||||
|
@ -5,6 +5,8 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Literals.hpp"
|
||||
|
||||
TEST_CASE("JsonDocument::containsKey()") {
|
||||
JsonDocument doc;
|
||||
|
||||
@ -23,7 +25,7 @@ TEST_CASE("JsonDocument::containsKey()") {
|
||||
SECTION("returns true when key is a std::string") {
|
||||
doc["hello"] = "world";
|
||||
|
||||
REQUIRE(doc.containsKey(std::string("hello")) == true);
|
||||
REQUIRE(doc.containsKey("hello"_s) == true);
|
||||
}
|
||||
|
||||
SECTION("returns false on object") {
|
||||
|
@ -2,6 +2,8 @@
|
||||
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Literals.hpp"
|
||||
|
||||
TEST_CASE("Issue #1120") {
|
||||
JsonDocument doc;
|
||||
constexpr char str[] =
|
||||
@ -10,12 +12,12 @@ TEST_CASE("Issue #1120") {
|
||||
|
||||
SECTION("MemberProxy<std::string>::isNull()") {
|
||||
SECTION("returns false") {
|
||||
auto value = doc[std::string("contents")];
|
||||
auto value = doc["contents"_s];
|
||||
CHECK(value.isNull() == false);
|
||||
}
|
||||
|
||||
SECTION("returns true") {
|
||||
auto value = doc[std::string("zontents")];
|
||||
auto value = doc["zontents"_s];
|
||||
CHECK(value.isNull() == true);
|
||||
}
|
||||
}
|
||||
@ -46,12 +48,12 @@ TEST_CASE("Issue #1120") {
|
||||
|
||||
SECTION("MemberProxy<ElementProxy<MemberProxy>, std::string>::isNull()") {
|
||||
SECTION("returns false") {
|
||||
auto value = doc["contents"][1][std::string("module")];
|
||||
auto value = doc["contents"][1]["module"_s];
|
||||
CHECK(value.isNull() == false);
|
||||
}
|
||||
|
||||
SECTION("returns true") {
|
||||
auto value = doc["contents"][1][std::string("zodule")];
|
||||
auto value = doc["contents"][1]["zodule"_s];
|
||||
CHECK(value.isNull() == true);
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
#include "Literals.hpp"
|
||||
|
||||
TEST_CASE("JsonDocument::overflowed()") {
|
||||
TimebombAllocator timebomb(10);
|
||||
@ -30,13 +31,13 @@ TEST_CASE("JsonDocument::overflowed()") {
|
||||
|
||||
SECTION("returns true after a failed string copy") {
|
||||
timebomb.setCountdown(0);
|
||||
doc.add(std::string("example"));
|
||||
doc.add("example"_s);
|
||||
CHECK(doc.overflowed() == true);
|
||||
}
|
||||
|
||||
SECTION("returns false after a successful string copy") {
|
||||
timebomb.setCountdown(3);
|
||||
doc.add(std::string("example"));
|
||||
doc.add("example"_s);
|
||||
CHECK(doc.overflowed() == false);
|
||||
}
|
||||
|
||||
|
@ -5,6 +5,8 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Literals.hpp"
|
||||
|
||||
TEST_CASE("JsonDocument::remove()") {
|
||||
JsonDocument doc;
|
||||
|
||||
@ -31,7 +33,7 @@ TEST_CASE("JsonDocument::remove()") {
|
||||
doc["a"] = 1;
|
||||
doc["b"] = 2;
|
||||
|
||||
doc.remove(std::string("b"));
|
||||
doc.remove("b"_s);
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "{\"a\":1}");
|
||||
}
|
||||
|
@ -9,6 +9,7 @@
|
||||
#include <string>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
#include "Literals.hpp"
|
||||
|
||||
using ArduinoJson::detail::sizeofArray;
|
||||
using ArduinoJson::detail::sizeofObject;
|
||||
@ -78,7 +79,7 @@ TEST_CASE("JsonDocument::shrinkToFit()") {
|
||||
}
|
||||
|
||||
SECTION("owned string") {
|
||||
doc.set(std::string("abcdefg"));
|
||||
doc.set("abcdefg"_s);
|
||||
REQUIRE(doc.as<std::string>() == "abcdefg");
|
||||
|
||||
doc.shrinkToFit();
|
||||
@ -114,7 +115,7 @@ TEST_CASE("JsonDocument::shrinkToFit()") {
|
||||
}
|
||||
|
||||
SECTION("owned key") {
|
||||
doc[std::string("abcdefg")] = 42;
|
||||
doc["abcdefg"_s] = 42;
|
||||
|
||||
doc.shrinkToFit();
|
||||
|
||||
@ -141,7 +142,7 @@ TEST_CASE("JsonDocument::shrinkToFit()") {
|
||||
}
|
||||
|
||||
SECTION("owned string in array") {
|
||||
doc.add(std::string("abcdefg"));
|
||||
doc.add("abcdefg"_s);
|
||||
|
||||
doc.shrinkToFit();
|
||||
|
||||
@ -168,7 +169,7 @@ TEST_CASE("JsonDocument::shrinkToFit()") {
|
||||
}
|
||||
|
||||
SECTION("owned string in object") {
|
||||
doc["key"] = std::string("abcdefg");
|
||||
doc["key"] = "abcdefg"_s;
|
||||
|
||||
doc.shrinkToFit();
|
||||
|
||||
|
@ -5,6 +5,8 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Literals.hpp"
|
||||
|
||||
TEST_CASE("JsonDocument::operator[]") {
|
||||
JsonDocument doc;
|
||||
const JsonDocument& cdoc = doc;
|
||||
@ -18,8 +20,8 @@ TEST_CASE("JsonDocument::operator[]") {
|
||||
}
|
||||
|
||||
SECTION("std::string") {
|
||||
REQUIRE(doc[std::string("hello")] == "world");
|
||||
REQUIRE(cdoc[std::string("hello")] == "world");
|
||||
REQUIRE(doc["hello"_s] == "world");
|
||||
REQUIRE(cdoc["hello"_s] == "world");
|
||||
}
|
||||
|
||||
SECTION("JsonVariant") {
|
||||
@ -29,8 +31,8 @@ TEST_CASE("JsonDocument::operator[]") {
|
||||
}
|
||||
|
||||
SECTION("supports operator|") {
|
||||
REQUIRE((doc["hello"] | "nope") == std::string("world"));
|
||||
REQUIRE((doc["world"] | "nope") == std::string("nope"));
|
||||
REQUIRE((doc["hello"] | "nope") == "world"_s);
|
||||
REQUIRE((doc["world"] | "nope") == "nope"_s);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user