]> Savannah Git Hosting - gnulib.git/commitdiff
libtextstyle-optional: Add tests.
authorBruno Haible <bruno@clisp.org>
Tue, 19 Mar 2019 23:37:52 +0000 (00:37 +0100)
committerBruno Haible <bruno@clisp.org>
Tue, 19 Mar 2019 23:39:50 +0000 (00:39 +0100)
* tests/test-libtextstyle.c: New file, based on libtextstyle's
adhoc-tests/hello.c.
* tests/test-libtextstyle-default.css: New file, copied from
libtextstyle's adhoc-tests/hello-default.css.
* modules/libtextstyle-optional-tests: New file.

ChangeLog
modules/libtextstyle-optional-tests [new file with mode: 0644]
tests/test-libtextstyle-default.css [new file with mode: 0644]
tests/test-libtextstyle.c [new file with mode: 0644]

index af9d7f1eb581e9292cd562ad1bbad61023fc8a69..7f937aae1121bc0020815e2b08e26e95f9384093 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
 2019-03-19  Bruno Haible  <bruno@clisp.org>
 
+       libtextstyle-optional: Add tests.
+       * tests/test-libtextstyle.c: New file, based on libtextstyle's
+       adhoc-tests/hello.c.
+       * tests/test-libtextstyle-default.css: New file, copied from
+       libtextstyle's adhoc-tests/hello-default.css.
+       * modules/libtextstyle-optional-tests: New file.
+
        libtextstyle-optional: New module.
        * lib/textstyle.in.h: New file, based on libtextstyle's textstyle.h.
        * m4/libtextstyle-optional.m4: New file, based on m4/libtextstyle.m4.
diff --git a/modules/libtextstyle-optional-tests b/modules/libtextstyle-optional-tests
new file mode 100644 (file)
index 0000000..2722a18
--- /dev/null
@@ -0,0 +1,14 @@
+Files:
+tests/test-libtextstyle.c
+tests/test-libtextstyle-default.css
+
+Depends-on:
+isatty
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-libtextstyle
+check_PROGRAMS += test-libtextstyle
+test_libtextstyle_CPPFLAGS = $(AM_CPPFLAGS) -DSRCDIR=\"$(srcdir)/\"
+test_libtextstyle_LDADD = $(LDADD) @LIBTEXTSTYLE@
diff --git a/tests/test-libtextstyle-default.css b/tests/test-libtextstyle-default.css
new file mode 100644 (file)
index 0000000..7eba906
--- /dev/null
@@ -0,0 +1,7 @@
+/* This file is in the public domain.
+
+   Styling rules for the color-hello example.  */
+
+.name      { text-decoration : underline; }
+.boy-name  { background-color : rgb(123,201,249); }
+.girl-name { background-color : rgb(250,149,158); }
diff --git a/tests/test-libtextstyle.c b/tests/test-libtextstyle.c
new file mode 100644 (file)
index 0000000..970713f
--- /dev/null
@@ -0,0 +1,101 @@
+/* Ad-hoc testing program for GNU libtextstyle.
+   Copyright (C) 2018-2019 Free Software Foundation, Inc.
+   Written by Bruno Haible <bruno@clisp.org>, 2018.
+
+   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 <https://www.gnu.org/licenses/>.  */
+
+#include <config.h>
+
+#include <textstyle.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+int
+main (int argc, char *argv[])
+{
+  const char *program_name = argv[0];
+  int i;
+
+  /* Parse the command-line arguments.  */
+  for (i = 1; i < argc; i++)
+    {
+      const char *arg = argv[i];
+      if (strncmp (arg, "--color=", 8) == 0)
+        handle_color_option (arg + 8);
+      else if (strncmp (arg, "--style=", 8) == 0)
+        handle_style_option (arg + 8);
+      else if (arg[0] == '-')
+        {
+          fprintf (stderr, "%s: invalid argument: %s\n", program_name, arg);
+          exit (1);
+        }
+      else
+        /* Handle non-option arguments here.  */
+        ;
+    }
+
+  /* Handle the --color=test special argument.  */
+  if (color_test_mode)
+    {
+      print_color_test ();
+      exit (0);
+    }
+
+  #if HAVE_LIBTEXTSTYLE
+  if (color_mode == color_yes
+      || (color_mode == color_tty && isatty (STDOUT_FILENO))
+      || color_mode == color_html)
+    {
+      /* If no style file is explicitly specified, use the default in the
+         source directory.  */
+      if (style_file_name == NULL)
+        style_file_name = SRCDIR "test-libtextstyle-default.css";
+    }
+  else
+    /* No styling.  */
+    style_file_name = NULL;
+  #endif
+
+  /* Create a terminal output stream that uses this style file.  */
+  styled_ostream_t stream =
+    (color_mode == color_html
+     ? html_styled_ostream_create (file_ostream_create (stdout),
+                                   style_file_name)
+     : styled_ostream_create (STDOUT_FILENO, "(stdout)", TTYCTL_AUTO,
+                              style_file_name));
+
+  ostream_write_str (stream, "Hello ");
+
+  /* Associate the entire full name in CSS class 'name'.  */
+  styled_ostream_begin_use_class (stream, "name");
+
+  ostream_write_str (stream, "Dr. ");
+  styled_ostream_begin_use_class (stream, "boy-name");
+  ostream_write_str (stream, "Linus");
+  styled_ostream_end_use_class (stream, "boy-name");
+  ostream_write_str (stream, " Pauling");
+
+  /* Terminate the name.  */
+  styled_ostream_end_use_class (stream, "name");
+
+  ostream_write_str (stream, "!\n");
+
+  /* Flush and close the terminal stream.  */
+  styled_ostream_free (stream);
+
+  return 0;
+}