File: test_class_selector.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 (65 lines) | stat: -rwxr-xr-x 1,637 bytes parent folder | download | duplicates (6)
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
#!/usr/bin/perl

use strict;
use warnings;

use XML::Twig;

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

my @DATA;
while( <DATA>) { chomp; my( $cond, $expected)= split /\s*=>\s*/; push @DATA, [$cond, $expected]; }

my $TMAX= 20;

print "1..$TMAX\n";

my $doc=q{<d><e class="c1">e1</e><e class="c1 c2" a="v1">e2</e><e class="c2" a="v2">e3</e></d>};
my $doc_dot=q{<d><e class="c1">wrong e1</e><e class="c1 c2" a="v1">wrong e2</e><e class="c2" a="v2">wrong e3</e><e.c1>e1</e.c1><e.c1 a="v1">e2</e.c1><e.c2 a="v2">e3</e.c2></d>};

my $t= XML::Twig->parse( $doc);

foreach my $test (@DATA)
  { my( $cond, $expected)= @$test;
    my $got= join '', map { $_->text } $t->root->children( $cond);
    is( $got, $expected, "navigation: $cond" );
  }

if( $] > 5.008)
{
foreach my $test (@DATA)
  { my( $cond, $expected)= @$test;
    my $got='';
    XML::Twig->new( twig_handlers => { $cond => sub { $got.= $_->text } },
                    css_sel => 1,
                  )
             ->parse( $doc);
    is( $got, $expected, "handlers (css_sel enabled): $cond" ); 
  }

foreach my $test (@DATA)
  { my( $cond, $expected)= @$test;
    next if $cond !~ m{^e};
    my $got='';
    XML::Twig->new( twig_handlers => { $cond => sub { $got.= $_->text } },)
             ->parse( $doc_dot);
    is( $got, $expected, "handlers (css_sel NOT enabled): $cond" );
  } 
}
else
{ skip( 12, 'not tested under perl < 5.8'); }




__DATA__
e.c1           => e1e2
e.c1[@a="v1"]  => e2
e.c1[@a]       => e2
e.c1[@a="v2"]  => 
*.c1[@a="v1"]  => e2
*.c1[@a="v2" or @a="v1"]  => e2
.c1[@a="v1"]  => e2
.c1[@a="v2" or @a="v1"]  => e2