Annotation of src/tests/usr.bin/indent/t_options.awk, Revision 1.8
1.8 ! rillig 1: # $NetBSD: t_options.awk,v 1.7 2021/11/20 13:14:34 rillig Exp $
1.1 rillig 2: #
3: # Copyright (c) 2021 The NetBSD Foundation, Inc.
4: # All rights reserved.
5: #
6: # Redistribution and use in source and binary forms, with or without
7: # modification, are permitted provided that the following conditions
8: # are met:
9: # 1. Redistributions of source code must retain the above copyright
10: # notice, this list of conditions and the following disclaimer.
11: # 2. Redistributions in binary form must reproduce the above copyright
12: # notice, this list of conditions and the following disclaimer in the
13: # documentation and/or other materials provided with the distribution.
14: #
15: # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16: # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17: # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18: # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19: # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20: # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21: # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22: # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23: # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24: # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25: # POSSIBILITY OF SUCH DAMAGE.
26:
27: # Test driver for indent that focuses on comparing the effects of the various
28: # command line options.
29: #
30: # The test files contain the input to be formatted, the formatting options
31: # and the output, all as close together as possible. The test files use the
32: # following directives:
33: #
34: # #indent input [description]
35: # Specifies the input to be formatted.
36: # #indent run [options]
37: # Runs indent on the input, using the given options.
38: # #indent end [description]
39: # Finishes an '#indent input' or '#indent run' section.
40: # #indent run-equals-input [options]
41: # Runs indent on the input, expecting unmodified output.
42: # #indent run-equals-prev-output [options]
43: # Runs indent on the input, expecting the same output as from
44: # the previous run.
45: #
46: # All text outside these directives is not passed to indent.
1.3 rillig 47: #
48: # The actual output from running indent is written to stdout, the expected
49: # test output is written to 'expected.out'.
1.1 rillig 50:
1.3 rillig 51: BEGIN {
52: warned = 0
53: died = 0
54:
1.5 rillig 55: prev_empty_lines = 0 # the finished "block" of empty lines
56: curr_empty_lines = 0 # the ongoing "block" of empty lines
57: seen_input_section = 0 # the first input section is not checked
58:
1.3 rillig 59: section = "" # "", "input" or "run"
60: section_excl_comm = "" # without dollar comments
61: section_incl_comm = "" # with dollar comments
62:
63: input_excl_comm = "" # stdin for indent
64: input_incl_comm = "" # used for duplicate checks
65: unused_input_lineno = 0
66:
67: output_excl_comm = "" # expected output
68: output_incl_comm = "" # used for duplicate checks
69: output_lineno = 0
70: }
1.1 rillig 71:
72: function die(lineno, msg)
73: {
74: if (!died) {
75: died = 1 # suppress further actions in END block
76: print FILENAME ":" lineno ": error: " msg > "/dev/stderr"
77: exit(1)
78: }
79: }
80:
81: function warn(lineno, msg)
82: {
83: print FILENAME ":" lineno ": warning: " msg > "/dev/stderr"
84: warned = 1
85: }
86:
87: function quote(s)
88: {
89: return "'" s "'"
90: }
91:
92: function check_unused_input()
93: {
94: if (unused_input_lineno != 0)
95: warn(unused_input_lineno, "input is not used")
96: }
97:
98: function run_indent(inp, i, cmd)
99: {
100: cmd = ENVIRON["INDENT"]
101: if (cmd == "")
102: cmd = "indent"
103: for (i = 3; i <= NF; i++)
104: cmd = cmd " " $i
105: printf("%s", inp) | cmd
106: close(cmd)
107: }
108:
1.5 rillig 109: section == "" {
110: if (NF == 0)
111: curr_empty_lines++
112: else {
113: if (curr_empty_lines > 0) {
114: if (prev_empty_lines > 1)
1.6 rillig 115: warn(NR - curr_empty_lines - 1,
116: prev_empty_lines " empty lines a few " \
117: "lines above, should be only 1")
1.5 rillig 118: prev_empty_lines = curr_empty_lines
119: }
120: curr_empty_lines = 0
121: }
122: }
123:
1.3 rillig 124: # Hide comments starting with dollar from indent; they are used for marking
125: # bugs and adding other remarks directly in the input or output sections.
126: /^[[:space:]]*\/[*][[:space:]]*[$].*[*]\/$/ ||
127: /^[[:space:]]*\/\/[[:space:]]*[$]/ {
128: if (section != "")
129: section_incl_comm = section_incl_comm $0 "\n"
130: next
131: }
132:
1.1 rillig 133: /^#/ && $1 == "#indent" {
134: print $0
135: print $0 > "expected.out"
136:
137: if ($2 == "input") {
1.5 rillig 138: if (prev_empty_lines != 2 && seen_input_section)
139: warn(NR, "input section needs 2 empty lines above, " \
140: "not " prev_empty_lines)
1.1 rillig 141: check_unused_input()
142: section = "input"
143: section_excl_comm = ""
144: section_incl_comm = ""
145: unused_input_lineno = NR
1.5 rillig 146: seen_input_section = 1
1.1 rillig 147:
148: } else if ($2 == "run") {
149: if (section != "")
150: warn(NR, "unfinished section " quote(section))
1.5 rillig 151: if (prev_empty_lines != 1)
152: warn(NR, "run section needs 1 empty line above, " \
153: "not " prev_empty_lines)
1.1 rillig 154: section = "run"
155: output_lineno = NR
156: section_excl_comm = ""
157: section_incl_comm = ""
158:
159: run_indent(input_excl_comm)
160: unused_input_lineno = 0
161:
162: } else if ($2 == "run-equals-input") {
163: run_indent(input_excl_comm)
164: printf("%s", input_excl_comm) > "expected.out"
165: unused_input_lineno = 0
166:
167: } else if ($2 == "run-equals-prev-output") {
168: run_indent(input_excl_comm)
169: printf("%s", output_excl_comm) > "expected.out"
170:
171: } else if ($2 == "end" && section == "input") {
172: if (section_incl_comm == input_incl_comm)
173: warn(NR, "duplicate input; remove this section")
174:
175: input_excl_comm = section_excl_comm
176: input_incl_comm = section_incl_comm
177: section = ""
178:
179: } else if ($2 == "end" && section == "run") {
180: if (section_incl_comm == input_incl_comm)
181: warn(output_lineno,
182: "output == input; use run-equals-input")
183: if (section_incl_comm == output_incl_comm)
184: warn(output_lineno,
185: "duplicate output; use run-equals-prev-output")
186:
187: output_excl_comm = section_excl_comm
188: output_incl_comm = section_incl_comm
189: section = ""
190:
191: } else if ($2 == "end") {
192: warn(NR, "misplaced " quote("#indent end"))
193:
194: } else {
195: die(NR, "invalid line " quote($0))
196: }
197:
1.5 rillig 198: prev_empty_lines = 0
199: curr_empty_lines = 0
1.1 rillig 200: next
201: }
202:
203: section == "input" || section == "run" {
204: section_excl_comm = section_excl_comm $0 "\n"
205: section_incl_comm = section_incl_comm $0 "\n"
206: }
207:
208: section == "run" {
209: print $0 > "expected.out"
210: }
211:
1.2 rillig 212: section == "" && !/^$/ && !/^#/ && !/^\// && !/^ \*/ {
213: warn(NR, "non-comment line outside 'input' or 'run' section")
214: }
215:
1.1 rillig 216: END {
217: if (section != "")
218: die(NR, "still in section " quote(section))
219: check_unused_input()
220: if (warned)
221: exit(1)
222: }
CVSweb <webmaster@jp.NetBSD.org>