#!/bin/bash
#shellcheck disable=SC2016,SC1090
. "$TF_DIR/include/subtest.sh"
. "$TF_DIR/include/tools.sh"
PRETTY=plain
. "$(sfpath)" || tf_exit_error "failed to init shellfu"
shellfu import pxpath || tf_exit_error "failed to import pxpath"
tf_enum_subtests() {
echo elem_abs
echo elem_rel
echo attr
echo inv_xpath
echo usage_noargs
echo usage_1arg
echo io_nofile
echo io_pipe
#FIXME: missing coverage: more usage/xml/io errors, [] expressions, namespaces...
}
mkxml() {
echo ''
echo ' '
echo ' '
echo ' pass'
echo ' '
echo ' '
echo ' none'
echo ' '
echo ' '
echo ' pass'
echo ' '
echo ' '
echo ''
}
mka() {
local what=$1
case $name:$what in
elem_abs:xpath)
echo '/benchmark/testresult/foo'
;;
elem_abs:o_out)
echo ''
echo ' pass'
echo ' '
echo ''
echo ' none'
echo ' '
echo ''
echo ' pass'
echo ' '
;;
elem_rel:xpath)
echo '//foo/bar'
;;
elem_rel:o_out)
echo 'pass'
echo 'none'
echo 'pass'
;;
attr:xpath)
echo '//foo/@id'
;;
attr:o_out)
echo 'one'
echo 'two'
echo 'three'
;;
inv_xpath:xpath)
echo '//foo@id'
;;
inv_xpath:o_err)
echo 'Invalid expression'
echo 'Traceback (most recent call last):'
echo ' File "/usr/libexec/shellfu-bash-pxpath/pxpath", line 54, in '
echo ' matches = context.xpathEvalExpression(expr)'
echo ' File "/usr/lib64/python2.7/site-packages/libxml2.py", line 7346, in xpathEvalExpression'
echo " if ret is None:raise xpathError('xmlXPathEvalExpression() failed')"
echo 'libxml2.xpathError: xmlXPathEvalExpression() failed'
;;
inv_xpath:o_es)
echo 1
;;
usage_*:o_err)
echo 'usage: __pxpath__usage usage: pxpath [-f FMT] EXPR FILE [NSNAME:NSURI]'
;;
usage_*:o_es)
echo 2
;;
io_nofile:o_err)
echo 'no such file, ignoring query: nonexistent.xml'
;;
io_nofile:o_es)
echo 1
;;
io_pipe:o_out)
echo 'pass'
echo 'none'
echo 'pass'
;;
*:o_out) true ;;
*:o_err) true ;;
*:o_es) echo 0 ;;
*:xpath) echo '//foo/bar' ;;
*:*) tf_exit_error "bad test name or part name: $name:$what" ;;
esac
}
runtest() {
local xpath
mkdir -p "test" "result/$name"
mkxml > "test.xml"
xpath=$(mka xpath)
case $name in
usage_1arg) pxpath "$xpath" ;;
usage_noargs) pxpath ;;
io_nofile) pxpath "$xpath" "nonexistent.xml" ;;
io_adir) pxpath "$xpath" oracle ;;
io_pipe) <"test.xml" pxpath "$xpath" - ;;
*) pxpath "$xpath" "test.xml" ;;
esac
}
tf_do_subtest() {
local name=$1
local o_out=oracle/$name.stdout
local o_err=oracle/$name.stderr
mka o_out >"$o_out"
mka o_err >"$o_err"
o_es=$(mka o_es)
tf_testflt -n "$name" -E "$o_err" -O "$o_out" -S "$o_es" runtest
}
mkdir -p oracle
tf_do_subtests