t_options.sh revision 1.3 1 #! /bin/sh
2 # $NetBSD: t_options.sh,v 1.3 2021/10/17 17:20:47 rillig Exp $
3 #
4 # Copyright (c) 2021 The NetBSD Foundation, Inc.
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 # POSSIBILITY OF SUCH DAMAGE.
27 #
28 # $FreeBSD$
29
30 # Tests for indent that focus on comparing the effects of the various command
31 # line options.
32 #
33 # The test files contain the input to be formatted, the formatting options
34 # and the output, all as close together as possible. The test files use the
35 # following directives:
36 #
37 # #indent input [description]
38 # Specifies the input to be formatted.
39 # #indent run [options]
40 # Runs indent on the input, using the given options.
41 # #indent end [description]
42 # Finishes an '#indent input' or '#indent run' section.
43 # #indent run-identity [options]
44 # Runs indent on the input, expecting unmodified output.
45 #
46 # All text between these directives is not passed to indent.
47
48 srcdir=$(atf_get_srcdir)
49 indent=$(atf_config_get usr.bin.indent.test_indent /usr/bin/indent)
50
51 # Read the test specification from stdin, output the actual test output on
52 # stdout, write the expected test output to 'expected.out'.
53 #
54 # shellcheck disable=SC2016
55 check_awk='
56 function die(msg)
57 {
58 if (!died) {
59 died = 1
60 print msg > "/dev/stderr"
61 exit(1)
62 }
63 }
64
65 # Skip comments starting with dollar; they are used for marking bugs and
66 # adding other remarks directly in the input or output sections.
67 /^[[:space:]]*\/[*][[:space:]]*[$].*[*]\/$/ ||
68 /^[[:space:]]*\/\/[[:space:]]*[$]/ {
69 next
70 }
71
72 /^#/ && $1 == "#indent" {
73 print $0
74 print $0 > "expected.out"
75
76 if ($2 == "input") {
77 if (unused != 0)
78 die(FILENAME ":" unused ": input is not used")
79 mode = "input"
80 in_lines_len = 0
81 prev_input_all = input_all
82 input_all = ""
83 unused = NR
84
85 } else if ($2 == "run") {
86 mode = "run"
87 cmd = ENVIRON["INDENT"]
88 for (i = 3; i <= NF; i++)
89 cmd = cmd " " $i
90 for (i = 1; i <= in_lines_len; i++)
91 print in_lines[i] | cmd
92 close(cmd)
93 unused = 0
94
95 } else if ($2 == "run-identity") {
96 cmd = ENVIRON["INDENT"]
97 for (i = 3; i <= NF; i++)
98 cmd = cmd " " $i
99 for (i = 1; i <= in_lines_len; i++) {
100 print in_lines[i] | cmd
101 print in_lines[i] > "expected.out"
102 }
103 close(cmd)
104 unused = 0
105
106 } else if ($2 == "end") {
107 if (mode == "input" && input_all == prev_input_all)
108 die(FILENAME ":" NR ": error: duplicate input")
109 mode = ""
110
111 } else {
112 die(FILENAME ":" NR ": error: invalid line \"" $0 "\"")
113 }
114
115 next
116 }
117
118 mode == "input" {
119 in_lines[++in_lines_len] = $0
120 input_all = input_all $0 "\n"
121 }
122
123 mode == "run" {
124 print $0 > "expected.out"
125 }
126
127 END {
128 if (mode != "")
129 die(FILENAME ":" NR ": still in mode \"" mode "\"")
130 if (unused != 0)
131 die(FILENAME ":" unused ": input is not used")
132 }
133 '
134
135 check()
136 {
137 printf '%s\n' "$check_awk" > check.awk
138
139 atf_check -o "file:expected.out" \
140 env INDENT="$indent" awk -f check.awk "$srcdir/$1.c"
141 }
142
143 atf_init_test_cases()
144 {
145 for fname in "$srcdir"/opt_*.c; do
146 test_name=${fname##*/}
147 test_name=${test_name%.c}
148
149 atf_test_case "$test_name"
150 eval "${test_name}_body() { check '$test_name'; }"
151 atf_add_test_case "$test_name"
152 done
153 }
154