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