Debian Codegraph @

gnulib-tests/test-argmatch.c

raw symbols SCIP
1 /* Test of exact or abbreviated match search.
2 Copyright (C) 1990, 1998-1999, 2001-2020 Free Software Foundation, Inc.
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16
17 /* Written by Bruno Haible <[email protected]>, 2007, based on test code
18 by David MacKenzie <[email protected]>. */
19
20 #include <config.h>
21
22 #include "argmatch.h"
23
24 #include <stdlib.h>
25
26 #include "macros.h"
27
28 # define N_(Msgid) (Msgid)
29
30 /* Some packages define ARGMATCH_DIE and ARGMATCH_DIE_DECL in <config.h>, and
31 thus must link with a definition of that function. Provide it here. */
32 #ifdef ARGMATCH_DIE_DECL
33
34 _Noreturn ARGMATCH_DIE_DECL;
35 ARGMATCH_DIE_DECL { exit (1); }
36
37 #endif
38
39 enum backup_type
40 {
41 no_backups,
42 simple_backups,
43 numbered_existing_backups,
44 numbered_backups
45 };
46
47 static const char *const backup_args[] =
48 {
49 "no", "none", "off",
50 "simple", "never", "single",
51 "existing", "nil", "numbered-existing",
52 "numbered", "t", "newstyle",
53 NULL
54 };
55
56 static const enum backup_type backup_vals[] =
57 {
58 no_backups, no_backups, no_backups,
59 simple_backups, simple_backups, simple_backups,
60 numbered_existing_backups, numbered_existing_backups, numbered_existing_backups,
61 numbered_backups, numbered_backups, numbered_backups
62 };
63
64 ARGMATCH_DEFINE_GROUP(backup, enum backup_type);
65
66 static const argmatch_backup_doc argmatch_backup_docs[] =
67 {
68 { "no", N_("never make backups (even if --backup is given)") },
69 { "numbered", N_("make numbered backups") },
70 { "existing", N_("numbered if numbered backups exist, simple otherwise") },
71 { "simple", N_("always make simple backups") },
72 { NULL, NULL }
73 };
74
75 static const argmatch_backup_arg argmatch_backup_args[] =
76 {
77 { "no", no_backups },
78 { "none", no_backups },
79 { "off", no_backups },
80 { "simple", simple_backups },
81 { "never", simple_backups },
82 { "single", simple_backups },
83 { "existing", numbered_existing_backups },
84 { "nil", numbered_existing_backups },
85 { "numbered-existing", numbered_existing_backups },
86 { "numbered", numbered_backups },
87 { "t", numbered_backups },
88 { "newstyle", numbered_backups },
89 { NULL, no_backups }
90 };
91
92 const argmatch_backup_group_type argmatch_backup_group =
93 {
94 argmatch_backup_args,
95 argmatch_backup_docs,
96 N_("\
97 The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
98 The version control method may be selected via the --backup option or through\n\
99 the VERSION_CONTROL environment variable. Here are the values:\n"),
100 NULL
101 };
102
103 int
104 main (int argc, char *argv[])
105 {
106 #define CHECK(Input, Output) \
107 do { \
108 ASSERT (ARGMATCH (Input, backup_args, backup_vals) == Output); \
109 ASSERT (argmatch_backup_choice (Input) == Output); \
110 if (0 <= Output) \
111 { \
112 enum backup_type val = argmatch_backup_args[Output].val; \
113 ASSERT (*argmatch_backup_value ("test", Input) == val); \
114 ASSERT (*argmatch_backup_value ("test", \
115 argmatch_backup_argument (&val)) \
116 == val); \
117 } \
118 } while (0)
119
120 /* Not found. */
121 CHECK ("klingon", -1);
122
123 /* Exact match. */
124 CHECK ("none", 1);
125 CHECK ("nil", 7);
126
127 /* Too long. */
128 CHECK ("nilpotent", -1);
129
130 /* Abbreviated. */
131 CHECK ("simpl", 3);
132 CHECK ("simp", 3);
133 CHECK ("sim", 3);
134
135 /* Exact match and abbreviated. */
136 CHECK ("numbered", 9);
137 CHECK ("numbere", -2);
138 CHECK ("number", -2);
139 CHECK ("numbe", -2);
140 CHECK ("numb", -2);
141 CHECK ("num", -2);
142 CHECK ("nu", -2);
143 CHECK ("n", -2);
144
145 /* Ambiguous abbreviated. */
146 CHECK ("ne", -2);
147
148 /* Ambiguous abbreviated, but same value ("single" and "simple"). */
149 CHECK ("si", 3);
150 CHECK ("s", 3);
151 #undef CHECK
152
153 argmatch_backup_usage (stdout);
154
155 return 0;
156 }