Module Name:    src
Committed By:   rillig
Date:           Mon Oct 18 20:18:00 UTC 2021

Modified Files:
        src/distrib/sets/lists/tests: mi
        src/tests/usr.bin/indent: Makefile t_options.sh
Added Files:
        src/tests/usr.bin/indent: label.c t_options.awk
Removed Files:
        src/tests/usr.bin/indent: label.0 label.0.pro label.0.stdout

Log Message:
tests/indent: extend tests for labels, extract test driver

Having the test driver in a separate file allows to run it on its own,
as 'awk -f t_options.awk label.c'. When the test driver warns, exit 1;
this is only needed for the standalone version, when run from inside ATF
the warnings on stderr already suffice to make the test fail in the end.


To generate a diff of this commit:
cvs rdiff -u -r1.1143 -r1.1144 src/distrib/sets/lists/tests/mi
cvs rdiff -u -r1.18 -r1.19 src/tests/usr.bin/indent/Makefile
cvs rdiff -u -r1.2 -r0 src/tests/usr.bin/indent/label.0 \
    src/tests/usr.bin/indent/label.0.stdout
cvs rdiff -u -r1.1 -r0 src/tests/usr.bin/indent/label.0.pro
cvs rdiff -u -r0 -r1.1 src/tests/usr.bin/indent/label.c \
    src/tests/usr.bin/indent/t_options.awk
cvs rdiff -u -r1.7 -r1.8 src/tests/usr.bin/indent/t_options.sh

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/distrib/sets/lists/tests/mi
diff -u src/distrib/sets/lists/tests/mi:1.1143 src/distrib/sets/lists/tests/mi:1.1144
--- src/distrib/sets/lists/tests/mi:1.1143	Mon Oct 18 19:36:29 2021
+++ src/distrib/sets/lists/tests/mi	Mon Oct 18 20:18:00 2021
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.1143 2021/10/18 19:36:29 rillig Exp $
+# $NetBSD: mi,v 1.1144 2021/10/18 20:18:00 rillig Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -4755,9 +4755,10 @@
 ./usr/tests/usr.bin/indent/indent_variables.0				tests-usr.bin-tests	compattestfile,atf
 ./usr/tests/usr.bin/indent/indent_variables.0.pro			tests-usr.bin-tests	compattestfile,atf
 ./usr/tests/usr.bin/indent/indent_variables.0.stdout			tests-usr.bin-tests	compattestfile,atf
-./usr/tests/usr.bin/indent/label.0					tests-usr.bin-tests	compattestfile,atf
-./usr/tests/usr.bin/indent/label.0.pro					tests-usr.bin-tests	compattestfile,atf
-./usr/tests/usr.bin/indent/label.0.stdout				tests-usr.bin-tests	compattestfile,atf
+./usr/tests/usr.bin/indent/label.0					tests-obsolete		obsolete,atf
+./usr/tests/usr.bin/indent/label.0.pro					tests-obsolete		obsolete,atf
+./usr/tests/usr.bin/indent/label.0.stdout				tests-obsolete		obsolete,atf
+./usr/tests/usr.bin/indent/label.c					tests-usr.bin-tests	compattestfile,atf
 ./usr/tests/usr.bin/indent/lex.0					tests-usr.bin-tests	compattestfile,atf
 ./usr/tests/usr.bin/indent/lex.0.pro					tests-usr.bin-tests	compattestfile,atf
 ./usr/tests/usr.bin/indent/lex.0.stdout					tests-usr.bin-tests	compattestfile,atf
@@ -5034,6 +5035,7 @@
 ./usr/tests/usr.bin/indent/t_indent					tests-usr.bin-tests	compattestfile,atf
 ./usr/tests/usr.bin/indent/t_misc					tests-usr.bin-tests	compattestfile,atf
 ./usr/tests/usr.bin/indent/t_options					tests-usr.bin-tests	compattestfile,atf
+./usr/tests/usr.bin/indent/t_options.awk				tests-usr.bin-tests	compattestfile,atf
 ./usr/tests/usr.bin/indent/token-binary_op.0				tests-usr.bin-tests	compattestfile,atf
 ./usr/tests/usr.bin/indent/token-binary_op.0.pro			tests-usr.bin-tests	compattestfile,atf
 ./usr/tests/usr.bin/indent/token-binary_op.0.stdout			tests-usr.bin-tests	compattestfile,atf

Index: src/tests/usr.bin/indent/Makefile
diff -u src/tests/usr.bin/indent/Makefile:1.18 src/tests/usr.bin/indent/Makefile:1.19
--- src/tests/usr.bin/indent/Makefile:1.18	Mon Oct 18 19:36:30 2021
+++ src/tests/usr.bin/indent/Makefile	Mon Oct 18 20:18:00 2021
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile,v 1.18 2021/10/18 19:36:30 rillig Exp $
+#	$NetBSD: Makefile,v 1.19 2021/10/18 20:18:00 rillig Exp $
 
 .include <bsd.own.mk>
 
@@ -22,9 +22,7 @@ FILES+=		f_decls.0.stdout
 FILES+=		indent_variables.0
 FILES+=		indent_variables.0.pro
 FILES+=		indent_variables.0.stdout
-FILES+=		label.0
-FILES+=		label.0.stdout
-FILES+=		label.0.pro
+FILES+=		label.c
 FILES+=		lex.0
 FILES+=		lex.0.pro
 FILES+=		lex.0.stdout
@@ -147,6 +145,7 @@ FILES+=		struct.0.stdout
 FILES+=		surplusbad.0
 FILES+=		surplusbad.0.stdout
 FILES+=		surplusbad.0.pro
+FILES+=		t_options.awk
 FILES+=		token-binary_op.0
 FILES+=		token-binary_op.0.pro
 FILES+=		token-binary_op.0.stdout

Index: src/tests/usr.bin/indent/t_options.sh
diff -u src/tests/usr.bin/indent/t_options.sh:1.7 src/tests/usr.bin/indent/t_options.sh:1.8
--- src/tests/usr.bin/indent/t_options.sh:1.7	Mon Oct 18 19:36:30 2021
+++ src/tests/usr.bin/indent/t_options.sh	Mon Oct 18 20:18:00 2021
@@ -1,5 +1,5 @@
 #! /bin/sh
-# $NetBSD: t_options.sh,v 1.7 2021/10/18 19:36:30 rillig Exp $
+# $NetBSD: t_options.sh,v 1.8 2021/10/18 20:18:00 rillig Exp $
 #
 # Copyright (c) 2021 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -51,149 +51,10 @@
 srcdir=$(atf_get_srcdir)
 indent=$(atf_config_get usr.bin.indent.test_indent /usr/bin/indent)
 
-# Read the test specification from stdin, output the actual test output on
-# stdout, write the expected test output to 'expected.out'.
-#
-# shellcheck disable=SC2016
-check_awk='
-function die(lineno, msg)
-{
-	if (!died) {
-		died = 1
-		print FILENAME ":" lineno ": error: " msg > "/dev/stderr"
-		exit(1)
-	}
-}
-
-function warn(lineno, msg)
-{
-	print FILENAME ":" lineno ": warning: " msg > "/dev/stderr"
-}
-
-function quote(s)
-{
-	return "'\''" s "'\''"
-}
-
-BEGIN {
-	section = ""		# "", "input" or "run"
-	section_excl_comm = ""	# without dollar comments
-	section_incl_comm = ""	# with dollar comments
-
-	input_excl_comm = ""	# stdin for indent
-	input_incl_comm = ""	# used for duplicate checks
-	unused_input_lineno = 0
-
-	output_excl_comm = ""	# expected output
-	output_incl_comm = ""	# used for duplicate checks
-	output_lineno = 0
-}
-
-# Hide comments starting with dollar from indent; they are used for marking
-# bugs and adding other remarks directly in the input or output sections.
-/^[[:space:]]*\/[*][[:space:]]*[$].*[*]\/$/ ||
-    /^[[:space:]]*\/\/[[:space:]]*[$]/ {
-	if (section != "")
-		section_incl_comm = section_incl_comm $0 "\n"
-	next
-}
-
-function check_unused_input()
-{
-	if (unused_input_lineno != 0)
-		warn(unused_input_lineno, "input is not used")
-}
-
-function run_indent(inp,   i, cmd)
-{
-	cmd = ENVIRON["INDENT"]
-	for (i = 3; i <= NF; i++)
-		cmd = cmd " " $i
-	printf("%s", inp) | cmd
-	close(cmd)
-}
-
-/^#/ && $1 == "#indent" {
-	print $0
-	print $0 > "expected.out"
-
-	if ($2 == "input") {
-		check_unused_input()
-		section = "input"
-		section_excl_comm = ""
-		section_incl_comm = ""
-		unused_input_lineno = NR
-
-	} else if ($2 == "run") {
-		section = "run"
-		output_lineno = NR
-		section_excl_comm = ""
-		section_incl_comm = ""
-
-		run_indent(input_excl_comm)
-		unused_input_lineno = 0
-
-	} else if ($2 == "run-equals-input") {
-		run_indent(input_excl_comm)
-		printf("%s", input_excl_comm) > "expected.out"
-		unused_input_lineno = 0
-
-	} else if ($2 == "run-equals-prev-output") {
-		run_indent(input_excl_comm)
-		printf("%s", output_excl_comm) > "expected.out"
-
-	} else if ($2 == "end" && section == "input") {
-		if (section_incl_comm == input_incl_comm)
-			warn(NR, "duplicate input; remove this section")
-
-		input_excl_comm = section_excl_comm
-		input_incl_comm = section_incl_comm
-		section = ""
-
-	} else if ($2 == "end" && section == "run") {
-		if (section_incl_comm == input_incl_comm)
-			warn(output_lineno,
-			    "output == input; use run-equals-input")
-		if (section_incl_comm == output_incl_comm)
-			warn(output_lineno,
-			    "duplicate output; use run-equals-prev-output")
-
-		output_excl_comm = section_excl_comm
-		output_incl_comm = section_incl_comm
-		section = ""
-
-	} else if ($2 == "end") {
-		warn(NR, "misplaced " quote("#indent end"))
-
-	} else {
-		die(NR, "invalid line " quote($0))
-	}
-
-	next
-}
-
-section == "input" || section == "run" {
-	section_excl_comm = section_excl_comm $0 "\n"
-	section_incl_comm = section_incl_comm $0 "\n"
-}
-
-section == "run" {
-	print $0 > "expected.out"
-}
-
-END {
-	if (section != "")
-		die(NR, "still in section " quote(section))
-	check_unused_input()
-}
-'
-
 check()
 {
-	printf '%s\n' "$check_awk" > check.awk
-
 	atf_check -o "file:expected.out" \
-	    env INDENT="$indent" awk -f check.awk "$srcdir/$1.c"
+	    env INDENT="$indent" awk -f "$srcdir/t_options.awk" "$srcdir/$1.c"
 }
 
 atf_init_test_cases()

Added files:

Index: src/tests/usr.bin/indent/label.c
diff -u /dev/null src/tests/usr.bin/indent/label.c:1.1
--- /dev/null	Mon Oct 18 20:18:00 2021
+++ src/tests/usr.bin/indent/label.c	Mon Oct 18 20:18:00 2021
@@ -0,0 +1,124 @@
+/*	$NetBSD: label.c,v 1.1 2021/10/18 20:18:00 rillig Exp $	*/
+/* $FreeBSD: head/usr.bin/indent/tests/label.0 313544 2017-02-10 09:31:39Z pstef $ */
+
+/* See FreeBSD r303489 */
+
+#indent input
+void t(void) {
+	switch (1)
+	{
+		case 1:	/* test */
+		case 2:		/* test */
+	}
+CLEANUP:
+	;
+V: ;
+U: ;
+}
+#indent end
+
+#indent run
+void
+t(void)
+{
+	switch (1) {
+	case 1:			/* test */
+	case 2:			/* test */
+	}
+CLEANUP:
+	;
+V:	;
+U:	;
+}
+#indent end
+
+#indent input
+void
+label_width(void)
+{
+L:;
+L2:;
+L_3:;
+L__4:;
+L___5:;
+L____6:;
+L_____7:;
+L______8:;
+}
+#indent end
+
+#indent run
+void
+label_width(void)
+{
+L:	;
+L2:	;
+L_3:	;
+L__4:	;
+L___5:	;
+L____6:	;
+L_____7:;
+L______8:;
+}
+#indent end
+
+/*
+ * The indentation of statement labels is fixed to -2, it is not configurable.
+ */
+#indent input
+void
+label_indentation(void)
+{
+	if (level1) {
+	if (level2) {
+	if (level3) {
+	if (level4) {
+	if (level5) {
+	label5:
+	statement();
+	}
+	label4:
+	statement();
+	}
+	label3:
+	statement();
+	}
+	label2:
+	statement();
+	}
+	label1:
+	statement();
+	}
+	label0:
+	statement();
+}
+#indent end
+
+#indent run
+void
+label_indentation(void)
+{
+	if (level1) {
+		if (level2) {
+			if (level3) {
+				if (level4) {
+					if (level5) {
+				label5:
+						statement();
+					}
+			label4:
+					statement();
+				}
+		label3:
+				statement();
+			}
+	label2:
+			statement();
+		}
+label1:
+		statement();
+	}
+label0:
+	statement();
+}
+#indent end
Index: src/tests/usr.bin/indent/t_options.awk
diff -u /dev/null src/tests/usr.bin/indent/t_options.awk:1.1
--- /dev/null	Mon Oct 18 20:18:00 2021
+++ src/tests/usr.bin/indent/t_options.awk	Mon Oct 18 20:18:00 2021
@@ -0,0 +1,192 @@
+# $NetBSD: t_options.awk,v 1.1 2021/10/18 20:18:00 rillig Exp $
+#
+# Copyright (c) 2021 The NetBSD Foundation, Inc.
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+# $FreeBSD$
+
+# Test driver for indent that focuses on comparing the effects of the various
+# command line options.
+#
+# The test files contain the input to be formatted, the formatting options
+# and the output, all as close together as possible. The test files use the
+# following directives:
+#
+#	#indent input [description]
+#		Specifies the input to be formatted.
+#	#indent run [options]
+#		Runs indent on the input, using the given options.
+#	#indent end [description]
+#		Finishes an '#indent input' or '#indent run' section.
+#	#indent run-equals-input [options]
+#		Runs indent on the input, expecting unmodified output.
+#	#indent run-equals-prev-output [options]
+#		Runs indent on the input, expecting the same output as from
+#		the previous run.
+#
+# All text outside these directives is not passed to indent.
+
+# Read the test specification from stdin, output the actual test output on
+# stdout, write the expected test output to 'expected.out'.
+
+function die(lineno, msg)
+{
+	if (!died) {
+		died = 1	# suppress further actions in END block
+		print FILENAME ":" lineno ": error: " msg > "/dev/stderr"
+		exit(1)
+	}
+}
+
+function warn(lineno, msg)
+{
+	print FILENAME ":" lineno ": warning: " msg > "/dev/stderr"
+	warned = 1
+}
+
+function quote(s)
+{
+	return "'" s "'"
+}
+
+BEGIN {
+	warned = 0
+	died = 0
+
+	section = ""		# "", "input" or "run"
+	section_excl_comm = ""	# without dollar comments
+	section_incl_comm = ""	# with dollar comments
+
+	input_excl_comm = ""	# stdin for indent
+	input_incl_comm = ""	# used for duplicate checks
+	unused_input_lineno = 0
+
+	output_excl_comm = ""	# expected output
+	output_incl_comm = ""	# used for duplicate checks
+	output_lineno = 0
+}
+
+# Hide comments starting with dollar from indent; they are used for marking
+# bugs and adding other remarks directly in the input or output sections.
+/^[[:space:]]*\/[*][[:space:]]*[$].*[*]\/$/ ||
+    /^[[:space:]]*\/\/[[:space:]]*[$]/ {
+	if (section != "")
+		section_incl_comm = section_incl_comm $0 "\n"
+	next
+}
+
+function check_unused_input()
+{
+	if (unused_input_lineno != 0)
+		warn(unused_input_lineno, "input is not used")
+}
+
+function run_indent(inp,   i, cmd)
+{
+	cmd = ENVIRON["INDENT"]
+	if (cmd == "")
+		cmd = "indent"
+	for (i = 3; i <= NF; i++)
+		cmd = cmd " " $i
+	printf("%s", inp) | cmd
+	close(cmd)
+}
+
+/^#/ && $1 == "#indent" {
+	print $0
+	print $0 > "expected.out"
+
+	if ($2 == "input") {
+		check_unused_input()
+		section = "input"
+		section_excl_comm = ""
+		section_incl_comm = ""
+		unused_input_lineno = NR
+
+	} else if ($2 == "run") {
+		if (section != "")
+			warn(NR, "unfinished section " quote(section))
+		section = "run"
+		output_lineno = NR
+		section_excl_comm = ""
+		section_incl_comm = ""
+
+		run_indent(input_excl_comm)
+		unused_input_lineno = 0
+
+	} else if ($2 == "run-equals-input") {
+		run_indent(input_excl_comm)
+		printf("%s", input_excl_comm) > "expected.out"
+		unused_input_lineno = 0
+
+	} else if ($2 == "run-equals-prev-output") {
+		run_indent(input_excl_comm)
+		printf("%s", output_excl_comm) > "expected.out"
+
+	} else if ($2 == "end" && section == "input") {
+		if (section_incl_comm == input_incl_comm)
+			warn(NR, "duplicate input; remove this section")
+
+		input_excl_comm = section_excl_comm
+		input_incl_comm = section_incl_comm
+		section = ""
+
+	} else if ($2 == "end" && section == "run") {
+		if (section_incl_comm == input_incl_comm)
+			warn(output_lineno,
+			    "output == input; use run-equals-input")
+		if (section_incl_comm == output_incl_comm)
+			warn(output_lineno,
+			    "duplicate output; use run-equals-prev-output")
+
+		output_excl_comm = section_excl_comm
+		output_incl_comm = section_incl_comm
+		section = ""
+
+	} else if ($2 == "end") {
+		warn(NR, "misplaced " quote("#indent end"))
+
+	} else {
+		die(NR, "invalid line " quote($0))
+	}
+
+	next
+}
+
+section == "input" || section == "run" {
+	section_excl_comm = section_excl_comm $0 "\n"
+	section_incl_comm = section_incl_comm $0 "\n"
+}
+
+section == "run" {
+	print $0 > "expected.out"
+}
+
+END {
+	if (section != "")
+		die(NR, "still in section " quote(section))
+	check_unused_input()
+	if (warned)
+		exit(1)
+}

Reply via email to