-
Notifications
You must be signed in to change notification settings - Fork 3
/
log_entry_test.cpp
168 lines (138 loc) Β· 5.46 KB
/
log_entry_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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <fmt/format.h>
#include <gtest/gtest.h>
#include "log/log_file.hpp"
#include "utils/string.hpp"
namespace caps_log::log::testing {
namespace {
struct ParsingTestData {
std::string text;
std::set<std::string> expectedResult;
};
inline ParsingTestData makeTagParsingData(const std::string &base, std::string title) {
return {fmt::format(fmt::runtime(base), title), {caps_log::utils::lowercase(title)}};
}
inline ParsingTestData makeSectionParsingData(const std::string &base, std::string title) {
// prepend a newline since first line is ignored when it comes to parsing sections
return {fmt::format(fmt::runtime("\n" + base), title), {caps_log::utils::lowercase(title)}};
}
const std::vector<ParsingTestData> validTagsTestData{
makeTagParsingData("* {}", "tag"),
makeTagParsingData("* {}", "TAG"),
makeTagParsingData("* {}", "TAG"),
makeTagParsingData("* {}\n", "TAG"),
makeTagParsingData(" * {} ", "TAG"),
makeTagParsingData(" * {}", "TAG"),
makeTagParsingData(" * {}", "Multi Word Title"),
makeTagParsingData(" * {}", "Multi Word Title"),
makeTagParsingData(
"* {}\n", "VERRY long tag title with a bunchhhh of words and words and words and words"),
makeTagParsingData("* {} (info)\n", "title 123"),
makeTagParsingData("* {}: some other information", "tag title"),
makeTagParsingData("* {} (minor info): some other information", "tag title"),
makeTagParsingData("* {} (minor info): some other information\non two lines", "tag title"),
};
const std::vector<ParsingTestData> invalidTagsTestData{
makeTagParsingData("* {} (123)\n", "123)"),
makeTagParsingData("* {} ($$$$) ", "123)"),
makeTagParsingData("* {}\n", "title with)"),
makeTagParsingData("* {}\n", "# title with"),
};
const std::vector<ParsingTestData> validSectionsTestData{
makeSectionParsingData("# {}", "section"),
makeSectionParsingData("# {}", "section title"),
makeSectionParsingData("# {}", "section title"),
makeSectionParsingData(" # {}", "section"),
};
const std::vector<ParsingTestData> invalidSectionsTestData{
makeSectionParsingData("## {}", "section"),
makeSectionParsingData("### {}", "section"),
};
const std::chrono::year_month_day kDate{std::chrono::year{2021}, std::chrono::month{1},
std::chrono::day{1}};
} // namespace
TEST(LogEntry, ParseTagTitles_Valid) {
for (const auto &[text, expectedTagTitles] : validTagsTestData) {
const auto parsedTagTitles = LogFile{kDate, text}.parse().getTagTitles();
EXPECT_EQ(parsedTagTitles, expectedTagTitles) << "Failed at: " << text;
}
}
TEST(LogEntry, ParseSectionTitles_Valid) {
for (const auto &[text, expectedTagTitles] : validSectionsTestData) {
const auto parsedSectionTitles = LogFile{kDate, text}.parse().getSectionTitles();
EXPECT_EQ(parsedSectionTitles, expectedTagTitles) << "Failed at: " << text;
}
}
TEST(LogEntry, ParseTagTitles_Invalid) {
for (const auto &[text, _] : invalidSectionsTestData) {
const auto parsedTagTitles = LogFile{kDate, text}.parse().getTagTitles();
EXPECT_TRUE(parsedTagTitles.empty()) << "Failed at: " << text;
}
}
TEST(LogEntry, ParseSectionTitles_Invalid) {
for (const auto &[text, _] : invalidSectionsTestData) {
const auto parsedSectionTitles = LogFile{kDate, text}.parse().getSectionTitles();
EXPECT_TRUE(parsedSectionTitles.empty()) << "Failed at: " << text;
}
}
TEST(LogEntry, ParseTagsPerSection) {
const auto *content = R"(
* tag 0 1
# section 1
* tag 1 1
* tag 1 2
# section 2
* tag 2 1
* tag 2 2
* tag 2 3
# empty section
)";
auto parsedTagsPerSection = LogFile{kDate, content}.parse().getTagsPerSection();
EXPECT_EQ(parsedTagsPerSection.size(), 4);
const auto expectedSection1Tags = std::set<std::string>{"tag 1 1", "tag 1 2"};
EXPECT_EQ(parsedTagsPerSection["section 1"], expectedSection1Tags);
const auto expectedSection2Tags = std::set<std::string>{"tag 2 1", "tag 2 2", "tag 2 3"};
EXPECT_EQ(parsedTagsPerSection["section 2"], expectedSection2Tags);
const auto expectedSection0Tags = std::set<std::string>{"tag 0 1"};
EXPECT_EQ(parsedTagsPerSection[LogFile::kRootSectionKey.data()], expectedSection0Tags);
ASSERT_TRUE(parsedTagsPerSection.contains("empty section"));
EXPECT_TRUE(parsedTagsPerSection.at("empty section").empty());
}
TEST(LogEntry, ParseSectionTitels_IgnoreSectionsInCodeBlocks) {
const auto *sectionInCodeBlock = R"(
```
# section title
# section title 2
```
)";
const auto *sectionInCodeBlock2 = R"(
```sh
# section title
# section title 2
```
)";
auto parsedSectionTitles = LogFile{kDate, sectionInCodeBlock}.parse().getSectionTitles();
EXPECT_TRUE(parsedSectionTitles.empty());
parsedSectionTitles = LogFile{kDate, sectionInCodeBlock2}.parse().getSectionTitles();
EXPECT_TRUE(parsedSectionTitles.empty());
}
TEST(LogEntry, ParseTagTitles_IgnoreTagInCodeBlocks) {
const auto *const sectionInCodeBlock = R"(
```
* tag title
* tag title 2
* tag title 3
```
)";
const auto *const sectionInCodeBlock2 = R"(
```sh
* tag title
* tag title 2
* tag title 3
```
)";
auto parsedSectionTag = LogFile{kDate, sectionInCodeBlock}.parse().getTagTitles();
EXPECT_TRUE(parsedSectionTag.empty());
parsedSectionTag = LogFile{kDate, sectionInCodeBlock2}.parse().getTagTitles();
EXPECT_TRUE(parsedSectionTag.empty());
}
} // namespace caps_log::log::testing