[gnome-builder] tests: add test-snippet



commit 86505123c6480c6609953e57ea782dd411e292ba
Author: Christian Hergert <chergert redhat com>
Date:   Tue Nov 8 15:13:40 2016 -0800

    tests: add test-snippet
    
    This is a new test that helps us start testing snippets manually. It's not
    pretty, but we can start tweaking this to make sure that we handle
    movements and changes properly.
    
    The code to wait for stuff to finish is pretty touchy, since we don't have
    signals to keep off in the lower layers of textbuffer/textview.

 tests/Makefile.am    |    6 ++
 tests/test-snippet.c |  226 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 232 insertions(+), 0 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index c47863a..b2c775a 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -162,6 +162,12 @@ test_vim_CFLAGS = $(tests_cflags)
 test_vim_LDADD = $(tests_libs)
 
 
+TESTS += test-snippet
+test_snippet_SOURCES = test-snippet.c
+test_snippet_CFLAGS = $(tests_cflags)
+test_snippet_LDADD = $(tests_libs)
+
+
 misc_programs += test-cpu-graph
 test_cpu_graph_SOURCES = test-cpu-graph.c
 test_cpu_graph_CFLAGS = $(rg_cflags)
diff --git a/tests/test-snippet.c b/tests/test-snippet.c
new file mode 100644
index 0000000..7ea8dd4
--- /dev/null
+++ b/tests/test-snippet.c
@@ -0,0 +1,226 @@
+/* test-snippet.c
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * 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 <ide.h>
+#include <string.h>
+
+#include "application/ide-application-tests.h"
+#include "snippets/ide-source-snippet-private.h"
+#include "util/ide-gdk.h"
+
+static gboolean
+mark_done (gpointer data)
+{
+  gboolean *done = data;
+  *done = TRUE;
+  return G_SOURCE_REMOVE;
+}
+
+static void
+pump_loop (void)
+{
+  gboolean done = FALSE;
+
+  /*
+   * timeout value of 100 was found experimentally.  this is utter crap, but i
+   * don't have a clear event to key off with signals. we are sort of just
+   * waiting for the textview to complete processing events.
+   */
+
+  g_timeout_add (100, mark_done, &done);
+
+  for (;;)
+    {
+      gtk_main_iteration_do (TRUE);
+      if (done)
+        break;
+    }
+}
+
+static void
+emit_and_pump_loop (gpointer     instance,
+                    const gchar *name,
+                    ...)
+{
+  guint signal_id;
+
+  va_list args;
+
+  signal_id = g_signal_lookup (name, G_OBJECT_TYPE (instance));
+  g_assert (signal_id != 0);
+
+  va_start (args, name);
+  g_signal_emit_valist (instance, signal_id, 0, args);
+  va_end (args);
+
+  pump_loop ();
+}
+
+static void
+new_context_cb (GObject      *object,
+                GAsyncResult *result,
+                gpointer      user_data)
+{
+  g_autoptr(GTask) task = user_data;
+  g_autoptr(IdeSourceSnippet) snippet = NULL;
+  g_autoptr(IdeSourceSnippetChunk) chunk1 = NULL;
+  g_autoptr(IdeSourceSnippetChunk) chunk2 = NULL;
+  g_autoptr(IdeSourceSnippetChunk) chunk3 = NULL;
+  g_autoptr(IdeSourceSnippetChunk) chunk4 = NULL;
+  g_autoptr(IdeSourceSnippetChunk) chunk5 = NULL;
+  g_autoptr(IdeBuffer) buffer = NULL;
+  g_autoptr(IdeFile) file = NULL;
+  IdeSourceView *view;
+  IdeContext *context;
+  IdeProject *project;
+  GtkWidget *window;
+  GError *error = NULL;
+
+  context = ide_context_new_finish (result, &error);
+  g_assert_no_error (error);
+  g_assert (context != NULL);
+  g_assert (IDE_IS_CONTEXT (context));
+
+  project = ide_context_get_project (context);
+
+  /*
+   * This test creates a new source view and adds a snippet to it. We
+   * create snippet and chunks manually so we can see how they are modified
+   * based on edits we make to the buffer manually.
+   */
+
+  snippet = ide_source_snippet_new ("foobarbaz", "c");
+
+  chunk1 = ide_source_snippet_chunk_new ();
+  ide_source_snippet_chunk_set_spec (chunk1, "this is\nchunk 1 ");
+  ide_source_snippet_add_chunk (snippet, chunk1);
+
+  chunk2 = ide_source_snippet_chunk_new ();
+  ide_source_snippet_chunk_set_spec (chunk2, "this is tab stop 1");
+  ide_source_snippet_chunk_set_tab_stop (chunk2, 1);
+  ide_source_snippet_add_chunk (snippet, chunk2);
+
+  chunk3 = ide_source_snippet_chunk_new ();
+  ide_source_snippet_chunk_set_spec (chunk3, ",\nthis is chunk 3");
+  ide_source_snippet_add_chunk (snippet, chunk3);
+
+  chunk4 = ide_source_snippet_chunk_new ();
+  ide_source_snippet_chunk_set_spec (chunk4, "$1");
+  ide_source_snippet_add_chunk (snippet, chunk4);
+
+  chunk5 = ide_source_snippet_chunk_new ();
+  ide_source_snippet_chunk_set_spec (chunk5, "this is tab stop 2");
+  ide_source_snippet_chunk_set_tab_stop (chunk5, 2);
+  ide_source_snippet_add_chunk (snippet, chunk5);
+
+  file = ide_project_get_file_for_path (project, "test.txt");
+  buffer = g_object_new (IDE_TYPE_BUFFER,
+                         "context", context,
+                         "file", file,
+                         "highlight-diagnostics", FALSE,
+                         "highlight-syntax", FALSE,
+                         NULL);
+
+  window = gtk_offscreen_window_new ();
+  view = g_object_new (IDE_TYPE_SOURCE_VIEW,
+                       "auto-indent", TRUE,
+                       "buffer", buffer,
+                       "visible", TRUE,
+                       NULL);
+  gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (view));
+  gtk_window_present (GTK_WINDOW (window));
+
+  ide_source_view_push_snippet (view, snippet, NULL);
+
+  pump_loop ();
+
+  g_assert_cmpstr ("this is\nchunk 1 ", ==, ide_source_snippet_chunk_get_text (chunk1));
+  g_assert_cmpstr ("this is tab stop 1", ==, ide_source_snippet_chunk_get_text (chunk2));
+  g_assert_cmpstr (",\nthis is chunk 3", ==, ide_source_snippet_chunk_get_text (chunk3));
+  g_assert_cmpstr ("this is tab stop 1", ==, ide_source_snippet_chunk_get_text (chunk4));
+  g_assert_cmpstr ("this is tab stop 2", ==, ide_source_snippet_chunk_get_text (chunk5));
+
+  /*
+   * Now is where we start getting tricky. We want to move to
+   * to various locations and remove/insert text ensure that
+   * our run-length detecters in the snippets reaction to
+   * insert-text/delete-range are effective.
+   */
+
+  /* overwrite the current snippet text at tab stop 1, our current focus */
+  emit_and_pump_loop (view, "backspace");
+  emit_and_pump_loop (view, "insert-at-cursor", "this is tab stop 1, edit 1");
+
+  g_assert_cmpstr ("this is\nchunk 1 ", ==, ide_source_snippet_chunk_get_text (chunk1));
+  g_assert_cmpstr ("this is tab stop 1, edit 1", ==, ide_source_snippet_chunk_get_text (chunk2));
+  g_assert_cmpstr (",\nthis is chunk 3", ==, ide_source_snippet_chunk_get_text (chunk3));
+  g_assert_cmpstr ("this is tab stop 1, edit 1", ==, ide_source_snippet_chunk_get_text (chunk4));
+  g_assert_cmpstr ("this is tab stop 2", ==, ide_source_snippet_chunk_get_text (chunk5));
+
+  /* Now move to our second tab stop, but exercise forward/backward/forward */
+  g_assert (ide_source_snippet_move_next (snippet));
+  g_assert (ide_source_snippet_move_previous (snippet));
+  g_assert (ide_source_snippet_move_next (snippet));
+  g_assert (ide_source_snippet_move_previous (snippet));
+  g_assert (ide_source_snippet_move_next (snippet));
+
+  /* Now tweak tab stop 2 values, and see what happens */
+  //emit_and_pump_loop (view, "backspace");
+  //emit_and_pump_loop (view, "insert-at-cursor", "this is tab stop 2, edit 1");
+
+  //g_assert_cmpstr ("this is\nchunk 1 ", ==, ide_source_snippet_chunk_get_text (chunk1));
+  //g_assert_cmpstr ("this is tab stop 1, edit 1", ==, ide_source_snippet_chunk_get_text (chunk2));
+  //g_assert_cmpstr (",\nthis is chunk 3", ==, ide_source_snippet_chunk_get_text (chunk3));
+  //g_assert_cmpstr ("this is tab stop 1, edit 1", ==, ide_source_snippet_chunk_get_text (chunk4));
+  //g_assert_cmpstr ("this is tab stop 2, edit 1", ==, ide_source_snippet_chunk_get_text (chunk5));
+
+  g_task_return_boolean (task, TRUE);
+}
+
+static void
+test_snippets_basic (GCancellable        *cancellable,
+                     GAsyncReadyCallback  callback,
+                     gpointer             user_data)
+{
+  g_autoptr(GFile) project_file = NULL;
+  GTask *task;
+
+  task = g_task_new (NULL, cancellable, callback, user_data);
+  project_file = g_file_new_for_path (TEST_DATA_DIR"/project1/configure.ac");
+  ide_context_new_async (project_file, NULL, new_context_cb, task);
+}
+
+gint
+main (gint   argc,
+      gchar *argv[])
+{
+  IdeApplication *app;
+  gint ret;
+
+  g_test_init (&argc, &argv, NULL);
+
+  ide_log_init (TRUE, NULL);
+  ide_log_set_verbosity (4);
+
+  app = ide_application_new ();
+  ide_application_add_test (app, "/Ide/Snippets/basic", test_snippets_basic, NULL);
+  ret = g_application_run (G_APPLICATION (app), argc, argv);
+  g_object_unref (app);
+
+  return ret;
+}


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