#!/usr/bin/env bash
# SPDX-License-Identifier: MIT
export PATH
PATH=./bin${PATH:+:}${PATH-}
. quoter_pipe.sh

result() {
	if [ $? -eq 0 ]
	then	echo "$1"
	else	echo "Test '$1' failed" >&2
		echo "result: $quoter_pipe" >&2
		exit 1
	fi
}

testfunc() {
	echo $1
	return $2
}

quoter_pipe testfunc A 7
[ $? -eq 1 ] || 'quoter_pipe ret 1a'
[ "$quoter_pipe$quoter_pipestatus" = "'A
'7" ]
result 'quoter_pipe ret 1'

quoter_pipe -e testfunc B 6
[ $? -eq 1 ] || result 'quoter_pipe ret 2a'
[ "$quoter_pipe$quoter_pipestatus" = "'B
'6" ]
result 'quoter_pipe ret 2'

quoter_pipe -n testfunc C 5
[ $? -eq 1 ] || result 'quoter_pipe ret 3a'
[ "$quoter_pipe$quoter_pipestatus" = "'C
'5" ]
result 'quoter_pipe ret 3'

quoter_pipe -ne testfunc D 4
[ $? -eq 1 ] || result 'quoter_pipe ret 4a'
[ "$quoter_pipe$quoter_pipestatus" = "'D
'4" ]
result 'quoter_pipe ret 4'

q() {
	quoter_pipe "$1" -- printf "$2" || result "quoter_pipe $4"a
	outputC=`printf "$2" | quoter -i "$1"; ret=$?; echo x; exit $ret` || \
		result "quoter_pipe $4"b
	outputC=${outputC%x}
	[ x"$quoter_pipe
" = x"$outputC" ] || result "quoter_pipe $4"c
	[ x"$quoter_pipe" = x"$3" ]
	result "quoter_pipe $4"
}

q -S '' "" "''"
q -e '' "" "-e ''"
q -n '' "" "-n ''"
q -ne '' "" "-ne ''"

q -S "'" \\\' "'"
q -e "'" \\\' "-e '"
q -n "'" \\\' "-n '"
q -ne "'" \\\' "-ne '"

q -S '\0' "''" "0"
q -e '\0' "'' ''" "-e 0"
q -n '\0' "''" "-n 0"
q -ne '\0' "'' "'\
'"''" "-ne 0"

q -S 'a b\0' "'a b'" "a b0"
q -e 'a b\0' "'a b' ''" "-e a b0"
q -n 'a b\0' "'a b'" "-n a b0"
q -ne 'a b\0' "'a b' "'\
'"''" "-ne a b0"

q -S 'a b\0 ' "'a b' ' '" "a b0s"
q -e 'a b\0 ' "'a b' ' '" "-e a b0s"
q -n 'a b\0 ' "'a b' "'\
'"' '" "-n a b0s"
q -ne 'a b\0 ' "'a b' "'\
'"' '" "-ne a b0s"

q -S 'a b\0 \\\n' "'a b' ' "'\
'"'" "a b0n"
q -e 'a b\0 \\\n' "'a b' ' "'\
'"'" "-e a b0n"
q -n 'a b\0 \\\n' "'a b' "'\
'"'"' \
'"'" "-n a b0n"
q -ne 'a b\0 \\\n' "'a b' "'\
'"'"' \
'"'" "-ne a b0n"

q -S '~a' "'~a'" "~a"
q -S 'a~' "a~" "a~"
q -l '~a' "'~a'" "-l ~a"
q -l 'a~' "'a~'" "-l a~"

Q() {
	arg=$2
	name='quoter '$4
	expected=$3
	quoter_pipe=`quoter -c "$1" -- "$arg"; res=$?; printf x; exit $res` \
		|| result "$name"a
	outputC=`quoter "$1" -- "$arg"; res=$?; printf x; exit $res` \
		|| result "$name"b
	quoter_pipe=${quoter_pipe%x}
	outputC=${outputC%x}
	[ x"$quoter_pipe
" = x"$outputC" ] || result "$name"c
	eval "set -- $quoter_pipe"
	[ $# -eq 1 ] && [ x"$1" = x"$arg" ] ||result "$name"d
	[ x"$quoter_pipe" = x"$expected" ]
	result "$name"
}

Q -s "a b'
" "a\\ b\\''
'" "-s space"
Q -S "a b'
" "'a b'\\''
'" "-S space"
Q -l "a b'
" "'a b'\\''
'" "-l space"

Q -s "a~'@'?" "a~\\'@\\'\\?" "-s specials"
Q -S "a~'@'?" "a~\\'@\\''?'" "-S specials"
Q -l "a~'@'?" "'a~'\\''@'\\''?'" "-l specials"

i() {
	opt=$1
	shift
	name="-i$opt buffer $1"
	shift
	input=$1
	shift
	output=$1
	shift
	quoter_pipe ${opt:+--empty-last} -- printf "$input" ${1+"$@"} \
		|| result "$name"a
	result=`printf "$input" ${1+"$@"} | quoter --cut --stdin \
		${opt:+--empty-last}` \
		|| result "$name"b
	[ "$quoter_pipe" = "$result" ] || result "$name"c
	check=`printf "$output" ${1+"$@"}`
	[ -z "$opt" ] || case $input in
	*'\0')
		check=$check" ''";;
	esac
	[ "$quoter_pipe" = "$check" ]
	result "$name"
}

j() {
	i '' "$@"
	i e "$@"
}

for count in 4094 4095 4096 8190 8191 8192
do	j "0 $count" '\0%'$count's' "'' '%${count}s'" a
	j "$count" "%$count"s "'%${count}s'" a
	j "$count 0" "%$count"s'\0' "'%${count}s'" a
	j "$count 00" "%$count"s'\0\0' "'%${count}s' ''" a
	j "$count 0 $count" "%$count"s'\0'"%$count"s \
		"'%${count}s' '%${count}s'" a a
	j "$count 0 $count 0" "%$count"s'\0'"%$count"s'\0' \
		"'%${count}s' '%${count}s'" a a
	j "$count 0 $count 00" "%$count"s'\0'"%$count"s'\0\0' \
		"'%${count}s' '%${count}s' ''" a a
	j "$count 00 $count" "%$count"s'\0\0'"%$count"s \
		"'%${count}s' '' '%${count}s'" a a
	j "$count 00 $count 0" "%$count"s'\0\0'"%$count"s'\0' \
		"'%${count}s' '' '%${count}s'" a a
	j "$count 00 $count 00" "%$count"s'\0\0'"%$count"s'\0\0' \
		"'%${count}s' '' '%${count}s' ''" a a
done

echo 'All tests succeeded'
