|
| 1 | +/* |
| 2 | + * If not stated otherwise in this file or this component's LICENSE file the |
| 3 | + * following copyright and licenses apply: |
| 4 | + * |
| 5 | + * Copyright 2020 Metrological |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#ifdef __APPLE__ |
| 21 | +#include <time.h> |
| 22 | +#endif |
| 23 | + |
| 24 | +#include <gtest/gtest.h> |
| 25 | + |
| 26 | +#ifndef MODULE_NAME |
| 27 | +#include "../Module.h" |
| 28 | +#endif |
| 29 | + |
| 30 | +#include <core/core.h> |
| 31 | + |
| 32 | +namespace Thunder { |
| 33 | +namespace Tests { |
| 34 | +namespace Core { |
| 35 | + |
| 36 | + TEST(JSONOBJECT, FirstKeyRemove) |
| 37 | + { |
| 38 | + const std::string keyValues = R"({"Name": "Your full name", "Age": 0, "Gender": "Undisclosed"})"; |
| 39 | + |
| 40 | + JsonObject container; |
| 41 | + |
| 42 | + EXPECT_TRUE(container.FromString(keyValues)); |
| 43 | + |
| 44 | + EXPECT_FALSE(container.HasLabel("name")); |
| 45 | + |
| 46 | + EXPECT_TRUE(container.HasLabel("Name")); |
| 47 | + |
| 48 | + /* void */ container.Remove("Name"); |
| 49 | + |
| 50 | + EXPECT_FALSE(container.HasLabel("Name")); |
| 51 | + } |
| 52 | + |
| 53 | + TEST(JSONOBJECT, SecondKeyRemove) |
| 54 | + { |
| 55 | + const std::string keyValues = R"({"Name": "Your full name", "Age": 0, "Gender": "Undisclosed"})"; |
| 56 | + |
| 57 | + JsonObject container; |
| 58 | + |
| 59 | + EXPECT_TRUE(container.FromString(keyValues)); |
| 60 | + |
| 61 | + EXPECT_FALSE(container.HasLabel("age")); |
| 62 | + |
| 63 | + EXPECT_TRUE(container.HasLabel("Age")); |
| 64 | + |
| 65 | + /* void */ container.Remove("Age"); |
| 66 | + |
| 67 | + EXPECT_FALSE(container.HasLabel("Age")); |
| 68 | + } |
| 69 | + |
| 70 | + TEST(JSONOBJECT, ThirdKeyRemove) |
| 71 | + { |
| 72 | + const std::string keyValues = R"({"Name": "Your full name", "Age": 0, "Gender": "Undisclosed"})"; |
| 73 | + |
| 74 | + JsonObject container; |
| 75 | + |
| 76 | + EXPECT_TRUE(container.FromString(keyValues)); |
| 77 | + |
| 78 | + EXPECT_FALSE(container.HasLabel("gender")); |
| 79 | + |
| 80 | + EXPECT_TRUE(container.HasLabel("Gender")); |
| 81 | + |
| 82 | + /* void */ container.Remove("Gender"); |
| 83 | + |
| 84 | + EXPECT_FALSE(container.HasLabel("Gender")); |
| 85 | + } |
| 86 | + |
| 87 | + TEST(JSONOBJECT, IdenticalKeyValueRemove) |
| 88 | + { |
| 89 | + const std::string keyValues = R"({"Name": "Your full name", "Age": "Age", "Gender": "Undisclosed"})"; |
| 90 | + |
| 91 | + JsonObject container; |
| 92 | + |
| 93 | + EXPECT_TRUE(container.FromString(keyValues)); |
| 94 | + |
| 95 | + EXPECT_FALSE(container.HasLabel("age")); |
| 96 | + |
| 97 | + EXPECT_TRUE(container.HasLabel("Age")); |
| 98 | + |
| 99 | + /* void */ container.Remove("Age"); |
| 100 | + |
| 101 | + EXPECT_FALSE(container.HasLabel("Age")); |
| 102 | + } |
| 103 | + |
| 104 | + TEST(JSONOBJECT, KeyValueAccess) |
| 105 | + { |
| 106 | + JsonObject container; |
| 107 | + |
| 108 | + ::Thunder::Core::JSON::Variant intType{ 123 }; |
| 109 | + ::Thunder::Core::JSON::Variant floatType{ 123.456f }; |
| 110 | + ::Thunder::Core::JSON::Variant doubleType{ 123.456 }; // defaults to double with omitted suffix |
| 111 | + ::Thunder::Core::JSON::Variant boolType{ true }; |
| 112 | + ::Thunder::Core::JSON::Variant stringType{ "scribble" }; |
| 113 | + |
| 114 | + // An empty JsonObject is valid |
| 115 | + EXPECT_TRUE(container.IsValid()); |
| 116 | + |
| 117 | + /* void */ container.Set("integer", intType); |
| 118 | + /* void */ container.Set("float", floatType); |
| 119 | + /* void */ container.Set("double", doubleType); |
| 120 | + /* void */ container.Set("boolean", boolType); |
| 121 | + /* void */ container.Set("string", stringType); |
| 122 | + |
| 123 | + // All contained elements are valid |
| 124 | + EXPECT_TRUE(container.IsValid()); |
| 125 | + |
| 126 | + ::Thunder::Core::JSON::Variant intFound = container.Get("integer"); |
| 127 | + // Create and add if it does not exist otherwise return the existing element |
| 128 | + ::Thunder::Core::JSON::Variant& intRefFound = container["integer"]; |
| 129 | + |
| 130 | + EXPECT_TRUE(intFound.Content() == ::Thunder::Core::JSON::Variant::type::NUMBER); |
| 131 | + EXPECT_EQ(intFound.Number(), 123); |
| 132 | + EXPECT_TRUE(intFound == intRefFound); |
| 133 | + |
| 134 | + ::Thunder::Core::JSON::Variant floatFound = container.Get("float"); |
| 135 | + ::Thunder::Core::JSON::Variant& floatRefFound = container["float"]; |
| 136 | + EXPECT_TRUE(floatFound.Content() == ::Thunder::Core::JSON::Variant::type::FLOAT); |
| 137 | + EXPECT_FLOAT_EQ(floatFound.Float(), 123.456); |
| 138 | + EXPECT_TRUE(floatFound == floatRefFound); |
| 139 | + |
| 140 | + ::Thunder::Core::JSON::Variant doubleFound = container.Get("double"); |
| 141 | + ::Thunder::Core::JSON::Variant& doubleRefFound = container["double"]; |
| 142 | + EXPECT_TRUE(doubleFound.Content() == ::Thunder::Core::JSON::Variant::type::DOUBLE); |
| 143 | + EXPECT_FLOAT_EQ(doubleFound.Float(), 123.456); |
| 144 | + EXPECT_TRUE(doubleFound == doubleRefFound); |
| 145 | + |
| 146 | + ::Thunder::Core::JSON::Variant boolFound = container.Get("boolean"); |
| 147 | + ::Thunder::Core::JSON::Variant& boolRefFound = container["boolean"]; |
| 148 | + EXPECT_TRUE(boolFound.Content() == ::Thunder::Core::JSON::Variant::type::BOOLEAN); |
| 149 | + EXPECT_EQ(boolFound.Boolean(), true); |
| 150 | + EXPECT_TRUE(boolFound == boolRefFound); |
| 151 | + |
| 152 | + ::Thunder::Core::JSON::Variant stringFound = container.Get("string"); |
| 153 | + ::Thunder::Core::JSON::Variant& stringRefFound = container["string"]; |
| 154 | + EXPECT_TRUE(stringFound.Content() == ::Thunder::Core::JSON::Variant::type::STRING); |
| 155 | + EXPECT_STREQ(stringFound.String().c_str(), "scribble"); |
| 156 | + EXPECT_TRUE(stringFound == stringRefFound); |
| 157 | + |
| 158 | + container.Clear(); |
| 159 | + |
| 160 | + // The container should have nothing to index |
| 161 | + ::Thunder::Core::JSON::VariantContainer::Iterator it = container.Variants(); |
| 162 | + EXPECT_FALSE(it.IsValid()); |
| 163 | + |
| 164 | + // It does not exist so a default constructed variant is returned |
| 165 | + // Create and add a default variant with the label 'integer' |
| 166 | + ::Thunder::Core::JSON::Variant& intRefCreated = container["integer"]; |
| 167 | + EXPECT_TRUE(container.HasLabel("integer")); |
| 168 | + EXPECT_TRUE(intRefCreated.Content() == ::Thunder::Core::JSON::Variant::type::EMPTY); |
| 169 | + |
| 170 | + ::Thunder::Core::JSON::Variant& floatRefCreated = container["float"]; |
| 171 | + EXPECT_TRUE(container.HasLabel("float")); |
| 172 | + EXPECT_TRUE(floatRefCreated.Content() == ::Thunder::Core::JSON::Variant::type::EMPTY); |
| 173 | + |
| 174 | + ::Thunder::Core::JSON::Variant& doubleRefCreated = container["double"]; |
| 175 | + EXPECT_TRUE(container.HasLabel("double")); |
| 176 | + EXPECT_TRUE(doubleRefCreated.Content() == ::Thunder::Core::JSON::Variant::type::EMPTY); |
| 177 | + |
| 178 | + ::Thunder::Core::JSON::Variant& boolRefCreated = container["boolean"]; |
| 179 | + EXPECT_TRUE(container.HasLabel("boolean")); |
| 180 | + EXPECT_TRUE(boolRefCreated.Content() == ::Thunder::Core::JSON::Variant::type::EMPTY); |
| 181 | + |
| 182 | + ::Thunder::Core::JSON::Variant& stringRefCreated = container["string"]; |
| 183 | + EXPECT_TRUE(container.HasLabel("string")); |
| 184 | + EXPECT_TRUE(stringRefCreated.Content() == ::Thunder::Core::JSON::Variant::type::EMPTY); |
| 185 | + } |
| 186 | + |
| 187 | + |
| 188 | +} // Core |
| 189 | +} // Tests |
| 190 | +} // Thunder |
0 commit comments