summaryrefslogtreecommitdiff
path: root/tests/test-argmatch.c
blob: 80aba527a7d00a68c9253865ac239ddc397c2f88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* Test of exact or abbreviated match search.
   Copyright (C) 1990, 1998-1999, 2001-2023 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 <https://www.gnu.org/licenses/>.  */

/* Written by Bruno Haible <bruno@clisp.org>, 2007, based on test code
   by David MacKenzie <djm@gnu.ai.mit.edu>.  */

#include <config.h>

/* As of GCC 11.2.1, gcc -Wanalyzer-too-complex reports that main's
   use of CHECK macros expands to code that is too complicated for gcc
   -fanalyzer.  Suppress the resulting bogus warnings.  */
#if 10 <= __GNUC__
# pragma GCC diagnostic ignored "-Wanalyzer-null-argument"
#endif

#include "argmatch.h"

#include <stdlib.h>

#include "macros.h"

# define N_(Msgid) (Msgid)

/* Some packages define ARGMATCH_DIE and ARGMATCH_DIE_DECL in <config.h>, and
   thus must link with a definition of that function.  Provide it here.  */
#ifdef ARGMATCH_DIE_DECL

_Noreturn ARGMATCH_DIE_DECL;
ARGMATCH_DIE_DECL { exit (1); }

#endif

enum backup_type
{
  no_backups,
  simple_backups,
  numbered_existing_backups,
  numbered_backups
};

static const char *const backup_args[] =
{
  "no", "none", "off",
  "simple", "never", "single",
  "existing", "nil", "numbered-existing",
  "numbered", "t", "newstyle",
  NULL
};

static const enum backup_type backup_vals[] =
{
  no_backups, no_backups, no_backups,
  simple_backups, simple_backups, simple_backups,
  numbered_existing_backups, numbered_existing_backups, numbered_existing_backups,
  numbered_backups, numbered_backups, numbered_backups
};

ARGMATCH_DEFINE_GROUP(backup, enum backup_type)

static const argmatch_backup_doc argmatch_backup_docs[] =
{
  { "no",       N_("never make backups (even if --backup is given)") },
  { "numbered", N_("make numbered backups") },
  { "existing", N_("numbered if numbered backups exist, simple otherwise") },
  { "simple",   N_("always make simple backups") },
  { NULL, NULL }
};

static const argmatch_backup_arg argmatch_backup_args[] =
{
  { "no",                no_backups },
  { "none",              no_backups },
  { "off",               no_backups },
  { "simple",            simple_backups },
  { "never",             simple_backups },
  { "single",            simple_backups },
  { "existing",          numbered_existing_backups },
  { "nil",               numbered_existing_backups },
  { "numbered-existing", numbered_existing_backups },
  { "numbered",          numbered_backups },
  { "t",                 numbered_backups },
  { "newstyle",          numbered_backups },
  { NULL, no_backups }
};

const argmatch_backup_group_type argmatch_backup_group =
{
  argmatch_backup_args,
  argmatch_backup_docs,
  N_("\
The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
The version control method may be selected via the --backup option or through\n\
the VERSION_CONTROL environment variable.  Here are the values:\n"),
  NULL
};

int
main (int argc, char *argv[])
{
#define CHECK(Input, Output)                                            \
  do {                                                                  \
    ASSERT (ARGMATCH (Input, backup_args, backup_vals) == Output);      \
    ASSERT (argmatch_backup_choice (Input) == Output);                  \
    if (0 <= Output)                                                    \
      {                                                                 \
        enum backup_type val                                            \
          = argmatch_backup_args[Output < 0 ? 0 : Output].val;          \
        ASSERT (*argmatch_backup_value ("test", Input) == val);         \
        ASSERT (*argmatch_backup_value ("test",                         \
                                        argmatch_backup_argument (&val)) \
                == val);                                                \
      }                                                                 \
  } while (0)

#define CHECK_EXACT(Input, Output)                                      \
  do {                                                                  \
    ASSERT (ARGMATCH_EXACT (Input, backup_args) == Output);             \
  } while (0)

  /* Not found.  */
  CHECK ("klingon", -1);
  CHECK_EXACT ("klingon", -1);

  /* Exact match.  */
  CHECK ("none", 1);
  CHECK_EXACT ("none", 1);
  CHECK ("nil", 7);
  CHECK_EXACT ("nil", 7);

  /* Too long.  */
  CHECK ("nilpotent", -1);
  CHECK_EXACT ("nilpotent", -1);

  /* Abbreviated.  */
  CHECK ("simpl", 3);
  CHECK_EXACT ("simpl", -1);
  CHECK ("simp", 3);
  CHECK_EXACT ("simp", -1);
  CHECK ("sim", 3);
  CHECK_EXACT ("sim", -1);

  /* Exact match and abbreviated.  */
  CHECK ("numbered", 9);
  CHECK_EXACT ("numbered", 9);
  CHECK ("numbere", -2);
  CHECK_EXACT ("numbere", -1);
  CHECK ("number", -2);
  CHECK_EXACT ("number", -1);
  CHECK ("numbe", -2);
  CHECK_EXACT ("numbe", -1);
  CHECK ("numb", -2);
  CHECK_EXACT ("numb", -1);
  CHECK ("num", -2);
  CHECK_EXACT ("num", -1);
  CHECK ("nu", -2);
  CHECK_EXACT ("nu", -1);
  CHECK ("n", -2);
  CHECK_EXACT ("n", -1);

  /* Ambiguous abbreviated.  */
  CHECK ("ne", -2);
  CHECK_EXACT ("ne", -1);

  /* Ambiguous abbreviated, but same value ("single" and "simple").  */
  CHECK ("si", 3);
  CHECK_EXACT ("si", -1);
  CHECK ("s", 3);
  CHECK_EXACT ("s", -1);

#undef CHECK
#undef CHECK_EXACT

  argmatch_backup_usage (stdout);

  return 0;
}