|
Dries Verachtert |
a359f0 |
#!/bin/bash
|
|
Dries Verachtert |
a359f0 |
|
|
Dries Verachtert |
a359f0 |
# Author: Dries Verachtert <dries.verachtert@dries.eu>
|
|
Dries Verachtert |
a359f0 |
|
|
Dries Verachtert |
a359f0 |
t_Log "Running $0 - test XmlTextReader of libxml2-python"
|
|
Dries Verachtert |
a359f0 |
|
|
Pablo Greco |
850d34 |
if [ "$centos_ver" -ge 8 ] ; then
|
|
Pablo Greco |
850d34 |
PYTHON=python3
|
|
Pablo Greco |
850d34 |
else
|
|
Pablo Greco |
850d34 |
PYTHON=python
|
|
Pablo Greco |
850d34 |
fi
|
|
Pablo Greco |
850d34 |
|
|
Pablo Greco |
850d34 |
cat << 'EOF' | $PYTHON - | grep -q 'test succeeded'
|
|
Dries Verachtert |
a359f0 |
import libxml2
|
|
Dries Verachtert |
a359f0 |
import sys
|
|
Pablo Greco |
850d34 |
try:
|
|
Pablo Greco |
850d34 |
from StringIO import StringIO
|
|
Pablo Greco |
850d34 |
except ImportError:
|
|
Pablo Greco |
850d34 |
from io import StringIO
|
|
Dries Verachtert |
a359f0 |
|
|
Dries Verachtert |
a359f0 |
# Load a small xml structure
|
|
Pablo Greco |
850d34 |
xmlStr = StringIO("""
|
|
Dries Verachtert |
a359f0 |
<tests><test name="XmlTextReader"><key1>val1</key1><key2>val2</key2><key3 /></test></tests>""")
|
|
Dries Verachtert |
a359f0 |
xmlBuf = libxml2.inputBuffer(xmlStr)
|
|
Dries Verachtert |
a359f0 |
xmlReader = xmlBuf.newTextReader("reader")
|
|
Dries Verachtert |
a359f0 |
|
|
Dries Verachtert |
a359f0 |
def checkRead(reader, name, isEmptyElementVal, nodeTypeVal, hasAttributesVal):
|
|
Dries Verachtert |
a359f0 |
retval = reader.Read()
|
|
Dries Verachtert |
a359f0 |
if retval != 1:
|
|
Dries Verachtert |
a359f0 |
print("Error: bad retval when reading " + name)
|
|
Dries Verachtert |
a359f0 |
sys.exit(1)
|
|
Dries Verachtert |
a359f0 |
if reader.Name() != name:
|
|
Dries Verachtert |
a359f0 |
print("Error: expected=" + name + ", actual=" + reader.Name())
|
|
Dries Verachtert |
a359f0 |
sys.exit(1)
|
|
Dries Verachtert |
a359f0 |
if reader.NodeType() != nodeTypeVal:
|
|
Dries Verachtert |
a359f0 |
print("Error: node " + name + " has wrong nodetype: " + str(reader.NodeType()))
|
|
Dries Verachtert |
a359f0 |
sys.exit(1)
|
|
Dries Verachtert |
a359f0 |
if reader.IsEmptyElement() != isEmptyElementVal:
|
|
Dries Verachtert |
a359f0 |
print("Error: node " + name + " has wrong isEmptyElement: " + str(reader.IsEmptyElement()))
|
|
Dries Verachtert |
a359f0 |
sys.exit(1)
|
|
Dries Verachtert |
a359f0 |
if reader.HasAttributes() != hasAttributesVal:
|
|
Dries Verachtert |
a359f0 |
print("Error: node " + name + " has wrong hasAttributes: " + str(reader.HasAttributes()))
|
|
Dries Verachtert |
a359f0 |
sys.exit(1)
|
|
Dries Verachtert |
a359f0 |
|
|
Dries Verachtert |
a359f0 |
# Test one by one each part of the loaded xml structure
|
|
Dries Verachtert |
a359f0 |
checkRead(xmlReader, "tests", 0, 1, 0) # 1 = start of element
|
|
Dries Verachtert |
a359f0 |
checkRead(xmlReader, "test", 0, 1, 1)
|
|
Dries Verachtert |
a359f0 |
checkRead(xmlReader, "key1", 0, 1, 0)
|
|
Dries Verachtert |
a359f0 |
checkRead(xmlReader, "#text", 0, 3, 0) # 3 = text node
|
|
Dries Verachtert |
a359f0 |
checkRead(xmlReader, "key1", 0, 15, 0) # 15 = close tag
|
|
Dries Verachtert |
a359f0 |
checkRead(xmlReader, "key2", 0, 1, 0)
|
|
Dries Verachtert |
a359f0 |
checkRead(xmlReader, "#text", 0, 3, 0)
|
|
Dries Verachtert |
a359f0 |
checkRead(xmlReader, "key2", 0, 15, 0)
|
|
Dries Verachtert |
a359f0 |
checkRead(xmlReader, "key3", 1, 1, 0)
|
|
Dries Verachtert |
a359f0 |
checkRead(xmlReader, "test", 0, 15, 1)
|
|
Dries Verachtert |
a359f0 |
checkRead(xmlReader, "tests", 0, 15, 0)
|
|
Pablo Greco |
850d34 |
print ("test succeeded")
|
|
Dries Verachtert |
a359f0 |
EOF
|