#============================================================= -*-perl-*- # # t/xml.t # # Test the XML plugin. # # Written by Andy Wardley # # Copyright (C) 1996-2006 Andy Wardley. All Rights Reserved. # # This is free software; you can redistribute it and/or modify it # under the same terms as Perl itself. # # $Id: dom.t,v 2.8 2002/08/12 11:07:14 abw Exp $ # #======================================================================== use strict; use warnings; use lib qw( ./lib ../lib ); use Template; use Template::Test; use Cwd qw( abs_path ); my $dir = abs_path( -d 't' ? 't/test/xml' : 'test/xml' ); my $file = 'example.xml'; my $libxml = eval { require XML::LibXML }; my $vars = { dir => $dir, file => $file, libxml => $libxml, debug_on => sub { $Template::Plugin::XML::DEBUG = 1 }, debug_off => sub { $Template::Plugin::XML::DEBUG = 0 }, libxml_on => sub { $Template::Plugin::XML::LIBXML = 1 }, libxml_off => sub { $Template::Plugin::XML::LIBXML = 0 }, libxml_save => sub { $libxml = $Template::Plugin::XML::LIBXML }, libxml_restore => sub { $Template::Plugin::XML::LIBXML = $libxml }, }; test_expect(\*DATA, undef, $vars); __END__ #------------------------------------------------------------------------ # test the $DEBUG package variable sets debugging on/off by default # unless overridden by a debug named parameter #------------------------------------------------------------------------ -- test -- [% CALL debug_on; USE XML; 'debugging is '; XML.debug ? 'on' : 'off' -%] -- expect -- debugging is on -- test -- [% CALL debug_off; USE XML; 'debugging is '; XML.debug ? 'on' : 'off' -%] -- expect -- debugging is off -- test -- [% USE XML(debug=1); 'debugging is '; XML.debug ? 'on' : 'off' %] -- expect -- debugging is on -- test -- [% CALL debug_on; USE XML(debug=0); 'debugging is '; XML.debug ? 'on' : 'off' %] -- expect -- debugging is off #------------------------------------------------------------------------ # test to see if $LIBXML is defined (if XML::LibXML is available). It # should match whatever the libxml is set to. Also check that we can # disable with a parameter and also that we get an error if we try to # enable it when it's not available. #------------------------------------------------------------------------ -- test -- [% USE XML; 'libxml is '; XML.libxml ? 'enabled' : 'disabled' -%] -- expect -- -- process -- libxml is [% libxml ? 'enabled' : 'disabled' %] -- test -- [% CALL libxml_off; USE XML; 'libxml is '; XML.libxml ? 'enabled' : 'disabled'; -%] -- expect -- libxml is disabled -- test -- [% USE XML(libxml = 0); 'libxml is '; XML.libxml ? 'enabled' : 'disabled' -%] -- expect -- libxml is disabled -- test -- [% CALL libxml_off; TRY; USE XML(libxml=1); "should have got 'XML::LibXML not available' error but didn't"; CATCH; "good, we got an error: $error"; END -%] -- expect -- good, we got an error: XML error - XML::LibXML is not available # if libxml is available then check we can set and get various options # such as expand_entities -- test -- [% IF libxml; CALL libxml_restore; USE XML(expand_entities=1); "expanding: $XML.libxml.expand_entities\n"; CALL XML.libxml.expand_entities(0); "expanding: $XML.libxml.expand_entities\n"; ELSE; "no libxml"; END -%] -- expect -- -- process -- [% IF libxml -%] expanding: 1 expanding: 0 [% ELSE -%] no libxml [% END %] -- stop -- -- test -- [% USE XML(file='xmlfile.xml') -%] [% XML.type %]: [% XML.source %] -- expect -- file: xmlfile.xml -- test -- [% USE XML %] got libxml? [% XML.libxml ? 'yes' : 'no' %] -- expect -- -- process -- Hmmm: [% libxml ? 'yes' : 'no' %] -- stop -- # load a file via the file() method -- test -- [% USE XML; xfile = XML.file(file); xfile.name # a Template::Plugin::XML::File object -%] -- expect -- -- process -- [% file %] -- stop -- -- test -- [% USE XML; XML.type or 'No type'; %] -- expect -- No type # specify directory as constructor parameter -- test -- [% USE XML( dir => dir ); xfile = XML.file(file); -%] name: [% xfile.name %] dir: [% xfile.dir %] -- expect -- -- process -- name: [% file %] dir: [% dir %] # specify directory via dir() method -- test -- [% USE XML; xdir = XML.dir(dir); # Template::Plugin::XML::Directory xfile = XML.file(file); -%] path: [% xdir.path %] name: [% xfile.name %] dir: [% xfile.dir %] -- expect -- -- process -- path: [% dir %] name: [% file %] dir: [% dir %] # specify file via single argument to constructor method -- test -- [% USE xfile = XML(file) -%] name: [% xfile.name %] -- expect -- -- process -- name: [% file %] # specify file via named parameter to constructor method -- test -- [% USE xfile = XML( file = file ) -%] name: [% xfile.name %] -- expect -- -- process -- name: [% file %] # specify file and dir via named params to constructor method -- test -- [% USE xfile = XML( dir=dir, file=file ) -%] name: [% xfile.name %] dir: [% xfile.dir %] -- expect -- -- process -- name: [% file %] dir: [% dir %] -- stop -- # TODO: more tests... -- test -- [% XML.dom(file) %] and [% XML.dom(file=file) %] -- test -- [% xdir.dom(file) %] and [% xdir.dom(file=file) %] -- test -- [% xfile.dom %] => T::P::XML::DOM # repeat above tests for xpath, simple and rss # these tests below are copied from dom.t for reference -- test -- [% USE dom = XML.DOM -%] [% doc = dom.parse(filename => xmlfile) -%] [% FOREACH tag = doc.getElementsByTagName('page') -%] * [% tag.href %] [% tag.title %] [% END %] -- expect -- * /foo/bar The Foo Page * /bar/baz The Bar Page * /baz/qux The Baz Page -- test -- [% global.xmltext = BLOCK %]
Hello World!
[% END -%] [% USE dom = XML.DOM -%] [% doc = dom.parse(global.xmltext) -%] [% FOREACH tag = doc.getElementsByTagName('page') -%] * [% tag.href %] [% tag.title %] [% END %] -- expect -- * /foo/bar The Foo Page * /bar/baz The Bar Page * /baz/qux The Baz Page -- test -- [% USE dom = XML.DOM -%] [% doc = dom.parse(text => global.xmltext) -%] [% FOREACH tag = doc.getElementsByTagName('page') -%] * [% tag.href %] [% tag.title %] [% END %] -- expect -- * /foo/bar The Foo Page * /bar/baz The Bar Page * /baz/qux The Baz Page -- test -- [% USE dom = XML.DOM -%] [% doc = dom.parse(xml => global.xmltext) -%] [% FOREACH tag = doc.getElementsByTagName('page') -%] * [% tag.href %] [% tag.title %] [% END %] -- expect -- * /foo/bar The Foo Page * /bar/baz The Bar Page * /baz/qux The Baz Page -- test -- [% USE parser = XML.DOM -%] [% doc = parser.parse(global.xmltext) -%] [% FOREACH node = doc.getElementsByTagName('section') -%] [% node.toTemplate %] [% END %] [% BLOCK section -%] Section name: [% node.name %] title: [% node.title %] [% node.childrenToTemplate -%] [% END %] [% BLOCK page -%] [% node.title %] [% node.childrenToTemplate -%] [% END %] [% BLOCK msg -%] [% node.childrenToTemplate(verbose=1) %] [% END %] -- expect -- Section name: alpha title: The Alpha Zone The Foo Page Hello World! The Bar Page The Baz Page -- test -- [% xmltext = BLOCK %]
> Andy Wardley This is the first page This is the second page
This is the first page in section b This is the second page in section b
[% END -%] [% USE parser = XML.DOM -%] [% doc = parser.parse(xmltext) -%] [% node.allChildrenToTemplate(default='anynode') FOREACH node = doc.getChildNodes %] [% BLOCK section -%] SECTION [% node.id %]: [% node.title %] [% children -%] END OF SECTION [% node.id %] [% END %] [% BLOCK page -%] PAGE: [% node.title %] [% node.children -%] END OF PAGE [% END %] [% BLOCK head -%] HEADER: [% node.toString; prune %]END_HEADER [% END %] [% BLOCK anynode -%] [% node.toString; node.prune %] [% END %] -- expect -- SECTION a: First Section PAGE: page 1 HEADER: Andy WardleyEND_HEADER This is the first page END OF PAGE PAGE: page 2 END OF PAGE END OF SECTION a SECTION b: Second Section PAGE: page 1 END OF PAGE PAGE: page 2 END OF PAGE END OF SECTION b