[pango] Add tests for pango markup



commit 5d319c5989d599e9e867df76795eb81472b4db7a
Author: Matthias Clasen <mclasen redhat com>
Date:   Thu Aug 28 01:19:29 2014 -0400

    Add tests for pango markup
    
    This test is modeled after the GMarkup parser tests in GLib.
    Test file with a name ending in .markup can be dumped in
    tests/markups/, and tests/markup-parse can be used to generate
    the expected output files which should be in the same location
    and with the extension .markup replaced by .expected.
    
    The test is also used as an installed test.

 tests/Makefile.am              |   18 +++-
 tests/markup-parse.c           |  249 ++++++++++++++++++++++++++++++++++++++++
 tests/markups/fail-1.expected  |    1 +
 tests/markups/fail-1.markup    |    1 +
 tests/markups/valid-1.expected |   12 ++
 tests/markups/valid-1.markup   |    1 +
 tests/markups/valid-2.expected |   12 ++
 tests/markups/valid-2.markup   |    1 +
 tests/markups/valid-3.expected |   18 +++
 tests/markups/valid-3.markup   |   11 ++
 tests/markups/valid-4.expected |   38 ++++++
 tests/markups/valid-4.markup   |    1 +
 12 files changed, 360 insertions(+), 3 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 652c42f..d889183 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -43,7 +43,7 @@ TESTS_ENVIRONMENT =                    \
    G_TEST_BUILDDIR=$(abs_builddir)     \
    PANGO_RC_FILE=./pangorc
 
-check_PROGRAMS = testboundaries testboundaries_ucd testcolor testscript
+check_PROGRAMS = testboundaries testboundaries_ucd testcolor testscript markup-parse
 
 if HAVE_CAIRO
 check_PROGRAMS += testiter test-pangocairo-threads
@@ -65,6 +65,7 @@ testscript_LDADD = $(TEST_PANGO_LIBS) $(GLIB_LIBS)
 test_ot_tags_LDADD = $(TEST_PANGOFT2_LIBS) $(GLIB_LIBS)
 test_pangocairo_threads_LDADD = $(TEST_PANGOCAIRO_LIBS) $(CAIRO_LIBS) $(GLIB_LIBS)
 dump_boundaries_LDADD = $(TEST_PANGO_LIBS) $(GLIB_LIBS)
+markup_parse_LDADD = $(TEST_PANGOCAIRO_LIBS) $(GLIB_LIBS)
 
 if HAVE_CXX
 check_PROGRAMS += cxx-test
@@ -81,15 +82,26 @@ $(noinst_PROGRAMS) $(check_PROGRAMS): pangorc
 
 noinst_PROGRAMS = $(check_PROGRAMS) gen-all-unicode dump-boundaries
 
+markup_tests = \
+       valid-1 valid-2 valid-3 valid-4 \
+       fail-1 \
+       $(NULL)
+
+all_markup_data = \
+       $(addprefix markups/,$(markup_tests:=.markup) $(markup_tests:=.expected))
+
+EXTRA_DIST += $(all_markup_data)
+
 if BUILDOPT_INSTALL_TESTS
 insttestdir=$(libexecdir)/installed-tests/$(PACKAGE)
 insttest_PROGRAMS = $(check_PROGRAMS)
 
 testdatadir = $(insttestdir)
-testdata_DATA =                \
+nobase_testdata_DATA =                 \
        all-unicode.txt         \
        boundaries.utf8         \
-       GraphemeBreakTest.txt
+       GraphemeBreakTest.txt   \
+       $(all_markup_data)
 
 testmetadir = $(datadir)/installed-tests/$(PACKAGE)
 testmeta_DATA = $(check_PROGRAMS:=.test)
diff --git a/tests/markup-parse.c b/tests/markup-parse.c
new file mode 100644
index 0000000..e9cb6a5
--- /dev/null
+++ b/tests/markup-parse.c
@@ -0,0 +1,249 @@
+/* Pango
+ * markup-parse.c: Test Pango markup
+ *
+ * Copyright (C) 2014 Red Hat, Inc
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include <glib.h>
+#include <string.h>
+#include <locale.h>
+
+#include <pango/pangocairo.h>
+
+static void
+print_attr (PangoAttribute *attr, GString *string)
+{
+  g_string_append_printf (string, "[%d %d] ", attr->start_index, attr->end_index);
+  switch (attr->klass->type)
+    {
+    case PANGO_ATTR_LANGUAGE:
+      g_string_append_printf (string,"language %s\n", pango_language_to_string (((PangoAttrLanguage 
*)attr)->value));
+      break;
+    case PANGO_ATTR_FAMILY:
+      g_string_append_printf (string,"family %s\n", ((PangoAttrString *)attr)->value);
+      break;
+    case PANGO_ATTR_STYLE:
+      g_string_append_printf (string,"style %d\n", ((PangoAttrInt *)attr)->value);
+      break;
+    case PANGO_ATTR_WEIGHT:
+      g_string_append_printf (string,"weight %d\n", ((PangoAttrInt *)attr)->value);
+      break;
+    case PANGO_ATTR_VARIANT:
+      g_string_append_printf (string,"variant %d\n", ((PangoAttrInt *)attr)->value);
+      break;
+    case PANGO_ATTR_STRETCH:
+      g_string_append_printf (string,"stretch %d\n", ((PangoAttrInt *)attr)->value);
+      break;
+    case PANGO_ATTR_SIZE:
+      g_string_append_printf (string,"size %d\n", ((PangoAttrSize *)attr)->size);
+      break;
+    case PANGO_ATTR_FONT_DESC:
+      g_string_append_printf (string,"font %s\n", pango_font_description_to_string (((PangoAttrFontDesc 
*)attr)->desc));
+      break;
+    case PANGO_ATTR_FOREGROUND:
+      g_string_append_printf (string,"foreground %s\n", pango_color_to_string (&((PangoAttrColor 
*)attr)->color));
+      break;
+    case PANGO_ATTR_BACKGROUND:
+      g_string_append_printf (string,"background %s\n", pango_color_to_string (&((PangoAttrColor 
*)attr)->color));
+      break;
+    case PANGO_ATTR_UNDERLINE:
+      g_string_append_printf (string,"underline %d\n", ((PangoAttrInt *)attr)->value);
+      break;
+    case PANGO_ATTR_STRIKETHROUGH:
+      g_string_append_printf (string,"strikethrough %d\n", ((PangoAttrInt *)attr)->value);
+      break;
+    case PANGO_ATTR_RISE:
+      g_string_append_printf (string,"rise %d\n", ((PangoAttrInt *)attr)->value);
+      break;
+    case PANGO_ATTR_SHAPE:
+      g_string_append_printf (string,"shape\n");
+      break;
+    case PANGO_ATTR_SCALE:
+      g_string_append_printf (string,"scale %f\n", ((PangoAttrFloat *)attr)->value);
+      break;
+    case PANGO_ATTR_FALLBACK:
+      g_string_append_printf (string,"fallback %d\n", ((PangoAttrInt *)attr)->value);
+      break;
+    case PANGO_ATTR_LETTER_SPACING:
+      g_string_append_printf (string,"letter-spacing %d\n", ((PangoAttrInt *)attr)->value);
+      break;
+    case PANGO_ATTR_UNDERLINE_COLOR:
+      g_string_append_printf (string,"underline-color %s\n", pango_color_to_string (&((PangoAttrColor 
*)attr)->color));
+      break;
+    case PANGO_ATTR_STRIKETHROUGH_COLOR:
+      g_string_append_printf (string,"strikethrough-color %s\n", pango_color_to_string (&((PangoAttrColor 
*)attr)->color));
+      break;
+    case PANGO_ATTR_ABSOLUTE_SIZE:
+      g_string_append_printf (string,"absolute-size %d\n", ((PangoAttrSize *)attr)->size);
+      break;
+    case PANGO_ATTR_GRAVITY:
+      g_string_append_printf (string,"gravity %d\n", ((PangoAttrInt *)attr)->value);
+      break;
+    case PANGO_ATTR_GRAVITY_HINT:
+      g_string_append_printf (string,"gravity-hint %d\n", ((PangoAttrInt *)attr)->value);
+      break;
+    default:
+      g_assert_not_reached ();
+      break;
+    }
+}
+
+static void
+attr_list_dump (PangoAttrList *attrs, GString *string)
+{
+  PangoAttrIterator *iter;
+
+  iter = pango_attr_list_get_iterator (attrs);
+  do {
+    gint start, end;
+    GSList *list, *l;
+
+    pango_attr_iterator_range (iter, &start, &end);
+    g_string_append_printf (string, "range %d %d\n", start, end);
+    list = pango_attr_iterator_get_attrs (iter);
+    for (l = list; l; l = l->next)
+      {
+        PangoAttribute *attr = l->data;
+        print_attr (attr, string);
+      }
+    g_slist_free_full (list, (GDestroyNotify)pango_attribute_destroy);
+  } while (pango_attr_iterator_next (iter));
+
+  pango_attr_iterator_destroy (iter);
+}
+
+static void
+test_file (const gchar *filename, GString *string)
+{
+  gchar *contents;
+  gsize  length;
+  GError *error = NULL;
+  gchar *text;
+  PangoAttrList *attrs;
+  gboolean ret;
+
+  if (!g_file_get_contents (filename, &contents, &length, &error))
+    {
+      fprintf (stderr, "%s\n", error->message);
+      g_error_free (error);
+      return;
+    }
+
+  ret = pango_parse_markup (contents, length, 0, &attrs, &text, NULL, &error);
+  g_free (contents);
+
+  if (ret)
+    {
+      g_assert_no_error (error);
+      g_string_append (string, text);
+      g_string_append (string, "\n\n---\n\n");
+      attr_list_dump (attrs, string);
+      pango_attr_list_unref (attrs);
+      g_free (text);
+    }
+  else
+    {
+      g_string_append_printf (string, "ERROR: %s", error->message);
+      g_error_free (error);
+    }
+}
+
+static gchar *
+get_expected_filename (const gchar *filename)
+{
+  gchar *f, *p, *expected;
+
+  f = g_strdup (filename);
+  p = strstr (f, ".markup");
+  if (p)
+    *p = 0;
+  expected = g_strconcat (f, ".expected", NULL);
+
+  g_free (f);
+
+  return expected;
+}
+
+static void
+test_parse (gconstpointer d)
+{
+  const gchar *filename = d;
+  gchar *expected_file;
+  gchar *expected;
+  GError *error = NULL;
+  GString *string;
+
+  expected_file = get_expected_filename (filename);
+
+  string = g_string_sized_new (0);
+
+  test_file (filename, string);
+
+  g_file_get_contents (expected_file, &expected, NULL, &error);
+  g_assert_no_error (error);
+  g_assert_cmpstr (string->str, ==, expected);
+  g_free (expected);
+
+  g_string_free (string, TRUE);
+
+  g_free (expected_file);
+}
+
+int
+main (int argc, char *argv[])
+{
+  GDir *dir;
+  GError *error = NULL;
+  const gchar *name;
+  gchar *path;
+
+  g_setenv ("LC_ALL", "C", TRUE);
+  setlocale (LC_ALL, "");
+
+  g_test_init (&argc, &argv, NULL);
+
+  /* allow to easily generate expected output for new test cases */
+  if (argc > 1)
+    {
+      GString *string;
+
+      string = g_string_sized_new (0);
+      test_file (argv[1], string);
+      g_print ("%s", string->str);
+
+      return 0;
+    }
+
+  path = g_test_build_filename (G_TEST_DIST, "markups", NULL);
+  dir = g_dir_open (path, 0, &error);
+  g_free (path);
+  g_assert_no_error (error);
+  while ((name = g_dir_read_name (dir)) != NULL)
+    {
+      if (!strstr (name, "markup"))
+        continue;
+
+      path = g_strdup_printf ("/markup/parse/%s", name);
+      g_test_add_data_func_full (path, g_test_build_filename (G_TEST_DIST, "markups", name, NULL),
+                                 test_parse, g_free);
+      g_free (path);
+    }
+  g_dir_close (dir);
+
+  return g_test_run ();
+}
diff --git a/tests/markups/fail-1.expected b/tests/markups/fail-1.expected
new file mode 100644
index 0000000..57d8818
--- /dev/null
+++ b/tests/markups/fail-1.expected
@@ -0,0 +1 @@
+ERROR: Attribute 'foo' is not allowed on the <span> tag on line 1 char 25
\ No newline at end of file
diff --git a/tests/markups/fail-1.markup b/tests/markups/fail-1.markup
new file mode 100644
index 0000000..df0c487
--- /dev/null
+++ b/tests/markups/fail-1.markup
@@ -0,0 +1 @@
+<span foo="bar">blah</span>
diff --git a/tests/markups/valid-1.expected b/tests/markups/valid-1.expected
new file mode 100644
index 0000000..acd5f1b
--- /dev/null
+++ b/tests/markups/valid-1.expected
@@ -0,0 +1,12 @@
+Blue text is cool!
+
+
+---
+
+range 0 9
+[0 9] scale 1.440000
+[0 9] foreground #00000000ffff
+range 9 13
+range 13 17
+[13 17] style 2
+range 17 2147483647
diff --git a/tests/markups/valid-1.markup b/tests/markups/valid-1.markup
new file mode 100644
index 0000000..3c7ea7d
--- /dev/null
+++ b/tests/markups/valid-1.markup
@@ -0,0 +1 @@
+<markup><span foreground="blue" size="x-large">Blue text</span> is <span style="italic">cool</span>!</markup>
diff --git a/tests/markups/valid-2.expected b/tests/markups/valid-2.expected
new file mode 100644
index 0000000..acd5f1b
--- /dev/null
+++ b/tests/markups/valid-2.expected
@@ -0,0 +1,12 @@
+Blue text is cool!
+
+
+---
+
+range 0 9
+[0 9] scale 1.440000
+[0 9] foreground #00000000ffff
+range 9 13
+range 13 17
+[13 17] style 2
+range 17 2147483647
diff --git a/tests/markups/valid-2.markup b/tests/markups/valid-2.markup
new file mode 100644
index 0000000..3e4136f
--- /dev/null
+++ b/tests/markups/valid-2.markup
@@ -0,0 +1 @@
+<span foreground="blue" size="x-large">Blue text</span> is <span style="italic">cool</span>!
diff --git a/tests/markups/valid-3.expected b/tests/markups/valid-3.expected
new file mode 100644
index 0000000..f1030ea
--- /dev/null
+++ b/tests/markups/valid-3.expected
@@ -0,0 +1,18 @@
+Lots of attrs
+
+
+---
+
+range 0 13
+[0 13] font Sans Italic 12
+[0 13] foreground #0000ffff0000
+[0 13] background #f0f0f0f0f0f0
+[0 13] underline 2
+[0 13] underline-color #ffff00000000
+[0 13] gravity 0
+[0 13] gravity-hint 1
+[0 13] strikethrough 1
+[0 13] strikethrough-color #00000000ffff
+[0 13] fallback 0
+[0 13] language de
+range 13 2147483647
diff --git a/tests/markups/valid-3.markup b/tests/markups/valid-3.markup
new file mode 100644
index 0000000..c2f7ab8
--- /dev/null
+++ b/tests/markups/valid-3.markup
@@ -0,0 +1,11 @@
+<span font="Sans Italic 12"
+      foreground="#00ff00"
+      background="#f0f0f0"
+      underline="double"
+      underline_color="red"
+      strikethrough="true"
+      strikethrough_color="blue"
+      fallback="false"
+      lang="de"
+      gravity="south"
+      gravity_hint="strong">Lots of attrs</span>
diff --git a/tests/markups/valid-4.expected b/tests/markups/valid-4.expected
new file mode 100644
index 0000000..eb46086
--- /dev/null
+++ b/tests/markups/valid-4.expected
@@ -0,0 +1,38 @@
+bold big italic strikethroughsub smallsup tt underline
+
+
+---
+
+range 0 5
+[0 15] weight 700
+range 5 8
+[0 15] weight 700
+[5 8] scale 1.200000
+range 8 9
+[0 15] weight 700
+range 9 15
+[0 15] weight 700
+[9 15] style 2
+range 15 16
+range 16 29
+[16 41] strikethrough 1
+range 29 32
+[16 41] strikethrough 1
+[29 32] scale 0.833333
+[29 32] rise -5000
+range 32 33
+[16 41] strikethrough 1
+range 33 38
+[16 41] strikethrough 1
+[33 38] scale 0.833333
+range 38 41
+[16 41] strikethrough 1
+[38 41] scale 0.833333
+[38 41] rise 5000
+range 41 42
+range 42 45
+[42 54] family Monospace
+range 45 54
+[42 54] family Monospace
+[45 54] underline 1
+range 54 2147483647
diff --git a/tests/markups/valid-4.markup b/tests/markups/valid-4.markup
new file mode 100644
index 0000000..d0e58f3
--- /dev/null
+++ b/tests/markups/valid-4.markup
@@ -0,0 +1 @@
+<b>bold <big>big</big> <i>italic</i></b> <s>strikethrough<sub>sub</sub> 
<small>small</small><sup>sup</sup></s> <tt>tt <u>underline</u></tt>


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