File: 10consumers.t

package info (click to toggle)
libxml-sax-writer-perl 0.57-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 176 kB
  • sloc: perl: 553; makefile: 2
file content (110 lines) | stat: -rwxr-xr-x 2,487 bytes parent folder | download | duplicates (10)
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

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

use strict;

package MyConsumer;
@MyConsumer::ISA = qw( XML::SAX::Writer::ConsumerInterface );

sub new {
    my $self = shift->SUPER::new( my $output );
    $$self = '' ;   # Note the extra '$'

    return $self;
}

sub output {
    my $self = shift;
    $$self .= uc shift;
}

sub get_output {
    my $self = shift;
    return $$self;
}


package main;
use Test::More tests => 15;
use XML::SAX::Writer qw();

# StringConsumer
my $ref1 = 'MUST_CLEAR';
my $str = XML::SAX::Writer::StringConsumer->new(\$ref1);
isa_ok($str, 'XML::SAX::Writer::StringConsumer', 'StringConsumer');
$str->output('CONTENT');
my $res1 = $str->finalize;
ok($$res1 eq 'CONTENT', 'content is set');


# ArrayConsumer
my $arr = XML::SAX::Writer::ArrayConsumer->new([]);
isa_ok($arr, 'XML::SAX::Writer::ArrayConsumer', 'ArrayConsumer');
$arr->output('CONTENT0');
$arr->output('CONTENT1');
my $res2 = $arr->finalize;
ok($res2->[0] eq 'CONTENT0', 'content (1)');
ok($res2->[1] eq 'CONTENT1', 'content (2)');


# HandleConsumer and FileConsumer
my $fil1 = XML::SAX::Writer::FileConsumer->new('test_file1');
isa_ok($fil1, 'XML::SAX::Writer::FileConsumer', 'FileConsumer');
isa_ok($fil1, 'XML::SAX::Writer::HandleConsumer', 'HandleConsumer');
$fil1->output('FILE ONE');
my $fil2 = XML::SAX::Writer::FileConsumer->new('test_file2');
$fil2->output('FILE TWO');
$fil1->output(' FILE ONE');
$fil2->output(' FILE TWO');
$fil1->finalize;
$fil2->finalize;

open FH1, "test_file1" or die $!;
my $cnt1 = <FH1>;
close FH1;

open FH2, "test_file2" or die $!;
my $cnt2 = <FH2>;
close FH2;

ok($cnt1 eq 'FILE ONE FILE ONE', 'file content (1)');
ok($cnt2 eq 'FILE TWO FILE TWO', 'file content (2)');


##
## Now, test some of the consumers "in situ"
##

sub push_events {
    my $w = XML::SAX::Writer->new( Output => shift );
    $w->start_document( {} );
    $w->start_element( { Name => "foo" } );
    $w->end_element( { Name => "foo" } );
    $w->end_document( {} );
}

{
    my @events;
    push_events( sub { push @events, [ @_ ] } );
    ok @events >= 3;
    ok $events[0]->[0], "start_document";
    ok $events[1]->[0], "data";
    ok $events[-1]->[0], "end_document";
    ## This next test might break id X::S::W ever stops
    ## putting that space in there.
    ok join( "", map $_->[1], @events ), "<foo />";
}

##
## A custom consumer
##

{
    my $c = MyConsumer->new;
    push_events( $c );
    ok $c->get_output, "<foo />";
}