mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-23 23:37:37 +02:00
Tests: add user-defined literal ""_s
for std::string
This commit is contained in:
@ -8,6 +8,7 @@
|
||||
#include <string>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
#include "Literals.hpp"
|
||||
|
||||
using ArduinoJson::detail::sizeofArray;
|
||||
using ArduinoJson::detail::sizeofObject;
|
||||
@ -15,7 +16,7 @@ using ArduinoJson::detail::sizeofObject;
|
||||
TEST_CASE("deserializeJson(JsonDocument&)") {
|
||||
SpyingAllocator spy;
|
||||
JsonDocument doc(&spy);
|
||||
doc.add(std::string("hello"));
|
||||
doc.add("hello"_s);
|
||||
spy.clearLog();
|
||||
|
||||
auto err = deserializeJson(doc, "[42]");
|
||||
@ -34,7 +35,7 @@ TEST_CASE("deserializeJson(JsonVariant)") {
|
||||
SECTION("variant is bound") {
|
||||
SpyingAllocator spy;
|
||||
JsonDocument doc(&spy);
|
||||
doc.add(std::string("hello"));
|
||||
doc.add("hello"_s);
|
||||
spy.clearLog();
|
||||
|
||||
JsonVariant variant = doc[0];
|
||||
@ -60,7 +61,7 @@ TEST_CASE("deserializeJson(JsonVariant)") {
|
||||
TEST_CASE("deserializeJson(ElementProxy)") {
|
||||
SpyingAllocator spy;
|
||||
JsonDocument doc(&spy);
|
||||
doc.add(std::string("hello"));
|
||||
doc.add("hello"_s);
|
||||
spy.clearLog();
|
||||
|
||||
SECTION("element already exists") {
|
||||
@ -85,7 +86,7 @@ TEST_CASE("deserializeJson(ElementProxy)") {
|
||||
TEST_CASE("deserializeJson(MemberProxy)") {
|
||||
SpyingAllocator spy;
|
||||
JsonDocument doc(&spy);
|
||||
doc[std::string("hello")] = std::string("world");
|
||||
doc["hello"_s] = "world"_s;
|
||||
spy.clearLog();
|
||||
|
||||
SECTION("member already exists") {
|
||||
|
@ -10,6 +10,7 @@
|
||||
#include <string>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
#include "Literals.hpp"
|
||||
|
||||
using ArduinoJson::detail::sizeofArray;
|
||||
using ArduinoJson::detail::sizeofObject;
|
||||
@ -732,7 +733,7 @@ TEST_CASE("Overloads") {
|
||||
}
|
||||
|
||||
SECTION("const std::string&, Filter") {
|
||||
deserializeJson(doc, std::string("{}"), Filter(filter));
|
||||
deserializeJson(doc, "{}"_s, Filter(filter));
|
||||
}
|
||||
|
||||
SECTION("std::istream&, Filter") {
|
||||
@ -760,7 +761,7 @@ TEST_CASE("Overloads") {
|
||||
}
|
||||
|
||||
SECTION("const std::string&, Filter, NestingLimit") {
|
||||
deserializeJson(doc, std::string("{}"), Filter(filter), NestingLimit(5));
|
||||
deserializeJson(doc, "{}"_s, Filter(filter), NestingLimit(5));
|
||||
}
|
||||
|
||||
SECTION("std::istream&, Filter, NestingLimit") {
|
||||
@ -788,7 +789,7 @@ TEST_CASE("Overloads") {
|
||||
}
|
||||
|
||||
SECTION("const std::string&, NestingLimit, Filter") {
|
||||
deserializeJson(doc, std::string("{}"), NestingLimit(5), Filter(filter));
|
||||
deserializeJson(doc, "{}"_s, NestingLimit(5), Filter(filter));
|
||||
}
|
||||
|
||||
SECTION("std::istream&, NestingLimit, Filter") {
|
||||
|
@ -9,6 +9,7 @@
|
||||
|
||||
#include "Allocators.hpp"
|
||||
#include "CustomReader.hpp"
|
||||
#include "Literals.hpp"
|
||||
|
||||
using ArduinoJson::detail::sizeofObject;
|
||||
|
||||
@ -69,7 +70,7 @@ TEST_CASE("deserializeJson(const std::string&)") {
|
||||
}
|
||||
|
||||
SECTION("should accept temporary string") {
|
||||
DeserializationError err = deserializeJson(doc, std::string("[42]"));
|
||||
DeserializationError err = deserializeJson(doc, "[42]"_s);
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
}
|
||||
@ -82,7 +83,7 @@ TEST_CASE("deserializeJson(const std::string&)") {
|
||||
|
||||
JsonArray array = doc.as<JsonArray>();
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(std::string("hello") == array[0]);
|
||||
REQUIRE("hello"_s == array[0]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -108,7 +109,7 @@ TEST_CASE("deserializeJson(std::istream&)") {
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(1 == obj.size());
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
REQUIRE("world"_s == obj["hello"]);
|
||||
}
|
||||
|
||||
SECTION("Should not read after the closing brace of an empty object") {
|
||||
|
@ -7,6 +7,8 @@
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include "Literals.hpp"
|
||||
|
||||
#define SHOULD_WORK(expression) REQUIRE(DeserializationError::Ok == expression);
|
||||
#define SHOULD_FAIL(expression) \
|
||||
REQUIRE(DeserializationError::TooDeep == expression);
|
||||
@ -63,23 +65,23 @@ TEST_CASE("JsonDeserializer nesting") {
|
||||
SECTION("Input = std::string") {
|
||||
SECTION("limit = 0") {
|
||||
DeserializationOption::NestingLimit nesting(0);
|
||||
SHOULD_WORK(deserializeJson(doc, std::string("\"toto\""), nesting));
|
||||
SHOULD_WORK(deserializeJson(doc, std::string("123"), nesting));
|
||||
SHOULD_WORK(deserializeJson(doc, std::string("true"), nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, std::string("[]"), nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, std::string("{}"), nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, std::string("[\"toto\"]"), nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, std::string("{\"toto\":1}"), nesting));
|
||||
SHOULD_WORK(deserializeJson(doc, "\"toto\""_s, nesting));
|
||||
SHOULD_WORK(deserializeJson(doc, "123"_s, nesting));
|
||||
SHOULD_WORK(deserializeJson(doc, "true"_s, nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, "[]"_s, nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, "{}"_s, nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, "[\"toto\"]"_s, nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, "{\"toto\":1}"_s, nesting));
|
||||
}
|
||||
|
||||
SECTION("limit = 1") {
|
||||
DeserializationOption::NestingLimit nesting(1);
|
||||
SHOULD_WORK(deserializeJson(doc, std::string("[\"toto\"]"), nesting));
|
||||
SHOULD_WORK(deserializeJson(doc, std::string("{\"toto\":1}"), nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, std::string("{\"toto\":{}}"), nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, std::string("{\"toto\":[]}"), nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, std::string("[[\"toto\"]]"), nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, std::string("[{\"toto\":1}]"), nesting));
|
||||
SHOULD_WORK(deserializeJson(doc, "[\"toto\"]"_s, nesting));
|
||||
SHOULD_WORK(deserializeJson(doc, "{\"toto\":1}"_s, nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, "{\"toto\":{}}"_s, nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, "{\"toto\":[]}"_s, nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, "[[\"toto\"]]"_s, nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, "[{\"toto\":1}]"_s, nesting));
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user