Skip to content

Commit

Permalink
Clean up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bblanchon committed Jul 18, 2018
1 parent c19cd18 commit de44adf
Showing 1 changed file with 62 additions and 67 deletions.
129 changes: 62 additions & 67 deletions test/JsonVariant/subscript.cpp
Expand Up @@ -6,94 +6,89 @@
#include <catch.hpp>

TEST_CASE("JsonVariant::operator[]") {
SECTION("Array") {
DynamicJsonDocument doc;
JsonArray array = doc.to<JsonArray>();
array.add("element at index 0");
array.add("element at index 1");

JsonVariant var = array;

REQUIRE(2 == var.size());
REQUIRE(std::string("element at index 0") == var[0]);
REQUIRE(std::string("element at index 1") == var[1]);
REQUIRE(std::string("element at index 0") ==
var[static_cast<unsigned char>(0)]); // issue #381
REQUIRE(var[666].isNull());
REQUIRE(var[3].isNull());
REQUIRE(var["0"].isNull());
}

SECTION("Object") {
DynamicJsonDocument doc;
JsonObject object = doc.to<JsonObject>();
object["a"] = "element at key \"a\"";
object["b"] = "element at key \"b\"";

JsonVariant var = object;

REQUIRE(2 == var.size());
REQUIRE(std::string("element at key \"a\"") == var["a"]);
REQUIRE(std::string("element at key \"b\"") == var["b"]);
REQUIRE(var["c"].isNull());
REQUIRE(var[0].isNull());
}

SECTION("Undefined") {
SECTION("The JsonVariant is undefined") {
JsonVariant var = JsonVariant();
REQUIRE(0 == var.size());
REQUIRE(var["0"].isNull());
REQUIRE(var[0].isNull());
}

SECTION("String") {
SECTION("The JsonVariant is a string") {
JsonVariant var = "hello world";
REQUIRE(0 == var.size());
REQUIRE(var["0"].isNull());
REQUIRE(var[0].isNull());
}

SECTION("set value in object, key is a const char*") {
SECTION("The JsonVariant is a JsonArray") {
DynamicJsonDocument doc;
JsonObject obj = doc.to<JsonObject>();
JsonVariant var = obj;
JsonArray array = doc.to<JsonArray>();
JsonVariant var = array;

var["hello"] = "world";
SECTION("get value") {
array.add("element at index 0");
array.add("element at index 1");

REQUIRE(1 == var.size());
REQUIRE(std::string("world") == var["hello"]);
}
REQUIRE(2 == var.size());
REQUIRE(std::string("element at index 0") == var[0]);
REQUIRE(std::string("element at index 1") == var[1]);
REQUIRE(std::string("element at index 0") ==
var[static_cast<unsigned char>(0)]); // issue #381
REQUIRE(var[666].isNull());
REQUIRE(var[3].isNull());
REQUIRE(var["0"].isNull());
}

SECTION("set value in object, key is a char[]") {
DynamicJsonDocument doc;
JsonObject obj = doc.to<JsonObject>();
JsonVariant var = obj;
SECTION("set value") {
array.add("hello");

char key[] = "hello";
var[key] = "world";
key[0] = '!'; // make sure the key is duplicated
var[0] = "world";

REQUIRE(1 == var.size());
REQUIRE(std::string("world") == var["hello"]);
}
REQUIRE(1 == var.size());
REQUIRE(std::string("world") == var[0]);
}

SECTION("ArraySetValue") {
DynamicJsonDocument doc;
JsonArray arr = doc.to<JsonArray>();
arr.add("hello");
JsonVariant var = arr;
var[0] = "world";
REQUIRE(1 == var.size());
REQUIRE(std::string("world") == var[0]);
SECTION("set value in a nested object") {
array.createNestedObject();

var[0]["hello"] = "world";

REQUIRE(1 == var.size());
REQUIRE(1 == var[0].size());
REQUIRE(std::string("world") == var[0]["hello"]);
}
}

SECTION("NestedObjectSetValue") {
SECTION("The JsonVariant is a JsonObject") {
DynamicJsonDocument doc;
deserializeJson(doc, "[{}]");
JsonVariant var = doc.as<JsonVariant>();
var[0]["hello"] = "world";
REQUIRE(1 == var.size());
REQUIRE(1 == var[0].size());
REQUIRE(std::string("world") == var[0]["hello"]);
JsonObject object = doc.to<JsonObject>();
JsonVariant var = object;

SECTION("get value") {
object["a"] = "element at key \"a\"";
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(var["c"].isNull());
REQUIRE(var[0].isNull());
}

SECTION("set value, key is a const char*") {
var["hello"] = "world";

REQUIRE(1 == var.size());
REQUIRE(std::string("world") == var["hello"]);
}

SECTION("set value, key is a char[]") {
char key[] = "hello";
var[key] = "world";
key[0] = '!'; // make sure the key is duplicated

REQUIRE(1 == var.size());
REQUIRE(std::string("world") == var["hello"]);
}
}
}

0 comments on commit de44adf

Please sign in to comment.