-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils_test.cpp
153 lines (133 loc) · 4.08 KB
/
utils_test.cpp
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "flexi_cfg/utils.h"
#include <gtest/gtest.h>
#include <string>
#include <vector>
namespace {
void compareVecEq(const std::vector<std::string>& expected, const std::vector<std::string>& test) {
ASSERT_EQ(expected.size(), test.size());
for (size_t i = 0; i < expected.size(); ++i) {
EXPECT_EQ(expected[i], test[i]);
}
};
} // namespace
TEST(UtilsTest, trim) {
std::string base = "This is a test";
{
// Leading whitespace
std::string test_str = " " + base;
EXPECT_EQ(flexi_cfg::utils::trim(test_str), base);
}
{
// Trailing whitespace
std::string test_str = base + " \n\n ";
EXPECT_EQ(flexi_cfg::utils::trim(test_str), base);
}
{
// Leading and trailing whitespace
std::string test_str = " \n " + base + " \n\t\t\t ";
EXPECT_EQ(flexi_cfg::utils::trim(test_str), base);
}
{
// Leading non-whitespace
std::string test_str = "{{{" + base;
EXPECT_EQ(flexi_cfg::utils::trim(test_str, "{"), base);
}
{
// Trailing non-whitespace
std::string test_str = base + "}}}}}";
EXPECT_EQ(flexi_cfg::utils::trim(test_str, "}"), base);
}
{
// Enclosing non-whitespace
std::string test_str = "{{" + base + "}}}}";
EXPECT_EQ(flexi_cfg::utils::trim(test_str, "{}"), base);
}
}
TEST(UtilsTest, split) {
auto combine_str = [](const std::vector<std::string>& in, const std::string& sep) -> std::string {
std::string combined = in[0];
for (size_t i = 1; i < in.size(); ++i) {
combined += sep;
combined += in[i];
}
return combined;
};
const std::vector<std::string> parts = {"this", "is", "a", "test"};
{
const auto combined = combine_str(parts, ".");
const auto split = flexi_cfg::utils::split(combined, '.');
compareVecEq(parts, split);
}
{
const auto combined = combine_str(parts, ";");
const auto split = flexi_cfg::utils::split(combined, ';');
compareVecEq(parts, split);
}
{
const auto combined = combine_str(parts, "\t");
const auto split = flexi_cfg::utils::split(combined, '\t');
compareVecEq(parts, split);
}
}
TEST(UtilsTest, join) {
{
const std::vector<std::string> input{"this", "is", "a", "test"};
const std::string expected = "this.is.a.test";
const auto output = flexi_cfg::utils::join(input, ".");
EXPECT_EQ(expected, output);
}
{
const std::vector<std::string> input{"just_one"};
const std::string& expected = input[0];
const auto output = flexi_cfg::utils::join(input, ".");
EXPECT_EQ(expected, output);
}
{
const std::string expected{};
const auto output = flexi_cfg::utils::join({}, ";");
EXPECT_EQ(expected, output);
}
}
TEST(UtilsTest, splitAndJoin) {
{
const std::vector<std::string> input = {"This", "should", "always", "pass"};
const auto joined = flexi_cfg::utils::join(input, ".");
const auto split = flexi_cfg::utils::split(joined, '.');
compareVecEq(input, split);
}
{
const std::vector<std::string> input = {"one_value"};
const auto joined = flexi_cfg::utils::join(input, ".");
const auto split = flexi_cfg::utils::split(joined, '.');
compareVecEq(input, split);
}
{
const std::vector<std::string> input = {"this.should", "fail"};
const auto joined = flexi_cfg::utils::join(input, ".");
const auto split = flexi_cfg::utils::split(joined, '.');
EXPECT_NE(input.size(), split.size());
}
}
TEST(UtilsTest, makeName) {
{
// Single value first
const std::string expected = "a_string_here";
const auto result = flexi_cfg::utils::makeName(expected, "");
EXPECT_EQ(expected, result);
}
{
// Single value second
const std::string expected = "a_string_here";
const auto result = flexi_cfg::utils::makeName("", expected);
EXPECT_EQ(expected, result);
}
{
// Both values
const std::string part1 = "first_part";
const std::string part2 = "second_part";
const std::string expected = part1 + "." + part2;
const auto result = flexi_cfg::utils::makeName(part1, part2);
EXPECT_EQ(expected, result);
}
{ EXPECT_THROW(flexi_cfg::utils::makeName("", ""), std::runtime_error); }
}