File: 05basic.t

package info (click to toggle)
libxml-sax-writer-perl 0.44-8
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 124 kB
  • ctags: 65
  • sloc: perl: 483; sh: 146; makefile: 54
file content (91 lines) | stat: -rwxr-xr-x 3,340 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

###
# XML::SAX::Writer tests
# Robin Berjon <robin@knowscape.com>
# 06/01/2002 - v0.01
###

use strict;
use Test::More tests => 27;
BEGIN { use_ok('XML::SAX::Writer'); }


# default options of XML::SAX::Writer
my $w1 = XML::SAX::Writer->new->{Handler};
ok(        $w1->{EncodeFrom} eq 'utf-8',                       'default EncodeFrom');
ok(        $w1->{EncodeTo}   eq 'utf-8',                       'default EncodeTo');
isa_ok(    $w1->{Output},  'IO::Handle',                       'default Output');
is_deeply( $w1->{Format},  {},                                 'default Format');
is_deeply( $w1->{Escape},  \%XML::SAX::Writer::DEFAULT_ESCAPE, 'default Escape');


# set default options of XML::SAX::Writer
my %fmt2 = ( FooBar => 1 );
my $o2 = \'';
my $w2 = XML::SAX::Writer->new({
                                EncodeFrom  => 'iso-8859-1',
                                EncodeTo    => 'iso-8859-2',
                                Output      => $o2,
                                Format      => \%fmt2,
                                Escape      => {},
                              })->{Handler};
ok(        $w2->{EncodeFrom} eq 'iso-8859-1', 'set EncodeFrom');
ok(        $w2->{EncodeTo}   eq 'iso-8859-2', 'set EncodeTo');
ok(        "$w2->{Output}"   eq  "$o2",       'set Output');
is_deeply( $w2->{Format},   \%fmt2,           'set Format');
is_deeply( $w2->{Escape},   {},               'set Escape');


# options after initialisation
$w1->start_document;
isa_ok($w1->{Encoder},  'XML::SAX::Writer::NullConverter', 'null converter for noop encoding');
isa_ok($w1->{NSHelper}, 'XML::NamespaceSupport',           'ns support');
ok(ref($w1->{EscaperRegex}) eq 'Regexp',                    'escaper regex');
ok(ref($w1->{NSDecl})       eq 'ARRAY',                    'ns stack');
ok(@{$w1->{NSDecl}} == 0,                                  'ns stack is clear');
isa_ok($w1->{Consumer}, 'XML::SAX::Writer::ConsumerInterface', 'consumer is set');

# different inits (mostly for Consumer DWIM)
$w1->{EncodeFrom} = 'iso-8859-1';
$w1->start_document;
isa_ok($w1->{Encoder}, 'Text::Iconv', 'iconv converter for real encoding');

$w1->{Output} = 'test_file_for_output';
$w1->start_document;
isa_ok($w1->{Consumer}, 'XML::SAX::Writer::FileConsumer',   'consumer is FileConsumer');

my $ot = '';
$w1->{Output} = \$ot;
$w1->start_document;
isa_ok($w1->{Consumer}, 'XML::SAX::Writer::StringConsumer', 'consumer is StringConsumer');

$w1->{Output} = [];
$w1->start_document;
isa_ok($w1->{Consumer}, 'XML::SAX::Writer::ArrayConsumer',  'consumer is ArrayConsumer');

$w1->{Output} = *STDOUT;
$w1->start_document;
isa_ok($w1->{Consumer}, 'XML::SAX::Writer::HandleConsumer', 'consumer is HandleConsumer');

$w1->{Output} = bless [], 'Test__XSW1';
sub Test__XSW1::output {}
$w1->start_document;
isa_ok($w1->{Consumer}, 'Test__XSW1',                       'consumer is custom');

$w1->{Output} = bless [], 'Test__XSW2';
eval { $w1->start_document; };
ok($@, 'bad consumer');
isa_ok($@, 'XML::SAX::Writer::Exception', 'bad consumer exception');


# escaping
my $esc1 = '<>&"\'';
my $eq1  = '&lt;&gt;&amp;&quot;&apos;';
my $res1 = $w1->escape($esc1);
ok($res1 eq $eq1, 'escaping (default)');

# converting
my $conv = XML::SAX::Writer::NullConverter->new;
my $str = 'TEST';
my $res = $conv->convert($str);
ok($str eq $res, 'noop converter');