#!/bin/bash
# Test: XML parser tests and JSON translation
# @see https://www.w3.org/TR/2008/REC-xml-20081126
# https://www.w3.org/TR/2009/REC-xml-names-20091208
#PROG="valgrind --leak-check=full --show-leak-kinds=all ../util/clixon_util_xml"
# Magic line must be first in script (see README.md)
s="$_" ; . ./lib.sh || if [ "$s" = $0 ]; then exit 0; else return 0; fi
: ${clixon_util_xml:=clixon_util_xml -o} # -o is output
new "xml parse"
expecteof "$clixon_util_xml" 0 "" "^$"
new "xml parse to json"
expecteof "$clixon_util_xml -j" 0 "" '{"a": {"b": null}}'
new "xml parse strange names"
expecteof "$clixon_util_xml" 0 "<_->" "<_->"
new "xml parse name errors"
expecteof "$clixon_util_xml" 255 "<-a/>" ""
new "xml parse name errors"
expecteof "$clixon_util_xml" 255 "<9/>" ""
new "xml parse name errors"
expecteof "$clixon_util_xml" 255 "" ""
LF='
'
new "xml parse content with CR LF -> LF, CR->LF (see https://www.w3.org/TR/REC-xml/#sec-line-ends)"
ret=$(echo "a
b${LF}c
${LF}d" | $clixon_util_xml)
if [ "$ret" != "a${LF}b${LF}c${LF}d" ]; then
err 'a$LFb$LFc' "$ret"
fi
new "xml simple CDATA"
expecteofx "$clixon_util_xml" 0 '' ''
new "xml simple CDATA to json"
expecteofx "$clixon_util_xml -j" 0 '' '{"a": "a text"}'
new "xml complex CDATA"
XML=$(cat <An example of escaped CENDs
y" so I guess that means that z > x ]]>
]]>
]]>
EOF
)
expecteof "$clixon_util_xml" 0 "$XML" "^An example of escaped CENDs
y\" so I guess that means that z > x ]]>
]]>]]>$"
JSON=$(cat < y\" so I guess that means that z > x ","data": "This text contains a CEND ]]>","alternative": "This text contains a CEND ]]>"}}
EOF
)
new "xml complex CDATA to json"
expecteofx "$clixon_util_xml -j" 0 "$XML" "$JSON"
XML=$(cat <Less than: < , greater than: > ampersand: &
EOF
)
new "xml encode <>&"
expecteof "$clixon_util_xml" 0 "$XML" "$XML"
new "xml encode <>& to json"
expecteof "$clixon_util_xml -j" 0 "$XML" '{"message": "Less than: < , greater than: > ampersand: & "}'
XML=$(cat <single-quote character ' represented as ' and double-quote character as "
EOF
)
new "xml single and double quote"
expecteof "$clixon_util_xml" 0 "$XML" "single-quote character ' represented as ' and double-quote character as \""
JSON=$(cat <a\b" "a\b"
new "xml backspace to json"
expecteofx "$clixon_util_xml -j" 0 "a\b" '{"a": "a\\b"}'
new "Double quotes for attributes"
expecteof "$clixon_util_xml" 0 '' ''
new "Single quotes for attributes (returns double quotes but at least parses right)"
expecteof "$clixon_util_xml" 0 "" ''
new "Mixed quotes"
expecteof "$clixon_util_xml" 0 "" ''
new "XMLdecl version"
expecteof "$clixon_util_xml" 0 '' ''
new "XMLdecl version, single quotes"
expecteof "$clixon_util_xml" 0 "" ''
new "XMLdecl version no element"
expecteof "$clixon_util_xml" 255 '' ''
new "XMLdecl no version"
expecteof "$clixon_util_xml" 255 '' ''
new "XMLdecl misspelled version"
expecteof "$clixon_util_xml -l o" 255 '' ''
new "XMLdecl version + encoding"
expecteof "$clixon_util_xml" 0 '' ''
new "XMLdecl version + misspelled encoding"
expecteof "$clixon_util_xml -l o" 255 '' 'syntax error: at or before: e'
new "XMLdecl version + standalone"
expecteof "$clixon_util_xml" 0 '' ''
new "PI - Processing instruction empty"
expecteof "$clixon_util_xml" 0 '' ''
new "PI some content"
expecteof "$clixon_util_xml" 0 '' ''
new "prolog element misc*"
expecteof "$clixon_util_xml" 0 '' ''
# We allow it as an internal necessity for parsing of xml fragments
#new "double element error"
#expecteof "$clixon_util_xml" 255 '' ''
new "namespace: DefaultAttName"
expecteof "$clixon_util_xml" 0 'hello' 'hello'
new "namespace: PrefixedAttName"
expecteof "$clixon_util_xml" 0 'hello' '^hello$'
new "First example 6.1 from https://www.w3.org/TR/2009/REC-xml-names-20091208"
XML=$(cat <
Frobnostication
Moved to
here.
EOF
)
expecteof "$clixon_util_xml" 0 "$XML" "$XML"
new "Second example 6.1 from https://www.w3.org/TR/2009/REC-xml-names-20091208"
XML=$(cat <
Cheaper by the Dozen
1568491379
EOF
)
expecteof "$clixon_util_xml" 0 "$XML" "$XML"
rm -rf $dir