File: test_new_features_3_18.t

package info (click to toggle)
libxml-twig-perl 1%3A3.52-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 4,952 kB
  • sloc: perl: 21,221; xml: 423; makefile: 9
file content (136 lines) | stat: -rwxr-xr-x 4,785 bytes parent folder | download | duplicates (7)
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
#!/usr/bin/perl -w
use strict;

use Carp;

use File::Spec;
use lib File::Spec->catdir(File::Spec->curdir,"t");
use tools;

use XML::Twig;

my $DEBUG=0;
print "1..44\n";

{ # test tag regexp handler
  my @res;
  my $doc=q{<doc><foo_f1/><foo_f2/><foo_f1/><foo_f3/><afoo_f1/><FOO_f4/></doc>};
  my $handlers= { qr/^foo_/ => sub { push @res, $_->tag; },
                  foo_f2    => sub { push @res, uc $_->tag; 0 },
                };
  my $expected= 'foo_f1:FOO_F2:foo_f1:foo_f3';
  XML::Twig->new( twig_handlers => $handlers)->parse( $doc);
  my $res= join( ':', @res);
  is( $res, $expected, "tag regexp handlers");
}

{ # test tag regexp handler with i modifier
  my @res;
  my $doc=q{<doc><foo_f1/><foo_f2/><foo_f1/><foo_f3/><afoo_f1/><FOO_f4/></doc>};
  my $handlers= { qr/^foo_/i => sub { push @res, $_->tag; },
                  foo_f2     => sub { push @res, uc $_->tag; 0 },
                };
  my $expected= 'foo_f1:FOO_F2:foo_f1:foo_f3:FOO_f4';
  XML::Twig->new( twig_handlers => $handlers)->parse( $doc);
  my $res= join( ':', @res);
  is( $res, $expected, "tag regexp handlers");
}

{ # test tag regexp handler with all modifier
  my @res;
  my $doc=q{<doc><foo_f1/><foo_f2/><foo_f1/><foo_f3/><afoo_f1/><FOO_f4/></doc>};
  my $handlers= { qr/^foo_/xism => sub { push @res, $_->tag; },
                  foo_f2     => sub { push @res, uc $_->tag; 0 },
                };
  my $expected= 'foo_f1:FOO_F2:foo_f1:foo_f3:FOO_f4';
  XML::Twig->new( twig_handlers => $handlers)->parse( $doc);
  my $res= join( ':', @res);
  is( $res, $expected, "tag regexp handlers");
}


{ # testing last_descendant
  my $t= XML::Twig->new->parse( '<doc id="doc">
                                   <e3 id="e3">t_e_3</e3>
                                   <e4 id="e4" />
                                   <e id="e1">t_e_1</e>
                                   <e id="e2">t_e_2<n id="n1">t_n</n></e>
                                 </doc>
                                '
                              );
  my %exp2id= ( ''            => 't_n',
                'n'           => 'n1',
                '#ELT'        => 'n1',
                'e'           => 'e2',
                'e[@id="e1"]' => 'e1',
                'e2'          => undef,
              );
  foreach my $exp (sort keys %exp2id)
    { my $expected= $exp2id{$exp};
      is( result( $t->last_elt( $exp)), $expected, "last_elt( $exp)");
      is( result( $t->root->last_descendant( $exp)), $expected, "last_descendant( $exp)");
    }

  # some more tests to check that we stay in te subtree and that we get the last descendant if it is itself
  is( result( $t->last_elt( 'e3')), 'e3', 'last_elt( e3)');
  is( result( $t->root->last_descendant( 'e3')), 'e3', 'last_descendant( e3)');
  is( result( $t->root->first_child( 'e3')->last_descendant( 'e3')), 'e3', 'last_descendant( e3) (on e3)');
  is( result( $t->root->first_child( 'e3')->last_descendant()), 't_e_3', 'last_descendant() (on e3)');
  is_undef( $t->root->last_child->last_descendant( 'e3'), 'last_descendant (no result)');

  is( result( $t->root->first_child( 'e4')->last_descendant( 'e4')), 'e4', 'last_descendant( e4) (on e4)');
  is( result( $t->root->first_child( 'e4')->last_descendant( )), 'e4', 'last_descendant( ) (on e4)');

  sub result
    { my( $elt)= @_;
      return undef unless $elt;
      return $elt->id || $elt->text;
    }
}        

{# testing trim
  my $expected;
  while( <DATA>)
    { chomp;
      next unless( m{\S});
      if( s{^#}{}) { $expected= $_; }
      is( XML::Twig->new->parse( $_)->trim->root->sprint, $expected, "trimming '$_'");
    }
}

{ # testing children_trimmed_text
  my $t = XML::Twig->new; 
  $t->parse("<o><e> hell </e><i> foo </i><e> o, \n   world</e></o>"); 
  is( join( ':', $t->root->children_trimmed_text("e")), "hell:o, world" , "children_trimmed_text (list context)");
  my $scalar= $t->root->children_trimmed_text("e");
  is( $scalar, "hello, world" , "children_trimmed_text (scalar context)");
  is( join( ':', $t->root->children_text("e")), " hell : o, \n   world" , "children_text (list context)");
  $scalar= $t->root->children_text("e");
  is( $scalar, " hell  o, \n   world" , "children_text (scalar context)");
}


__DATA__
#<doc>text1 text2</doc>
<doc>  text1 text2</doc>
<doc>   text1 text2</doc>
<doc>text1 text2 </doc>
<doc>text1 text2  </doc>
<doc>text1 text2   </doc>
<doc>text1  text2</doc>
<doc> text1  text2 </doc>
<doc>  text1   text2  </doc>

#<doc>text1 <e>text2</e> text3</doc>
<doc>text1  <e>text2</e> text3 </doc>


#<doc>text1 <e> text2 </e> text3</doc>
<doc>text1  <e>  text2  </e>  text3 </doc>

#<doc><![CDATA[text1 text2]]></doc>
<doc> <![CDATA[text1  text2]]> </doc>
<doc><![CDATA[ text1  text2 ]]></doc>

#<doc>text <b> hah! </b> yep</doc>
<doc>  text <b>  hah! </b>  yep</doc>