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