1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <check.h>
4 #include <string.h>
5 
6 #include <gtk/gtk.h>
7 #include "geany.h"
8 #include "geanyprj.h"
9 
10 
11 void
file_setup(void)12 file_setup(void)
13 {
14 	system("mkdir -p test_list_dir/nested/nested2");
15 	system("mkdir -p test_list_dir/nested/nested3");
16 	system("echo A > test_list_dir/A.txt");
17 	system("echo A > test_list_dir/B.txt");
18 	system("echo A > test_list_dir/nested/C.txt");
19 	system("echo A > test_list_dir/nested/nested2/D.txt");
20 }
21 
22 void
file_teardown(void)23 file_teardown(void)
24 {
25 	system("rm -rf test_list_dir");
26 }
27 
28 gboolean
29 true(G_GNUC_UNUSED const gchar * arg)
30 {
31 	return TRUE;
32 }
33 
START_TEST(test_get_file_list)34 START_TEST(test_get_file_list)
35 {
36 	GSList *files = get_file_list("test_list_dir", NULL, NULL, NULL);
37 	fail_unless(g_slist_length(files) == 4, "expected %d, get %d", 4, g_slist_length(files));
38 }
39 
40 END_TEST;
41 
START_TEST(test_get_full_path)42 START_TEST(test_get_full_path)
43 {
44 	gchar *newpath = get_full_path("/home/yura/src/geanyprj/.geanyprj", "./abc.c");
45 	fail_unless(strcmp(newpath, "/home/yura/src/geanyprj/abc.c") == 0,
46 		    "expected %s, get %s", "/home/yura/src/geanyprj/abc.c", newpath);
47 	g_free(newpath);
48 }
49 
50 END_TEST;
51 
START_TEST(test_get_relative_path)52 START_TEST(test_get_relative_path)
53 {
54 	gchar *newpath = get_relative_path("/home/yura/src/geanyprj/.geanyprj",
55 					   "/home/yura/src/geanyprj/src/a.c");
56 	fail_unless(strcmp(newpath, "src/a.c") == 0, "expected %s, get %s", "src/a.c", newpath);
57 	g_free(newpath);
58 
59 	newpath = get_relative_path("/home/yura/src/geanyprj/.geanyprj", "/home/yura/src/geanyprj");
60 	fail_unless(strcmp(newpath, "./") == 0, "expected %s, get %s", "./", newpath);
61 	g_free(newpath);
62 }
63 
64 END_TEST;
65 
START_TEST(test_find_file_path)66 START_TEST(test_find_file_path)
67 {
68 	fail_unless(find_file_path("insane_dir", "insane_file!!!!!!!!!!!!!!!2") == NULL);
69 }
70 
71 END_TEST;
72 
73 
START_TEST(test_normpath)74 START_TEST(test_normpath)
75 {
76 	gchar *newpath = normpath("/a/b");
77 	fail_unless(strcmp(newpath, "/a/b") == 0, "expected %s, get %s", "/a/b", newpath);
78 	g_free(newpath);
79 
80 	newpath = normpath("./a/b");
81 	fail_unless(strcmp(newpath, "./a/b") == 0, "expected %s, get %s", "./a/b", newpath);
82 	g_free(newpath);
83 
84 	newpath = normpath("/a/./b");
85 	fail_unless(strcmp(newpath, "/a/b") == 0, "expected %s, get %s", "/a/b", newpath);
86 	g_free(newpath);
87 
88 	newpath = normpath("/a/.//b");
89 	fail_unless(strcmp(newpath, "/a/b") == 0, "expected %s, get %s", "/a/b", newpath);
90 	g_free(newpath);
91 
92 	newpath = normpath("c:\\a\\.\\b");
93 	fail_unless(strcmp(newpath, "c:/a/b") == 0, "expected %s, get %s", "c:/a/b", newpath);
94 	g_free(newpath);
95 
96 	newpath = normpath("/a/../b");
97 	fail_unless(strcmp(newpath, "/b") == 0, "expected %s, get %s", "/b", newpath);
98 	g_free(newpath);
99 
100 	newpath = normpath("../a");
101 	fail_unless(strcmp(newpath, "../a") == 0, "expected %s, get %s", "../a", newpath);
102 	g_free(newpath);
103 
104 	newpath = normpath("../a/..");
105 	fail_unless(strcmp(newpath, "..") == 0, "expected %s, get %s", "..", newpath);
106 	g_free(newpath);
107 }
108 
109 END_TEST;
110 
111 Suite *
my_suite(void)112 my_suite(void)
113 {
114 	Suite *s = suite_create("Project");
115 	TCase *tc_core = tcase_create("utils");
116 
117 	suite_add_tcase(s, tc_core);
118 	tcase_add_test(tc_core, test_get_full_path);
119 	tcase_add_test(tc_core, test_get_relative_path);
120 	tcase_add_test(tc_core, test_find_file_path);
121 	tcase_add_test(tc_core, test_normpath);
122 
123 	TCase *tc_file = tcase_create("file_utils");
124 	suite_add_tcase(s, tc_file);
125 	tcase_add_test(tc_file, test_get_file_list);
126 
127 	tcase_add_checked_fixture(tc_file, file_setup, file_teardown);
128 	return s;
129 }
130 
131 int
main(void)132 main(void)
133 {
134 	int nf;
135 	Suite *s = my_suite();
136 	SRunner *sr = srunner_create(s);
137 	srunner_run_all(sr, CK_NORMAL);
138 	nf = srunner_ntests_failed(sr);
139 	srunner_free(sr);
140 	return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
141 }
142