[gtksourceview/wip/loader-saver: 33/42] Unit tests for the file loader



commit 1ef02d5ddc9227fccf86e751c7fda753a731004d
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Fri Mar 7 18:44:35 2014 +0100

    Unit tests for the file loader
    
    There is a segfault to fix.

 tests/Makefile.am        |    7 ++
 tests/test-file-loader.c |  245 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 252 insertions(+), 0 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index e4d63bc..e4b13ba 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -100,6 +100,13 @@ test_completion_words_LDADD =                                      \
        $(DEP_LIBS)                                             \
        $(TESTS_LIBS)
 
+UNIT_TEST_PROGS += test-file-loader
+test_file_loader_SOURCES = test-file-loader.c
+test_file_loader_LDADD =                                       \
+       $(top_builddir)/gtksourceview/libgtksourceview-3.0.la   \
+       $(DEP_LIBS)                                             \
+       $(TESTS_LIBS)
+
 UNIT_TEST_PROGS += test-file-saver
 test_file_saver_SOURCES = test-file-saver.c
 test_file_saver_LDADD =                                                \
diff --git a/tests/test-file-loader.c b/tests/test-file-loader.c
new file mode 100644
index 0000000..05a52db
--- /dev/null
+++ b/tests/test-file-loader.c
@@ -0,0 +1,245 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*- */
+/* test-file-loader.c
+ * This file is part of GtkSourceView
+ *
+ * Copyright (C) 2010 - Jesse van den Kieboom
+ * Copyright (C) 2014 - Sébastien Wilmet
+ *
+ * GtkSourceView 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 Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * GtkSourceView 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include <gtksourceview/gtksource.h>
+#include <string.h>
+
+typedef struct
+{
+       const gchar *expected_buffer_contents;
+       gint newline_type;
+} LoaderTestData;
+
+static GtkSourceFile *
+create_file (const gchar *filename,
+            const gchar *contents)
+{
+       GError *error = NULL;
+       GFile *location;
+       GtkSourceBuffer *buffer;
+       GtkSourceFile *file;
+
+       g_file_set_contents (filename, contents, -1, &error);
+       g_assert_no_error (error);
+
+       location = g_file_new_for_path (filename);
+       buffer = gtk_source_buffer_new (NULL);
+
+       file = gtk_source_file_new (location, buffer);
+       g_object_unref (location);
+       g_object_unref (buffer);
+
+       return file;
+}
+
+static void
+delete_file (GtkSourceFile *file)
+{
+       GFile *location = gtk_source_file_get_location (file);
+
+       if (g_file_query_exists (location, NULL))
+       {
+               GError *error = NULL;
+
+               g_file_delete (location, NULL, &error);
+               g_assert_no_error (error);
+       }
+
+       g_object_unref (location);
+}
+
+static void
+load_file_cb (GtkSourceFile  *file,
+             GAsyncResult   *result,
+             LoaderTestData *data)
+{
+       GError *error = NULL;
+
+       gtk_source_file_load_finish (file, result, &error);
+       g_assert_no_error (error);
+
+       if (data->expected_buffer_contents != NULL)
+       {
+               GtkTextBuffer *buffer;
+               GtkTextIter start;
+               GtkTextIter end;
+               gchar *buffer_contents;
+
+               buffer = GTK_TEXT_BUFFER (gtk_source_file_get_buffer (file));
+
+               gtk_text_buffer_get_bounds (buffer, &start, &end);
+               buffer_contents = gtk_text_iter_get_slice (&start, &end);
+
+               g_assert_cmpstr (buffer_contents, ==, data->expected_buffer_contents);
+
+               g_free (buffer_contents);
+       }
+
+       if (data->newline_type != -1)
+       {
+               g_assert_cmpint (gtk_source_file_get_newline_type (file),
+                                ==,
+                                data->newline_type);
+       }
+}
+
+static void
+test_loader (const gchar *filename,
+             const gchar *contents,
+             const gchar *expected_buffer_contents,
+             gint         newline_type)
+{
+       GtkSourceFile *file;
+       LoaderTestData *data;
+
+       file = create_file (filename, contents);
+
+       gtk_source_file_set_encoding (file, gtk_source_encoding_get_utf8 ());
+
+       data = g_slice_new (LoaderTestData);
+       data->expected_buffer_contents = expected_buffer_contents;
+       data->newline_type = newline_type;
+
+       gtk_source_file_load_async (file,
+                                   G_PRIORITY_DEFAULT,
+                                   NULL,
+                                   NULL,
+                                   NULL,
+                                   (GAsyncReadyCallback) load_file_cb,
+                                   data);
+
+       gtk_main ();
+
+       g_slice_free (LoaderTestData, data);
+       delete_file (file);
+       g_object_unref (file);
+}
+
+static void
+test_end_line_stripping (void)
+{
+       test_loader ("file-loader.txt",
+                    "hello world\n",
+                    "hello world",
+                    -1);
+
+       test_loader ("file-loader.txt",
+                    "hello world",
+                    "hello world",
+                    -1);
+
+       test_loader ("file-loader.txt",
+                    "\nhello world",
+                    "\nhello world",
+                    -1);
+
+       test_loader ("file-loader.txt",
+                    "\nhello world\n",
+                    "\nhello world",
+                    -1);
+
+       test_loader ("file-loader.txt",
+                    "hello world\n\n",
+                    "hello world\n",
+                    -1);
+
+       test_loader ("file-loader.txt",
+                    "hello world\r\n",
+                    "hello world",
+                    -1);
+
+       test_loader ("file-loader.txt",
+                    "hello world\r\n\r\n",
+                    "hello world\r\n",
+                    -1);
+
+       test_loader ("file-loader.txt",
+                    "\n",
+                    "",
+                    -1);
+
+       test_loader ("file-loader.txt",
+                    "\r\n",
+                    "",
+                    -1);
+
+       test_loader ("file-loader.txt",
+                    "\n\n",
+                    "\n",
+                    -1);
+
+       test_loader ("file-loader.txt",
+                    "\r\n\r\n",
+                    "\r\n",
+                    -1);
+}
+
+static void
+test_end_new_line_detection (void)
+{
+       test_loader ("file-loader.txt",
+                    "hello world\n",
+                    NULL,
+                    GTK_SOURCE_NEWLINE_TYPE_LF);
+
+       test_loader ("file-loader.txt",
+                    "hello world\r\n",
+                    NULL,
+                    GTK_SOURCE_NEWLINE_TYPE_CR_LF);
+
+       test_loader ("file-loader.txt",
+                    "hello world\r",
+                    NULL,
+                    GTK_SOURCE_NEWLINE_TYPE_CR);
+}
+
+static void
+test_begin_new_line_detection (void)
+{
+       test_loader ("file-loader.txt",
+                    "\nhello world",
+                    NULL,
+                    GTK_SOURCE_NEWLINE_TYPE_LF);
+
+       test_loader ("file-loader.txt",
+                    "\r\nhello world",
+                    NULL,
+                    GTK_SOURCE_NEWLINE_TYPE_CR_LF);
+
+       test_loader ("file-loader.txt",
+                    "\rhello world",
+                    NULL,
+                    GTK_SOURCE_NEWLINE_TYPE_CR);
+}
+
+gint
+main (gint   argc,
+      gchar *argv[])
+{
+       g_test_init (&argc, &argv, NULL);
+
+       g_test_add_func ("/file-loader/end-line-stripping", test_end_line_stripping);
+       g_test_add_func ("/file-loader/end-new-line-detection", test_end_new_line_detection);
+       g_test_add_func ("/file-loader/begin-new-line-detection", test_begin_new_line_detection);
+
+       return g_test_run ();
+}


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