shithub: xmltools

ref: dd05e41b9a71be3441f36d9b944274da3cd64f35
dir: /test/xq.rc/

View raw version
#!/bin/rc

rfork en
ramfs
cd ..

flagfmt='e:extended, c:console'
if (! ifs=() eval `{aux/getflags $*}) {
	aux/usage
	exit usage
}

nl='
'

fn testxq{
	# hack to print test cases more correct
	p=`{echo $"1 | sed 's/''''/''/g'}
	# hack to make nested quotes in test cases more intuitive
	n=`{echo $"1 | sed 's/''/''''/g'}
	c=`{echo $"cmd ''''^$"n^''' >/tmp/out >[2]/tmp/err'}
	eval $"c
	if (~ $2 '')
		echo -n '' >/tmp/want
	if not
		echo $"2 >/tmp/want
	if (~ $#extended 1) {
		echo $nl^'expect:' $"p
		cat /tmp/want
		echo got:
		cat /tmp/out
		echo err:
		cat /tmp/err
	}
	if (cmp -s /tmp/want /tmp/out) {
		if (~ $#extended 1)
			echo '→ success' $"p
	}
	if not {
		echo '→ failed' $"1
	}
}

cat <<EOF >/tmp/test.xml
<?xml?>
<hello hattr="hval">
	<world wattr="wval" wattr2="bla">
		Free text
		<stuff sattr="sval"/>
	</world>
	<world wattr="wval2">
		Another free text
		<stuff sattr="sval2"/>
	</world>
</hello>
EOF

# first test expects data from pipe
cmd='cat /tmp/test.xml | 6.xq -t '
testxq '/hello/world' \
'<world wattr=''wval'' wattr2=''bla'' />
<world wattr=''wval2'' />'

# remaining tests read from file directly
cmd='6.xq -t -f /tmp/test.xml '

testxq '/hello/world' \
'<world wattr=''wval'' wattr2=''bla'' />
<world wattr=''wval2'' />'

testxq '/hello/world/@wattr' \
'wval
wval2'

testxq '/hello/world/text()' \
'Free text
Another free text'

testxq '/hello/world[@wattr=''wval2'']/text()' \
'Another free text'

testxq '/hello/world[@wattr=''wval2'']/stuff' \
'<stuff sattr=''sval2'' />'

testxq '/hello/world[@wattr=''none'']' ''

testxq '/hello//stuff/@sattr' \
'sval
sval2'

testxq '/hello/world[2]' \
'<world wattr=''wval2'' />'

testxq '/hello/world[2]/stuff' \
'<stuff sattr=''sval2'' />'

if (~ $#console 0)
	exit

echo '
enter console, ^D to exit'
6.xq -f /tmp/test.xml