Test suite for argp module
authorSergey Poznyakoff <gray@gnu.org.ua>
Sat, 21 Jan 2006 19:09:06 +0000 (19:09 +0000)
committerSergey Poznyakoff <gray@gnu.org.ua>
Sat, 21 Jan 2006 19:09:06 +0000 (19:09 +0000)
modules/argp-tests [new file with mode: 0644]
tests/test-argp-2.sh [new file with mode: 0755]
tests/test-argp.c [new file with mode: 0644]

diff --git a/modules/argp-tests b/modules/argp-tests
new file mode 100644 (file)
index 0000000..00cd685
--- /dev/null
@@ -0,0 +1,12 @@
+Files:
+tests/test-argp.c
+tests/test-argp-2.sh
+
+Depends-on:
+
+Makefile.am:
+TESTS += test-argp test-argp-2.sh
+noinst_PROGRAMS += test-argp 
+EXTRA_DIST += test-argp-2.sh
+test_argp_SOURCES = test-argp.c
+
diff --git a/tests/test-argp-2.sh b/tests/test-argp-2.sh
new file mode 100755 (executable)
index 0000000..2bd8d95
--- /dev/null
@@ -0,0 +1,92 @@
+#! /bin/sh
+# Test suite for argp.
+# Copyright (C) 2006 Free Software Foundation, Inc.
+# This file is part of the GNUlib Library.
+#
+# 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 2, 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, write to the Free Software Foundation,
+# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+
+TMP=argp.$$
+
+unset ARGP_HELP_FMT
+ERR=0
+
+func_compare() {
+# If argp was compiled without base_name, it will display full program name
+  sed '1{s,: [^ ]*/test-argp,: test-argp,;}' | cmp - $TMP
+}  
+
+####
+# Test --usage output
+cat > $TMP <<EOT
+Usage: test-argp [-tvO?V] [-f FILE] [-o[ARG]] [--test] [--file=FILE]
+            [--input=FILE] [--verbose] [--optional[=ARG]] [--option] [--help]
+            [--usage] [--version] ARGS...
+EOT
+
+./test-argp --usage | func_compare || ERR=1
+
+####
+# Test working usage-indent format
+
+cat > $TMP <<EOT
+Usage: test-argp [-tvO?V] [-f FILE] [-o[ARG]] [--test] [--file=FILE]
+[--input=FILE] [--verbose] [--optional[=ARG]] [--option] [--help] [--usage]
+[--version] ARGS...
+EOT
+
+ARGP_HELP_FMT='usage-indent=0' ./test-argp --usage | func_compare || ERR=1
+
+####
+# Test --help output
+cat >$TMP <<EOT
+Usage: test-argp [OPTION...] ARGS...
+documentation string
+
+ Main options
+  -t, --test
+
+ Option Group 1
+  -f, --file=FILE, --input=FILE   Option with a mandatory argument
+  -v, --verbose              Simple option without arguments
+
+ Option Group 2
+  -o, --optional[=ARG]       Option with an optional argument. ARG is one of
+                             the following:
+  -O, --option               An option
+
+  many                       many units
+  one                        one unit
+  two                        two units
+
+  -?, --help                 Give this help list
+      --usage                Give a short usage message
+  -V, --version              Print program version
+
+Mandatory or optional arguments to long options are also mandatory or optional
+for any corresponding short options.
+
+Report bugs to <>.
+EOT
+
+./test-argp --help | func_compare || ERR=1
+
+####
+# Test ambiguous option handling
+
+./test-argp --optio 2>/dev/null && ERR=1
+
+rm $TMP
+
+exit $ERR
diff --git a/tests/test-argp.c b/tests/test-argp.c
new file mode 100644 (file)
index 0000000..667ca9d
--- /dev/null
@@ -0,0 +1,378 @@
+/* Test suite for argp.
+   Copyright (C) 2006 Free Software Foundation, Inc.
+   This file is part of the GNUlib Library.
+
+   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 2, 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, write to the Free Software Foundation,
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#if HAVE_STRING_H
+# include <string.h>
+#endif
+#if HAVE_STRINGS_H
+# include <strings.h>
+#endif
+
+#include "argp.h"
+
+struct test_args
+{
+  int test;
+  int verbose;
+  char *file;
+  char *hidden;
+  int opt;
+  char *optional;
+  int optional_set;
+};
+
+static struct argp_option group1_option[] = 
+{
+  { NULL, 0, NULL, 0, "Option Group 1", 0},
+  { "verbose", 'v', NULL, 0, "Simple option without arguments", 1 },
+  { "file", 'f', "FILE", 0, "Option with a mandatory argument", 1 },
+  { "input", 0, NULL, OPTION_ALIAS, NULL, 1 }, 
+  { "hidden", 'H', "FILE", OPTION_HIDDEN, "Hidden option", 1 },
+  { NULL, 0, NULL, 0, NULL, 0 }
+};
+
+static error_t
+group1_parser (int key, char *arg, struct argp_state *state)
+{
+  struct test_args *args = state->input;
+
+  switch (key)
+    {
+    case 'v':
+      args->verbose++;
+      break;
+      
+    case 'f':
+      args->file = arg;
+      break;
+      
+    case 'H':
+      args->hidden = arg;
+      break;
+      
+    default:
+      return ARGP_ERR_UNKNOWN;
+    }
+  return 0;
+}
+
+struct argp group1_argp = {
+  group1_option,
+  group1_parser
+};
+
+struct argp_child group1_child = {
+  &group1_argp,
+  0,
+  "",
+  1
+};
+
+\f
+static struct argp_option group2_option[] = 
+{
+  { NULL, 0, NULL, 0, "Option Group 2", 0},
+  { "option", 'O', NULL, 0, "An option", 1 },
+  { "optional", 'o', "ARG", OPTION_ARG_OPTIONAL,
+    "Option with an optional argument. ARG is one of the following:", 1 },
+  { "one", 0, NULL, OPTION_DOC|OPTION_NO_TRANS, "one unit", 2 },
+  { "two", 0, NULL, OPTION_DOC|OPTION_NO_TRANS, "two units", 2 },
+  { "many", 0, NULL, OPTION_DOC|OPTION_NO_TRANS, "many units", 2 },
+  { NULL, 0, NULL, 0, NULL, 0 }
+};
+
+static error_t
+group2_parser (int key, char *arg, struct argp_state *state)
+{
+  struct test_args *args = state->input;
+
+  switch (key)
+    {
+    case 'O':
+      args->opt = 1;
+      break;
+      
+    case 'o':
+      args->optional_set = 1;
+      args->optional = arg;
+      break;
+      
+    default:
+      return ARGP_ERR_UNKNOWN;
+    }
+  return 0;
+}
+
+struct argp group2_argp = {
+  group2_option,
+  group2_parser
+};
+
+struct argp_child group2_child = {
+  &group2_argp,
+  0,
+  "",
+  2
+};
+
+static struct argp_option main_options[] =
+  {
+    { NULL, 0, NULL, 0, "Main options", 0},
+    { "test", 't', NULL, 0, NULL, 1 }, 
+    { NULL, 0, NULL, 0, NULL, 0 }
+  };
+
+static error_t
+parse_opt (int key, char *arg, struct argp_state *state)
+{
+  struct test_args *args = state->input;
+  int i;
+  
+  switch (key)
+    {
+    case ARGP_KEY_INIT:
+      for (i = 0; state->root_argp->children[i].argp; i++)
+       state->child_inputs[i] = args;
+      break;
+
+    case 't':
+      args->test = 1;
+      break;
+      
+    default:
+      return ARGP_ERR_UNKNOWN;
+    }
+  return 0;
+}
+
+const char *argp_program_version = "test_argp (" PACKAGE_NAME ") " VERSION;
+const char *argp_program_bug_address = "<" PACKAGE_BUGREPORT ">";
+static char doc[] = "documentation string";
+
+struct argp test_argp = {
+  main_options,
+  parse_opt,
+  "ARGS...",
+  doc,
+  NULL,
+  NULL,
+  NULL
+};
+
+#define NARGS(a) (sizeof(a) / sizeof((a)[0]) - 1)
+#define ARGV0 "test-argp"
+#define init_args(a) memset (&(a), 0, sizeof (a));
+
+#define INIT_TEST_COMMON(n)     \
+ int argc = NARGS (argv);       \
+ struct test_args test_args;    \
+ init_args(test_args);          \
+ test_number = n;
+
+#define INIT_TEST1(n, arg1)            \
+ char *argv[] = { ARGV0, arg1, NULL }; \
+ INIT_TEST_COMMON(n)
+
+#define INIT_TEST2(n, arg1, arg2)            \
+ char *argv[] = { ARGV0, arg1, arg2, NULL }; \
+ INIT_TEST_COMMON(n)
+
+#define INIT_TEST3(n, arg1, arg2, arg3)            \
+ char *argv[] = { ARGV0, arg1, arg2, arg3, NULL }; \
+ INIT_TEST_COMMON(n)
+
+int test_number;
+unsigned failure_count = 0;
+
+void
+fail(const char *msg)
+{
+  fprintf(stderr, "Test %d: %s\n", test_number, msg);
+  failure_count++;
+}
+
+void
+test1(struct argp *argp)
+{
+  INIT_TEST1 (1, "--test");
+  if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
+    fail("argp_parse failed");
+  else if (test_args.test != 1)
+    fail("option not processed");
+}
+
+void
+test2(struct argp *argp)
+{
+  INIT_TEST1 (2, "-t");
+  if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
+    fail("argp_parse failed");
+  else if (test_args.test != 1)
+    fail("option not processed");
+}
+
+void
+test_file(struct argp *argp, int argc, char **argv, struct test_args *args)
+{
+  if (argp_parse (argp, argc, argv, 0, NULL, args))
+    fail("argp_parse failed");
+  else if (!args->file)
+    fail("option not processed");
+  else if (strcmp (args->file, "FILE"))
+    fail("option processed incorrectly");
+}
+
+void
+test3(struct argp *argp)
+{
+  INIT_TEST1 (3, "--file=FILE");
+  test_file (argp, argc, argv, &test_args);
+}
+
+void
+test4(struct argp *argp)
+{
+  INIT_TEST2 (4, "--file", "FILE");
+  test_file (argp, argc, argv, &test_args);
+}
+
+void
+test5(struct argp *argp)
+{
+  INIT_TEST1 (5, "--input=FILE");
+  test_file (argp, argc, argv, &test_args);
+}
+
+void
+test6(struct argp *argp)
+{
+  INIT_TEST2 (6, "--input", "FILE");
+  test_file (argp, argc, argv, &test_args);
+}
+
+void
+test_optional(struct argp *argp, int argc, char **argv,
+             struct test_args *args, char *val, char *a)
+{
+  int index;
+  if (argp_parse (argp, argc, argv, 0, &index, args))
+    fail("argp_parse failed");
+  else if (!args->optional_set)
+    fail("option not processed");
+
+  if (!val)
+    {
+      if (args->optional)
+       fail("option processed incorrectly");
+    }
+  else if (strcmp (args->optional, val))
+    fail("option processed incorrectly");
+
+  if (a)
+    {
+      if (index == argc)
+       fail("expected command line argument not found");
+      else if (strcmp(argv[index], a))
+       fail("expected command line argument does not match");
+    }
+}
+
+void
+test7(struct argp *argp)
+{
+  INIT_TEST1 (7, "-oARG");
+  test_optional(argp, argc, argv, &test_args, "ARG", NULL);
+}
+
+void
+test8(struct argp *argp)
+{
+  INIT_TEST2 (8, "-o", "ARG");
+  test_optional(argp, argc, argv, &test_args, NULL, "ARG");
+}
+
+void
+test9(struct argp *argp)
+{
+  INIT_TEST1 (9, "--optional=ARG");
+  test_optional(argp, argc, argv, &test_args, "ARG", NULL);
+}
+
+void
+test10(struct argp *argp)
+{
+  INIT_TEST2 (10, "--optional", "ARG");
+  test_optional(argp, argc, argv, &test_args, NULL, "ARG");
+}
+  
+void
+test11(struct argp *argp)
+{
+  INIT_TEST1 (11, "--optiona=ARG");
+  test_optional(argp, argc, argv, &test_args, "ARG", NULL);
+}
+
+void
+test12(struct argp *argp)
+{
+  INIT_TEST3 (12, "--option", "--optional=OPT", "FILE");
+  test_optional(argp, argc, argv, &test_args, "OPT", "FILE");
+}  
+
+
+typedef void (*test_fp) (struct argp *argp);
+
+test_fp test_fun[] = {
+  test1,  test2,  test3,  test4,
+  test5,  test6,  test7,  test8,
+  test9,  test10, test11, test12,
+  NULL
+};
+
+int
+main (int argc, char **argv)
+{
+  struct argp_child argp_children[3];
+  test_fp *fun;
+  
+  argp_children[0] = group1_child;
+  argp_children[1] = group2_child;
+  argp_children[2].argp = NULL;
+  test_argp.children = argp_children;
+
+  if (argc > 0)
+    {
+      struct test_args test_args;    
+      init_args(test_args);          
+      return argp_parse (&test_argp, argc, argv, 0, NULL, &test_args);
+    }
+  
+  for (fun = test_fun; *fun; fun++)
+    (*fun) (&test_argp);
+
+  if (failure_count)
+    return 1;
+
+  return 0;
+}