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
|
#include "configuration.h"
#include <gtest/gtest.h>
TEST(Configuration, ctor_empty_initializer_list)
{
Configuration conf({});
std::cout << conf;
}
TEST(Configuration, ctor_initializer_list)
{
Configuration conf({
{ "some_string", "value1" },
{ "some_bool", true },
{ "some_int", 42 },
});
// operator<< should not throw exceptions
std::cout << conf;
EXPECT_EQ(*conf.value<std::string>("some_string"), "value1");
EXPECT_EQ(*conf.value<bool>("some_bool"), true);
EXPECT_EQ(*conf.value<int>("some_int"), 42);
EXPECT_FALSE(conf.value<int>("no_int"));
}
TEST(Configuration, read_file)
{
// because the init_list is empty, all types are strings
Configuration conf({});
EXPECT_TRUE(conf.read_file("defaultrc.ini"));
std::cout << "---------\n";
std::cout << conf;
std::cout << "---------\n";
// defaultrc.ini contents
EXPECT_EQ(*conf.value<std::string>("name"), "Top level");
EXPECT_FALSE(conf.value<std::string>("comment"));
EXPECT_EQ(*conf.value<std::string>("main/name"), "Section Testing");
// casting a string to an integer
EXPECT_EQ(*conf.value<int>("number"), 12);
// casting a string to a boolean
EXPECT_EQ(*conf.value<std::string>("toggle"), "true");
EXPECT_EQ(*conf.value<bool>("toggle"), true);
EXPECT_EQ(*conf.value<bool>("main/toggle"), false);
// extrarc.ini contents
EXPECT_EQ(*conf.value<std::string>("over"), "extra");
EXPECT_EQ(*conf.value<std::string>("extra/name"), "extra section");
}
|