xref: /freebsd/contrib/one-true-awk/testdir/T.csv (revision f32a6403d34654ac6e61182d09abb5e85850e1ee)
1*f32a6403SWarner Losh#!/bin/sh
2*f32a6403SWarner Losh
3*f32a6403SWarner Loshecho T.csv: tests of csv field splitting, no embedded newlines
4*f32a6403SWarner Losh
5*f32a6403SWarner Loshawk=${awk-../a.out}
6*f32a6403SWarner Losh
7*f32a6403SWarner Losh$awk '
8*f32a6403SWarner LoshBEGIN {
9*f32a6403SWarner Losh	FS = "\t"
10*f32a6403SWarner Losh	awk = "../a.out --csv"
11*f32a6403SWarner Losh}
12*f32a6403SWarner LoshNF == 0 || $1 ~ /^#/ {
13*f32a6403SWarner Losh	next
14*f32a6403SWarner Losh}
15*f32a6403SWarner Losh$1 ~ /try/ {	# new test
16*f32a6403SWarner Losh	nt++
17*f32a6403SWarner Losh	sub(/try /, "")
18*f32a6403SWarner Losh	prog = $0
19*f32a6403SWarner Losh	printf("%3d  %s\n", nt, prog)
20*f32a6403SWarner Losh	prog = sprintf("%s '"'"'%s'"'"'", awk, prog)
21*f32a6403SWarner Losh	# print "prog is", prog
22*f32a6403SWarner Losh	nt2 = 0
23*f32a6403SWarner Losh	while (getline > 0) {
24*f32a6403SWarner Losh		if (NF == 0)	# blank line terminates a sequence
25*f32a6403SWarner Losh			break
26*f32a6403SWarner Losh		input = $1
27*f32a6403SWarner Losh		for (i = 2; i < NF; i++)	# input data
28*f32a6403SWarner Losh			input = input "\t" $i
29*f32a6403SWarner Losh		test = sprintf("./echo '"'"'%s'"'"' | %s >foo1; ",
30*f32a6403SWarner Losh			input, prog)
31*f32a6403SWarner Losh		if ($NF == "\"\"")
32*f32a6403SWarner Losh			output = ">foo2;"
33*f32a6403SWarner Losh		else
34*f32a6403SWarner Losh			output = sprintf("./echo '"'"'%s'"'"' >foo2; ", $NF)
35*f32a6403SWarner Losh		gsub(/\\t/, "\t", output)
36*f32a6403SWarner Losh		gsub(/\\n/, "\n", output)
37*f32a6403SWarner Losh		run = sprintf("cmp foo1 foo2 || echo test %d.%d failed",
38*f32a6403SWarner Losh			nt, ++nt2)
39*f32a6403SWarner Losh		# print  "input is", input
40*f32a6403SWarner Losh		# print  "test is", test
41*f32a6403SWarner Losh		# print  "output is", output
42*f32a6403SWarner Losh		# print  "run is", run
43*f32a6403SWarner Losh		system(test output run)
44*f32a6403SWarner Losh	}
45*f32a6403SWarner Losh	tt += nt2
46*f32a6403SWarner Losh}
47*f32a6403SWarner LoshEND { print tt, "tests" }
48*f32a6403SWarner Losh' <<\!!!!
49*f32a6403SWarner Losh# General format:
50*f32a6403SWarner Losh# try program as rest of line
51*f32a6403SWarner Losh# $1	$2	$3	output1  (\t for tab, \n for newline,
52*f32a6403SWarner Losh# $1	$2	$3	output2  ("" for null)
53*f32a6403SWarner Losh# ... terminated by blank line
54*f32a6403SWarner Losh
55*f32a6403SWarner Losh
56*f32a6403SWarner Loshtry  { for (i=1; i<=NF; i++) printf("[%s]", $i); printf("\n") }
57*f32a6403SWarner Losha	[a]
58*f32a6403SWarner Losh  a	[  a]
59*f32a6403SWarner Losh,a	[][a]
60*f32a6403SWarner Losh , a	[ ][ a]
61*f32a6403SWarner Losha,b	[a][b]
62*f32a6403SWarner Losha,b,c	[a][b][c]
63*f32a6403SWarner Losh""	[]
64*f32a6403SWarner Losh"abc"	[abc]
65*f32a6403SWarner Losh"a""b"	[a"b]
66*f32a6403SWarner Losh"a","b"	[a][b]
67*f32a6403SWarner Losha""b	[a""b]
68*f32a6403SWarner Losh"a,b"	[a,b]
69*f32a6403SWarner Losh""""	["]
70*f32a6403SWarner Losh""""""	[""]
71*f32a6403SWarner Losh"""x"""	["x"]
72*f32a6403SWarner Losh""","""	[","]
73*f32a6403SWarner Losh,,""	[][][]
74*f32a6403SWarner Losha""b	[a""b]
75*f32a6403SWarner Losha''b	[a''b]
76*f32a6403SWarner Losh,,	[][][]
77*f32a6403SWarner Losha,	[a][]
78*f32a6403SWarner Losh"",	[][]
79*f32a6403SWarner Losh,	[][]
80*f32a6403SWarner Losh!!!!
81