#!/bin/bash

. ../init.sh

set -e

export ROOT=${as}/root
export Q_VDB=/

mktmpdir

test() {
	local num=$1 exp=$2 ret
	local cmd=( qdepends "${@:3}" )

	"${cmd[@]}" >& list && ret=0 || ret=$?
	if [[ ${ret} -ne ${exp} ]] ; then
		tend 1 "exit code: wanted ${exp} but got ${ret}: ${cmd[*]}"
	elif [[ ${exp} -ne 0 ]] ; then
		# for errors, let the message be anything
		return
	fi

	local good="${as}/list${num}.good"
	if [[ ! -e ${good} ]] ; then
		cp list "${good}"
	fi
	diff -u list "${good}"
	tend $? "${num} ${cmd[*]}"
}

testf() { test "$1" "${3:-0}" -f "$2"; }

# basic sanity checks
testf 00 '|' 1
testf 01 ''
testf 02 'a/b'
testf 03 'foo? ( a/b )'
testf 04 '|| ( a/b )'

# a bit more complicated with or deps
testf 05 '|| ( || ( || ( x ) a ) )'

# hande use deps on atoms #470180
testf 06 'a[foo(+)]'

testQ() { test "$1" "${3:-0}" -Q "$2"; }

# reverse checks #504636
testQ 07 xinit

testq() { test "$1" "${3:-0}" -q "$2"; }

# forward checks #504636
testq 08 xdm

cleantmpdir

end
