Added as<JsonArray>() as a synonym for as<JsonArray&>()... (issue #291)

This commit is contained in:
Benoit Blanchon
2016-07-17 17:22:58 +02:00
parent 2dbd94951c
commit c87a0e97ab
13 changed files with 175 additions and 30 deletions

View File

@ -59,6 +59,7 @@ TEST_(StoreBoolean) {
TEST_(StoreString) {
_array[0] = "hello";
EXPECT_STREQ("hello", _array[0].as<const char*>());
EXPECT_STREQ("hello", _array[0].as<char*>()); // <- short hand
EXPECT_TRUE(_array[0].is<const char*>());
EXPECT_FALSE(_array[0].is<int>());
}
@ -69,6 +70,9 @@ TEST_(StoreNestedArray) {
_array[0] = arr;
EXPECT_EQ(&arr, &_array[0].as<JsonArray&>());
EXPECT_EQ(&arr, &_array[0].as<JsonArray>()); // <- short hand
EXPECT_EQ(&arr, &_array[0].as<const JsonArray&>());
EXPECT_EQ(&arr, &_array[0].as<const JsonArray>()); // <- short hand
EXPECT_TRUE(_array[0].is<JsonArray&>());
EXPECT_FALSE(_array[0].is<int>());
}
@ -79,6 +83,9 @@ TEST_(StoreNestedObject) {
_array[0] = obj;
EXPECT_EQ(&obj, &_array[0].as<JsonObject&>());
EXPECT_EQ(&obj, &_array[0].as<JsonObject>()); // <- short hand
EXPECT_EQ(&obj, &_array[0].as<const JsonObject&>());
EXPECT_EQ(&obj, &_array[0].as<const JsonObject>()); // <- short hand
EXPECT_TRUE(_array[0].is<JsonObject&>());
EXPECT_FALSE(_array[0].is<int>());
}

View File

@ -68,6 +68,7 @@ TEST_(StoreString) {
EXPECT_TRUE(_object["hello"].is<const char*>());
EXPECT_FALSE(_object["hello"].is<long>());
EXPECT_STREQ("h3110", _object["hello"].as<const char*>());
EXPECT_STREQ("h3110", _object["hello"].as<char*>()); // <- short hand
}
TEST_(StoreArray) {
@ -75,8 +76,15 @@ TEST_(StoreArray) {
_object["hello"] = arr;
EXPECT_EQ(&arr, &_object["hello"].asArray());
EXPECT_EQ(&arr, &_object["hello"].asArray()); // <- DEPRECATED
EXPECT_EQ(&arr, &_object["hello"].as<JsonArray&>());
EXPECT_EQ(&arr, &_object["hello"].as<JsonArray>()); // <- short hand
EXPECT_EQ(&arr, &_object["hello"].as<const JsonArray&>());
EXPECT_EQ(&arr, &_object["hello"].as<const JsonArray>()); // <- short hand
EXPECT_TRUE(_object["hello"].is<JsonArray&>());
EXPECT_TRUE(_object["hello"].is<JsonArray>());
EXPECT_TRUE(_object["hello"].is<const JsonArray&>());
EXPECT_TRUE(_object["hello"].is<const JsonArray>());
EXPECT_FALSE(_object["hello"].is<JsonObject&>());
}
@ -85,8 +93,15 @@ TEST_(StoreObject) {
_object["hello"] = obj;
EXPECT_EQ(&obj, &_object["hello"].asObject());
EXPECT_EQ(&obj, &_object["hello"].asObject()); // DEPRECATED
EXPECT_EQ(&obj, &_object["hello"].as<JsonObject&>());
EXPECT_EQ(&obj, &_object["hello"].as<JsonObject>()); // <- short hand
EXPECT_EQ(&obj, &_object["hello"].as<const JsonObject&>());
EXPECT_EQ(&obj, &_object["hello"].as<const JsonObject>()); // <- short hand
EXPECT_TRUE(_object["hello"].is<JsonObject&>());
EXPECT_TRUE(_object["hello"].is<JsonObject>());
EXPECT_TRUE(_object["hello"].is<const JsonObject&>());
EXPECT_TRUE(_object["hello"].is<const JsonObject>());
EXPECT_FALSE(_object["hello"].is<JsonArray&>());
}

View File

@ -214,3 +214,21 @@ TEST(JsonVariant_As_Tests, ArrayAsString) {
JsonVariant variant = arr;
ASSERT_EQ(String("[4,2]"), variant.as<String>());
}
TEST(JsonVariant_As_Tests, ArrayAsJsonArray) {
DynamicJsonBuffer buffer;
JsonArray& arr = buffer.createArray();
JsonVariant variant = arr;
ASSERT_EQ(&arr, &variant.as<JsonArray&>());
ASSERT_EQ(&arr, &variant.as<JsonArray>()); // <- shorthand
}
TEST(JsonVariant_As_Tests, ObjectAsJsonObject) {
DynamicJsonBuffer buffer;
JsonObject& arr = buffer.createObject();
JsonVariant variant = arr;
ASSERT_EQ(&arr, &variant.as<JsonObject&>());
ASSERT_EQ(&arr, &variant.as<JsonObject>()); // <- shorthand
}