Branch data Line data Source code
1 : : /*
2 : : * Copyright (C) 2025 Khalid Abu Shawarib.
3 : : *
4 : : * SPDX-License-Identifier: LGPL-2.1-or-later
5 : : *
6 : : * Author: Khalid Abu Shawarib <kas@gnome.org>
7 : : */
8 : :
9 : : #include <gio/gio.h>
10 : :
11 : : static void
12 : 1 : create_files (const GStrv hier)
13 : : {
14 : 1 : g_mkdir_with_parents (g_get_home_dir (), 0700);
15 : :
16 : 5 : for (guint i = 0; hier[i] != NULL; i++)
17 : : {
18 : 4 : GFile *file = g_file_new_build_filename (g_get_home_dir (), hier[i], NULL);
19 : 4 : gboolean is_directory = g_str_has_suffix (hier[i], G_DIR_SEPARATOR_S);
20 : :
21 : 4 : if (is_directory)
22 : 2 : g_file_make_directory (file, NULL, NULL);
23 : : else
24 : : {
25 : 2 : GFileOutputStream *stream = g_file_create (file, G_FILE_CREATE_NONE, NULL, NULL);
26 : :
27 : 2 : g_object_unref (stream);
28 : : }
29 : :
30 : 4 : g_assert_true (g_file_query_exists (file, NULL));
31 : :
32 : 4 : g_object_unref (file);
33 : : }
34 : 1 : }
35 : :
36 : : static GStrv
37 : 6 : prefix_filenames (const gchar *const strings[],
38 : : const gchar *prefix)
39 : : {
40 : 6 : GStrvBuilder *builder = g_strv_builder_new ();
41 : :
42 : 18 : for (guint i = 0; strings[i] != NULL; i++)
43 : 12 : g_strv_builder_take (builder, g_build_filename (prefix, strings[i], NULL));
44 : :
45 : 6 : return g_strv_builder_unref_to_strv (builder);
46 : : }
47 : :
48 : : typedef struct
49 : : {
50 : : const gchar *string;
51 : : const gchar *all_completion_suffix;
52 : : const gchar *dirs_completion_suffix;
53 : : const gchar *const all_completions[5];
54 : : const gchar *const dirs_completions[5];
55 : : } FilenameCompleterTestCase;
56 : :
57 : : static void
58 : 1 : run_test_cases (const FilenameCompleterTestCase test_cases[],
59 : : guint len)
60 : : {
61 : 1 : const gchar *base_path = g_get_home_dir ();
62 : :
63 : 4 : for (guint i = 0; i < len; i++)
64 : : {
65 : 3 : GFilenameCompleter *all_completer = g_filename_completer_new ();
66 : 3 : GFilenameCompleter *dirs_completer = g_filename_completer_new ();
67 : : gchar *completion_suffix;
68 : : GStrv all_results_completions, all_expected_completions;
69 : : GStrv dirs_results_completions, dirs_expected_completions;
70 : 3 : gchar *path_to_complete = g_build_filename (base_path, test_cases[i].string, NULL);
71 : 3 : GMainLoop *loop = g_main_loop_new (NULL, FALSE);
72 : :
73 : : /* dirs_only = FALSE */
74 : 3 : g_signal_connect_swapped (all_completer, "got-completion-data",
75 : : G_CALLBACK (g_main_loop_quit), loop);
76 : 3 : g_filename_completer_set_dirs_only (all_completer, FALSE);
77 : 3 : g_strfreev (g_filename_completer_get_completions (all_completer, path_to_complete));
78 : :
79 : 3 : g_main_loop_run (loop);
80 : :
81 : 3 : all_expected_completions = prefix_filenames (test_cases[i].all_completions, base_path);
82 : 3 : all_results_completions = g_filename_completer_get_completions (all_completer, path_to_complete);
83 : 11 : g_assert_cmpstrv (all_expected_completions, all_results_completions);
84 : 3 : g_strfreev (all_expected_completions);
85 : 3 : g_strfreev (all_results_completions);
86 : :
87 : 3 : completion_suffix = g_filename_completer_get_completion_suffix (all_completer,
88 : : path_to_complete);
89 : 3 : g_assert_cmpstr (test_cases[i].all_completion_suffix, ==, completion_suffix);
90 : 3 : g_free (completion_suffix);
91 : :
92 : : /* dirs_only = TRUE */
93 : 3 : g_signal_connect_swapped (dirs_completer, "got-completion-data",
94 : : G_CALLBACK (g_main_loop_quit), loop);
95 : 3 : g_filename_completer_set_dirs_only (dirs_completer, TRUE);
96 : 3 : g_strfreev (g_filename_completer_get_completions (dirs_completer, path_to_complete));
97 : :
98 : 3 : g_main_loop_run (loop);
99 : :
100 : 3 : dirs_expected_completions = prefix_filenames (test_cases[i].dirs_completions, base_path);
101 : 3 : dirs_results_completions = g_filename_completer_get_completions (dirs_completer, path_to_complete);
102 : 7 : g_assert_cmpstrv (dirs_expected_completions, dirs_results_completions);
103 : :
104 : 3 : g_strfreev (dirs_expected_completions);
105 : 3 : g_strfreev (dirs_results_completions);
106 : :
107 : 3 : completion_suffix = g_filename_completer_get_completion_suffix (dirs_completer,
108 : : path_to_complete);
109 : 3 : g_assert_cmpstr (test_cases[i].dirs_completion_suffix, ==, completion_suffix);
110 : 3 : g_free (completion_suffix);
111 : :
112 : 3 : g_object_unref (all_completer);
113 : 3 : g_object_unref (dirs_completer);
114 : 3 : g_free (path_to_complete);
115 : 3 : g_main_loop_unref (loop);
116 : : }
117 : 1 : }
118 : :
119 : : static void
120 : 1 : test_completions (void)
121 : : {
122 : 1 : const GStrv files_hier = (char *[]){
123 : : "file_1",
124 : : "file_2",
125 : : "folder_1" G_DIR_SEPARATOR_S,
126 : : "folder_2" G_DIR_SEPARATOR_S,
127 : : NULL
128 : : };
129 : 1 : const FilenameCompleterTestCase test_cases[] = {
130 : : {
131 : : "f",
132 : : "",
133 : : "older_",
134 : : { "file_1",
135 : : "file_2",
136 : : "folder_1" G_DIR_SEPARATOR_S,
137 : : "folder_2" G_DIR_SEPARATOR_S,
138 : : NULL },
139 : : { "folder_1" G_DIR_SEPARATOR_S,
140 : : "folder_2" G_DIR_SEPARATOR_S,
141 : : NULL },
142 : : },
143 : :
144 : : {
145 : : "fi",
146 : : "le_",
147 : : NULL,
148 : : { "file_1",
149 : : "file_2",
150 : : NULL },
151 : : { NULL },
152 : : },
153 : :
154 : : {
155 : : "fo",
156 : : "lder_",
157 : : "lder_",
158 : : { "folder_1" G_DIR_SEPARATOR_S,
159 : : "folder_2" G_DIR_SEPARATOR_S,
160 : : NULL },
161 : : { "folder_1" G_DIR_SEPARATOR_S,
162 : : "folder_2" G_DIR_SEPARATOR_S,
163 : : NULL },
164 : : },
165 : : };
166 : :
167 : 1 : create_files (files_hier);
168 : :
169 : 1 : run_test_cases (test_cases, G_N_ELEMENTS (test_cases));
170 : 1 : }
171 : :
172 : : int
173 : 1 : main (int argc, char *argv[])
174 : : {
175 : 1 : g_setenv ("LC_ALL", "C", TRUE);
176 : :
177 : 1 : g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
178 : :
179 : 1 : g_test_add_func ("/filenamecompleter/basic", test_completions);
180 : :
181 : 1 : return g_test_run ();
182 : : }
|