2022-12-07 00:40:01 +03:00
#!/usr/bin/env python3
2002-03-01 19:14:17 +03:00
import sys
2023-08-15 13:49:27 +03:00
import setup_test
2002-03-01 19:14:17 +03:00
import libxml2
#memory debug specific
libxml2 . debugMemory ( 1 )
#
# A document hosting the nodes returned from the extension function
#
mydoc = libxml2 . newDoc ( " 1.0 " )
def foo ( ctx , str ) :
global mydoc
#
# test returning a node set works as expected
#
parent = mydoc . newDocNode ( None , ' p ' , None )
mydoc . addChild ( parent )
node = mydoc . newDocText ( str )
parent . addChild ( node )
return [ parent ]
doc = libxml2 . parseFile ( " tst.xml " )
ctxt = doc . xpathNewContext ( )
libxml2 . registerXPathFunction ( ctxt . _o , " foo " , None , foo )
res = ctxt . xpathEval ( " foo( ' hello ' ) " )
if type ( res ) != type ( [ ] ) :
2013-03-30 17:38:20 +04:00
print ( " Failed to return a nodeset " )
2002-03-01 19:14:17 +03:00
sys . exit ( 1 )
if len ( res ) != 1 :
2013-03-30 17:38:20 +04:00
print ( " Unexpected nodeset size " )
2002-03-01 19:14:17 +03:00
sys . exit ( 1 )
node = res [ 0 ]
if node . name != ' p ' :
2013-03-30 17:38:20 +04:00
print ( " Unexpected nodeset element result " )
2002-03-01 19:14:17 +03:00
sys . exit ( 1 )
node = node . children
if node . type != ' text ' :
2013-03-30 17:38:20 +04:00
print ( " Unexpected nodeset element children type " )
2002-03-01 19:14:17 +03:00
sys . exit ( 1 )
if node . content != ' hello ' :
2013-03-30 17:38:20 +04:00
print ( " Unexpected nodeset element children content " )
2002-03-01 19:14:17 +03:00
sys . exit ( 1 )
doc . freeDoc ( )
mydoc . freeDoc ( )
ctxt . xpathFreeContext ( )
#memory debug specific
libxml2 . cleanupParser ( )
if libxml2 . debugMemory ( 1 ) == 0 :
2013-03-30 17:38:20 +04:00
print ( " OK " )
2002-03-01 19:14:17 +03:00
else :
2013-03-30 17:38:20 +04:00
print ( " Memory leak %d bytes " % ( libxml2 . debugMemory ( 1 ) ) )