[gtksourceview/wip/chergert/vim: 357/363] start on some basic input tests




commit cd7c05347a47e49e16815fa9197cfbc25b68a440
Author: Christian Hergert <chergert redhat com>
Date:   Sun Nov 7 12:57:11 2021 -0800

    start on some basic input tests

 testsuite/meson.build      |   1 +
 testsuite/test-vim-input.c | 113 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 114 insertions(+)
---
diff --git a/testsuite/meson.build b/testsuite/meson.build
index bfc8358e..2940639f 100644
--- a/testsuite/meson.build
+++ b/testsuite/meson.build
@@ -37,6 +37,7 @@ testsuite_sources = [
   ['test-syntax'],
   ['test-utils'],
   ['test-view'],
+  ['test-vim-input'],
   ['test-vim-state'],
   ['test-vim-text-object'],
 ]
diff --git a/testsuite/test-vim-input.c b/testsuite/test-vim-input.c
new file mode 100644
index 00000000..92a019fb
--- /dev/null
+++ b/testsuite/test-vim-input.c
@@ -0,0 +1,113 @@
+/*
+ * This file is part of GtkSourceView
+ *
+ * Copyright 2021 Christian Hergert <chergert redhat com>
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ */
+
+#include "config.h"
+
+#include <gtksourceview/gtksource.h>
+#include <gtksourceview/vim/gtk-source-vim.h>
+#include <gtksourceview/vim/gtk-source-vim-command.h>
+#include <gtksourceview/vim/gtk-source-vim-insert.h>
+#include <gtksourceview/vim/gtk-source-vim-normal.h>
+#include <gtksourceview/vim/gtk-source-vim-state.h>
+
+static void
+run_test (const char *text,
+          const char *input,
+          const char *expected)
+{
+       GtkSourceView *view = GTK_SOURCE_VIEW (g_object_ref_sink (gtk_source_view_new ()));
+       GtkSourceBuffer *buffer = GTK_SOURCE_BUFFER (gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)));
+       GtkSourceVim *vim = gtk_source_vim_new (view);
+       GtkTextIter begin, end;
+       char *ret;
+
+       gtk_text_buffer_set_text (GTK_TEXT_BUFFER (buffer), text, -1);
+       gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (buffer), &begin, &end);
+       gtk_text_buffer_select_range (GTK_TEXT_BUFFER (buffer), &begin, &begin);
+
+       for (const char *c = input; *c; c = g_utf8_next_char (c))
+       {
+               GtkSourceVimState *current = gtk_source_vim_state_get_current (GTK_SOURCE_VIM_STATE (vim));
+               gunichar ch = g_utf8_get_char (c);
+               char string[16];
+               GdkModifierType mods = 0;
+               guint keyval;
+
+               /* It would be nice to send GdkEvent, but we have to rely on
+                * the fact that our engine knows key-presses pretty much
+                * everywhere so that we can send keypresses based on chars.
+                */
+               g_unichar_to_utf8 (ch, string);
+
+               if (ch == '\e')
+               {
+                       string[0] = '^';
+                       string[1] = '[';
+                       string[2] = 0;
+                       keyval = GDK_KEY_Escape;
+               }
+               else
+               {
+                       keyval = gdk_unicode_to_keyval (ch);
+               }
+
+               GTK_SOURCE_VIM_STATE_GET_CLASS (current)->handle_keypress (current, keyval, 0, mods, string);
+       }
+
+       gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (buffer), &begin, &end);
+       ret = gtk_text_iter_get_slice (&begin, &end);
+       g_assert_cmpstr (ret, ==, expected);
+       g_free (ret);
+
+       g_object_unref (vim);
+       g_object_unref (view);
+}
+
+static void
+test_motion (void)
+{
+       run_test ("a word here.", "v$x", "");
+       run_test ("a word here.", "vex", " here.");
+       run_test ("line1", "o\e", "line1\n");
+       run_test ("line1", "O\e", "\nline1");
+       run_test ("1\n2\n3", "yGP", "1\n2\n3\n1\n2\n3");
+       run_test ("1\n2\n3", "yGp", "1\n1\n2\n3\n2\n3");
+       run_test ("line1", "dd", "");
+       run_test ("line1\n", "dj", "");
+       run_test ("line1\n\n", "dj", "");
+       run_test ("1\n2\n", "d2j", "");
+       run_test ("1\n2\n", "d10j", "");
+}
+
+int
+main (int argc,
+      char *argv[])
+{
+       int ret;
+
+       gtk_init ();
+       gtk_source_init ();
+       g_test_init (&argc, &argv, NULL);
+       g_test_add_func ("/GtkSourceView/vim-input/motion", test_motion);
+       ret = g_test_run ();
+       gtk_source_finalize ();
+       return ret;
+}


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