[tepl] FileMetadata unit tests: adapt them to the new API



commit 2557e677a1236a0412fc56909a944c710aec2fbc
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Thu Apr 2 13:38:31 2020 +0200

    FileMetadata unit tests: adapt them to the new API
    
    It's simplified because TeplFileMetadata uses only GVfs metadata
    currently. A later commit will add support for TeplMetadataStore to
    TeplFileMetadata, and the unit tests will get again a little more
    complicated.

 testsuite/meson.build          |   2 +-
 testsuite/test-file-metadata.c | 333 ++++++++++++-----------------------------
 2 files changed, 94 insertions(+), 241 deletions(-)
---
diff --git a/testsuite/meson.build b/testsuite/meson.build
index 30012cb..b978ee0 100644
--- a/testsuite/meson.build
+++ b/testsuite/meson.build
@@ -5,7 +5,7 @@ unit_tests = [
   'test-file',
   'test-file-content',
   'test-file-loader',
-  #'test-file-metadata',
+  'test-file-metadata',
   'test-file-saver',
   'test-fold-region',
   'test-info-bar',
diff --git a/testsuite/test-file-metadata.c b/testsuite/test-file-metadata.c
index 6d99170..6414a61 100644
--- a/testsuite/test-file-metadata.c
+++ b/testsuite/test-file-metadata.c
@@ -1,7 +1,7 @@
 /*
  * This file is part of Tepl, a text editor library.
  *
- * Copyright 2016 - Sébastien Wilmet <swilmet gnome org>
+ * Copyright 2016-2020 - Sébastien Wilmet <swilmet gnome org>
  *
  * Tepl is free software; you can redistribute it and/or modify it under
  * the terms of the GNU Lesser General Public License as published by the
@@ -18,63 +18,89 @@
  */
 
 #include <tepl/tepl.h>
-#include <glib/gstdio.h>
 
 #define TEST_KEY "tepl-test-key"
 #define TEST_OTHER_KEY "tepl-test-other-key"
 
-static gchar *
-get_metadata_manager_path (void)
+typedef struct
 {
-       return g_build_filename (g_get_tmp_dir (), "tepl-metadata-manager-store.xml", NULL);
-}
+       GError **error;
+       gboolean ok;
+} Data;
 
 static void
-setup_unit_test (void)
+save_sync_cb (GObject      *source_object,
+             GAsyncResult *result,
+             gpointer      user_data)
 {
-       gchar *path;
+       TeplFileMetadata *metadata = TEPL_FILE_METADATA (source_object);
+       Data *data = user_data;
 
-       path = get_metadata_manager_path ();
-       tepl_metadata_manager_init (path);
-       _tepl_metadata_manager_set_unit_test_mode ();
-       g_free (path);
+       data->ok = tepl_file_metadata_save_finish (metadata, result, data->error);
+
+       gtk_main_quit ();
 }
 
-static void
-teardown_unit_test (void)
+static gboolean
+save_sync (TeplFileMetadata  *metadata,
+          GFile             *location,
+          GError           **error)
 {
-       gchar *path;
+       Data data;
+       data.error = error;
+       data.ok = FALSE;
 
-       tepl_metadata_manager_shutdown ();
+       tepl_file_metadata_save_async (metadata,
+                                      location,
+                                      G_PRIORITY_DEFAULT,
+                                      NULL,
+                                      save_sync_cb,
+                                      &data);
+       gtk_main ();
 
-       path = get_metadata_manager_path ();
-       g_unlink (path);
-       g_free (path);
+       return data.ok;
 }
 
-static TeplFile *
-create_file (gboolean use_gvfs_metadata)
+static void
+load_sync_cb (GObject      *source_object,
+             GAsyncResult *result,
+             gpointer      user_data)
 {
-       TeplFile *file;
-       TeplFileMetadata *metadata;
+       TeplFileMetadata *metadata = TEPL_FILE_METADATA (source_object);
+       Data *data = user_data;
 
-       file = tepl_file_new ();
+       data->ok = tepl_file_metadata_load_finish (metadata, result, data->error);
 
-       metadata = tepl_file_get_file_metadata (file);
-       _tepl_file_metadata_set_use_gvfs_metadata (metadata, use_gvfs_metadata);
+       gtk_main_quit ();
+}
+
+static gboolean
+load_sync (TeplFileMetadata  *metadata,
+          GFile             *location,
+          GError           **error)
+{
+       Data data;
+       data.error = error;
+       data.ok = FALSE;
+
+       tepl_file_metadata_load_async (metadata,
+                                      location,
+                                      G_PRIORITY_DEFAULT,
+                                      NULL,
+                                      load_sync_cb,
+                                      &data);
+       gtk_main ();
 
-       return file;
+       return data.ok;
 }
 
 static void
-do_test_get_set_metadata (gboolean use_gvfs_metadata)
+test_get_set_metadata (void)
 {
-       TeplFile *file;
        TeplFileMetadata *metadata;
        gchar *value;
 
-       file = create_file (use_gvfs_metadata);
-       metadata = tepl_file_get_file_metadata (file);
+       metadata = tepl_file_metadata_new ();
 
        value = tepl_file_metadata_get (metadata, TEST_KEY);
        g_assert_true (value == NULL);
@@ -102,86 +128,47 @@ do_test_get_set_metadata (gboolean use_gvfs_metadata)
        value = tepl_file_metadata_get (metadata, TEST_OTHER_KEY);
        g_assert_true (value == NULL);
 
-       g_object_unref (file);
-}
-
-static void
-test_get_set_metadata (void)
-{
-       setup_unit_test ();
-       do_test_get_set_metadata (TRUE);
-       do_test_get_set_metadata (FALSE);
-       teardown_unit_test ();
+       g_object_unref (metadata);
 }
 
 static void
-do_test_load_save_metadata_sync (gboolean use_gvfs_metadata)
+test_load_save_metadata (void)
 {
-       TeplFile *file;
        TeplFileMetadata *metadata;
-       gchar *value;
        gchar *path;
        GFile *location;
+       gchar *value;
        GError *error = NULL;
        gboolean ok;
 
-       file = create_file (use_gvfs_metadata);
-       metadata = tepl_file_get_file_metadata (file);
-
-       /* NULL location */
-
+       metadata = tepl_file_metadata_new ();
        tepl_file_metadata_set (metadata, TEST_KEY, "epica");
 
-       ok = tepl_file_metadata_load (metadata, NULL, &error);
-       g_assert_no_error (error);
-       g_assert_true (!ok);
-
-       ok = tepl_file_metadata_save (metadata, NULL, &error);
-       g_assert_no_error (error);
-       g_assert_true (!ok);
-
-       value = tepl_file_metadata_get (metadata, TEST_KEY);
-       g_assert_cmpstr (value, ==, "epica");
-       g_free (value);
-
        /* Save metadata */
 
-       path = g_build_filename (g_get_tmp_dir (), "tepl-metadata-test-sync", NULL);
+       path = g_build_filename (g_get_tmp_dir (), "tepl-file-metadata-test", NULL);
        location = g_file_new_for_path (path);
 
-       tepl_file_set_location (file, location);
-
-       ok = tepl_file_metadata_save (metadata, NULL, &error);
-       if (use_gvfs_metadata)
-       {
-               g_assert_true (error != NULL); /* No such file or directory */
-               g_clear_error (&error);
-               g_assert_true (!ok);
-       }
-       else
-       {
-               g_assert_no_error (error);
-               g_assert_true (ok);
-       }
+       ok = save_sync (metadata, location, &error);
+       g_assert_true (error != NULL); /* No such file or directory */
+       g_clear_error (&error);
+       g_assert_true (!ok);
 
        g_file_set_contents (path, "blum", -1, &error);
        g_assert_no_error (error);
 
-       ok = tepl_file_metadata_save (metadata, NULL, &error);
+       ok = save_sync (metadata, location, &error);
        g_assert_no_error (error);
        g_assert_true (ok);
 
-       g_object_unref (file);
+       g_object_unref (metadata);
 
        /* Load metadata */
 
-       file = create_file (use_gvfs_metadata);
-       metadata = tepl_file_get_file_metadata (file);
-       tepl_file_set_location (file, location);
-
+       metadata = tepl_file_metadata_new ();
        tepl_file_metadata_set (metadata, TEST_OTHER_KEY, "embrace");
 
-       ok = tepl_file_metadata_load (metadata, NULL, &error);
+       ok = load_sync (metadata, location, &error);
        g_assert_no_error (error);
        g_assert_true (ok);
 
@@ -195,11 +182,11 @@ do_test_load_save_metadata_sync (gboolean use_gvfs_metadata)
        /* Unset */
 
        tepl_file_metadata_set (metadata, TEST_KEY, NULL);
-       ok = tepl_file_metadata_save (metadata, NULL, &error);
+       ok = save_sync (metadata, location, &error);
        g_assert_no_error (error);
        g_assert_true (ok);
 
-       ok = tepl_file_metadata_load (metadata, NULL, &error);
+       ok = load_sync (metadata, location, &error);
        g_assert_no_error (error);
        g_assert_true (ok);
 
@@ -211,142 +198,22 @@ do_test_load_save_metadata_sync (gboolean use_gvfs_metadata)
        g_file_delete (location, NULL, &error);
        g_assert_no_error (error);
 
-       ok = tepl_file_metadata_load (metadata, NULL, &error);
-       if (use_gvfs_metadata)
-       {
-               g_assert_true (error != NULL); /* No such file or directory */
-               g_clear_error (&error);
-               g_assert_true (!ok);
-       }
-       else
-       {
-               g_assert_no_error (error);
-               g_assert_true (ok);
-       }
+       ok = load_sync (metadata, location, &error);
+       g_assert_true (error != NULL); /* No such file or directory */
+       g_clear_error (&error);
+       g_assert_true (!ok);
 
+       g_object_unref (metadata);
        g_free (path);
        g_object_unref (location);
-       g_object_unref (file);
-}
-
-static void
-test_load_save_metadata_sync (void)
-{
-       setup_unit_test ();
-       do_test_load_save_metadata_sync (TRUE);
-       do_test_load_save_metadata_sync (FALSE);
-       teardown_unit_test ();
-}
-
-static void
-load_metadata_async_cb (GObject      *source_object,
-                       GAsyncResult *result,
-                       gpointer      user_data)
-{
-       TeplFileMetadata *metadata = TEPL_FILE_METADATA (source_object);
-       TeplFile *file;
-       gchar *value;
-       GFile *location;
-       GError *error = NULL;
-       gboolean ok;
-
-       file = tepl_file_metadata_get_file (metadata);
-
-       ok = tepl_file_metadata_load_finish (metadata, result, &error);
-       g_assert_no_error (error);
-       g_assert_true (ok);
-
-       value = tepl_file_metadata_get (metadata, TEST_KEY);
-       g_assert_cmpstr (value, ==, "in flames");
-       g_free (value);
-
-       /* Unset and clean-up */
-
-       tepl_file_metadata_set (metadata, TEST_KEY, NULL);
-       ok = tepl_file_metadata_save (metadata, NULL, &error);
-       g_assert_no_error (error);
-       g_assert_true (ok);
-
-       location = tepl_file_get_location (file);
-       g_file_delete (location, NULL, &error);
-       g_assert_no_error (error);
-
-       g_object_unref (file);
-       gtk_main_quit ();
-}
-
-static void
-save_metadata_async_cb (GObject      *source_object,
-                       GAsyncResult *result,
-                       gpointer      user_data)
-{
-       TeplFileMetadata *metadata = TEPL_FILE_METADATA (source_object);
-       GError *error = NULL;
-       gboolean ok;
-
-       ok = tepl_file_metadata_save_finish (metadata, result, &error);
-       g_assert_no_error (error);
-       g_assert_true (ok);
-
-       tepl_file_metadata_set (metadata, TEST_KEY, NULL);
-
-       tepl_file_metadata_load_async (metadata,
-                                      G_PRIORITY_DEFAULT,
-                                      NULL,
-                                      load_metadata_async_cb,
-                                      NULL);
-}
-
-/* Unit test not as complete as the sync version. */
-static void
-do_test_load_save_metadata_async (gboolean use_gvfs_metadata)
-{
-       TeplFile *file;
-       TeplFileMetadata *metadata;
-       gchar *path;
-       GFile *location;
-       GError *error = NULL;
-
-       file = create_file (use_gvfs_metadata);
-       metadata = tepl_file_get_file_metadata (file);
-
-       path = g_build_filename (g_get_tmp_dir (), "tepl-metadata-test-async", NULL);
-
-       location = g_file_new_for_path (path);
-       tepl_file_set_location (file, location);
-       g_object_unref (location);
-
-       g_file_set_contents (path, "blum", -1, &error);
-       g_assert_no_error (error);
-       g_free (path);
-
-       tepl_file_metadata_set (metadata, TEST_KEY, "in flames");
-
-       tepl_file_metadata_save_async (metadata,
-                                      G_PRIORITY_DEFAULT,
-                                      NULL,
-                                      save_metadata_async_cb,
-                                      NULL);
-
-       gtk_main ();
-}
-
-static void
-test_load_save_metadata_async (void)
-{
-       setup_unit_test ();
-       do_test_load_save_metadata_async (TRUE);
-       do_test_load_save_metadata_async (FALSE);
-       teardown_unit_test ();
 }
 
 /* Setting and saving metadata should not erase previously set metadata if the
  * metadata were not loaded beforehand.
  */
 static void
-do_test_set_without_load (gboolean use_gvfs_metadata)
+test_set_without_load (void)
 {
-       TeplFile *file;
        TeplFileMetadata *metadata;
        gchar *path;
        GFile *location;
@@ -354,34 +221,30 @@ do_test_set_without_load (gboolean use_gvfs_metadata)
        GError *error = NULL;
        gboolean ok;
 
-       file = create_file (use_gvfs_metadata);
-       metadata = tepl_file_get_file_metadata (file);
-       path = g_build_filename (g_get_tmp_dir (), "tepl-metadata-test-set-without-load", NULL);
+       metadata = tepl_file_metadata_new ();
+       path = g_build_filename (g_get_tmp_dir (), "tepl-file-metadata-test-set-without-load", NULL);
        location = g_file_new_for_path (path);
-       tepl_file_set_location (file, location);
 
        g_file_set_contents (path, "blom", -1, &error);
        g_assert_no_error (error);
 
        /* Set and save one metadata */
        tepl_file_metadata_set (metadata, TEST_KEY, "dimmu");
-       ok = tepl_file_metadata_save (metadata, NULL, &error);
+       ok = save_sync (metadata, location, &error);
        g_assert_no_error (error);
        g_assert_true (ok);
 
-       g_object_unref (file);
+       g_object_unref (metadata);
 
        /* Set and save another metadata, independently */
-       file = create_file (use_gvfs_metadata);
-       metadata = tepl_file_get_file_metadata (file);
-       tepl_file_set_location (file, location);
+       metadata = tepl_file_metadata_new ();
        tepl_file_metadata_set (metadata, TEST_OTHER_KEY, "borgir");
-       ok = tepl_file_metadata_save (metadata, NULL, &error);
+       ok = save_sync (metadata, location, &error);
        g_assert_no_error (error);
        g_assert_true (ok);
 
        /* Load */
-       ok = tepl_file_metadata_load (metadata, NULL, &error);
+       ok = load_sync (metadata, location, &error);
        g_assert_no_error (error);
        g_assert_true (ok);
 
@@ -397,37 +260,27 @@ do_test_set_without_load (gboolean use_gvfs_metadata)
        /* Clean-up */
        tepl_file_metadata_set (metadata, TEST_KEY, NULL);
        tepl_file_metadata_set (metadata, TEST_OTHER_KEY, NULL);
-       ok = tepl_file_metadata_save (metadata, NULL, &error);
+       ok = save_sync (metadata, location, &error);
        g_assert_no_error (error);
        g_assert_true (ok);
 
        g_file_delete (location, NULL, &error);
        g_assert_no_error (error);
 
+       g_object_unref (metadata);
        g_free (path);
        g_object_unref (location);
-       g_object_unref (file);
-}
-
-static void
-test_set_without_load (void)
-{
-       setup_unit_test ();
-       do_test_set_without_load (TRUE);
-       do_test_set_without_load (FALSE);
-       teardown_unit_test ();
 }
 
-gint
-main (gint    argc,
-      gchar **argv)
+int
+main (int    argc,
+      char **argv)
 {
        gtk_test_init (&argc, &argv);
 
-       g_test_add_func ("/file/get_set_metadata", test_get_set_metadata);
-       g_test_add_func ("/file/load_save_metadata_sync", test_load_save_metadata_sync);
-       g_test_add_func ("/file/load_save_metadata_async", test_load_save_metadata_async);
-       g_test_add_func ("/file/set_without_load", test_set_without_load);
+       g_test_add_func ("/file_metadata/get_set_metadata", test_get_set_metadata);
+       g_test_add_func ("/file_metadata/load_save_metadata", test_load_save_metadata);
+       g_test_add_func ("/file_metadata/set_without_load", test_set_without_load);
 
        return g_test_run ();
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]