1 /* Unit tests for the cli-utils.c file.
3 Copyright (C) 2018 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "cli/cli-utils.h"
28 test_number_or_range_parser ()
30 /* Test parsing a simple integer. */
32 number_or_range_parser
one ("1");
34 SELF_CHECK (!one
.finished ());
35 SELF_CHECK (one
.get_number () == 1);
36 SELF_CHECK (one
.finished ());
37 SELF_CHECK (strcmp (one
.cur_tok (), "") == 0);
40 /* Test parsing an integer followed by a non integer. */
42 number_or_range_parser
one_after ("1 after");
44 SELF_CHECK (!one_after
.finished ());
45 SELF_CHECK (one_after
.get_number () == 1);
46 SELF_CHECK (one_after
.finished ());
47 SELF_CHECK (strcmp (one_after
.cur_tok (), "after") == 0);
50 /* Test parsing a range. */
52 number_or_range_parser
one_three ("1-3");
54 for (int i
= 1; i
< 4; i
++)
56 SELF_CHECK (!one_three
.finished ());
57 SELF_CHECK (one_three
.get_number () == i
);
59 SELF_CHECK (one_three
.finished ());
60 SELF_CHECK (strcmp (one_three
.cur_tok (), "") == 0);
63 /* Test parsing a range followed by a non-integer. */
65 number_or_range_parser
one_three_after ("1-3 after");
67 for (int i
= 1; i
< 4; i
++)
69 SELF_CHECK (!one_three_after
.finished ());
70 SELF_CHECK (one_three_after
.get_number () == i
);
72 SELF_CHECK (one_three_after
.finished ());
73 SELF_CHECK (strcmp (one_three_after
.cur_tok (), "after") == 0);
76 /* Test a negative integer gives an error. */
78 number_or_range_parser
minus_one ("-1");
80 SELF_CHECK (!minus_one
.finished ());
83 minus_one
.get_number ();
86 CATCH (ex
, RETURN_MASK_ERROR
)
88 SELF_CHECK (ex
.reason
== RETURN_ERROR
);
89 SELF_CHECK (ex
.error
== GENERIC_ERROR
);
90 SELF_CHECK (strcmp (ex
.message
, "negative value") == 0);
91 SELF_CHECK (strcmp (minus_one
.cur_tok (), "-1") == 0);
96 /* Test that a - followed by not a number does not give an error. */
98 number_or_range_parser
nan ("-whatever");
100 SELF_CHECK (nan
.finished ());
101 SELF_CHECK (strcmp (nan
.cur_tok (), "-whatever") == 0);
108 const char *flags
= "abc";
109 const char *non_flags_args
= "non flags args";
111 /* Extract twice the same flag, separated by one space. */
113 const char *t1
= "-a -a non flags args";
115 SELF_CHECK (parse_flags (&t1
, flags
) == 1);
116 SELF_CHECK (parse_flags (&t1
, flags
) == 1);
117 SELF_CHECK (strcmp (t1
, non_flags_args
) == 0);
120 /* Extract some flags, separated by one or more spaces. */
122 const char *t2
= "-c -b -c -b -c non flags args";
124 SELF_CHECK (parse_flags (&t2
, flags
) == 3);
125 SELF_CHECK (parse_flags (&t2
, flags
) == 2);
126 SELF_CHECK (parse_flags (&t2
, flags
) == 3);
127 SELF_CHECK (parse_flags (&t2
, flags
) == 2);
128 SELF_CHECK (parse_flags (&t2
, flags
) == 3);
129 SELF_CHECK (strcmp (t2
, non_flags_args
) == 0);
132 /* Check behaviour where there is no flag to extract. */
134 const char *t3
= non_flags_args
;
136 SELF_CHECK (parse_flags (&t3
, flags
) == 0);
137 SELF_CHECK (strcmp (t3
, non_flags_args
) == 0);
140 /* Extract 2 known flags in front of unknown flags. */
142 const char *t4
= "-c -b -x -y -z -c";
144 SELF_CHECK (parse_flags (&t4
, flags
) == 3);
145 SELF_CHECK (parse_flags (&t4
, flags
) == 2);
146 SELF_CHECK (strcmp (t4
, "-x -y -z -c") == 0);
147 SELF_CHECK (parse_flags (&t4
, flags
) == 0);
148 SELF_CHECK (strcmp (t4
, "-x -y -z -c") == 0);
151 /* Check combined flags are not recognised. */
153 const char *t5
= "-c -cb -c";
155 SELF_CHECK (parse_flags (&t5
, flags
) == 3);
156 SELF_CHECK (parse_flags (&t5
, flags
) == 0);
157 SELF_CHECK (strcmp (t5
, "-cb -c") == 0);
162 test_parse_flags_qcs ()
164 const char *non_flags_args
= "non flags args";
166 /* Test parsing of 2 flags out of the known 3. */
168 const char *t1
= "-q -s non flags args";
171 SELF_CHECK (parse_flags_qcs ("test_parse_flags_qcs.t1.q",
174 SELF_CHECK (flags
.quiet
&& !flags
.cont
&& !flags
.silent
);
175 SELF_CHECK (parse_flags_qcs ("test_parse_flags_qcs.t1.s",
178 SELF_CHECK (flags
.quiet
&& !flags
.cont
&& flags
.silent
);
179 SELF_CHECK (strcmp (t1
, non_flags_args
) == 0);
182 /* Test parsing when there is no flag. */
184 const char *t2
= "non flags args";
187 SELF_CHECK (parse_flags_qcs ("test_parse_flags_qcs.t2",
190 SELF_CHECK (!flags
.quiet
&& !flags
.cont
&& !flags
.silent
);
191 SELF_CHECK (strcmp (t2
, non_flags_args
) == 0);
194 /* Test parsing stops at a negative integer. */
196 const char *t3
= "-123 non flags args";
197 const char *orig_t3
= t3
;
200 SELF_CHECK (parse_flags_qcs ("test_parse_flags_qcs.t3",
203 SELF_CHECK (!flags
.quiet
&& !flags
.cont
&& !flags
.silent
);
204 SELF_CHECK (strcmp (t3
, orig_t3
) == 0);
207 /* Test mutual exclusion between -c and -s. */
209 const char *t4
= "-c -s non flags args";
214 SELF_CHECK (parse_flags_qcs ("test_parse_flags_qcs.t4.cs",
218 (void) parse_flags_qcs ("test_parse_flags_qcs.t4.cs",
223 CATCH (ex
, RETURN_MASK_ERROR
)
225 SELF_CHECK (ex
.reason
== RETURN_ERROR
);
226 SELF_CHECK (ex
.error
== GENERIC_ERROR
);
229 "test_parse_flags_qcs.t4.cs: "
230 "-c and -s are mutually exclusive") == 0);
240 selftests::cli_utils::test_number_or_range_parser ();
241 selftests::cli_utils::test_parse_flags ();
242 selftests::cli_utils::test_parse_flags_qcs ();
249 _initialize_cli_utils_selftests ()
251 selftests::register_test ("cli_utils",
252 selftests::cli_utils::test_cli_utils
);
This page took 0.034727 seconds and 4 git commands to generate.