1 #include "cmake.hpp"
2 #include "config.hpp"
3 #include "process.hpp"
4 #include "project_build.hpp"
5 #include <boost/filesystem.hpp>
6 #include <glib.h>
7 
main()8 int main() {
9   {
10     bool called = false;
11     std::map<std::string, std::list<std::string>> variables;
12     CMake::parse_file("", variables, [&called](CMake::Function && /*function*/) {
13       called = true;
14     });
15     g_assert(!called);
16   }
17   {
18     bool called = false;
19     std::map<std::string, std::list<std::string>> variables;
20     CMake::parse_file("project(", variables, [&called](CMake::Function && /*function*/) {
21       called = true;
22     });
23     g_assert(!called);
24   }
25   {
26     bool called = false;
27     std::map<std::string, std::list<std::string>> variables;
28     CMake::parse_file("project(test", variables, [&called](CMake::Function && /*function*/) {
29       called = true;
30     });
31     g_assert(!called);
32   }
33   {
34     bool called = false;
35     std::map<std::string, std::list<std::string>> variables;
36     CMake::parse_file("project(test)", variables, [&called](CMake::Function &&function) {
37       called = true;
38       g_assert(function.name == "project");
39       g_assert(function.parameters.size() == 1);
40       g_assert(function.parameters.front() == "test");
41     });
42     g_assert(variables.size() == 2);
43     auto it = variables.begin();
44     g_assert(it->first == "CMAKE_PROJECT_NAME");
45     g_assert(it->second == std::list<std::string>{"test"});
46     ++it;
47     g_assert(it->first == "PROJECT_NAME");
48     g_assert(it->second == std::list<std::string>{"test"});
49     g_assert(called);
50   }
51   {
52     bool called = false;
53     std::map<std::string, std::list<std::string>> variables;
54     CMake::parse_file("project(\"test\")", variables, [&called](CMake::Function &&function) {
55       called = true;
56       g_assert(function.name == "project");
57       g_assert(function.parameters.size() == 1);
58       g_assert(function.parameters.front() == "test");
59     });
60     g_assert(called);
61   }
62   {
63     bool called = false;
64     std::map<std::string, std::list<std::string>> variables;
65     CMake::parse_file("project(\"te\\\"st\")", variables, [&called](CMake::Function &&function) {
66       called = true;
67       g_assert(function.name == "project");
68       g_assert(function.parameters.size() == 1);
69       g_assert(function.parameters.front() == "te\"st");
70     });
71     g_assert(called);
72   }
73   {
74     int called = 0;
75     std::map<std::string, std::list<std::string>> variables;
76     CMake::parse_file("set(TEST testing)\nadd_executable(${TEST} test.cpp)", variables, [&called](CMake::Function &&function) {
77       called++;
78       if(called == 1)
79         g_assert(function.name == "set");
80       else {
81         g_assert(function.name == "add_executable");
82         g_assert(function.parameters.size() == 2);
83         auto it = function.parameters.begin();
84         g_assert(*it == "testing");
85         g_assert(*(++it) == "test.cpp");
86       }
87     });
88     g_assert(called == 2);
89   }
90   {
91     bool called = false;
92     std::map<std::string, std::list<std::string>> variables;
93     CMake::parse_file("test(${})", variables, [&called](CMake::Function &&function) {
94       called = true;
95       g_assert(function.name == "test");
96       g_assert(function.parameters.size() == 1);
97       g_assert(function.parameters.front() == "");
98     });
99     g_assert(called);
100   }
101   {
102     bool called = false;
103     std::map<std::string, std::list<std::string>> variables;
104     CMake::parse_file("test(\"${}\")", variables, [&called](CMake::Function &&function) {
105       called = true;
106       g_assert(function.name == "test");
107       g_assert(function.parameters.size() == 1);
108       g_assert(function.parameters.front() == "");
109     });
110     g_assert(called);
111   }
112   {
113     bool called = false;
114     std::map<std::string, std::list<std::string>> variables;
115     CMake::parse_file("test($TEST)", variables, [&called](CMake::Function &&function) {
116       called = true;
117       g_assert(function.name == "test");
118       g_assert(function.parameters.size() == 1);
119       g_assert(function.parameters.front() == "$TEST");
120     });
121     g_assert(called);
122   }
123   {
124     bool called = false;
125     std::map<std::string, std::list<std::string>> variables;
126     CMake::parse_file("test(${TEST})", variables, [&called](CMake::Function &&function) {
127       called = true;
128       g_assert(function.name == "test");
129       g_assert(function.parameters.size() == 1);
130       g_assert(function.parameters.front() == "");
131     });
132     g_assert(called);
133   }
134   {
135     bool called = false;
136     std::map<std::string, std::list<std::string>> variables;
137     CMake::parse_file("test(\"$TEST\")", variables, [&called](CMake::Function &&function) {
138       called = true;
139       g_assert(function.name == "test");
140       g_assert(function.parameters.size() == 1);
141       g_assert(function.parameters.front() == "$TEST");
142     });
143     g_assert(called);
144   }
145   {
146     bool called = false;
147     std::map<std::string, std::list<std::string>> variables;
148     CMake::parse_file("test(\"${TEST}\")", variables, [&called](CMake::Function &&function) {
149       called = true;
150       g_assert(function.name == "test");
151       g_assert(function.parameters.size() == 1);
152       g_assert(function.parameters.front() == "");
153     });
154     g_assert(called);
155   }
156   {
157     bool called = false;
158     std::map<std::string, std::list<std::string>> variables;
159     CMake::parse_file("test(${TEST} ${TEST})", variables, [&called](CMake::Function &&function) {
160       called = true;
161       g_assert(function.name == "test");
162       g_assert(function.parameters.size() == 2);
163       auto it = function.parameters.begin();
164       g_assert(*it == "");
165       g_assert(*(++it) == "");
166     });
167     g_assert(called);
168   }
169   {
170     bool called = false;
171     std::map<std::string, std::list<std::string>> variables;
172     CMake::parse_file("test(\"${TEST}\" \"${TEST}\")", variables, [&called](CMake::Function &&function) {
173       called = true;
174       g_assert(function.name == "test");
175       g_assert(function.parameters.size() == 2);
176       auto it = function.parameters.begin();
177       g_assert(*it == "");
178       g_assert(*(++it) == "");
179     });
180     g_assert(called);
181   }
182   {
183     bool called = false;
184     std::map<std::string, std::list<std::string>> variables;
185     CMake::parse_file("test(${TEST} \"${TEST}\")", variables, [&called](CMake::Function &&function) {
186       called = true;
187       g_assert(function.name == "test");
188       g_assert(function.parameters.size() == 2);
189       auto it = function.parameters.begin();
190       g_assert(*it == "");
191       g_assert(*(++it) == "");
192     });
193     g_assert(called);
194   }
195   {
196     bool called = false;
197     std::map<std::string, std::list<std::string>> variables;
198     CMake::parse_file("test(\"${TEST}\" ${TEST})", variables, [&called](CMake::Function &&function) {
199       called = true;
200       g_assert(function.name == "test");
201       g_assert(function.parameters.size() == 2);
202       auto it = function.parameters.begin();
203       g_assert(*it == "");
204       g_assert(*(++it) == "");
205     });
206     g_assert(called);
207   }
208   {
209     bool called = false;
210     std::map<std::string, std::list<std::string>> variables;
211     CMake::parse_file("test(\"\" \"\")", variables, [&called](CMake::Function &&function) {
212       called = true;
213       g_assert(function.name == "test");
214       g_assert(function.parameters.size() == 2);
215       auto it = function.parameters.begin();
216       g_assert(*it == "");
217       g_assert(*(++it) == "");
218     });
219     g_assert(called);
220   }
221   {
222     bool called = false;
223     std::map<std::string, std::list<std::string>> variables;
224     CMake::parse_file("test(    \"\"   \"\"    )", variables, [&called](CMake::Function &&function) {
225       called = true;
226       g_assert(function.name == "test");
227       g_assert(function.parameters.size() == 2);
228       auto it = function.parameters.begin();
229       g_assert(*it == "");
230       g_assert(*(++it) == "");
231     });
232     g_assert(called);
233   }
234   {
235     bool called = false;
236     std::map<std::string, std::list<std::string>> variables;
237     CMake::parse_file("test\n(\n\"\"\n\"\"\n)", variables, [&called](CMake::Function &&function) {
238       called = true;
239       g_assert(function.name == "test");
240       g_assert(function.parameters.size() == 2);
241       auto it = function.parameters.begin();
242       g_assert(*it == "");
243       g_assert(*(++it) == "");
244     });
245     g_assert(called);
246   }
247   {
248     int called = 0;
249     std::map<std::string, std::list<std::string>> variables;
250     CMake::parse_file("set(TEST testing)\nadd_executable(test${TEST}test test.cpp)", variables, [&called](CMake::Function &&function) {
251       called++;
252       if(called == 1)
253         g_assert(function.name == "set");
254       else {
255         g_assert(function.name == "add_executable");
256         g_assert(function.parameters.size() == 2);
257         auto it = function.parameters.begin();
258         g_assert(*it == "testtestingtest");
259         g_assert(*(++it) == "test.cpp");
260       }
261     });
262     g_assert(called == 2);
263   }
264   {
265     int called = 0;
266     std::map<std::string, std::list<std::string>> variables;
267     CMake::parse_file("set(TEST testing)\nadd_executable(\"${TEST}\" test.cpp)", variables, [&called](CMake::Function &&function) {
268       called++;
269       if(called == 1)
270         g_assert(function.name == "set");
271       else {
272         g_assert(function.name == "add_executable");
273         g_assert(function.parameters.size() == 2);
274         auto it = function.parameters.begin();
275         g_assert(*it == "testing");
276         g_assert(*(++it) == "test.cpp");
277       }
278     });
279     g_assert(called == 2);
280   }
281   {
282     int called = 0;
283     std::map<std::string, std::list<std::string>> variables;
284     CMake::parse_file("set(TEST testing)\nadd_executable(\"test${TEST}test\" test.cpp)", variables, [&called](CMake::Function &&function) {
285       called++;
286       if(called == 1)
287         g_assert(function.name == "set");
288       else {
289         g_assert(function.name == "add_executable");
290         g_assert(function.parameters.size() == 2);
291         auto it = function.parameters.begin();
292         g_assert(*it == "testtestingtest");
293         g_assert(*(++it) == "test.cpp");
294       }
295     });
296     g_assert(called == 2);
297   }
298   {
299     int called = 0;
300     std::map<std::string, std::list<std::string>> variables;
301     CMake::parse_file("set(TEST 1 2 3)\nadd_executable(\"${TEST}\" test.cpp)", variables, [&called](CMake::Function &&function) {
302       called++;
303       if(called == 1)
304         g_assert(function.name == "set");
305       else {
306         g_assert(function.name == "add_executable");
307         g_assert(function.parameters.size() == 2);
308         auto it = function.parameters.begin();
309         g_assert(*it == "1;2;3");
310         g_assert(*(++it) == "test.cpp");
311       }
312     });
313     g_assert(called == 2);
314   }
315   {
316     int called = 0;
317     std::map<std::string, std::list<std::string>> variables;
318     CMake::parse_file("set(TEST 1 2 3)\nadd_executable(\"aaa${TEST}bbb\" test.cpp)", variables, [&called](CMake::Function &&function) {
319       called++;
320       if(called == 1)
321         g_assert(function.name == "set");
322       else {
323         g_assert(function.name == "add_executable");
324         g_assert(function.parameters.size() == 2);
325         auto it = function.parameters.begin();
326         g_assert(*it == "aaa1;2;3bbb");
327         g_assert(*(++it) == "test.cpp");
328       }
329     });
330     g_assert(called == 2);
331   }
332   {
333     int called = 0;
334     std::map<std::string, std::list<std::string>> variables;
335     CMake::parse_file("set(TEST 1 2 3)\nadd_executable(${TEST} test.cpp)", variables, [&called](CMake::Function &&function) {
336       called++;
337       if(called == 1)
338         g_assert(function.name == "set");
339       else {
340         g_assert(function.name == "add_executable");
341         g_assert(function.parameters.size() == 4);
342         auto it = function.parameters.begin();
343         g_assert(*it == "1");
344         g_assert(*(++it) == "2");
345         g_assert(*(++it) == "3");
346         g_assert(*(++it) == "test.cpp");
347       }
348     });
349     g_assert(called == 2);
350   }
351   {
352     int called = 0;
353     std::map<std::string, std::list<std::string>> variables;
354     CMake::parse_file("set(TEST 1 2 3)\nadd_executable(aaa${TEST}bbb test.cpp)", variables, [&called](CMake::Function &&function) {
355       called++;
356       if(called == 1)
357         g_assert(function.name == "set");
358       else {
359         g_assert(function.name == "add_executable");
360         g_assert(function.parameters.size() == 4);
361         auto it = function.parameters.begin();
362         g_assert(*it == "aaa1");
363         g_assert(*(++it) == "2");
364         g_assert(*(++it) == "3bbb");
365         g_assert(*(++it) == "test.cpp");
366       }
367     });
368     g_assert(called == 2);
369   }
370 
371   auto tests_path = boost::filesystem::canonical(JUCI_TESTS_PATH);
372   {
373     auto project_path = boost::filesystem::canonical(tests_path / "..");
374 
375     {
376       CMake cmake(project_path);
377       TinyProcessLib::Process process("cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..", (project_path / "build").string(), [](const char *bytes, size_t n) {});
378       g_assert(process.get_exit_status() == 0);
379 
380       g_assert(cmake.get_executable(project_path / "build", project_path) == "");
381       g_assert(cmake.get_executable(project_path / "build" / "non_existing_file.cpp", project_path) == "");
382     }
383     {
384       CMake cmake(project_path / "src");
385       g_assert(cmake.get_executable(project_path / "build", project_path / "src") == project_path / "build" / "src" / "juci");
386       g_assert(cmake.get_executable(project_path / "build", project_path / "src" / "cmake.cpp") == project_path / "build" / "src" / "juci");
387       g_assert(cmake.get_executable(project_path / "build", project_path / "src" / "juci.cpp") == project_path / "build" / "src" / "juci");
388       g_assert(cmake.get_executable(project_path / "build", project_path / "src" / "non_existing_file.cpp") == project_path / "build" / "src" / "juci");
389     }
390     {
391       CMake cmake(tests_path);
392 
393       g_assert(cmake.project_path == project_path);
394 
395       g_assert(cmake.get_executable(project_path / "build", tests_path).parent_path() == project_path / "build" / "tests");
396       g_assert(cmake.get_executable(project_path / "build", tests_path / "cmake_build_test.cpp") == project_path / "build" / "tests" / "cmake_build_test");
397       g_assert(cmake.get_executable(project_path / "build", tests_path / "non_existing_file.cpp").parent_path() == project_path / "build" / "tests");
398     }
399 
400     auto build = Project::Build::create(tests_path);
401     g_assert(dynamic_cast<Project::CMakeBuild *>(build.get()));
402 
403     build = Project::Build::create(tests_path / "stubs");
404     g_assert(dynamic_cast<Project::CMakeBuild *>(build.get()));
405     g_assert(build->project_path == project_path);
406 
407     Config::get().project.default_build_path = "./build";
408     g_assert(build->get_default_path() == project_path / "build");
409 
410     Config::get().project.debug_build_path = "<default_build_path>/debug";
411     g_assert(build->get_debug_path() == project_path / "build/debug");
412 
413     auto project_path_filename = project_path.filename();
414     Config::get().project.debug_build_path = "../debug_<project_directory_name>";
415     g_assert(build->get_debug_path() == project_path.parent_path() / ("debug_" + project_path_filename.string()));
416 
417     Config::get().project.default_build_path = "../build_<project_directory_name>";
418     g_assert(build->get_default_path() == project_path.parent_path() / ("build_" + project_path_filename.string()));
419   }
420   {
421     auto project_path = tests_path / "source_clang_test_files";
422     CMake cmake(project_path);
423 
424     g_assert(cmake.project_path == project_path);
425 
426     g_assert(cmake.get_executable(project_path / "build", project_path / "main.cpp") == boost::filesystem::path(".") / "test");
427     g_assert(cmake.get_executable(project_path / "build", project_path / "non_existing_file.cpp") == boost::filesystem::path(".") / "test");
428     g_assert(cmake.get_executable(project_path / "build", project_path) == boost::filesystem::path(".") / "test");
429   }
430 }
431