tests: Break off --draw-mode from render-test into ascii-test.
authorBen Pfaff <blp@cs.stanford.edu>
Fri, 1 Jan 2021 06:43:57 +0000 (22:43 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Fri, 1 Jan 2021 06:47:41 +0000 (22:47 -0800)
My plan is for render-test to go away in favor of a comprehensive test for
pivot tables, which are the only kinds of tables that actually matter.
The --draw-mode tests are a different kind of beast, so it makes sense to
keep them and put them in a different place.

tests/automake.mk
tests/output/ascii-test.c [new file with mode: 0644]
tests/output/ascii.at
tests/output/render.at

index cb70fd3e8ec2f5ad645fde7b7a04dbb3e461a942..523e5708805bae1f6a7228148e84fbb8197e1e2c 100644 (file)
@@ -243,6 +243,13 @@ tests_output_render_test_LDADD = \
        src/libpspp-core.la \
        $(CAIRO_LIBS)
 
+check_PROGRAMS += tests/output/ascii-test
+tests_output_ascii_test_SOURCES = tests/output/ascii-test.c
+tests_output_ascii_test_LDADD = \
+       src/libpspp.la \
+       src/libpspp-core.la \
+       $(CAIRO_LIBS)
+
 check_PROGRAMS += tests/ui/syntax-gen-test
 tests_ui_syntax_gen_test_SOURCES = tests/ui/syntax-gen-test.c
 tests_ui_syntax_gen_test_LDADD = \
diff --git a/tests/output/ascii-test.c b/tests/output/ascii-test.c
new file mode 100644 (file)
index 0000000..7c5d8a7
--- /dev/null
@@ -0,0 +1,225 @@
+/* PSPP - a program for statistical analysis.
+   Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
+
+   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 <config.h>
+
+#include <errno.h>
+#include <getopt.h>
+#include <limits.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "data/file-handle-def.h"
+#include "libpspp/assertion.h"
+#include "libpspp/compiler.h"
+#include "libpspp/i18n.h"
+#include "libpspp/string-map.h"
+#include "output/ascii.h"
+#include "output/driver.h"
+#include "output/table.h"
+#include "output/table-item.h"
+
+#include "gl/error.h"
+#include "gl/progname.h"
+#include "gl/xalloc.h"
+#include "gl/xvasprintf.h"
+
+/* --emphasis: ASCII driver emphasis option. */
+static bool bold;
+static bool underline;
+
+/* --box: ASCII driver box option. */
+static char *box;
+
+/* ASCII driver, for ASCII driver test mode. */
+static struct output_driver *ascii_driver;
+
+static const char *parse_options (int argc, char **argv);
+static void usage (void) NO_RETURN;
+static void draw (FILE *);
+
+int
+main (int argc, char **argv)
+{
+  set_program_name (argv[0]);
+  i18n_init ();
+  output_engine_push ();
+  const char *input_file_name = parse_options (argc, argv);
+
+  FILE *input = (!strcmp (input_file_name, "-")
+                 ? stdin
+                 : fopen (input_file_name, "r"));
+  if (!input)
+    error (1, errno, "%s: open failed", input_file_name);
+
+  draw (input);
+
+  if (input != stdin)
+    fclose (input);
+
+  output_engine_pop ();
+  fh_done ();
+
+  return 0;
+}
+
+static struct output_driver *
+configure_driver (int width, int min_break)
+{
+  struct string_map options = STRING_MAP_INITIALIZER (options);
+  string_map_insert (&options, "format", "txt");
+  string_map_insert (&options, "output-file", "-");
+  string_map_insert_nocopy (&options, xstrdup ("width"),
+                            xasprintf ("%d", width));
+  if (min_break >= 0)
+    string_map_insert_nocopy (&options, xstrdup ("min-hbreak"),
+                              xasprintf ("%d", min_break));
+  if (bold || underline)
+    string_map_insert (&options, "emphasis", "true");
+  if (box != NULL)
+    string_map_insert (&options, "box", box);
+
+  struct output_driver *driver = output_driver_create (&options);
+  string_map_destroy (&options);
+  if (!driver)
+    exit (EXIT_FAILURE);
+  output_driver_register (driver);
+  return driver;
+}
+
+static const char *
+parse_options (int argc, char **argv)
+{
+  int width = 79;
+  int min_break = -1;
+
+  for (;;)
+    {
+      enum {
+        OPT_WIDTH = UCHAR_MAX + 1,
+        OPT_LENGTH,
+        OPT_MIN_BREAK,
+        OPT_EMPHASIS,
+        OPT_BOX,
+        OPT_HELP
+      };
+      static const struct option options[] =
+        {
+          {"width", required_argument, NULL, OPT_WIDTH},
+          {"length", required_argument, NULL, OPT_LENGTH},
+          {"emphasis", required_argument, NULL, OPT_EMPHASIS},
+          {"box", required_argument, NULL, OPT_BOX},
+          {"help", no_argument, NULL, OPT_HELP},
+          {NULL, 0, NULL, 0},
+        };
+
+      int c = getopt_long (argc, argv, "o:", options, NULL);
+      if (c == -1)
+        break;
+
+      switch (c)
+        {
+        case OPT_WIDTH:
+          width = atoi (optarg);
+          break;
+
+        case OPT_EMPHASIS:
+          if (!strcmp (optarg, "bold"))
+            {
+              bold = true;
+              underline = false;
+            }
+          else if (!strcmp (optarg, "underline"))
+            {
+              bold = false;
+              underline = true;
+            }
+          else if (!strcmp (optarg, "none"))
+            {
+              bold = underline = false;
+            }
+          else
+            error (1, 0, "argument to --emphasis must be \"bold\" or "
+                   "\"underline\" or \"none\"");
+          break;
+
+        case OPT_BOX:
+          box = optarg;
+          break;
+
+        case OPT_HELP:
+          usage ();
+
+        case 0:
+          break;
+
+        case '?':
+          exit(EXIT_FAILURE);
+          break;
+
+        default:
+          NOT_REACHED ();
+        }
+
+    }
+
+  ascii_driver = configure_driver (width, min_break);
+
+  if (optind + 1 != argc)
+    error (1, 0, "exactly one non-option argument required; "
+           "use --help for help");
+  return argv[optind];
+}
+
+static void
+usage (void)
+{
+  printf ("%s, to test PSPP ASCII driver drawing\n"
+          "usage: %s [OPTIONS] INPUT\n"
+          "\nOptions:\n"
+          "  --width=WIDTH   set page width in characters\n"
+          "  --length=LINE   set page length in lines\n",
+          program_name, program_name);
+  exit (EXIT_SUCCESS);
+}
+
+static void
+draw (FILE *stream)
+{
+  char buffer[1024];
+  int line = 0;
+
+  while (fgets (buffer, sizeof buffer, stream))
+    {
+      char text[sizeof buffer];
+      int length;
+      int emph;
+      int x, y;
+
+      line++;
+      if (strchr ("#\r\n", buffer[0]))
+        continue;
+
+      if (sscanf (buffer, "%d %d %d %[^\n]", &x, &y, &emph, text) == 4)
+        ascii_test_write (ascii_driver, text, x, y, emph ? bold : false,
+                          emph ? underline : false);
+      else if (sscanf (buffer, "set-length %d %d", &y, &length) == 2)
+        ascii_test_set_length (ascii_driver, y, length);
+      else
+        error (1, 0, "line %d has invalid format", line);
+    }
+  ascii_test_flush (ascii_driver);
+}
index f966fc6f061950286f0338213f3e0681a13949ea..1ca75106675c500be63423800c99531e8b970457 100644 (file)
@@ -83,7 +83,7 @@ AT_DATA([input], [dnl
 0 18 1 か
 4 18 1 くけ
 ])
-AT_CHECK([render-test --draw-mode --emph=none input], [0], [dnl
+AT_CHECK([ascii-test --emph=none input], [0], [dnl
 aBCD
 eFGH
 iJKL
@@ -104,7 +104,7 @@ kaきくけ
 かkiくけko
 かkiくけko
 ])
-AT_CHECK([render-test --draw-mode --emph=bold input], [0], [dnl
+AT_CHECK([ascii-test --emph=bold input], [0], [dnl
 aBCD
 eF\bFG\bGH\bH
 i\biJKL
@@ -125,7 +125,7 @@ k\bka\baき\bきく\bくけ\b
 かk\bki\biくけk\bko\bo
 か\bかk\bki\biく\bくけ\bけk\bko\bo
 ])
-AT_CHECK([render-test --draw-mode --emph=underline input], [0], [dnl
+AT_CHECK([ascii-test --emph=underline input], [0], [dnl
 aBCD
 e_\bF_\bG_\bH
 _\biJKL
@@ -279,7 +279,7 @@ AT_DATA([input], [dnl
 1 38 1 a
 5 38 1 kuke
 ])
-AT_CHECK([render-test --draw-mode --emph=none input], [0], [dnl
+AT_CHECK([ascii-test --emph=none input], [0], [dnl
 あきくけ
 あきくけ
 あきくけ
@@ -320,7 +320,7 @@ kaいkukeお
 ?aい?kuke?さ
 ?aい?kuke?さ
 ])
-AT_CHECK([render-test --draw-mode --emph=bold input], [0], [dnl
+AT_CHECK([ascii-test --emph=bold input], [0], [dnl
 あきくけ
 あき\bきく\bくけ\b
 あ\bあきくけ
@@ -361,7 +361,7 @@ k\bka\baい\bいk\bku\buk\bke\beお\b
 ?aい\bい?kuke?さ\b
 ?a\baい\bい?k\bku\buk\bke\be?さ\b
 ])
-AT_CHECK([render-test --draw-mode --emph=underline input], [0], [dnl
+AT_CHECK([ascii-test --emph=underline input], [0], [dnl
 あきくけ
 あ_\bき_\bく_\b
 _\bあきくけ
@@ -471,7 +471,7 @@ AT_DATA([input], [dnl
 0 18 1 à
 2 18 1 îo̧ũ
 ])
-AT_CHECK([render-test --draw-mode --emph=none input], [0], [dnl
+AT_CHECK([ascii-test --emph=none input], [0], [dnl
 àxyz
 àxyz
 àxyz
@@ -492,7 +492,7 @@ àeîo̧ũy
 àeîo̧ũy
 àeîo̧ũy
 ])
-AT_CHECK([render-test --draw-mode --emph=bold input], [0], [dnl
+AT_CHECK([ascii-test --emph=bold input], [0], [dnl
 àxyz
 àx\bxy\byz\bz
 a\bàxyz
@@ -513,7 +513,7 @@ a\bàei\bîo\bo̧u\bũy
 àe\beîo̧ũy\by
 a\bàe\bei\bîo\bo̧u\bũy\by
 ])
-AT_CHECK([render-test --draw-mode --emph=underline input], [0], [dnl
+AT_CHECK([ascii-test --emph=underline input], [0], [dnl
 àxyz
 à_\bx_\by_\bz
 _\bàxyz
@@ -563,7 +563,7 @@ set-length 8 2
 set-length 9 1
 set-length 10 0
 ])
-AT_CHECK([render-test --draw-mode input], [0], [dnl
+AT_CHECK([ascii-test input], [0], [dnl
 àéî
 àéî
 àé
@@ -577,26 +577,6 @@ à
 ])
 AT_CLEANUP
 
-AT_SETUP([ASCII driver Unicode box characters])
-AT_KEYWORDS([render rendering])
-AT_DATA([input], [3 3
-1*2 @abc
-2*1 @d\ne\nf
-2*1 @g\nh\ni
-@j
-1*2 @klm
-])
-AT_CHECK([render-test --box=unicode input], [0], [dnl
-╭───┬─╮
-│abc│d│
-├─┬─┤e│
-│g│j│f│
-│h├─┴─┤
-│i│klm│
-╰─┴───╯
-])
-AT_CLEANUP
-
 AT_SETUP([ASCII driver syntax printback])
 AT_DATA([ascii.sps], [dnl
 SET PRINTBACK=ON.
index 8172e33571826748d493f3cf2ea7e9819fbceec2..67e3290b76cb552387c4c612894d8d97be8b285f 100644 (file)
@@ -555,6 +555,27 @@ AT_CHECK([render-test input], [0],[dnl
 +-----+------+----+
 ])
 AT_CLEANUP
+
+
+AT_SETUP([ASCII driver Unicode box characters])
+AT_KEYWORDS([render rendering])
+AT_DATA([input], [3 3
+1*2 @abc
+2*1 @d\ne\nf
+2*1 @g\nh\ni
+@j
+1*2 @klm
+])
+AT_CHECK([render-test --box=unicode input], [0], [dnl
+╭───┬─╮
+│abc│d│
+├─┬─┤e│
+│g│j│f│
+│h├─┴─┤
+│i│klm│
+╰─┴───╯
+])
+AT_CLEANUP
 \f
 AT_BANNER([output rendering -- horizontal page breaks])