shithub: xmltools

ref: f896677cdcd52890b3bc215d655c7bea323a4755
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='
'

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

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 (~ $#extended 1) {
		echo $nl^'expect:' $"p $nl^$"2
		cat /tmp/out /tmp/err
	}
	r=`{cat /tmp/out}
	if (~ $#r 0)
		r=`{cat /tmp/err}
	if (~ $"2 $"r) {
		if (~ $#extended 1)
			echo '→ success' $"p
	}
	if not {
		echo '→ failed' $"1
	}
}

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

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

testxq '/hello/world' '<world wattr=''wval'' wattr2=''bla'' />'
testxq '/hello/world/@wattr' 'wval'
testxq '/hello/world/text()' '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'']' 'not found'
testxq '/hello//stuff/@sattr' 'sval'
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