File: test_ignore_elts.t

package info (click to toggle)
libxml-twig-perl 1%3A3.39-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 2,128 kB
  • sloc: perl: 19,329; xml: 202; makefile: 7
file content (116 lines) | stat: -rwxr-xr-x 2,353 bytes parent folder | download | duplicates (8)
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
#!/usr/bin/perl -w
use strict;

use XML::Twig;

$|=1;

my $TMAX=1; # do not forget to update!
print "1..$TMAX\n";

my $doc= read_data();

my $t= XML::Twig->new( ignore_elts => { ignore => 1 },
                       keep_spaces => 1,
		     );
my $result_file= "test_ignore_elt.res1";
open( RESULT, ">$result_file") or die "cannot create $result_file: $!";
select RESULT;
$t->parse( $doc);
$t->print;
select STDOUT;
close RESULT;
check_result( $result_file, 1);

exit 0;

# Not yet implemented

# test 2
$doc= read_data();

$t= XML::Twig->new( ignore_elts =>   { ignore => 'print' },
                    twig_handlers => { elt    => sub { $_->print; } },
                    keep_spaces => 1,
                  );
$result_file= "test_ignore_elt.res2";
open( RESULT, ">$result_file") or die "cannot create $result_file: $!";
select RESULT;
$t->parse( $doc);
$t->print;
select STDOUT;
close RESULT;
check_result( $result_file, 2);



sub read_data
  { local $/="\n\n";
    my $data= <DATA>;
    $data=~ s{^\s*#.*\n}{}m; # get rid of comments
    $data=~ s{\s*$}{}s;     # remove trailing spaces (and \n)
    $data=~  s{(^|\n)\s*(\n|$)}{}g;    # remove empty lines
    return $data;
  };

  
sub check_result
  { my( $result_file, $test_no)= @_;
    # now check result
    my $expected_result= read_data();
    my $result= read_result( $result_file);
    if( $result eq $expected_result)
      { print "ok $test_no\n"; }
    else
      { print "not ok $test_no\n"; 
        print STDERR "\ntest $test_no:\n",
	             "expected: \n$expected_result\n",
                     "real: \n$result\n";
      }
  }


sub read_result
  { my $file= shift;
    local $/="\n";
    open( RESULT, "<$file") or die "cannot read $file: $!";
    my @result= grep {m/\S/} <RESULT>;
    close RESULT;
    unlink $file;
    return join '', @result;
  }


  
__DATA__
# doc 1
<doc>
  <ignore>text<child ok="no"/></ignore>
  <elt>
    <child ok="yes"/>
    <ignore>text<child ok="no"/></ignore>
  </elt>
</doc>

# expected result 1
<doc>
  <elt>
    <child ok="yes"/>
  </elt>
</doc>

#doc 2
<doc>
  <ignore>text<child ok="no"/></ignore>
  <elt>
    <child ok="yes"/>
  </elt>
  <ignore>text<child ok="no"/></ignore>
</doc>

# expected result 2
  <ignore att="val">text<child ok="no"/></ignore>
  <elt>
    <child ok="yes"/>
  </elt>
  <ignore>text<child ok="no"/></ignore>