~ubuntu-branches/ubuntu/saucy/libhtml-stripscripts-perl/saucy

« back to all changes in this revision

Viewing changes to t/60rules_tag.t

  • Committer: Bazaar Package Importer
  • Author(s): Tim Retout
  • Date: 2008-08-07 17:21:30 UTC
  • Revision ID: james.westby@ubuntu.com-20080807172130-3l9o4bwl9ftodfqh
Tags: upstream-1.04
ImportĀ upstreamĀ versionĀ 1.04

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
use strict;
 
2
use Test::More tests => 48;
 
3
 
 
4
BEGIN { $^W = 1 }
 
5
 
 
6
use HTML::StripScripts;
 
7
 
 
8
my %tags_b = ( 'undef'   => undef,
 
9
               '0'       => 0,
 
10
               '1'       => 1,
 
11
               'sub'     => \&b_callback,
 
12
               'hash'    => {},
 
13
               'tag_sub' => { tag => \&b_callback }
 
14
);
 
15
my %tags_default = ( 'undef'   => undef,
 
16
                     '0'       => 0,
 
17
                     '1'       => 1,
 
18
                     'sub'     => \&default_callback,
 
19
                     'hash'    => {},
 
20
                     'tag_0'   => { tag => 0 },
 
21
                     'tag_1'   => { tag => 1 },
 
22
                     'tag_sub' => { tag => \&default_callback }
 
23
);
 
24
 
 
25
my %results;
 
26
 
 
27
foreach my $b (qw(undef 0 1 sub hash tag_sub)) {
 
28
    foreach my $default (qw(undef 0 1 sub hash tag_0 tag_1 tag_sub)) {
 
29
        my $test = "${b} :: ${default}";
 
30
        test_tag( $test, $tags_b{$b}, $tags_default{$default},
 
31
                  $results{$test} );
 
32
    }
 
33
}
 
34
 
 
35
#===================================
 
36
sub test_tag {
 
37
#===================================
 
38
    my ( $test, $b, $default, $result ) = @_;
 
39
    my %Rules;
 
40
    if ( defined $b ) {
 
41
        $Rules{'b'} = $b;
 
42
    }
 
43
 
 
44
    if ( defined $default ) {
 
45
        $Rules{'*'} = $default;
 
46
    }
 
47
 
 
48
    my $f = HTML::StripScripts->new( { Rules => \%Rules } );
 
49
 
 
50
    $f->input_start_document;
 
51
    $f->input_start('<b>');
 
52
    $f->input_text('foo');
 
53
    $f->input_end('</b>');
 
54
    $f->input_start('<i>');
 
55
    $f->input_text('bar');
 
56
    $f->input_end('</i>');
 
57
    $f->input_end_document;
 
58
    is( $f->filtered_document, $result, "$test" );
 
59
}
 
60
 
 
61
#===================================
 
62
sub b_callback {
 
63
#===================================
 
64
    my ( $filter, $e ) = @_;
 
65
    $e->{content}
 
66
        = "[B : tag='$e->{tag}', content='$e->{content}']" . $e->{content};
 
67
    return 1;
 
68
}
 
69
 
 
70
#===================================
 
71
sub default_callback {
 
72
#===================================
 
73
    my ( $filter, $e ) = @_;
 
74
    $e->{content} = "[DEFAULT : tag='$e->{tag}', content='$e->{content}']"
 
75
        . $e->{content};
 
76
    return 1;
 
77
}
 
78
 
 
79
 
 
80
BEGIN {
 
81
 
 
82
    my $filt  = '<!--filtered-->';
 
83
    my $def_b = "[DEFAULT : tag='b', content='foo']";
 
84
    my $def_i = "[DEFAULT : tag='i', content='bar']";
 
85
    my $b     = "[B : tag='b', content='foo']";
 
86
 
 
87
    %results = (
 
88
        'undef :: undef'   => "<b>foo</b><i>bar</i>",
 
89
        'undef :: 0'       => "${filt}foo${filt}${filt}bar${filt}",
 
90
        'undef :: 1'       => "<b>foo</b><i>bar</i>",
 
91
        'undef :: sub'     => "<b>${def_b}foo</b><i>${def_i}bar</i>",
 
92
        'undef :: hash'    => "<b>foo</b><i>bar</i>",
 
93
        'undef :: tag_0'   => "${filt}foo${filt}${filt}bar${filt}",
 
94
        'undef :: tag_1'   => "<b>foo</b><i>bar</i>",
 
95
        'undef :: tag_sub' => "<b>${def_b}foo</b><i>${def_i}bar</i>",
 
96
        '0 :: undef'       => "${filt}foo${filt}<i>bar</i>",
 
97
        '0 :: 0'           => "${filt}foo${filt}${filt}bar${filt}",
 
98
        '0 :: 1'           => "${filt}foo${filt}<i>bar</i>",
 
99
        '0 :: sub'         => "${filt}foo${filt}<i>${def_i}bar</i>",
 
100
        '0 :: hash'        => "${filt}foo${filt}<i>bar</i>",
 
101
        '0 :: tag_0'       => "${filt}foo${filt}${filt}bar${filt}",
 
102
        '0 :: tag_1'       => "${filt}foo${filt}<i>bar</i>",
 
103
        '0 :: tag_sub'     => "${filt}foo${filt}<i>${def_i}bar</i>",
 
104
        '1 :: undef'       => "<b>foo</b><i>bar</i>",
 
105
        '1 :: 0'           => "<b>foo</b>${filt}bar${filt}",
 
106
        '1 :: 1'           => "<b>foo</b><i>bar</i>",
 
107
        '1 :: sub'         => "<b>${def_b}foo</b><i>${def_i}bar</i>",
 
108
        '1 :: hash'        => "<b>foo</b><i>bar</i>",
 
109
        '1 :: tag_0'       => "<b>foo</b>${filt}bar${filt}",
 
110
        '1 :: tag_1'       => "<b>foo</b><i>bar</i>",
 
111
        '1 :: tag_sub'     => "<b>${def_b}foo</b><i>${def_i}bar</i>",
 
112
        'sub :: undef'     => "<b>${b}foo</b><i>bar</i>",
 
113
        'sub :: 0'         => "<b>${b}foo</b>${filt}bar${filt}",
 
114
        'sub :: 1'         => "<b>${b}foo</b><i>bar</i>",
 
115
        'sub :: sub'     => "<b>${b}foo</b><i>${def_i}bar</i>",
 
116
        'sub :: hash'    => "<b>${b}foo</b><i>bar</i>",
 
117
        'sub :: tag_0'   => "<b>${b}foo</b>${filt}bar${filt}",
 
118
        'sub :: tag_1'   => "<b>${b}foo</b><i>bar</i>",
 
119
        'sub :: tag_sub' => "<b>${b}foo</b><i>${def_i}bar</i>",
 
120
        'hash :: undef'  => "<b>foo</b><i>bar</i>",
 
121
        'hash :: 0'      => "<b>foo</b>${filt}bar${filt}",
 
122
        'hash :: 1'      => "<b>foo</b><i>bar</i>",
 
123
        'hash :: sub'        => "<b>${def_b}foo</b><i>${def_i}bar</i>",
 
124
        'hash :: hash'       => "<b>foo</b><i>bar</i>",
 
125
        'hash :: tag_0'      => "<b>foo</b>${filt}bar${filt}",
 
126
        'hash :: tag_1'      => "<b>foo</b><i>bar</i>",
 
127
        'hash :: tag_sub'    => "<b>${def_b}foo</b><i>${def_i}bar</i>",
 
128
        'tag_sub :: undef'   => "<b>${b}foo</b><i>bar</i>",
 
129
        'tag_sub :: 0'       => "<b>${b}foo</b>${filt}bar${filt}",
 
130
        'tag_sub :: 1'       => "<b>${b}foo</b><i>bar</i>",
 
131
        'tag_sub :: sub'     => "<b>${b}foo</b><i>${def_i}bar</i>",
 
132
        'tag_sub :: hash'    => "<b>${b}foo</b><i>bar</i>",
 
133
        'tag_sub :: tag_0'   => "<b>${b}foo</b>${filt}bar${filt}",
 
134
        'tag_sub :: tag_1'   => "<b>${b}foo</b><i>bar</i>",
 
135
        'tag_sub :: tag_sub' => "<b>${b}foo</b><i>${def_i}bar</i>",
 
136
 
 
137
    );
 
138
}