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