summaryrefslogtreecommitdiff
path: root/src/test/common/Configuration/Config.cpp
blob: c3c1e67d31ef2528fb12dc2cd58f643db9b40280 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * This file is part of the AzerothCore Project. See AUTHORS file for Copyright information
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by the
 * Free Software Foundation; either version 3 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "Config.h"
#include "gtest/gtest.h"

#include <boost/filesystem.hpp>
#include <cstdlib>
#include <fstream>
#include <string>

std::string CreateConfigWithMap(std::map<std::string, std::string> const& map)
{
    auto mTempFileRel = boost::filesystem::unique_path("deleteme.ini");
    auto mTempFileAbs = boost::filesystem::temp_directory_path() / mTempFileRel;
    std::ofstream iniStream;
    iniStream.open(mTempFileAbs.c_str());

    iniStream << "[test]\n";
    for (auto const& itr : map)
        iniStream << itr.first << " = " << itr.second << "\n";

    iniStream.close();

    return mTempFileAbs.native();
}

class ConfigEnvTest : public testing::Test {
protected:
    void SetUp() override {
        std::map<std::string, std::string> config;
        config["Int.Nested"] = "4242";
        config["lower"] = "simpleString";
        config["UPPER"] = "simpleString";
        config["SomeLong.NestedNameWithNumber.Like1"] = "1";
        config["GM.InGMList.Level"] = "50";

        confFilePath = CreateConfigWithMap(config);

        sConfigMgr->Configure(confFilePath, std::vector<std::string>());
        sConfigMgr->LoadAppConfigs();
    }

    void TearDown() override {
        std::remove(confFilePath.c_str());
    }

    std::string confFilePath;
};

TEST_F(ConfigEnvTest, NestedInt)
{
    EXPECT_EQ(sConfigMgr->GetOption<int32>("Int.Nested", 10), 4242);
    setenv("AC_INT_NESTED", "8080", 1);
    EXPECT_EQ(sConfigMgr->OverrideWithEnvVariablesIfAny().empty(), false);
    EXPECT_EQ(sConfigMgr->GetOption<int32>("Int.Nested", 10), 8080);
}

TEST_F(ConfigEnvTest, SimpleLowerString)
{
    EXPECT_EQ(sConfigMgr->GetOption<std::string>("lower", ""), "simpleString");
    setenv("AC_LOWER", "envstring", 1);
    EXPECT_EQ(sConfigMgr->OverrideWithEnvVariablesIfAny().empty(), false);
    EXPECT_EQ(sConfigMgr->GetOption<std::string>("lower", ""), "envstring");
}

TEST_F(ConfigEnvTest, SimpleUpperString)
{
    EXPECT_EQ(sConfigMgr->GetOption<std::string>("UPPER", ""), "simpleString");
    setenv("AC_UPPER", "envupperstring", 1);
    EXPECT_EQ(sConfigMgr->OverrideWithEnvVariablesIfAny().empty(), false);
    EXPECT_EQ(sConfigMgr->GetOption<std::string>("UPPER", ""), "envupperstring");
}

TEST_F(ConfigEnvTest, LongNestedNameWithNumber)
{
    EXPECT_EQ(sConfigMgr->GetOption<float>("SomeLong.NestedNameWithNumber.Like1", 0), 1);
    setenv("AC_SOME_LONG_NESTED_NAME_WITH_NUMBER_LIKE_1", "42", 1);
    EXPECT_EQ(sConfigMgr->OverrideWithEnvVariablesIfAny().empty(), false);
    EXPECT_EQ(sConfigMgr->GetOption<float>("SomeLong.NestedNameWithNumber.Like1", 0), 42);
}

TEST_F(ConfigEnvTest, ValueWithSeveralUpperlLaters)
{
    EXPECT_EQ(sConfigMgr->GetOption<int>("GM.InGMList.Level", 1), 50);
    setenv("AC_GM_IN_GMLIST_LEVEL", "42", 1);
    EXPECT_EQ(sConfigMgr->OverrideWithEnvVariablesIfAny().empty(), false);
    EXPECT_EQ(sConfigMgr->GetOption<int>("GM.InGMList.Level", 0), 42);
}

TEST_F(ConfigEnvTest, StringThatNotExistInConfig)
{
    setenv("AC_UNIQUE_STRING", "somevalue", 1);
    EXPECT_EQ(sConfigMgr->GetOption<std::string>("Unique.String", ""), "somevalue");
}

TEST_F(ConfigEnvTest, IntThatNotExistInConfig)
{
    setenv("AC_UNIQUE_INT", "100", 1);
    EXPECT_EQ(sConfigMgr->GetOption<int>("Unique.Int", 1), 100);
}

TEST_F(ConfigEnvTest, NotExistingString)
{
    EXPECT_EQ(sConfigMgr->GetOption<std::string>("NotFound.String", "none"), "none");
}

TEST_F(ConfigEnvTest, NotExistingInt)
{
    EXPECT_EQ(sConfigMgr->GetOption<int>("NotFound.Int", 1), 1);
}