t_misc.sh revision 1.12 1 1.1 rillig #! /bin/sh
2 1.12 rillig # $NetBSD: t_misc.sh,v 1.12 2021/10/30 09:32:46 rillig Exp $
3 1.1 rillig #
4 1.1 rillig # Copyright (c) 2021 The NetBSD Foundation, Inc.
5 1.1 rillig # All rights reserved.
6 1.1 rillig #
7 1.1 rillig # Redistribution and use in source and binary forms, with or without
8 1.1 rillig # modification, are permitted provided that the following conditions
9 1.1 rillig # are met:
10 1.1 rillig # 1. Redistributions of source code must retain the above copyright
11 1.1 rillig # notice, this list of conditions and the following disclaimer.
12 1.1 rillig # 2. Redistributions in binary form must reproduce the above copyright
13 1.1 rillig # notice, this list of conditions and the following disclaimer in the
14 1.1 rillig # documentation and/or other materials provided with the distribution.
15 1.1 rillig #
16 1.1 rillig # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 rillig # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 rillig # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 rillig # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 rillig # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 rillig # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 rillig # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 rillig # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 rillig # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 rillig # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 rillig # POSSIBILITY OF SUCH DAMAGE.
27 1.1 rillig #
28 1.1 rillig # $FreeBSD$
29 1.1 rillig
30 1.1 rillig # Tests for indent that do not follow the input-profile-output scheme that is
31 1.1 rillig # used in t_indent.
32 1.1 rillig
33 1.1 rillig indent=$(atf_config_get usr.bin.indent.test_indent /usr/bin/indent)
34 1.2 rillig nl='
35 1.2 rillig '
36 1.2 rillig
37 1.2 rillig atf_test_case 'in_place'
38 1.2 rillig in_place_body()
39 1.2 rillig {
40 1.2 rillig cat <<-\EOF > code.c
41 1.2 rillig int decl;
42 1.2 rillig EOF
43 1.2 rillig cat <<-\EOF > code.c.exp
44 1.2 rillig int decl;
45 1.2 rillig EOF
46 1.2 rillig cp code.c code.c.orig
47 1.2 rillig
48 1.2 rillig atf_check \
49 1.2 rillig env SIMPLE_BACKUP_SUFFIX=".bak" "$indent" code.c
50 1.2 rillig atf_check -o 'file:code.c.exp' \
51 1.2 rillig cat code.c
52 1.2 rillig atf_check -o 'file:code.c.orig' \
53 1.2 rillig cat code.c.bak
54 1.2 rillig }
55 1.1 rillig
56 1.1 rillig atf_test_case 'verbose_profile'
57 1.1 rillig verbose_profile_body()
58 1.1 rillig {
59 1.1 rillig cat <<-\EOF > .indent.pro
60 1.1 rillig -/* comment */bacc
61 1.1 rillig -v
62 1.1 rillig -fc1
63 1.1 rillig EOF
64 1.1 rillig cat <<-\EOF > before.c
65 1.1 rillig int decl;
66 1.1 rillig EOF
67 1.1 rillig cat <<-\EOF > after.c.exp
68 1.1 rillig int decl;
69 1.1 rillig EOF
70 1.1 rillig cat <<-\EOF > stdout.exp
71 1.1 rillig profile: -fc1
72 1.1 rillig profile: -bacc
73 1.1 rillig profile: -v
74 1.1 rillig profile: -fc1
75 1.1 rillig There were 1 output lines and 0 comments
76 1.1 rillig (Lines with comments)/(Lines with code): 0.000
77 1.1 rillig EOF
78 1.1 rillig
79 1.1 rillig # The code in args.c function set_profile suggests that options from
80 1.1 rillig # profile files are echoed to stdout during startup. But since the
81 1.1 rillig # command line options are handled after the profile files, a '-v' in
82 1.1 rillig # the command line has no effect. That's why '-bacc' is not listed
83 1.1 rillig # in stdout, but '-fc1' is. The second round of '-bacc', '-v', '-fc1'
84 1.1 rillig # is listed because when running ATF, $HOME equals $PWD.
85 1.1 rillig
86 1.1 rillig atf_check \
87 1.1 rillig -o 'file:stdout.exp' \
88 1.1 rillig "$indent" -v before.c after.c
89 1.1 rillig atf_check \
90 1.1 rillig -o 'file:after.c.exp' \
91 1.1 rillig cat after.c
92 1.1 rillig }
93 1.1 rillig
94 1.3 rillig atf_test_case 'nested_struct_declarations'
95 1.3 rillig nested_struct_declarations_body()
96 1.3 rillig {
97 1.3 rillig # Trigger the warning about nested struct declarations.
98 1.3 rillig
99 1.3 rillig cat <<-\EOF > code.c
100 1.3 rillig struct s01 { struct s02 { struct s03 { struct s04 {
101 1.3 rillig struct s05 { struct s06 { struct s07 { struct s08 {
102 1.3 rillig struct s09 { struct s10 { struct s11 { struct s12 {
103 1.3 rillig struct s13 { struct s14 { struct s15 { struct s16 {
104 1.3 rillig struct s17 { struct s18 { struct s19 { struct s20 {
105 1.3 rillig struct s21 { struct s22 { struct s23 { struct s24 {
106 1.3 rillig };};};};
107 1.3 rillig };};};};
108 1.3 rillig };};};};
109 1.3 rillig };};};};
110 1.3 rillig };};};};
111 1.3 rillig };};};};
112 1.3 rillig EOF
113 1.3 rillig cat <<-\EOF > expected.out
114 1.3 rillig struct s01 {
115 1.3 rillig struct s02 {
116 1.3 rillig struct s03 {
117 1.3 rillig struct s04 {
118 1.3 rillig struct s05 {
119 1.3 rillig struct s06 {
120 1.3 rillig struct s07 {
121 1.3 rillig struct s08 {
122 1.3 rillig struct s09 {
123 1.3 rillig struct s10 {
124 1.3 rillig struct s11 {
125 1.3 rillig struct s12 {
126 1.3 rillig struct s13 {
127 1.3 rillig struct s14 {
128 1.3 rillig struct s15 {
129 1.3 rillig struct s16 {
130 1.3 rillig struct s17 {
131 1.3 rillig struct s18 {
132 1.3 rillig struct s19 {
133 1.3 rillig struct s20 {
134 1.3 rillig struct s21 {
135 1.3 rillig struct s22 {
136 1.3 rillig struct s23 {
137 1.3 rillig struct s24 {
138 1.3 rillig };
139 1.3 rillig };
140 1.3 rillig };
141 1.3 rillig };
142 1.3 rillig };
143 1.3 rillig };
144 1.3 rillig };
145 1.3 rillig };
146 1.3 rillig };
147 1.3 rillig };
148 1.3 rillig };
149 1.3 rillig };
150 1.3 rillig };
151 1.3 rillig };
152 1.3 rillig };
153 1.3 rillig };
154 1.3 rillig };
155 1.3 rillig };
156 1.3 rillig };
157 1.3 rillig };
158 1.3 rillig };
159 1.3 rillig };
160 1.3 rillig };
161 1.3 rillig };
162 1.3 rillig EOF
163 1.3 rillig cat <<-\EOF > expected.err
164 1.8 rillig warning: Standard Input:5: Reached internal limit of 20 struct levels
165 1.8 rillig warning: Standard Input:6: Reached internal limit of 20 struct levels
166 1.8 rillig warning: Standard Input:6: Reached internal limit of 20 struct levels
167 1.8 rillig warning: Standard Input:6: Reached internal limit of 20 struct levels
168 1.8 rillig warning: Standard Input:6: Reached internal limit of 20 struct levels
169 1.3 rillig EOF
170 1.3 rillig
171 1.3 rillig atf_check -o 'file:expected.out' -e 'file:expected.err' \
172 1.3 rillig "$indent" -i1 -nut < 'code.c'
173 1.3 rillig }
174 1.3 rillig
175 1.4 rillig atf_test_case 'option_P_in_profile_file'
176 1.4 rillig option_P_in_profile_file_body()
177 1.4 rillig {
178 1.4 rillig # Mentioning another profile via -P has no effect since only a single
179 1.4 rillig # profile can be specified on the command line, and there is no
180 1.4 rillig # 'include' option.
181 1.4 rillig
182 1.4 rillig # It's syntactically possible to specify a profile file inside another
183 1.4 rillig # profile file. Such a profile file is ignored since only a single
184 1.4 rillig # profile file is ever loaded.
185 1.4 rillig printf '%s\n' '-P/nonexistent' > .indent.pro
186 1.4 rillig
187 1.4 rillig echo 'syntax # error' > code.c
188 1.4 rillig
189 1.4 rillig atf_check -o 'inline:syntax'"$nl"'#error'"$nl" \
190 1.4 rillig "$indent" < code.c
191 1.4 rillig }
192 1.4 rillig
193 1.5 rillig atf_test_case 'opt'
194 1.5 rillig opt_body()
195 1.5 rillig {
196 1.5 rillig # Test parsing of command line options from a profile file.
197 1.5 rillig
198 1.5 rillig cat <<-\EOF > code.c
199 1.5 rillig int global_var;
200 1.5 rillig
201 1.5 rillig int function(int expr) {
202 1.5 rillig switch (expr) { case 1: return 1; default: return 0; }
203 1.5 rillig }
204 1.5 rillig EOF
205 1.5 rillig
206 1.5 rillig cat << \EOF > .indent.pro
207 1.5 rillig /* The latter of the two options wins. */
208 1.5 rillig -di5
209 1.5 rillig -di12
210 1.5 rillig
211 1.5 rillig /*
212 1.5 rillig * It is possible to embed comments in the middle of an option, but nobody
213 1.5 rillig * does that.
214 1.5 rillig */
215 1.5 rillig -/* comment */bacc
216 1.5 rillig -T/* define
217 1.5 rillig a type */custom_type
218 1.5 rillig
219 1.9 rillig /* For int options, trailing garbage would be an error. */
220 1.5 rillig -i3
221 1.5 rillig
222 1.9 rillig /*For float options, trailing garbage would be an error. */
223 1.9 rillig -cli3.5
224 1.5 rillig
225 1.5 rillig -b/*/acc /* The comment is '/' '*' '/', making the option '-bacc'. */
226 1.5 rillig EOF
227 1.5 rillig
228 1.5 rillig sed '/[$]/d' << \EOF > code.exp
229 1.5 rillig /* $ The variable name is indented by 12 characters due to -di12. */
230 1.5 rillig int global_var;
231 1.5 rillig
232 1.5 rillig int
233 1.5 rillig function(int expr)
234 1.5 rillig {
235 1.5 rillig switch (expr) {
236 1.5 rillig /* $ The indentation is 3 + (int)(3.5 * 3), so 3 + 10.5, so 13. */
237 1.5 rillig /* $ See parse.c, function parse, 'case switch_expr'. */
238 1.5 rillig case 1:
239 1.5 rillig /* $ The indentation is 3 + (int)3.5 * 3 + 3, so 3 + 9 + 3, so 15. */
240 1.5 rillig /* $ See parse.c, function parse, 'case switch_expr'. */
241 1.5 rillig return 1;
242 1.5 rillig default:
243 1.5 rillig return 0;
244 1.5 rillig }
245 1.5 rillig }
246 1.5 rillig EOF
247 1.5 rillig
248 1.5 rillig atf_check -o 'file:code.exp' \
249 1.5 rillig "$indent" code.c -st
250 1.5 rillig }
251 1.5 rillig
252 1.5 rillig atf_test_case 'opt_npro'
253 1.5 rillig opt_npro_body()
254 1.5 rillig {
255 1.5 rillig # Mentioning the option -npro in a .pro file has no effect since at
256 1.5 rillig # that point, indent has already decided to load the .pro file, and
257 1.5 rillig # it only decides once.
258 1.5 rillig
259 1.5 rillig echo ' -npro -di8' > .indent.pro
260 1.5 rillig echo 'int var;' > code.c
261 1.5 rillig printf 'int\tvar;\n' > code.exp
262 1.5 rillig
263 1.5 rillig atf_check -o 'file:code.exp' \
264 1.5 rillig "$indent" code.c -st
265 1.5 rillig }
266 1.5 rillig
267 1.5 rillig atf_test_case 'opt_U'
268 1.5 rillig opt_U_body()
269 1.5 rillig {
270 1.5 rillig # From each line of this file, the first word is taken to be a type
271 1.5 rillig # name.
272 1.5 rillig #
273 1.5 rillig # Since neither '/*' nor '' are syntactically valid type names, this
274 1.5 rillig # means that all kinds of comments are effectively ignored. When a
275 1.5 rillig # type name is indented by whitespace, it is ignored as well.
276 1.5 rillig #
277 1.5 rillig # Since only the first word of each line is relevant, any remaining
278 1.5 rillig # words can be used for comments.
279 1.5 rillig cat <<-\EOF > code.types
280 1.5 rillig /* Comments are effectively ignored since they never match. */
281 1.5 rillig # This comment is ignored as well.
282 1.5 rillig ; So is this comment.
283 1.5 rillig # The following line is empty and adds a type whose name is empty.
284 1.5 rillig
285 1.5 rillig size_t from stddef.h
286 1.5 rillig off_t for file offsets
287 1.5 rillig ignored_t is ignored since it is indented
288 1.5 rillig EOF
289 1.5 rillig
290 1.5 rillig cat <<-\EOF > code.c
291 1.5 rillig int known_1 = (size_t) * arg;
292 1.5 rillig int known_2 = (off_t) * arg;
293 1.5 rillig int ignored = (ignored_t) * arg;
294 1.5 rillig EOF
295 1.5 rillig cat <<-\EOF > code.exp
296 1.5 rillig int known_1 = (size_t)*arg;
297 1.5 rillig int known_2 = (off_t)*arg;
298 1.5 rillig int ignored = (ignored_t) * arg;
299 1.5 rillig EOF
300 1.5 rillig
301 1.5 rillig atf_check -o 'file:code.exp' \
302 1.5 rillig "$indent" -Ucode.types code.c -di0 -st
303 1.5 rillig }
304 1.5 rillig
305 1.6 rillig atf_test_case 'line_no_counting'
306 1.6 rillig line_no_counting_body()
307 1.6 rillig {
308 1.7 rillig # Before NetBSD indent.c 1.147 from 2021-10-24, indent reported the
309 1.7 rillig # warning in line 2 instead of the correct line 3.
310 1.7 rillig
311 1.6 rillig cat <<-\EOF > code.c
312 1.6 rillig void line_no_counting(void)
313 1.6 rillig {
314 1.6 rillig ())
315 1.6 rillig }
316 1.6 rillig EOF
317 1.6 rillig
318 1.6 rillig cat <<-\EOF > code.err
319 1.10 rillig warning: code.c:3: Extra ')'
320 1.6 rillig EOF
321 1.6 rillig
322 1.6 rillig atf_check -o 'ignore' -e 'file:code.err' \
323 1.6 rillig "$indent" code.c -st
324 1.6 rillig }
325 1.6 rillig
326 1.11 rillig atf_test_case 'default_backup_extension'
327 1.11 rillig default_backup_extension_body()
328 1.11 rillig {
329 1.11 rillig echo 'int var;' > code.c
330 1.11 rillig echo 'int var;' > code.c.orig
331 1.11 rillig
332 1.11 rillig atf_check \
333 1.11 rillig "$indent" code.c
334 1.11 rillig atf_check -o 'file:code.c.orig' \
335 1.11 rillig cat code.c.BAK
336 1.11 rillig }
337 1.11 rillig
338 1.12 rillig atf_test_case 'several_profiles'
339 1.12 rillig several_profiles_body()
340 1.12 rillig {
341 1.12 rillig # If the option '-P' occurs several times, only the last of the
342 1.12 rillig # profiles is loaded, the others are ignored.
343 1.12 rillig
344 1.12 rillig echo ' --invalid-option' > error.pro
345 1.12 rillig echo '' > last.pro
346 1.12 rillig echo '' > code.c
347 1.12 rillig
348 1.12 rillig atf_check \
349 1.12 rillig "$indent" -Pnonexistent.pro -Perror.pro -Plast.pro code.c -st
350 1.12 rillig }
351 1.12 rillig
352 1.1 rillig atf_init_test_cases()
353 1.1 rillig {
354 1.2 rillig atf_add_test_case 'in_place'
355 1.1 rillig atf_add_test_case 'verbose_profile'
356 1.3 rillig atf_add_test_case 'nested_struct_declarations'
357 1.4 rillig atf_add_test_case 'option_P_in_profile_file'
358 1.5 rillig atf_add_test_case 'opt'
359 1.5 rillig atf_add_test_case 'opt_npro'
360 1.5 rillig atf_add_test_case 'opt_U'
361 1.6 rillig atf_add_test_case 'line_no_counting'
362 1.11 rillig atf_add_test_case 'default_backup_extension'
363 1.12 rillig atf_add_test_case 'several_profiles'
364 1.1 rillig }
365