File: test.pl

package info (click to toggle)
libxml-writer-perl 0.4-5
  • links: PTS
  • area: main
  • in suites: woody
  • size: 84 kB
  • ctags: 78
  • sloc: perl: 991; makefile: 33
file content (546 lines) | stat: -rw-r--r-- 12,112 bytes parent folder | download | duplicates (2)
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
########################################################################
# test.pl - test script for XML::Writer module.
# $Id: test.pl,v 0.2 1999/04/25 13:46:44 david Exp $
########################################################################

# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl test.pl'

######################### We start with some black magic to print on failure.

# Change 1..1 below to 1..last_test_to_print .
# (It may become useful if the test is moved to ./t subdirectory.)

BEGIN { $| = 1; print "1..43\n"; }
END {print "not ok 1\n" unless $loaded;}
use XML::Writer;
$loaded = 1;
print "ok 1\n";

######################### End of black magic.

# Insert your test code below (better if it prints "ok 13"
# (correspondingly "not ok 13") depending on the success of chunk 13
# of the test code):

use IO::File;
use strict;

my $output = IO::File->new_tmpfile
  || die "Cannot write to temporary file";
my $writer = new XML::Writer::Namespaces(OUTPUT => $output)
  || die "Cannot create XML writer";

#
# Reset the environment for an additional test.
#
sub resetEnv {
  $output->close();
  $output = IO::File->new_tmpfile;
  $writer = new XML::Writer::Namespaces(OUTPUT => $output);
}

#
# Check the results in the temporary output file.
#
# $number - the test number
# $expected - the exact output expected
#
sub checkResult {
  my ($number, $expected) = (@_);
  my $data = '';
  $output->seek(0,0);
  $output->read($data, 1024);
  if ($expected eq $data) {
    print "ok $number\n";
  } else {
    print "not ok $number\n";
    print STDERR "\t(Expected '$expected' but found '$data')\n";
  }
  resetEnv();
}

#
# Expect an error of some sort, and check that the message matches.
#
# $number - the test number
# $pattern - a regular expression that must match the error message
# $value - the return value from an eval{} block
#
sub expectError {
  my ($number, $pattern, $value) = (@_);
  if (defined($value)) {
    print STDERR "Expected error did not occur!\n";
    print "not ok $number\n";
  } elsif ($@ !~ $pattern) {
    print STDERR $@;
    print "not ok $number\n";
  } else {
    print "ok $number\n";
  }
  resetEnv();
}



# Test 2: Empty element tag.
TEST: {
  $writer->emptyTag("foo");
  $writer->end();
  checkResult(2, "<foo />\n");
};



# Test 3: Empty element tag with XML decl.
TEST: {
  $writer->xmlDecl();
  $writer->emptyTag("foo");
  $writer->end();
  checkResult(3, <<"EOS");
<?xml version="1.0" encoding="UTF-8"?>
<foo />
EOS
};



# Test 4: Start/end tag.
TEST: {
  $writer->startTag("foo");
  $writer->endTag("foo");
  $writer->end();
  checkResult(4, "<foo></foo>\n");
};



# Test 5: Attributes
TEST: {
  $writer->emptyTag("foo", "x" => "1>2");
  $writer->end();
  checkResult(5, "<foo x=\"1&gt;2\" />\n");
};



# Test 6: Character data
TEST: {
  $writer->startTag("foo");
  $writer->characters("<tag>&amp;</tag>");
  $writer->endTag("foo");
  $writer->end();
  checkResult(6, "<foo>&lt;tag&gt;&amp;amp;&lt;/tag&gt;</foo>\n");
};



# Test 7: Comment outside document element
TEST: {
  $writer->comment("comment");
  $writer->emptyTag("foo");
  $writer->end();
  checkResult(7, "<!-- comment -->\n<foo />\n");
};



# Test 8: Processing instruction without data (outside document element)
TEST: {
  $writer->pi("pi");
  $writer->emptyTag("foo");
  $writer->end();
  checkResult(8, "<?pi?>\n<foo />\n");
};


# Test 9: Processing instruction with data (outside document element)
TEST: {
  $writer->pi("pi", "data");
  $writer->emptyTag("foo");
  $writer->end();
  checkResult(9, "<?pi data?>\n<foo />\n");
};


# Test 10: comment inside document element
TEST: {
  $writer->startTag("foo");
  $writer->comment("comment");
  $writer->endTag("foo");
  $writer->end();
  checkResult(10, "<foo><!-- comment --></foo>\n");
};


# Test 11: processing instruction inside document element
TEST: {
  $writer->startTag("foo");
  $writer->pi("pi");
  $writer->endTag("foo");
  $writer->end();
  checkResult(11, "<foo><?pi?></foo>\n");
};


# Test 12: WFE for mismatched tags
TEST: {
  $writer->startTag("foo");
  expectError(12, "Attempt to end element \"foo\" with \"bar\" tag", eval {
    $writer->endTag("bar");
  });
};


# Test 13: WFE for unclosed elements
TEST: {
  $writer->startTag("foo");
  $writer->startTag("foo");
  $writer->endTag("foo");
  expectError(13, "Document ended with unmatched start tag\\(s\\)", eval {
    $writer->end();
  });
};


# Test 14: WFE for no document element
TEST: {
  $writer->xmlDecl();
  expectError(14, "Document cannot end without a document element", eval {
    $writer->end();
  });
};


# Test 15: WFE for multiple document elements (non-empty)
TEST: {
  $writer->startTag('foo');
  $writer->endTag('foo');
  expectError(15, "Attempt to insert start tag after close of", eval {
    $writer->startTag('foo');
  });
};


# Test 16: WFE for multiple document elements (empty)
TEST: {
  $writer->emptyTag('foo');
  expectError(16, "Attempt to insert empty tag after close of", eval {
    $writer->emptyTag('foo');
  });
};


# Test 17: DOCTYPE mismatch with empty tag
TEST: {
  $writer->doctype('foo');
  expectError(17, "Document element is \"bar\", but DOCTYPE is \"foo\"", eval {
    $writer->emptyTag('bar');
  });
};


# Test 18: DOCTYPE mismatch with start tag
TEST: {
  $writer->doctype('foo');
  expectError(18, "Document element is \"bar\", but DOCTYPE is \"foo\"", eval {
    $writer->startTag('bar');
  });
};


# Test 19: Multiple DOCTYPE declarations
TEST: {
  $writer->doctype('foo');
  expectError(19, "Attempt to insert second DOCTYPE", eval {
    $writer->doctype('bar');
  });
};


# Test 20: Misplaced DOCTYPE declaration
TEST: {
  $writer->startTag('foo');
  expectError(20, "The DOCTYPE declaration must come before", eval {
    $writer->doctype('foo');
  });
};


# Test 21: Multiple XML declarations
TEST: {
  $writer->xmlDecl();
  expectError(21, "The XML declaration is not the first thing", eval {
    $writer->xmlDecl();
  });
};


# Test 22: Misplaced XML declaration
TEST: {
  $writer->comment();
  expectError(22, "The XML declaration is not the first thing", eval {
    $writer->xmlDecl();
  });
};


# Test 23: Implied end-tag name.
TEST: {
  $writer->startTag('foo');
  $writer->endTag();
  $writer->end();
  checkResult(23, "<foo></foo>\n");
};


# Test 24: in_element query
TEST: {
  $writer->startTag('foo');
  $writer->startTag('bar');
  if ($writer->in_element('bar')) {
    print "ok 24\n";
  } else {
    print "not ok 24\n";
  }
  resetEnv();
};


# Test 25: within_element query
TEST: {
  $writer->startTag('foo');
  $writer->startTag('bar');
  if ($writer->within_element('foo') && $writer->within_element('bar')) {
    print "ok 25\n";
  } else {
    print "not ok 25\n";
  }
  resetEnv();
};


# Test 26: current_element query
TEST: {
  $writer->startTag('foo');
  $writer->startTag('bar');
  if ($writer->current_element() eq 'bar') {
    print "ok 26\n";
  } else {
    print "not ok 26\n";
  }
  resetEnv();
};


# Test 27: ancestor query
TEST: {
  $writer->startTag('foo');
  $writer->startTag('bar');
  if ($writer->ancestor(0) eq 'bar' && $writer->ancestor(1) eq 'foo') {
    print "ok 27\n";
  } else {
    print "not ok 27\n";
  }
  resetEnv();
};


# Test 28: basic namespace processing with empty element
TEST: {
  my $ns = 'http://www.foo.com/';
  $writer->addPrefix($ns, 'foo');
  $writer->emptyTag([$ns, 'doc']);
  $writer->end();
  checkResult(28, "<foo:doc xmlns:foo=\"$ns\" />\n");
};


# Test 29: basic namespace processing with start/end tags
TEST: {
  my $ns = 'http://www.foo.com/';
  $writer->addPrefix($ns, 'foo');
  $writer->startTag([$ns, 'doc']);
  $writer->endTag([$ns, 'doc']);
  $writer->end();
  checkResult(29, "<foo:doc xmlns:foo=\"$ns\"></foo:doc>\n");
};


# Test 30: basic namespace processing with generated prefix
TEST: {
  my $ns = 'http://www.foo.com/';
  $writer->startTag([$ns, 'doc']);
  $writer->endTag([$ns, 'doc']);
  $writer->end();
  checkResult(30, "<__NS1:doc xmlns:__NS1=\"$ns\"></__NS1:doc>\n");
};


# Test 31: basic namespace processing with attributes and empty tag.
TEST: {
  my $ns = 'http://www.foo.com/';
  $writer->addPrefix($ns, 'foo');
  $writer->emptyTag([$ns, 'doc'], [$ns, 'id'] => 'x');
  $writer->end();
  checkResult(31, "<foo:doc foo:id=\"x\" xmlns:foo=\"$ns\" />\n");
};


# Test 32: same as above, but with default namespace.
TEST: {
  my $ns = 'http://www.foo.com/';
  $writer->addPrefix($ns, '');
  $writer->emptyTag([$ns, 'doc'], [$ns, 'id'] => 'x');
  $writer->end();
  checkResult(32, "<doc __NS1:id=\"x\" xmlns=\"$ns\" xmlns:__NS1=\"$ns\" />\n");
};


# Test 33: test that autogenerated prefixes avoid collision.
TEST: {
  my $ns = 'http://www.foo.com/';
  $writer->addPrefix('http://www.bar.com/', '__NS1');
  $writer->emptyTag([$ns, 'doc']);
  $writer->end();
  checkResult(33, "<__NS2:doc xmlns:__NS2=\"$ns\" />\n");
};


# Test 34: check for proper declaration nesting with subtrees.
TEST: {
  my $ns = 'http://www.foo.com/';
  $writer->addPrefix($ns, 'foo');
  $writer->startTag('doc');
  $writer->characters("\n");
  $writer->emptyTag([$ns, 'ptr1']);
  $writer->characters("\n");
  $writer->emptyTag([$ns, 'ptr2']);
  $writer->characters("\n");
  $writer->endTag('doc');
  $writer->end();
  checkResult(34, <<"EOS");
<doc>
<foo:ptr1 xmlns:foo="$ns" />
<foo:ptr2 xmlns:foo="$ns" />
</doc>
EOS
};


# Test 35: check for proper declaration nesting with top level.
TEST: {
  my $ns = 'http://www.foo.com/';
  $writer->addPrefix($ns, 'foo');
  $writer->startTag([$ns, 'doc']);
  $writer->characters("\n");
  $writer->emptyTag([$ns, 'ptr1']);
  $writer->characters("\n");
  $writer->emptyTag([$ns, 'ptr2']);
  $writer->characters("\n");
  $writer->endTag([$ns, 'doc']);
  $writer->end();
  checkResult(35, <<"EOS");
<foo:doc xmlns:foo="$ns">
<foo:ptr1 />
<foo:ptr2 />
</foo:doc>
EOS
};


# Test 36: check for proper default declaration nesting with subtrees.
TEST: {
  my $ns = 'http://www.foo.com/';
  $writer->addPrefix($ns, '');
  $writer->startTag('doc');
  $writer->characters("\n");
  $writer->emptyTag([$ns, 'ptr1']);
  $writer->characters("\n");
  $writer->emptyTag([$ns, 'ptr2']);
  $writer->characters("\n");
  $writer->endTag('doc');
  $writer->end();
  checkResult(36, <<"EOS");
<doc>
<ptr1 xmlns="$ns" />
<ptr2 xmlns="$ns" />
</doc>
EOS
};


# Test 37: check for proper default declaration nesting with top level.
TEST: {
  my $ns = 'http://www.foo.com/';
  $writer->addPrefix($ns, '');
  $writer->startTag([$ns, 'doc']);
  $writer->characters("\n");
  $writer->emptyTag([$ns, 'ptr1']);
  $writer->characters("\n");
  $writer->emptyTag([$ns, 'ptr2']);
  $writer->characters("\n");
  $writer->endTag([$ns, 'doc']);
  $writer->end();
  checkResult(37, <<"EOS");
<doc xmlns="$ns">
<ptr1 />
<ptr2 />
</doc>
EOS
};


# Test 38: Namespace error: attribute name beginning 'xmlns'
TEST: {
  expectError(38, "Attribute name.*begins with 'xmlns'", eval {
    $writer->emptyTag('foo', 'xmlnsxxx' => 'x');
  });
};


# Test 39: Namespace error: Detect an illegal colon in a PI target.
TEST: {
  expectError(39, "PI target.*contains a colon", eval {
    $writer->pi('foo:foo');
  });
};


# Test 40: Namespace error: Detect an illegal colon in an element name.
TEST: {
  expectError(40, "Element name.*contains a colon", eval {
    $writer->emptyTag('foo:foo');
  });
};


# Test 41: Namespace error: Detect an illegal colon in local part of
# an element name.
TEST: {
  expectError(41, "Local part of element name.*contains a colon", eval {
    my $ns = 'http://www.foo.com/';
    $writer->emptyTag([$ns, 'foo:foo']);
  });
};


# Test 42: Namespace error: attribute name containing ':'.
TEST: {
  expectError(42, "Attribute name.*contains ':'", eval {
    $writer->emptyTag('foo', 'foo:bar' => 'x');
  });
};


# Test 43: Namespace error: Detect a colon in the local part of an att name.
TEST: {
  expectError(43, "Local part of attribute name.*contains a colon.", eval {
    my $ns = "http://www.foo.com/";
    $writer->emptyTag('foo', [$ns, 'foo:bar']);
  });
};

1;

__END__