]> git.draconx.ca Git - cdecl99.git/commitdiff
Use common help formatting in test cases.
authorNick Bowler <nbowler@draconx.ca>
Fri, 4 Feb 2022 04:50:38 +0000 (23:50 -0500)
committerNick Bowler <nbowler@draconx.ca>
Fri, 4 Feb 2022 04:50:38 +0000 (23:50 -0500)
There apparently was a copy of the help formatter hidden in the
test suite, let's port that over to the shared code too.

Makefile.am
test/crossparse.c
test/normalize.c
test/randomdecl.c
test/test.h
test/testlib.c

index 3955dd5026cc6e6b74eafde1343cbdedd9b366c0..081c1839a5110f66820a1ca1c8ae985124442472 100644 (file)
@@ -67,21 +67,20 @@ $(libmain_a_OBJECTS): $(gnulib_headers)
 $(libmain_a_OBJECTS): src/options.h
 
 check_PROGRAMS = test/crossparse test/normalize
-check_LTLIBRARIES = libtest.la
-libtest_la_LIBADD = $(GSL_LIBS)
-libtest_la_SOURCES = test/testlib.c
-$(libtest_la_OBJECTS): $(gnulib_headers)
+check_LIBRARIES = libtest.a
+libtest_a_SOURCES = test/testlib.c common/src/help.c
+$(libtest_a_OBJECTS): $(gnulib_headers)
 
 if HAVE_GSL
-libtest_la_SOURCES += test/declgen.c
 check_PROGRAMS += test/randomdecl
+test_randomdecl_SOURCES = test/randomdecl.c test/declgen.c
+test_randomdecl_LDADD = libtest.a libcdecl.la libgnu.la $(GSL_LIBS)
+$(test_randomdecl_OBJECTS): $(gnulib_headers)
 endif
 
-test_crossparse_LDADD = libtest.la libcdecl.la libgnu.la
+test_crossparse_LDADD = libtest.a libcdecl.la libgnu.la
 $(test_crossparse_OBJECTS): $(gnulib_headers)
-test_randomdecl_LDADD = libtest.la libcdecl.la libgnu.la
-$(test_randomdecl_OBJECTS): $(gnulib_headers)
-test_normalize_LDADD = libtest.la src/output.lo src/normalize.lo \
+test_normalize_LDADD = libtest.a src/output.lo src/normalize.lo \
                        libcdecl.la libgnu.la
 $(test_normalize_OBJECTS): $(gnulib_headers)
 
@@ -89,7 +88,7 @@ src/parse.lo: src/scan.h
 src/scan.lo: src/parse.h
 src/parse-decl.lo: src/scan.h src/parse.h src/typemap.h
 src/output.lo: src/specstr.h
-test/declgen.lo: test/typegen.h
+test/declgen.$(OBJEXT): test/typegen.h
 
 # Supporting rules for gettext.
 include $(top_srcdir)/common/snippet/gettext.mk
index 210714a3a5dada4fcfdaf9ca88a95387f45f6935..e19b43852f77e6a28d984376f8e45b9e688cf989 100644 (file)
@@ -49,14 +49,7 @@ static void print_help(void)
 
        print_usage(stdout);
        puts("Test that libcdecl can parse its own output.\n");
-
-       puts("Options:");
-       for (opt = lopts; opt->val; opt++) {
-               int w = print_option_start(opt, NULL);
-
-               if (w)
-                       putchar('\n');
-       }
+       test_print_options(lopts);
 }
 
 enum {
index b535973e26b03d9feac3bbbeac09ee24cd432529..bb5829f88b21ebd654cd96ddd04bd5cee94bc13a 100644 (file)
@@ -47,14 +47,7 @@ static void print_help(void)
 
        print_usage(stdout);
        puts("Test normalization of declaration specifiers.\n");
-
-       puts("Options:");
-       for (opt = lopts; opt->val; opt++) {
-               int w = print_option_start(opt, NULL);
-
-               if (w)
-                       putchar('\n');
-       }
+       test_print_options(lopts);
 }
 
 static unsigned to_spectype(const char *tok)
index a5b5d3cb39e99833c71a5603f5e70d72ca4ee172..c9f4e41793188e7271068590e9451aa0c88e5e1d 100644 (file)
@@ -58,14 +58,7 @@ static void print_help(void)
 
        print_usage(stdout);
        puts("Generate random C declarations for testing.\n");
-
-       puts("Options:");
-       for (opt = lopts; opt->val; opt++) {
-               int w = print_option_start(opt, NULL);
-
-               if (w)
-                       putchar('\n');
-       }
+       test_print_options(lopts);
 }
 
 static struct cdecl *random_decl(struct gen_rng *rng)
index dbf3f82e11170ff7e72fd85c3881b0fdf64a461d..c1be21a445fb4164025411e76c1ba3366af529d2 100644 (file)
 #include <stdbool.h>
 #include <limits.h>
 #include <assert.h>
-#include <getopt.h>
 
 #ifndef _
 #  define _(x) (x)
 #endif
 
-static inline int
-print_option_start(const struct option *opt, const char *argname)
-{
-       int w;
-
-       if (!argname)
-               argname = _("ARG");
-
-       if (opt->val >= CHAR_MIN && opt->val <= CHAR_MAX) {
-               switch (opt->has_arg) {
-               case 0:
-                       w = printf(_("  -%c, --%s"), opt->val, opt->name);
-                       break;
-               case 1:
-                       w = printf(_("  -%c, --%s=%s"),
-                                  opt->val, opt->name, argname);
-                       break;
-               case 2:
-                       w = printf(_("  -%c, --%s[=%s]"),
-                                  opt->val, opt->name, argname);
-                       break;
-               default:
-                       assert(0);
-               }
-       } else {
-               switch (opt->has_arg) {
-               case 0:
-                       w = printf(_("  --%s"), opt->name);
-                       break;
-               case 1:
-                       w = printf(_("  --%s=%s"), opt->name, argname);
-                       break;
-               case 2:
-                       w = printf(_("  --%s[=%s]"), opt->name, argname);
-                       break;
-               default:
-                       assert(0);
-               }
-       }
-
-       if (w > 18) {
-               putchar('\n');
-               w = 0;
-       }
-
-       return w;
-}
-
 struct cdecl_declspec;
+struct option;
 struct cdecl;
 
 void *malloc_nofail(size_t size);
@@ -88,5 +40,6 @@ void test_explain_decl(struct cdecl *decl);
 bool strict_strtoul(unsigned long *val, const char *str, int base);
 
 void test_print_version(const char *program);
+void test_print_options(const struct option *lopts);
 
 #endif
index 7471ece57e11ccf33ecd2408e7be0e25f829815b..93186635639250a1d1e5867708eb5d66db477ea3 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 #include <errno.h>
+#include <getopt.h>
 #include <cdecl.h>
+
+#include "help.h"
 #include "test.h"
 
 static size_t printbuf_size;
@@ -94,3 +97,14 @@ void test_print_version(const char *program)
        puts("This is free software: you are free to change and redistribute it.");
        puts("There is NO WARRANTY, to the extent permitted by law.");
 }
+
+void test_print_options(const struct option *lopts)
+{
+       const struct option *opt;
+
+       puts("Options:");
+       for (opt = lopts; opt->val; opt++) {
+               if (help_print_optstring(opt, "ARG", 20))
+                       putchar('\n');
+       }
+}