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