[gtk/im-context-work: 2/2] Add tests for GtkComposeTable




commit 569294070b4e5100a520121fceb838870e50fe34
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon Feb 1 15:48:06 2021 -0500

    Add tests for GtkComposeTable
    
    Add some tests for the code that parses Compose files.
    
    This tests the fix in the previous commit.

 testsuite/gtk/compose/basic              |   1 +
 testsuite/gtk/compose/basic.expected     |   3 +
 testsuite/gtk/compose/codepoint          |   1 +
 testsuite/gtk/compose/codepoint.expected |   3 +
 testsuite/gtk/compose/long               |   1 +
 testsuite/gtk/compose/long.expected      |   3 +
 testsuite/gtk/compose/multi              |   3 +
 testsuite/gtk/compose/multi.expected     |   5 ++
 testsuite/gtk/compose/octal              |   1 +
 testsuite/gtk/compose/octal.expected     |   3 +
 testsuite/gtk/composetable.c             | 113 +++++++++++++++++++++++++++++++
 testsuite/gtk/meson.build                |   7 ++
 12 files changed, 144 insertions(+)
---
diff --git a/testsuite/gtk/compose/basic b/testsuite/gtk/compose/basic
new file mode 100644
index 0000000000..536fb013fc
--- /dev/null
+++ b/testsuite/gtk/compose/basic
@@ -0,0 +1 @@
+<Multi_key> <s> <e> <q> : "!"
diff --git a/testsuite/gtk/compose/basic.expected b/testsuite/gtk/compose/basic.expected
new file mode 100644
index 0000000000..ab7a0d2b94
--- /dev/null
+++ b/testsuite/gtk/compose/basic.expected
@@ -0,0 +1,3 @@
+# n_seqs: 1
+# max_seq_len: 4
+<Uff20> <U73> <U65> <U71> : "!" # U21
diff --git a/testsuite/gtk/compose/codepoint b/testsuite/gtk/compose/codepoint
new file mode 100644
index 0000000000..22f44be8f9
--- /dev/null
+++ b/testsuite/gtk/compose/codepoint
@@ -0,0 +1 @@
+<Multi_key> <U73> <U6F> <U7a> : "!"
diff --git a/testsuite/gtk/compose/codepoint.expected b/testsuite/gtk/compose/codepoint.expected
new file mode 100644
index 0000000000..d2c09f6c3f
--- /dev/null
+++ b/testsuite/gtk/compose/codepoint.expected
@@ -0,0 +1,3 @@
+# n_seqs: 1
+# max_seq_len: 4
+<Uff20> <U73> <U6f> <U7a> : "!" # U21
diff --git a/testsuite/gtk/compose/long b/testsuite/gtk/compose/long
new file mode 100644
index 0000000000..84ef7acee8
--- /dev/null
+++ b/testsuite/gtk/compose/long
@@ -0,0 +1 @@
+<Multi_key> <e> <m> <m> <e> <n> <t> <a> <l> <e> <r> : "🧀"
diff --git a/testsuite/gtk/compose/long.expected b/testsuite/gtk/compose/long.expected
new file mode 100644
index 0000000000..17de9b5575
--- /dev/null
+++ b/testsuite/gtk/compose/long.expected
@@ -0,0 +1,3 @@
+# n_seqs: 1
+# max_seq_len: 11
+<Uff20> <U65> <U6d> <U6d> <U65> <U6e> <U74> <U61> <U6c> <U65> <U72> : "🧀" # U1f9c0
diff --git a/testsuite/gtk/compose/multi b/testsuite/gtk/compose/multi
new file mode 100644
index 0000000000..ee8416ea1b
--- /dev/null
+++ b/testsuite/gtk/compose/multi
@@ -0,0 +1,3 @@
+<Multi_key> <s> <e> <q> : "!"
+<Multi_key> <u> <b> <2> <3> : "/"
+<Multi_key> <s> <a> <s> : "_"
diff --git a/testsuite/gtk/compose/multi.expected b/testsuite/gtk/compose/multi.expected
new file mode 100644
index 0000000000..21b97a0fe5
--- /dev/null
+++ b/testsuite/gtk/compose/multi.expected
@@ -0,0 +1,5 @@
+# n_seqs: 3
+# max_seq_len: 5
+<Uff20> <U73> <U61> <U73> <U0> : "_" # U5f
+<Uff20> <U73> <U65> <U71> <U0> : "!" # U21
+<Uff20> <U75> <U62> <U32> <U33> : "/" # U2f
diff --git a/testsuite/gtk/compose/octal b/testsuite/gtk/compose/octal
new file mode 100644
index 0000000000..350ecf8951
--- /dev/null
+++ b/testsuite/gtk/compose/octal
@@ -0,0 +1 @@
+<Multi_key> <s> <e> <q> : "\041"
diff --git a/testsuite/gtk/compose/octal.expected b/testsuite/gtk/compose/octal.expected
new file mode 100644
index 0000000000..ab7a0d2b94
--- /dev/null
+++ b/testsuite/gtk/compose/octal.expected
@@ -0,0 +1,3 @@
+# n_seqs: 1
+# max_seq_len: 4
+<Uff20> <U73> <U65> <U71> : "!" # U21
diff --git a/testsuite/gtk/composetable.c b/testsuite/gtk/composetable.c
new file mode 100644
index 0000000000..2148163349
--- /dev/null
+++ b/testsuite/gtk/composetable.c
@@ -0,0 +1,113 @@
+#include <gtk/gtk.h>
+#include <locale.h>
+
+#include "../gtk/gtkcomposetable.h"
+#include "testsuite/testutils.h"
+
+static char *
+gtk_compose_table_print (GtkComposeTable *table)
+{
+  int i, j;
+  guint16 *seq;
+  GString *str;
+
+  str = g_string_new ("");
+
+  g_string_append_printf (str, "# n_seqs: %d\n# max_seq_len: %d\n",
+                          table->n_seqs,
+                          table->max_seq_len);
+
+  for (i = 0, seq = table->data; i < table->n_seqs; i++, seq += table->max_seq_len + 2)
+    {
+      gunichar value;
+      char buf[7] = { 0 };
+
+      for (j = 0; j < table->max_seq_len; j++)
+        g_string_append_printf (str, "<U%x> ", seq[j]);
+
+      value = 0x10000 * seq[table->max_seq_len] + seq[table->max_seq_len + 1];
+      g_unichar_to_utf8 (value, buf);
+
+      g_string_append_printf (str, ": \"%s\" # U%x\n", buf, value);
+    }
+
+  return g_string_free (str, FALSE);
+}
+
+static void
+generate_output (const char *file)
+{
+  GSList *tables = NULL;
+  GtkComposeTable *table;
+  char *output;
+
+  tables = gtk_compose_table_list_add_file (tables, file);
+  table = tables->data;
+  output = gtk_compose_table_print (table);
+
+  g_print ("%s", output);
+}
+
+static void
+compose_table_compare (gconstpointer data)
+{
+  const char *basename = data;
+  GSList *tables = NULL;
+  GtkComposeTable *table;
+  char *file;
+  char *expected;
+  char *output;
+  char *diff;
+  GError *error = NULL;
+
+  file = g_build_filename (g_test_get_dir (G_TEST_DIST), "compose", basename, NULL);
+  expected = g_strconcat (file, ".expected", NULL);
+
+  tables = gtk_compose_table_list_add_file (tables, file);
+
+  g_assert_true (g_slist_length (tables) == 1);
+
+  table = tables->data;
+
+  output = gtk_compose_table_print (table);
+  diff = diff_with_file (expected, output, -1, &error);
+  g_assert_no_error (error);
+
+  if (diff && diff[0])
+    {
+      g_print ("Resulting output doesn't match reference:\n%s", diff);
+      g_test_fail ();
+    }
+
+  g_free (output);
+  g_free (file);
+  g_free (expected);
+}
+
+int
+main (int argc, char *argv[])
+{
+  char *dir;
+
+  dir = g_dir_make_tmp ("composetableXXXXXX", NULL);
+  g_setenv ("XDG_CACHE_HOME", dir, TRUE);
+  g_free (dir);
+
+  if (argc == 3 && strcmp (argv[1], "--generate") == 0)
+    {
+      setlocale (LC_ALL, "");
+
+      generate_output (argv[2]);
+      return 0;
+    }
+
+  gtk_test_init (&argc, &argv, NULL);
+
+  g_test_add_data_func ("/compose-table/basic", "basic", compose_table_compare);
+  g_test_add_data_func ("/compose-table/long", "long", compose_table_compare);
+  g_test_add_data_func ("/compose-table/octal", "octal", compose_table_compare);
+  g_test_add_data_func ("/compose-table/codepoint", "codepoint", compose_table_compare);
+  g_test_add_data_func ("/compose-table/multi", "multi", compose_table_compare);
+
+  return g_test_run ();
+}
diff --git a/testsuite/gtk/meson.build b/testsuite/gtk/meson.build
index 1ba0bfd9d8..b0af6dc642 100644
--- a/testsuite/gtk/meson.build
+++ b/testsuite/gtk/meson.build
@@ -103,6 +103,13 @@ tests = [
 # Tests that test private apis and therefore are linked against libgtk-4.a
 internal_tests = [
   { 'name': 'bitmask' },
+  {
+    'name': 'composetable',
+    'sources': [
+      'composetable.c',
+      '../testutils.c'
+    ],
+  },
   { 'name': 'constraint-solver' },
   { 'name': 'rbtree-crash' },
   { 'name': 'propertylookuplistmodel' },


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