-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestbuilder.cpp
53 lines (40 loc) · 1.31 KB
/
testbuilder.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
#include "testbuilder.hpp"
#include <filesystem>
#include <fstream>
#include <string>
#include <algorithm>
#include <sstream>
TestBuilder::TestBuilder(const std::filesystem::path &package_directory) {
constexpr auto go_src = ".go";
constexpr auto test_prefix = "_test.go";
for (const auto &de : std::filesystem::directory_iterator(package_directory)) {
auto p = std::filesystem::path{de};
if (p.extension() != go_src || p.string().substr(0, 4) == test_prefix) {
continue;
}
this->parse_file(p);
}
this->whole_file << "package " << package_directory.filename().string() << std::endl;
for (const auto &imp : this->imports) {
this->whole_file << imp << std::endl;
}
}
bool TestBuilder::validate(const std::string &annotation) {
return std::all_of(std::begin(this->required_fields), std::end(this->required_fields),
[&](auto field) { return annotation.find(field) != std::string::npos; });
}
void TestBuilder::parse_file(const std::filesystem::path &file) {
auto f = std::ifstream{file};
std::string line;
while (std::getline(f, line)) {
if (line.substr(0, 2) == "//" && this->validate(line)) {
this->test_cases.emplace_back(line);
}
}
}
std::string TestBuilder::generate_tests() {
for (auto &tc : this->test_cases) {
this->whole_file << tc.get() << std::endl;
}
return this->whole_file.str();
}