File: dom.t

package info (click to toggle)
libtemplate-perl 2.14-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, sarge
  • size: 5,496 kB
  • ctags: 667
  • sloc: perl: 15,349; makefile: 62; xml: 7; sh: 5
file content (237 lines) | stat: -rw-r--r-- 5,774 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#============================================================= -*-perl-*-
#
# t/dom.t
#
# Test the XML::DOM plugin.
#
# Written by Andy Wardley <abw@kfs.org>
#
# Copyright (C) 1996-2000 Andy Wardley.  All Rights Reserved.
# Copyright (C) 1998-2000 Canon Research Centre Europe Ltd.
#
# 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 lib qw( ./lib ../lib );
#use lib qw( /home/abw/perl/modules/XML/libxml-enno-1.02/lib );
use Template;
use Template::Test;
use Cwd qw( abs_path );
$^W = 1;

#$Template::Test::DEBUG = 1;
$Template::Test::PRESERVE = 1;

# I hate having to do this
my $shut_up_warnings = $XML::DOM::VERSION;

eval "use XML::DOM";
if ($@ ||  $XML::DOM::VERSION < 1.27) {
    skip_all("XML::DOM v1.27 or later not installed");
}

# account for script being run in distribution root or 't' directory
my $file = abs_path( -d 't' ? 't/test/xml' : 'test/xml' );
$file .= '/testfile.xml';   

test_expect(\*DATA, undef, { 'xmlfile' => $file });

__END__
-- test --
[% TRY;
     # old usage style is now deprecated (it was badly broken)
     USE dom = XML.DOM(xmlfile);
   CATCH XML;
     error;
   END
%]
-- expect --
XML.DOM error - XML::DOM usage has changed - you must now call parse()

-- test --
## NOTE: this test disabled - appears to cause seg fault core dump
[%# TRY;
     # specify a dummy encoding, just to make sure it gets passed as an option
     USE dom = XML.DOM(ProtocolEncoding = 'ISO-666');
     doc = dom.parse(xmlfile);
   CATCH XML.DOM;
     error.info.split(' /').0;
   END;
%]
-- expect --
## NOTE: this test disabled - appears to cause seg fault core dump
## failed to parse xml file

-- test --
[% USE dom = XML.DOM -%]
[% doc = dom.parse(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 --
[% USE dom = XML.DOM -%]
[% doc = dom.parse(file => 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 --
[% 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 %]
<website id="webzone1">
  <section name="alpha" title="The Alpha Zone">
    <page href="/foo/bar" title="The Foo Page"><msg>Hello World!</msg></page>
    <page href="/bar/baz" title="The Bar Page"/>
    <page href="/baz/qux" title="The Baz Page"/>
  </section>
</website>
[% 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 -%]
<a href="[% node.href %]">[% node.title %]</a>
[% node.childrenToTemplate -%]
[% END %]

[% BLOCK msg -%]
<b>[% node.childrenToTemplate(verbose=1) %]</b>
[% END %]

-- expect --
Section name: alpha  title: The Alpha Zone
<a href="/foo/bar">The Foo Page</a>
<b>Hello World!</b>
<a href="/bar/baz">The Bar Page</a>
<a href="/baz/qux">The Baz Page</a>

-- test --
[% xmltext = BLOCK %]
<xml>
<section id="a" title="First Section">>
  <page id="a1" title="page 1">
    <head><author>Andy Wardley</author></head>
    <body>
    This is the first page
    </body>
  </page>
  <page id="a2" title="page 2">
    This is the second page
  </page>
</section>
<section id="b" title="Second Section">
  <page id="b1" title="page 1">
    This is the first page in section b
  </page>
  <page id="b2" title="page 2">
    This is the second page in section b
  </page>
</section>
</xml>
[% 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 -%]
<any>[% node.toString; node.prune %]</any>
[% END %]
-- expect --
SECTION a: First Section
PAGE: page 1
HEADER: <head><author>Andy Wardley</author></head>END_HEADER
<any><body>
    This is the first page
    </body></any>
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