Home | History | Annotate | Line # | Download | only in indent
t_options.sh revision 1.3
      1  1.1  rillig #! /bin/sh
      2  1.3  rillig # $NetBSD: t_options.sh,v 1.3 2021/10/17 17:20:47 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.3  rillig #	#indent run-identity [options]
     44  1.3  rillig #		Runs indent on the input, expecting unmodified output.
     45  1.1  rillig #
     46  1.1  rillig # All text between these directives is not passed to indent.
     47  1.1  rillig 
     48  1.1  rillig srcdir=$(atf_get_srcdir)
     49  1.1  rillig indent=$(atf_config_get usr.bin.indent.test_indent /usr/bin/indent)
     50  1.1  rillig 
     51  1.3  rillig # Read the test specification from stdin, output the actual test output on
     52  1.3  rillig # stdout, write the expected test output to 'expected.out'.
     53  1.3  rillig #
     54  1.1  rillig # shellcheck disable=SC2016
     55  1.1  rillig check_awk='
     56  1.1  rillig function die(msg)
     57  1.1  rillig {
     58  1.2  rillig 	if (!died) {
     59  1.2  rillig 		died = 1
     60  1.2  rillig 		print msg > "/dev/stderr"
     61  1.2  rillig 		exit(1)
     62  1.2  rillig 	}
     63  1.1  rillig }
     64  1.1  rillig 
     65  1.1  rillig # Skip comments starting with dollar; they are used for marking bugs and
     66  1.1  rillig # adding other remarks directly in the input or output sections.
     67  1.1  rillig /^[[:space:]]*\/[*][[:space:]]*[$].*[*]\/$/ ||
     68  1.1  rillig     /^[[:space:]]*\/\/[[:space:]]*[$]/ {
     69  1.1  rillig 	next
     70  1.1  rillig }
     71  1.1  rillig 
     72  1.1  rillig /^#/ && $1 == "#indent" {
     73  1.1  rillig 	print $0
     74  1.3  rillig 	print $0 > "expected.out"
     75  1.3  rillig 
     76  1.1  rillig 	if ($2 == "input") {
     77  1.1  rillig 		if (unused != 0)
     78  1.1  rillig 			die(FILENAME ":" unused ": input is not used")
     79  1.1  rillig 		mode = "input"
     80  1.1  rillig 		in_lines_len = 0
     81  1.2  rillig 		prev_input_all = input_all
     82  1.2  rillig 		input_all = ""
     83  1.1  rillig 		unused = NR
     84  1.3  rillig 
     85  1.1  rillig 	} else if ($2 == "run") {
     86  1.1  rillig 		mode = "run"
     87  1.1  rillig 		cmd = ENVIRON["INDENT"]
     88  1.1  rillig 		for (i = 3; i <= NF; i++)
     89  1.1  rillig 			cmd = cmd " " $i
     90  1.1  rillig 		for (i = 1; i <= in_lines_len; i++)
     91  1.1  rillig 			print in_lines[i] | cmd
     92  1.1  rillig 		close(cmd)
     93  1.1  rillig 		unused = 0
     94  1.3  rillig 
     95  1.1  rillig 	} else if ($2 == "run-identity") {
     96  1.1  rillig 		cmd = ENVIRON["INDENT"]
     97  1.1  rillig 		for (i = 3; i <= NF; i++)
     98  1.1  rillig 			cmd = cmd " " $i
     99  1.1  rillig 		for (i = 1; i <= in_lines_len; i++) {
    100  1.1  rillig 			print in_lines[i] | cmd
    101  1.1  rillig 			print in_lines[i] > "expected.out"
    102  1.1  rillig 		}
    103  1.1  rillig 		close(cmd)
    104  1.1  rillig 		unused = 0
    105  1.3  rillig 
    106  1.1  rillig 	} else if ($2 == "end") {
    107  1.2  rillig 		if (mode == "input" && input_all == prev_input_all)
    108  1.2  rillig 			die(FILENAME ":" NR ": error: duplicate input")
    109  1.1  rillig 		mode = ""
    110  1.3  rillig 
    111  1.1  rillig 	} else {
    112  1.1  rillig 		die(FILENAME ":" NR ": error: invalid line \"" $0 "\"")
    113  1.1  rillig 	}
    114  1.3  rillig 
    115  1.1  rillig 	next
    116  1.1  rillig }
    117  1.1  rillig 
    118  1.1  rillig mode == "input" {
    119  1.1  rillig 	in_lines[++in_lines_len] = $0
    120  1.2  rillig 	input_all = input_all $0 "\n"
    121  1.1  rillig }
    122  1.1  rillig 
    123  1.1  rillig mode == "run" {
    124  1.1  rillig 	print $0 > "expected.out"
    125  1.1  rillig }
    126  1.1  rillig 
    127  1.1  rillig END {
    128  1.1  rillig 	if (mode != "")
    129  1.1  rillig 		die(FILENAME ":" NR ": still in mode \"" mode "\"")
    130  1.1  rillig 	if (unused != 0)
    131  1.1  rillig 		die(FILENAME ":" unused ": input is not used")
    132  1.1  rillig }
    133  1.1  rillig '
    134  1.1  rillig 
    135  1.1  rillig check()
    136  1.1  rillig {
    137  1.1  rillig 	printf '%s\n' "$check_awk" > check.awk
    138  1.1  rillig 
    139  1.1  rillig 	atf_check -o "file:expected.out" \
    140  1.1  rillig 	    env INDENT="$indent" awk -f check.awk "$srcdir/$1.c"
    141  1.1  rillig }
    142  1.1  rillig 
    143  1.1  rillig atf_init_test_cases()
    144  1.1  rillig {
    145  1.1  rillig 	for fname in "$srcdir"/opt_*.c; do
    146  1.1  rillig 		test_name=${fname##*/}
    147  1.1  rillig 		test_name=${test_name%.c}
    148  1.1  rillig 
    149  1.1  rillig 		atf_test_case "$test_name"
    150  1.1  rillig 		eval "${test_name}_body() { check '$test_name'; }"
    151  1.1  rillig 		atf_add_test_case "$test_name"
    152  1.1  rillig 	done
    153  1.1  rillig }
    154