Repository navigation
Expand file tree
/
Copy pathtest.cpp
More file actions
219 lines (170 loc) · 5.6 KB
/
Copy pathtest.cpp
File metadata and controls
219 lines (170 loc) · 5.6 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <cstdio>
#include <cstdlib>
#include <filesystem>
#include <string>
#include <vector>
#include "mado.hpp"
// test framework
enum class TestCategory {
INIT,
};
struct TestCase {
std::vector<std::string> args;
TestCategory category;
const char *description;
void (*func)(TestCase *test);
};
size_t failed_tests = 0;
const char *category_name(TestCategory cat) {
switch (cat) {
case TestCategory::INIT:
return "Init";
default:
return "Unknown";
}
}
void test(bool check, TestCase *t) {
if (!check) {
failed_tests++;
fprintf(stderr, "%-20s %-50s FAIL\n",
category_name(t->category), t->description);
} else {
fprintf(stderr, "%-20s %-50s PASS\n",
category_name(t->category), t->description);
}
}
// helpers
std::filesystem::path create_temp_dir() {
auto temp_dir = std::filesystem::temp_directory_path() /
("mado_test_" + std::to_string(rand()));
std::filesystem::create_directory(temp_dir);
return temp_dir;
}
bool is_default_mado_structure(const std::filesystem::path &main_dir) {
// chekc main dir
if (!std::filesystem::exists(main_dir) ||
!std::filesystem::is_directory(main_dir))
return false;
// check templates dir
auto templates_dir = main_dir / ".templates";
if (!std::filesystem::exists(templates_dir) ||
!std::filesystem::is_directory(templates_dir))
return false;
// check default templates
auto task_template = templates_dir / "task.md";
auto note_template = templates_dir / "note.md";
if (!std::filesystem::exists(task_template) ||
!std::filesystem::is_regular_file(task_template))
return false;
if (!std::filesystem::exists(note_template) ||
!std::filesystem::is_regular_file(note_template))
return false;
return true;
}
// test init
void test_init_base(TestCase *t) {
auto temp_dir = create_temp_dir();
std::filesystem::current_path(temp_dir);
Mado_Config cfg;
mado_init_config(&cfg);
auto [main_dir, err] = mado_main_dir_init(&cfg, 0);
Mado_Error templates_err = mado_templates_dir_init(&cfg);
test(err == Mado_Error::OK &&
templates_err == Mado_Error::OK &&
is_default_mado_structure(main_dir),
t);
std::filesystem::remove_all(temp_dir);
}
// test init already exists
void test_init_already_exists_above(TestCase *t) {
auto temp_dir = create_temp_dir();
std::filesystem::current_path(temp_dir);
Mado_Config cfg;
mado_init_config(&cfg);
auto [main_dir1, err1] = mado_main_dir_init(&cfg, 0);
Mado_Error templates_err = mado_templates_dir_init(&cfg);
std::filesystem::current_path(main_dir1);
auto [main_dir2, err2] = mado_main_dir_init(&cfg, 0);
test(err1 == Mado_Error::OK &&
templates_err == Mado_Error::OK &&
err2 == Mado_Error::FOUND_ABOVE &&
is_default_mado_structure(main_dir1),
t);
std::filesystem::remove_all(temp_dir);
}
void test_init_already_exists_above_force(TestCase *t) {
auto temp_dir = create_temp_dir();
Mado_Config cfg;
std::filesystem::current_path(temp_dir);
mado_init_config(&cfg);
auto [main_dir1, err1] = mado_main_dir_init(&cfg, 0);
Mado_Error terr = mado_templates_dir_init(&cfg);
std::filesystem::current_path(main_dir1);
mado_init_config(&cfg);
auto [main_dir2, err2] = mado_main_dir_init(&cfg, 1);
Mado_Error terr2 = mado_templates_dir_init(&cfg);
test(err1 == Mado_Error::OK &&
terr == Mado_Error::OK &&
is_default_mado_structure(main_dir1) &&
err2 == Mado_Error::OK &&
terr2 == Mado_Error::OK &&
is_default_mado_structure(main_dir2),
t);
std::filesystem::remove_all(temp_dir);
}
void test_init_already_exists_current_force(TestCase *t) {
auto temp_dir = create_temp_dir();
Mado_Config cfg;
std::filesystem::current_path(temp_dir);
mado_init_config(&cfg);
auto [main_dir1, err1] = mado_main_dir_init(&cfg, 0);
Mado_Error terr = mado_templates_dir_init(&cfg);
std::filesystem::current_path(temp_dir);
mado_init_config(&cfg);
auto [main_dir2, err2] = mado_main_dir_init(&cfg, 1);
Mado_Error terr2 = mado_templates_dir_init(&cfg);
test(err1 == Mado_Error::OK &&
terr == Mado_Error::OK &&
is_default_mado_structure(main_dir1) &&
err2 == Mado_Error::ALREADY_EXISTS &&
terr2 == Mado_Error::OK,
t);
std::filesystem::remove_all(temp_dir);
}
// test suite
TestCase tests[] = {
{
.args = {},
.category = TestCategory::INIT,
.description = "base init",
.func = test_init_base,
},
{
.args = {},
.category = TestCategory::INIT,
.description = "fail init if already exists above",
.func = test_init_already_exists_above,
},
{
.args = {},
.category = TestCategory::INIT,
.description = "init if already exists above (force)",
.func = test_init_already_exists_above_force,
},
{
.args = {},
.category = TestCategory::INIT,
.description = "fail init if already exists current (force)",
.func = test_init_already_exists_current_force,
},
};
// entry point
int main() {
size_t tests_count = sizeof(tests) / sizeof(tests[0]);
for (size_t i = 0; i < tests_count; i++) {
tests[i].func(&tests[i]);
}
printf("\n(%zu / %zu) tests passed\n",
tests_count - failed_tests, tests_count);
return failed_tests >= 1;
}