Home | History | Annotate | Line # | Download | only in indent
t_options.sh revision 1.6
      1 #! /bin/sh
      2 # $NetBSD: t_options.sh,v 1.6 2021/10/18 18:10:20 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-equals-input [options]
     44 #		Runs indent on the input, expecting unmodified output.
     45 #	#indent run-equals-prev-output [options]
     46 #		Runs indent on the input, expecting the same output as from
     47 #		the previous run.
     48 #
     49 # All text outside these directives is not passed to indent.
     50 
     51 srcdir=$(atf_get_srcdir)
     52 indent=$(atf_config_get usr.bin.indent.test_indent /usr/bin/indent)
     53 
     54 # Read the test specification from stdin, output the actual test output on
     55 # stdout, write the expected test output to 'expected.out'.
     56 #
     57 # shellcheck disable=SC2016
     58 check_awk='
     59 function die(lineno, msg)
     60 {
     61 	if (!died) {
     62 		died = 1
     63 		print FILENAME ":" lineno ": error: " msg > "/dev/stderr"
     64 		exit(1)
     65 	}
     66 }
     67 
     68 BEGIN {
     69 	section = ""		# "", "input" or "run"
     70 	section_excl_comm = ""	# without dollar comments
     71 	section_incl_comm = ""	# with dollar comments
     72 
     73 	input_excl_comm = ""	# stdin for indent
     74 	input_incl_comm = ""	# used for duplicate checks
     75 	unused_input_lineno = 0
     76 
     77 	output_excl_comm = ""	# expected output
     78 	output_incl_comm = ""	# used for duplicate checks
     79 	output_lineno = 0
     80 }
     81 
     82 # Hide comments starting with dollar from indent; they are used for marking
     83 # bugs and adding other remarks directly in the input or output sections.
     84 /^[[:space:]]*\/[*][[:space:]]*[$].*[*]\/$/ ||
     85     /^[[:space:]]*\/\/[[:space:]]*[$]/ {
     86 	if (section != "")
     87 		section_incl_comm = section_incl_comm $0 "\n"
     88 	next
     89 }
     90 
     91 function check_unused_input()
     92 {
     93 	if (unused_input_lineno != 0)
     94 		die(unused_input_lineno, "input is not used")
     95 }
     96 
     97 function run_indent(inp,   i, cmd)
     98 {
     99 	cmd = ENVIRON["INDENT"]
    100 	for (i = 3; i <= NF; i++)
    101 		cmd = cmd " " $i
    102 	printf("%s", inp) | cmd
    103 	close(cmd)
    104 }
    105 
    106 /^#/ && $1 == "#indent" {
    107 	print $0
    108 	print $0 > "expected.out"
    109 
    110 	if ($2 == "input") {
    111 		section = "input"
    112 		section_excl_comm = ""
    113 		section_incl_comm = ""
    114 		unused_input_lineno = NR
    115 
    116 	} else if ($2 == "run") {
    117 		section = "run"
    118 		output_lineno = NR
    119 		section_excl_comm = ""
    120 		section_incl_comm = ""
    121 
    122 		run_indent(input_excl_comm)
    123 		unused_input_lineno = 0
    124 
    125 	} else if ($2 == "run-equals-input") {
    126 		run_indent(input_excl_comm)
    127 		printf("%s", input_excl_comm) > "expected.out"
    128 		unused_input_lineno = 0
    129 
    130 	} else if ($2 == "run-equals-prev-output") {
    131 		run_indent(input_excl_comm)
    132 		printf("%s", output_excl_comm) > "expected.out"
    133 		unused_input_lineno = 0
    134 
    135 	} else if ($2 == "end") {
    136 		if (section == "input" && section_incl_comm == input_incl_comm)
    137 			die(NR, "duplicate input; remove this section")
    138 		if (section == "run" && section_incl_comm == input_incl_comm)
    139 			die(output_lineno,
    140 			    "output == input; use run-equals-input")
    141 		if (section == "run" && section_incl_comm == output_incl_comm)
    142 			die(output_lineno,
    143 			    "duplicate output; use run-equals-prev-output")
    144 
    145 		if (section == "input") {
    146 			input_excl_comm = section_excl_comm
    147 			input_incl_comm = section_incl_comm
    148 		}
    149 		if (section == "run") {
    150 			output_excl_comm = section_excl_comm
    151 			output_incl_comm = section_incl_comm
    152 		}
    153 		section = ""
    154 
    155 	} else {
    156 		die(NR, "invalid line \"" $0 "\"")
    157 	}
    158 
    159 	next
    160 }
    161 
    162 section == "input" || section == "run" {
    163 	section_excl_comm = section_excl_comm $0 "\n"
    164 	section_incl_comm = section_incl_comm $0 "\n"
    165 }
    166 
    167 section == "run" {
    168 	print $0 > "expected.out"
    169 }
    170 
    171 END {
    172 	if (section != "")
    173 		die(NR, "still in section \"" section "\"")
    174 	check_unused_input()
    175 }
    176 '
    177 
    178 check()
    179 {
    180 	printf '%s\n' "$check_awk" > check.awk
    181 
    182 	atf_check -o "file:expected.out" \
    183 	    env INDENT="$indent" awk -f check.awk "$srcdir/$1.c"
    184 }
    185 
    186 atf_init_test_cases()
    187 {
    188 	for fname in "$srcdir"/*.c; do
    189 		test_name=${fname##*/}
    190 		test_name=${test_name%.c}
    191 
    192 		atf_test_case "$test_name"
    193 		eval "${test_name}_body() { check '$test_name'; }"
    194 		atf_add_test_case "$test_name"
    195 	done
    196 }
    197