* Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "yr/api/runtime_env.h"
#include <gtest/gtest.h>
#include <string>
#include "yr/api/constant.h"
namespace YR {
namespace internal {
class RuntimeEnvTest : public ::testing::Test {
protected:
void SetUp() override {
env.Set<int>("test_int", 100);
env.Set<std::string>("test_str", "测试字符串");
env.SetJsonStr("test_json", R"({"key":"value","num":123})");
}
RuntimeEnv env;
};
TEST_F(RuntimeEnvTest, ShouldHandlePrimitiveTypes) {
env.Set<int>("int_val", 42);
EXPECT_EQ(env.Get<int>("int_val"), 42);
env.Set<double>("double_val", 3.14);
EXPECT_DOUBLE_EQ(env.Get<double>("double_val"), 3.14);
env.Set<bool>("bool_val", true);
EXPECT_TRUE(env.Get<bool>("bool_val"));
}
TEST_F(RuntimeEnvTest, ShouldHandleString) {
env.Set<std::string>("str_val", "测试字符串");
EXPECT_EQ(env.Get<std::string>("str_val"), "测试字符串");
}
TEST_F(RuntimeEnvTest, ShouldHandleContainers) {
std::vector<int> vec{1, 2, 3};
env.Set<std::vector<int>>("vec_val", vec);
auto vec_ret = env.Get<std::vector<int>>("vec_val");
EXPECT_EQ(vec_ret, vec);
std::map<std::string, int> map_val{{"a", 1}, {"b", 2}};
env.Set<std::map<std::string, int>>("map_val", map_val);
auto map_ret = env.Get<std::map<std::string, int>>("map_val");
EXPECT_EQ(map_ret, map_val);
}
TEST_F(RuntimeEnvTest, ShouldThrowWhenFieldNotExist) {
EXPECT_THROW(env.Get<int>("non_exist"), YR::Exception);
}
TEST_F(RuntimeEnvTest, ShouldThrowWhenTypeMismatch) {
env.Set<int>("int_field", 100);
EXPECT_THROW(env.Get<std::string>("int_field"), YR::Exception);
try {
env.Get<std::string>("int_field");
} catch (const YR::Exception& e) {
EXPECT_NE(std::string(e.what()).find("Failed to get the field"), std::string::npos);
}
}
TEST_F(RuntimeEnvTest, ShouldHandleEmptyValues) {
env.Set<std::string>("empty_str", "");
EXPECT_TRUE(env.Get<std::string>("empty_str").empty());
std::vector<int> empty_vec;
env.Set<std::vector<int>>("empty_vec", empty_vec);
EXPECT_TRUE(env.Get<std::vector<int>>("empty_vec").empty());
}
TEST_F(RuntimeEnvTest, ShouldHandleSpecialCharNames) {
std::string special_name = "field@name#123";
env.Set<int>(special_name, 100);
EXPECT_EQ(env.Get<int>(special_name), 100);
}
TEST_F(RuntimeEnvTest, ShouldCorrectlySetAndGetValues) {
EXPECT_EQ(env.Get<int>("test_int"), 100);
EXPECT_EQ(env.Get<std::string>("test_str"), "测试字符串");
auto jsonStr = env.GetJsonStr("test_json");
nlohmann::json j = nlohmann::json::parse(jsonStr);
EXPECT_EQ(j["key"], "value");
EXPECT_EQ(j["num"], 123);
}
TEST_F(RuntimeEnvTest, ShouldThrowWhenGettingNonexistentField) {
EXPECT_THROW(env.Get<int>("non_exist"), YR::Exception);
EXPECT_THROW(env.GetJsonStr("non_exist"), YR::Exception);
}
TEST_F(RuntimeEnvTest, ShouldHandleJsonStringsProperly) {
EXPECT_THROW(env.SetJsonStr("bad_json", "{invalid}"), YR::Exception);
env.SetJsonStr("nested_json", R"({"nested":{"key":"value"}})");
auto jsonStr = env.GetJsonStr("nested_json");
nlohmann::json j = nlohmann::json::parse(jsonStr);
EXPECT_EQ(j["nested"]["key"], "value");
}
TEST_F(RuntimeEnvTest, ShouldCorrectlyCheckFieldExistence) {
EXPECT_TRUE(env.Contains("test_int"));
EXPECT_FALSE(env.Contains("non_exist"));
}
TEST_F(RuntimeEnvTest, ShouldRemoveFieldsCorrectly) {
EXPECT_TRUE(env.Remove("test_int"));
EXPECT_FALSE(env.Contains("test_int"));
EXPECT_FALSE(env.Remove("non_exist"));
}
TEST_F(RuntimeEnvTest, ShouldCheckEmptyState) {
RuntimeEnv emptyEnv;
EXPECT_TRUE(emptyEnv.Empty());
emptyEnv.Set<int>("temp", 1);
EXPECT_FALSE(emptyEnv.Empty());
emptyEnv.Remove("temp");
EXPECT_TRUE(emptyEnv.Empty());
}
TEST_F(RuntimeEnvTest, ShouldSupportVariousDataTypes) {
env.Set<double>("double_val", 3.14);
env.Set<bool>("bool_val", true);
EXPECT_DOUBLE_EQ(env.Get<double>("double_val"), 3.14);
EXPECT_TRUE(env.Get<bool>("bool_val"));
}
}
}