Debian Codegraph @

tests/options.c

raw symbols SCIP
1 /**
2 * SPDX-FileCopyrightText: 2016-2022 Dr. Tobias Quathamer <[email protected]>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 */
6
7 #include <glib.h>
8 #include "options.h"
9
10 /**
11 * Test default value for ISO standard.
12 */
13 void test_opt_standard_default(void)
14 {
15 GError *error = NULL;
16 gboolean result = FALSE;
17
18 gchar **command_line = g_strsplit("isoquery", " ", -1);
19 result = options_parse_command_line(command_line, &error);
20 g_strfreev(command_line);
21
22 g_assert_true(result);
23 g_assert_null(error);
24 g_assert_nonnull(option_standard);
25 g_assert_cmpstr(option_standard, ==, "3166-1");
26 }
27
28 /**
29 * Test provided value for ISO standard.
30 */
31 void test_opt_standard_provided(void)
32 {
33 GError *error = NULL;
34 gboolean result = FALSE;
35
36 gchar **command_line = g_strsplit("isoquery -i 639-2", " ", -1);
37 result = options_parse_command_line(command_line, &error);
38 g_strfreev(command_line);
39
40 g_assert_true(result);
41 g_assert_null(error);
42 g_assert_nonnull(option_standard);
43 g_assert_cmpstr(option_standard, ==, "639-2");
44 }
45
46 /**
47 * Test deprecated provided value for ISO standard.
48 */
49 void test_opt_standard_deprecated_639(void)
50 {
51 GError *error = NULL;
52 gboolean result = FALSE;
53
54 gchar **command_line = g_strsplit("isoquery -i 639", " ", -1);
55 result = options_parse_command_line(command_line, &error);
56 g_strfreev(command_line);
57
58 g_assert_true(result);
59 g_assert_null(error);
60 g_assert_nonnull(option_standard);
61 g_assert_cmpstr(option_standard, ==, "639-2");
62 }
63
64 /**
65 * Test deprecated provided value for ISO standard.
66 */
67 void test_opt_standard_deprecated_3166(void)
68 {
69 GError *error = NULL;
70 gboolean result = FALSE;
71
72 gchar **command_line = g_strsplit("isoquery -i 3166", " ", -1);
73 result = options_parse_command_line(command_line, &error);
74 g_strfreev(command_line);
75
76 g_assert_true(result);
77 g_assert_null(error);
78 g_assert_nonnull(option_standard);
79 g_assert_cmpstr(option_standard, ==, "3166-1");
80 }
81
82 /**
83 * Test invalid provided value for ISO standard.
84 */
85 void test_opt_standard_invalid(void)
86 {
87 GError *error = NULL;
88 gboolean result = FALSE;
89
90 gchar **command_line = g_strsplit("isoquery -i 1234", " ", -1);
91 result = options_parse_command_line(command_line, &error);
92 g_strfreev(command_line);
93
94 g_assert_false(result);
95 g_assert_error(error, g_quark_from_string("isoquery"), G_OPTION_ERROR_UNKNOWN_OPTION);
96 }
97
98 /**
99 * Test default value for pathname.
100 */
101 void test_opt_pathname_default(void)
102 {
103 GError *error = NULL;
104 gboolean result = FALSE;
105 gchar *filename;
106
107 gchar **command_line = g_strsplit("isoquery", " ", -1);
108 result = options_parse_command_line(command_line, &error);
109 g_strfreev(command_line);
110
111 g_assert_true(result);
112 g_assert_null(error);
113 g_assert_nonnull(option_pathname);
114 g_assert_cmpstr(option_pathname, ==, "/usr/share/iso-codes/json");
115
116 filename = options_get_filename();
117 g_assert_nonnull(filename);
118 g_assert_cmpstr(filename, ==, "/usr/share/iso-codes/json/iso_3166-1.json");
119 }
120
121 /**
122 * Test provided value for pathname.
123 */
124 void test_opt_pathname_provided(void)
125 {
126 GError *error = NULL;
127 gboolean result = FALSE;
128 gchar *filename;
129
130 gchar **command_line = g_strsplit("isoquery -p /path/to/another_directory", " ", -1);
131 result = options_parse_command_line(command_line, &error);
132 g_strfreev(command_line);
133
134 g_assert_true(result);
135 g_assert_null(error);
136 g_assert_nonnull(option_pathname);
137 g_assert_cmpstr(option_pathname, ==, "/path/to/another_directory");
138
139 filename = options_get_filename();
140 g_assert_nonnull(filename);
141 g_assert_cmpstr(filename, ==, "/path/to/another_directory/iso_3166-1.json");
142 }
143
144 /**
145 * Test provided value for pathname with a terminating directory separator.
146 */
147 void test_opt_pathname_provided_with_dir_separator(void)
148 {
149 GError *error = NULL;
150 gboolean result = FALSE;
151 gchar *filename;
152
153 gchar **command_line = g_strsplit("isoquery -p /path/to/another_directory/", " ", -1);
154 result = options_parse_command_line(command_line, &error);
155 g_strfreev(command_line);
156
157 g_assert_true(result);
158 g_assert_null(error);
159 g_assert_nonnull(option_pathname);
160 g_assert_cmpstr(option_pathname, ==, "/path/to/another_directory/");
161
162 filename = options_get_filename();
163 g_assert_nonnull(filename);
164 g_assert_cmpstr(filename, ==, "/path/to/another_directory/iso_3166-1.json");
165 }
166
167 /**
168 * Test default value for pathname from other standard.
169 */
170 void test_opt_pathname_from_standard(void)
171 {
172 GError *error = NULL;
173 gboolean result = FALSE;
174 gchar *filename;
175
176 gchar **command_line = g_strsplit("isoquery -i 15924", " ", -1);
177 result = options_parse_command_line(command_line, &error);
178 g_strfreev(command_line);
179
180 g_assert_true(result);
181 g_assert_null(error);
182 g_assert_nonnull(option_standard);
183 g_assert_cmpstr(option_standard, ==, "15924");
184 g_assert_nonnull(option_pathname);
185 g_assert_cmpstr(option_pathname, ==, "/usr/share/iso-codes/json");
186
187 filename = options_get_filename();
188 g_assert_nonnull(filename);
189 g_assert_cmpstr(filename, ==, "/usr/share/iso-codes/json/iso_15924.json");
190 }
191
192 /**
193 * Test default value for name field.
194 */
195 void test_opt_name_default(void)
196 {
197 GError *error = NULL;
198 gboolean result = FALSE;
199
200 gchar **command_line = g_strsplit("isoquery", " ", -1);
201 result = options_parse_command_line(command_line, &error);
202 g_strfreev(command_line);
203
204 g_assert_true(result);
205 g_assert_null(error);
206 g_assert_cmpstr(option_namefield, ==, "name");
207 }
208
209 /**
210 * Test "name" value for name field.
211 */
212 void test_opt_name_name(void)
213 {
214 GError *error = NULL;
215 gboolean result = FALSE;
216
217 gchar **command_line = g_strsplit("isoquery --name", " ", -1);
218 result = options_parse_command_line(command_line, &error);
219 g_strfreev(command_line);
220
221 g_assert_true(result);
222 g_assert_null(error);
223 g_assert_cmpstr(option_namefield, ==, "name");
224 }
225
226 /**
227 * Test "official_name" value for name field.
228 */
229 void test_opt_name_official_name(void)
230 {
231 GError *error = NULL;
232 gboolean result = FALSE;
233
234 gchar **command_line = g_strsplit("isoquery --official_name", " ", -1);
235 result = options_parse_command_line(command_line, &error);
236 g_strfreev(command_line);
237
238 g_assert_true(result);
239 g_assert_null(error);
240 g_assert_cmpstr(option_namefield, ==, "official_name");
241 }
242
243 /**
244 * Test "common_name" value for name field.
245 */
246 void test_opt_name_common_name(void)
247 {
248 GError *error = NULL;
249 gboolean result = FALSE;
250
251 gchar **command_line = g_strsplit("isoquery -c", " ", -1);
252 result = options_parse_command_line(command_line, &error);
253 g_strfreev(command_line);
254
255 g_assert_true(result);
256 g_assert_null(error);
257 g_assert_cmpstr(option_namefield, ==, "common_name");
258 }
259
260 /**
261 * Test null separator.
262 */
263 void test_opt_null_separator(void)
264 {
265 GError *error = NULL;
266 gboolean result = FALSE;
267
268 gchar **command_line = g_strsplit("isoquery --null", " ", -1);
269 result = options_parse_command_line(command_line, &error);
270 g_strfreev(command_line);
271
272 g_assert_true(result);
273 g_assert_null(error);
274 g_assert_true(option_null_separator);
275 }
276
277 /**
278 * Test flag output option.
279 */
280 void test_opt_flag_output(void)
281 {
282 GError *error = NULL;
283 gboolean result = FALSE;
284
285 gchar **command_line = g_strsplit("isoquery --flag", " ", -1);
286 result = options_parse_command_line(command_line, &error);
287 g_strfreev(command_line);
288
289 g_assert_true(result);
290 g_assert_null(error);
291 g_assert_true(option_flag_output);
292 }
293
294 /**
295 * Test invalid option.
296 */
297 void test_opt_invalid_option(void)
298 {
299 GError *error = NULL;
300 gboolean result = FALSE;
301
302 gchar **command_line = g_strsplit("isoquery -t", " ", -1);
303 result = options_parse_command_line(command_line, &error);
304 g_strfreev(command_line);
305
306 g_assert_false(result);
307 g_assert_nonnull(error);
308 g_assert_cmpint(error->code, ==, G_OPTION_ERROR_UNKNOWN_OPTION);
309 }
310
311 /**
312 * Initializing all test functions
313 */
314 int main(int argc, gchar *argv[])
315 {
316 g_test_init(&argc, &argv, NULL);
317 g_test_add_func("/options/standard_default", test_opt_standard_default);
318 g_test_add_func("/options/standard_provided", test_opt_standard_provided);
319 g_test_add_func("/options/standard_deprecated_639", test_opt_standard_deprecated_639);
320 g_test_add_func("/options/standard_deprecated_3166", test_opt_standard_deprecated_3166);
321 g_test_add_func("/options/standard_invalid", test_opt_standard_invalid);
322 g_test_add_func("/options/pathname_default", test_opt_pathname_default);
323 g_test_add_func("/options/pathname_provided", test_opt_pathname_provided);
324 g_test_add_func("/options/pathname_provided_with_dir_separator", test_opt_pathname_provided_with_dir_separator);
325 g_test_add_func("/options/pathname_from_standard", test_opt_pathname_from_standard);
326 g_test_add_func("/options/name_default", test_opt_name_default);
327 g_test_add_func("/options/name_name", test_opt_name_name);
328 g_test_add_func("/options/name_official_name", test_opt_name_official_name);
329 g_test_add_func("/options/name_common_name", test_opt_name_common_name);
330 g_test_add_func("/options/null_separator", test_opt_null_separator);
331 g_test_add_func("/options/flag_output", test_opt_flag_output);
332 g_test_add_func("/options/invalid_option", test_opt_invalid_option);
333 return g_test_run();
334 }