This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Part Integrate mainline
[perl5.git] / lib / Text / Balanced / t / gentag.t
1 # Before `make install' is performed this script should be runnable with
2 # `make test'. After `make install' it should work as `perl test.pl'
3
4 ######################### We start with some black magic to print on failure.
5
6 # Change 1..1 below to 1..last_test_to_print .
7 # (It may become useful if the test is moved to ./t subdirectory.)
8
9 BEGIN { $| = 1; print "1..35\n"; }
10 END {print "not ok 1\n" unless $loaded;}
11 use Text::Balanced qw ( gen_extract_tagged );
12 $loaded = 1;
13 print "ok 1\n";
14 $count=2;
15 use vars qw( $DEBUG );
16 sub debug { print "\t>>>",@_ if $DEBUG }
17
18 ######################### End of black magic.
19
20
21 $cmd = "print";
22 $neg = 0;
23 while (defined($str = <DATA>))
24 {
25         chomp $str;
26         $str =~ s/\\n/\n/g;
27         if ($str =~ s/\A# USING://)
28         {
29                 $neg = 0;
30                 eval{local$^W;*f = eval $str || die};
31                 next;
32         }
33         elsif ($str =~ /\A# TH[EI]SE? SHOULD FAIL/) { $neg = 1; next; }
34         elsif (!$str || $str =~ /\A#/) { $neg = 0; next }
35         $str =~ s/\\n/\n/g;
36         debug "\tUsing: $cmd\n";
37         debug "\t   on: [$str]\n";
38
39         my @res;
40         $var = eval { @res = f($str) };
41         debug "\t list got: [" . join("|",@res) . "]\n";
42         debug "\t list left: [$str]\n";
43         print "not " if (substr($str,pos($str)||0,1) eq ';')==$neg;
44         print "ok ", $count++;
45         print " ($@)" if $@ && $DEBUG;
46         print "\n";
47
48         pos $str = 0;
49         $var = eval { scalar f($str) };
50         $var = "<undef>" unless defined $var;
51         debug "\t scalar got: [$var]\n";
52         debug "\t scalar left: [$str]\n";
53         print "not " if ($str =~ '\A;')==$neg;
54         print "ok ", $count++;
55         print " ($@)" if $@ && $DEBUG;
56         print "\n";
57 }
58
59 __DATA__
60
61 # USING: gen_extract_tagged(qr/<[A-Z]+>/,undef, undef, {ignore=>["<BR>"]});
62         <A>aaa<B>bbb<BR>ccc</B>ddd</A>;
63
64 # USING: gen_extract_tagged("BEGIN","END");
65         BEGIN at the BEGIN keyword and END at the END;
66         BEGIN at the beginning and end at the END;
67
68 # USING: gen_extract_tagged(undef,undef,undef,{ignore=>["<[^>]*/>"]});
69         <A>aaa<B>bbb<BR/>ccc</B>ddd</A>;
70
71 # USING: gen_extract_tagged(";","-",undef,{reject=>[";"],fail=>"MAX"});
72         ; at the ;-) keyword
73
74 # USING: gen_extract_tagged("<[A-Z]+>",undef, undef, {ignore=>["<BR>"]});
75         <A>aaa<B>bbb<BR>ccc</B>ddd</A>;
76
77 # THESE SHOULD FAIL
78         BEGIN at the beginning and end at the end;
79         BEGIN at the BEGIN keyword and END at the end;
80
81 # TEST EXTRACTION OF TAGGED STRINGS
82 # USING: gen_extract_tagged("BEGIN","END",undef,{reject=>["BEGIN","END"]});
83 # THESE SHOULD FAIL
84         BEGIN at the BEGIN keyword and END at the end;
85
86 # USING: gen_extract_tagged(";","-",undef,{reject=>[";"],fail=>"PARA"});
87         ; at the ;-) keyword
88
89
90 # USING: gen_extract_tagged();
91         <A>some text</A>;
92         <B>some text<A>other text</A></B>;
93         <A>some text<A>other text</A></A>;
94         <A HREF="#section2">some text</A>;
95
96 # THESE SHOULD FAIL
97         <A>some text
98         <A>some text<A>other text</A>;
99         <B>some text<A>other text</B>;