forked from bblanchon/ArduinoJson
Tests: add user-defined literal ""_s for std::string
This commit is contained in:
1 parent
5b88b2c1f6
commit
45611924f3
55 files changed
+316
-229
No files matched your search
@@ -7,6 +7,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
#include "Literals.hpp"
|
||||
|
||||
TEST_CASE("JsonVariant::add(T)") {
|
||||
JsonDocument doc;
|
||||
@@ -25,7 +26,7 @@ TEST_CASE("JsonVariant::add(T)") {
|
||||
}
|
||||
|
||||
SECTION("add std::string to new variant") {
|
||||
var.add(std::string("hello"));
|
||||
var.add("hello"_s);
|
||||
|
||||
REQUIRE(var.as<std::string>() == "[\"hello\"]");
|
||||
}
|
||||
@@ -67,7 +68,7 @@ TEST_CASE("JsonVariant::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,8 @@
|
||||
#include <stdint.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Literals.hpp"
|
||||
|
||||
namespace my {
|
||||
using ArduinoJson::detail::isinf;
|
||||
} // namespace my
|
||||
@@ -156,20 +158,20 @@ TEST_CASE("JsonVariant::as()") {
|
||||
|
||||
REQUIRE(variant.as<bool>() == true);
|
||||
REQUIRE(variant.as<long>() == 0L);
|
||||
REQUIRE(variant.as<const char*>() == std::string("hello"));
|
||||
REQUIRE(variant.as<const char*>() == std::string("hello"));
|
||||
REQUIRE(variant.as<std::string>() == std::string("hello"));
|
||||
REQUIRE(variant.as<const char*>() == "hello"_s);
|
||||
REQUIRE(variant.as<const char*>() == "hello"_s);
|
||||
REQUIRE(variant.as<std::string>() == "hello"_s);
|
||||
REQUIRE(variant.as<JsonString>() == "hello");
|
||||
}
|
||||
|
||||
SECTION("set(std::string(\"4.2\"))") {
|
||||
variant.set(std::string("4.2"));
|
||||
variant.set("4.2"_s);
|
||||
|
||||
REQUIRE(variant.as<bool>() == true);
|
||||
REQUIRE(variant.as<long>() == 4L);
|
||||
REQUIRE(variant.as<double>() == 4.2);
|
||||
REQUIRE(variant.as<const char*>() == std::string("4.2"));
|
||||
REQUIRE(variant.as<std::string>() == std::string("4.2"));
|
||||
REQUIRE(variant.as<const char*>() == "4.2"_s);
|
||||
REQUIRE(variant.as<std::string>() == "4.2"_s);
|
||||
REQUIRE(variant.as<JsonString>() == "4.2");
|
||||
REQUIRE(variant.as<JsonString>().isLinked() == false);
|
||||
}
|
||||
@@ -227,13 +229,13 @@ TEST_CASE("JsonVariant::as()") {
|
||||
}
|
||||
|
||||
SECTION("as<std::string>()") {
|
||||
REQUIRE(variant.as<std::string>() == std::string("{\"key\":\"value\"}"));
|
||||
REQUIRE(variant.as<std::string>() == "{\"key\":\"value\"}"_s);
|
||||
}
|
||||
|
||||
SECTION("ObjectAsJsonObject") {
|
||||
JsonObject o = variant.as<JsonObject>();
|
||||
REQUIRE(o.size() == 1);
|
||||
REQUIRE(o["key"] == std::string("value"));
|
||||
REQUIRE(o["key"] == "value"_s);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -247,7 +249,7 @@ TEST_CASE("JsonVariant::as()") {
|
||||
}
|
||||
|
||||
SECTION("as<std::string>()") {
|
||||
REQUIRE(variant.as<std::string>() == std::string("[4,2]"));
|
||||
REQUIRE(variant.as<std::string>() == "[4,2]"_s);
|
||||
}
|
||||
|
||||
SECTION("as<JsonArray>()") {
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
#include "Literals.hpp"
|
||||
|
||||
TEST_CASE("JsonVariant::clear()") {
|
||||
SpyingAllocator spy;
|
||||
@@ -28,7 +29,7 @@ TEST_CASE("JsonVariant::clear()") {
|
||||
}
|
||||
|
||||
SECTION("releases owned string") {
|
||||
var.set(std::string("hello"));
|
||||
var.set("hello"_s);
|
||||
var.clear();
|
||||
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
#include <stdint.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Literals.hpp"
|
||||
|
||||
TEST_CASE("JsonVariant::containsKey()") {
|
||||
JsonDocument doc;
|
||||
JsonVariant var = doc.to<JsonVariant>();
|
||||
@@ -20,8 +22,8 @@ TEST_CASE("JsonVariant::containsKey()") {
|
||||
SECTION("containsKey(std::string)") {
|
||||
var["hello"] = "world";
|
||||
|
||||
REQUIRE(var.containsKey(std::string("hello")) == true);
|
||||
REQUIRE(var.containsKey(std::string("world")) == false);
|
||||
REQUIRE(var.containsKey("hello"_s) == true);
|
||||
REQUIRE(var.containsKey("world"_s) == false);
|
||||
}
|
||||
|
||||
SECTION("containsKey(JsonVariant)") {
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
#include <catch.hpp>
|
||||
#include "Allocators.hpp"
|
||||
|
||||
#include "Literals.hpp"
|
||||
|
||||
TEST_CASE("JsonVariant::set(JsonVariant)") {
|
||||
KillswitchAllocator killswitch;
|
||||
SpyingAllocator spyingAllocator(&killswitch);
|
||||
@@ -72,7 +74,7 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
|
||||
}
|
||||
|
||||
SECTION("stores std::string by copy") {
|
||||
var1.set(std::string("hello!!"));
|
||||
var1.set("hello!!"_s);
|
||||
spyingAllocator.clearLog();
|
||||
|
||||
var2.set(var1);
|
||||
@@ -106,7 +108,7 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
|
||||
}
|
||||
|
||||
SECTION("stores Serialized<std::string> by copy") {
|
||||
var1.set(serialized(std::string("hello!!")));
|
||||
var1.set(serialized("hello!!"_s));
|
||||
spyingAllocator.clearLog();
|
||||
|
||||
var2.set(var1);
|
||||
@@ -117,7 +119,7 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
|
||||
}
|
||||
|
||||
SECTION("fails gracefully if raw string allocation fails") {
|
||||
var1.set(serialized(std::string("hello!!")));
|
||||
var1.set(serialized("hello!!"_s));
|
||||
killswitch.on();
|
||||
spyingAllocator.clearLog();
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
#include "Literals.hpp"
|
||||
|
||||
using ArduinoJson::detail::sizeofArray;
|
||||
|
||||
@@ -15,9 +16,9 @@ TEST_CASE("JsonVariant::remove(int)") {
|
||||
JsonDocument doc(&spy);
|
||||
|
||||
SECTION("release top level strings") {
|
||||
doc.add(std::string("hello"));
|
||||
doc.add(std::string("hello"));
|
||||
doc.add(std::string("world"));
|
||||
doc.add("hello"_s);
|
||||
doc.add("hello"_s);
|
||||
doc.add("world"_s);
|
||||
|
||||
JsonVariant var = doc.as<JsonVariant>();
|
||||
REQUIRE(var.as<std::string>() == "[\"hello\",\"hello\",\"world\"]");
|
||||
@@ -43,7 +44,7 @@ TEST_CASE("JsonVariant::remove(int)") {
|
||||
}
|
||||
|
||||
SECTION("release strings in nested array") {
|
||||
doc[0][0] = std::string("hello");
|
||||
doc[0][0] = "hello"_s;
|
||||
|
||||
JsonVariant var = doc.as<JsonVariant>();
|
||||
REQUIRE(var.as<std::string>() == "[[\"hello\"]]");
|
||||
@@ -77,7 +78,7 @@ TEST_CASE("JsonVariant::remove(std::string)") {
|
||||
var["a"] = 1;
|
||||
var["b"] = 2;
|
||||
|
||||
var.remove(std::string("b"));
|
||||
var.remove("b"_s);
|
||||
|
||||
REQUIRE(var.as<std::string>() == "{\"a\":1}");
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
#include "Literals.hpp"
|
||||
|
||||
using ArduinoJson::detail::sizeofObject;
|
||||
|
||||
@@ -137,14 +138,14 @@ TEST_CASE("JsonVariant::set() with not enough memory") {
|
||||
JsonVariant v = doc.to<JsonVariant>();
|
||||
|
||||
SECTION("std::string") {
|
||||
bool result = v.set(std::string("hello world!!"));
|
||||
bool result = v.set("hello world!!"_s);
|
||||
|
||||
REQUIRE(result == false);
|
||||
REQUIRE(v.isNull());
|
||||
}
|
||||
|
||||
SECTION("Serialized<std::string>") {
|
||||
bool result = v.set(serialized(std::string("hello world!!")));
|
||||
bool result = v.set(serialized("hello world!!"_s));
|
||||
|
||||
REQUIRE(result == false);
|
||||
REQUIRE(v.isNull());
|
||||
@@ -178,7 +179,7 @@ TEST_CASE("JsonVariant::set(JsonDocument)") {
|
||||
TEST_CASE("JsonVariant::set() releases the previous value") {
|
||||
SpyingAllocator spy;
|
||||
JsonDocument doc(&spy);
|
||||
doc["hello"] = std::string("world");
|
||||
doc["hello"] = "world"_s;
|
||||
spy.clearLog();
|
||||
|
||||
JsonVariant v = doc["hello"];
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Literals.hpp"
|
||||
|
||||
TEST_CASE("JsonVariant::operator[]") {
|
||||
JsonDocument doc;
|
||||
JsonVariant var = doc.to<JsonVariant>();
|
||||
@@ -31,9 +33,9 @@ TEST_CASE("JsonVariant::operator[]") {
|
||||
|
||||
REQUIRE(2 == var.size());
|
||||
var[0].as<std::string>();
|
||||
// REQUIRE(std::string("element at index 0") == );
|
||||
REQUIRE(std::string("element at index 1") == var[1]);
|
||||
REQUIRE(std::string("element at index 0") ==
|
||||
// REQUIRE("element at index 0"_s == );
|
||||
REQUIRE("element at index 1"_s == var[1]);
|
||||
REQUIRE("element at index 0"_s ==
|
||||
var[static_cast<unsigned char>(0)]); // issue #381
|
||||
REQUIRE(var[666].isNull());
|
||||
REQUIRE(var[3].isNull());
|
||||
@@ -46,7 +48,7 @@ TEST_CASE("JsonVariant::operator[]") {
|
||||
var[1] = "world";
|
||||
|
||||
REQUIRE(var.size() == 2);
|
||||
REQUIRE(std::string("world") == var[1]);
|
||||
REQUIRE("world"_s == var[1]);
|
||||
}
|
||||
|
||||
SECTION("set value in a nested object") {
|
||||
@@ -56,7 +58,7 @@ TEST_CASE("JsonVariant::operator[]") {
|
||||
|
||||
REQUIRE(1 == var.size());
|
||||
REQUIRE(1 == var[0].size());
|
||||
REQUIRE(std::string("world") == var[0]["hello"]);
|
||||
REQUIRE("world"_s == var[0]["hello"]);
|
||||
}
|
||||
|
||||
SECTION("variant[0] when variant contains an integer") {
|
||||
@@ -86,8 +88,8 @@ TEST_CASE("JsonVariant::operator[]") {
|
||||
object["b"] = "element at key \"b\"";
|
||||
|
||||
REQUIRE(2 == var.size());
|
||||
REQUIRE(std::string("element at key \"a\"") == var["a"]);
|
||||
REQUIRE(std::string("element at key \"b\"") == var["b"]);
|
||||
REQUIRE("element at key \"a\""_s == var["a"]);
|
||||
REQUIRE("element at key \"b\""_s == var["b"]);
|
||||
REQUIRE(var["c"].isNull());
|
||||
REQUIRE(var[0].isNull());
|
||||
}
|
||||
@@ -96,7 +98,7 @@ TEST_CASE("JsonVariant::operator[]") {
|
||||
var["hello"] = "world";
|
||||
|
||||
REQUIRE(1 == var.size());
|
||||
REQUIRE(std::string("world") == var["hello"]);
|
||||
REQUIRE("world"_s == var["hello"]);
|
||||
}
|
||||
|
||||
SECTION("set value, key is a char[]") {
|
||||
@@ -105,7 +107,7 @@ TEST_CASE("JsonVariant::operator[]") {
|
||||
key[0] = '!'; // make sure the key is duplicated
|
||||
|
||||
REQUIRE(1 == var.size());
|
||||
REQUIRE(std::string("world") == var["hello"]);
|
||||
REQUIRE("world"_s == var["hello"]);
|
||||
}
|
||||
|
||||
SECTION("var[key].to<JsonArray>()") {
|
||||
@@ -133,7 +135,7 @@ TEST_CASE("JsonVariant::operator[]") {
|
||||
deserializeJson(doc, "{\"hello\":\"world\"}");
|
||||
JsonVariant variant = doc.as<JsonVariant>();
|
||||
|
||||
REQUIRE(std::string("world") == variant[vla]);
|
||||
REQUIRE("world"_s == variant[vla]);
|
||||
}
|
||||
|
||||
SECTION("key is a VLA, const JsonVariant") {
|
||||
@@ -144,7 +146,7 @@ TEST_CASE("JsonVariant::operator[]") {
|
||||
deserializeJson(doc, "{\"hello\":\"world\"}");
|
||||
const JsonVariant variant = doc.as<JsonVariant>();
|
||||
|
||||
REQUIRE(std::string("world") == variant[vla]);
|
||||
REQUIRE("world"_s == variant[vla]);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -5,6 +5,8 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Literals.hpp"
|
||||
|
||||
TEST_CASE("Unbound JsonVariant") {
|
||||
JsonVariant variant;
|
||||
|
||||
@@ -48,7 +50,7 @@ TEST_CASE("Unbound JsonVariant") {
|
||||
CHECK_FALSE(variant.set(42L));
|
||||
CHECK_FALSE(variant.set(42U));
|
||||
CHECK_FALSE(variant.set(serialized("42")));
|
||||
CHECK_FALSE(variant.set(serialized(std::string("42"))));
|
||||
CHECK_FALSE(variant.set(serialized("42"_s)));
|
||||
CHECK_FALSE(variant.set(true));
|
||||
CHECK_FALSE(variant.set(MsgPackBinary("hello", 5)));
|
||||
CHECK_FALSE(variant.set(MsgPackExtension(1, "hello", 5)));
|
||||
@@ -68,7 +70,7 @@ TEST_CASE("Unbound JsonVariant") {
|
||||
CHECK(variant["key"].isNull());
|
||||
CHECK_FALSE(variant[0].set(1));
|
||||
CHECK_FALSE(variant["key"].set(1));
|
||||
CHECK_FALSE(variant[std::string("key")].set(1));
|
||||
CHECK_FALSE(variant["key"_s].set(1));
|
||||
}
|
||||
|
||||
SECTION("containsKey()") {
|
||||
|
||||
Reference in new issue
Block a user