diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/.gitignore | 1 | ||||
-rw-r--r-- | tests/Makefile | 3 | ||||
-rw-r--r-- | tests/test-file-storage.c | 132 |
3 files changed, 135 insertions, 1 deletions
diff --git a/tests/.gitignore b/tests/.gitignore index c802245..362dcd3 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -1 +1,2 @@ /test-* +!/test-*.c diff --git a/tests/Makefile b/tests/Makefile index 6ebc85d..c3ce1ca 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -4,7 +4,8 @@ include ../config.mk TEST_PROGS = test-util \ test-shortcut \ - test-handler + test-handler \ + test-file-storage all: $(TEST_PROGS) $(Q)LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):." gtester --verbose $(TEST_PROGS) diff --git a/tests/test-file-storage.c b/tests/test-file-storage.c new file mode 100644 index 0000000..28859b2 --- /dev/null +++ b/tests/test-file-storage.c @@ -0,0 +1,132 @@ +/** + * vimb - a webkit based vim like browser. + * + * Copyright (C) 2012-2019 Daniel Carl + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://www.gnu.org/licenses/. + */ + +#include <src/file-storage.h> +#include <gio/gio.h> +#include <stdio.h> + +static char *pwd; +static char *none_existing_file = "_absent.txt"; +static char *created_file = "_created.txt"; +static char *existing_file = "_existent.txt"; + +static void test_ephemeral_no_file(void) +{ + FileStorage *s; + char **lines; + char *file_path; + + file_path = g_build_filename(pwd, none_existing_file, NULL); + + /* make sure the file does not exist */ + remove(file_path); + s = file_storage_new(pwd, none_existing_file, 0); + g_assert_nonnull(s); + + /* empty file storage */ + lines = file_storage_get_lines(s); + g_assert_cmpint(g_strv_length(lines), ==, 0); + g_strfreev(lines); + + file_storage_append(s, "-%s\n", "foo"); + file_storage_append(s, "-%s\n", "bar"); + + /* File must not be created on appending data to storage */ + g_assert_false(g_file_test(file_path, G_FILE_TEST_IS_REGULAR)); + + lines = file_storage_get_lines(s); + g_assert_cmpint(g_strv_length(lines), ==, 3); + g_assert_cmpstr(lines[0], ==, "-foo"); + g_assert_cmpstr(lines[1], ==, "-bar"); + g_strfreev(lines); + file_storage_free(s); + g_free(file_path); +} + +static void test_file_created(void) +{ + FileStorage *s; + char *file_path; + + file_path = g_build_filename(pwd, created_file, NULL); + remove(file_path); + + g_assert_false(g_file_test(file_path, G_FILE_TEST_IS_REGULAR)); + s = file_storage_new(pwd, created_file, 0640); + g_assert_true(g_file_test(file_path, G_FILE_TEST_IS_REGULAR)); + + file_storage_free(s); + g_free(file_path); +} + +static void test_ephemeral_with_file(void) +{ + FileStorage *s; + char *file_path; + char **lines; + char *content = NULL; + gboolean result; + + file_path = g_build_filename(pwd, existing_file, NULL); + result = g_file_set_contents(file_path, "one\n", -1, NULL); + g_assert_true(result); + + s = file_storage_new(pwd, existing_file, 0); + g_assert_nonnull(s); + + /* empty file storage but file with two lines */ + lines = file_storage_get_lines(s); + g_assert_cmpint(g_strv_length(lines), ==, 2); + g_strfreev(lines); + + file_storage_append(s, "%s\n", "two ephemeral"); + + lines = file_storage_get_lines(s); + g_assert_cmpint(g_strv_length(lines), ==, 3); + g_assert_cmpstr(lines[0], ==, "one"); + g_assert_cmpstr(lines[1], ==, "two ephemeral"); + g_strfreev(lines); + + /* now make sure the file was not changed */ + g_file_get_contents(file_path, &content, NULL, NULL); + g_assert_cmpstr(content, ==, "one\n"); + + file_storage_free(s); + g_free(file_path); +} + +int main(int argc, char *argv[]) +{ + int result; + g_test_init(&argc, &argv, NULL); + + pwd = g_get_current_dir(); + + g_test_add_func("/test-file-storage/ephemeral-no-file", test_ephemeral_no_file); + g_test_add_func("/test-file-storage/file-created", test_file_created); + g_test_add_func("/test-file-storage/ephemeral-with-file", test_ephemeral_with_file); + + result = g_test_run(); + + remove(existing_file); + remove(created_file); + g_free(pwd); + + return result; +} |