This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate mainline
[perl5.git] / lib / Text / Balanced / t / xtagg.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..53\n"; }
15 END {print "not ok 1\n" unless $loaded;}
16 use Text::Balanced qw ( extract_tagged 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         if ($str =~ s/\A# USING://) { $neg = 0; $cmd = $str; next; }
32         elsif ($str =~ /\A# TH[EI]SE? SHOULD FAIL/) { $neg = 1; next; }
33         elsif (!$str || $str =~ /\A#/) { $neg = 0; next }
34         $str =~ s/\\n/\n/g;
35         debug "\tUsing: $cmd\n";
36         debug "\t   on: [$str]\n";
37
38         my @res;
39         $var = eval "\@res = $cmd";
40         debug "\t list got: [" . join("|",@res) . "]\n";
41         debug "\t list left: [$str]\n";
42         print "not " if (substr($str,pos($str)||0,1) eq ';')==$neg;
43         print "ok ", $count++;
44         print " ($@)" if $@ && $DEBUG;
45         print "\n";
46
47         pos $str = 0;
48         $var = eval $cmd;
49         $var = "<undef>" unless defined $var;
50         debug "\t scalar got: [$var]\n";
51         debug "\t scalar left: [$str]\n";
52         print "not " if ($str =~ '\A;')==$neg;
53         print "ok ", $count++;
54         print " ($@)" if $@ && $DEBUG;
55         print "\n";
56 }
57
58 __DATA__
59 # USING: gen_extract_tagged("BEGIN([A-Z]+)",'END$1',"(?s).*?(?=BEGIN)")->($str);
60         ignore\n this and then BEGINHERE at the ENDHERE;
61         ignore\n this and then BEGINTHIS at the ENDTHIS;
62
63 # USING: extract_tagged($str,"BEGIN([A-Z]+)",'END$1',"(?s).*?(?=BEGIN)");
64         ignore\n this and then BEGINHERE at the ENDHERE;
65         ignore\n this and then BEGINTHIS at the ENDTHIS;
66
67 # USING: extract_tagged($str,"BEGIN([A-Z]+)",'END$1',"(?s).*?(?=BEGIN)");
68         ignore\n this and then BEGINHERE at the ENDHERE;
69         ignore\n this and then BEGINTHIS at the ENDTHIS;
70
71 # THIS SHOULD FAIL
72         ignore\n this and then BEGINTHIS at the ENDTHAT;
73
74 # USING: extract_tagged($str,"BEGIN","END","(?s).*?(?=BEGIN)");
75         ignore\n this and then BEGIN at the END;
76
77 # USING: extract_tagged($str);
78         <A-1 HREF="#section2">some text</A-1>;
79
80 # USING: extract_tagged($str,qr/<[A-Z]+>/,undef, undef, {ignore=>["<BR>"]});
81         <A>aaa<B>bbb<BR>ccc</B>ddd</A>;
82
83 # USING: extract_tagged($str,"BEGIN","END");
84         BEGIN at the BEGIN keyword and END at the END;
85         BEGIN at the beginning and end at the END;
86
87 # USING: extract_tagged($str,undef,undef,undef,{ignore=>["<[^>]*/>"]});
88         <A>aaa<B>bbb<BR/>ccc</B>ddd</A>;
89
90 # USING: extract_tagged($str,";","-",undef,{reject=>[";"],fail=>"MAX"});
91         ; at the ;-) keyword
92
93 # USING: extract_tagged($str,"<[A-Z]+>",undef, undef, {ignore=>["<BR>"]});
94         <A>aaa<B>bbb<BR>ccc</B>ddd</A>;
95
96 # THESE SHOULD FAIL
97         BEGIN at the beginning and end at the end;
98         BEGIN at the BEGIN keyword and END at the end;
99
100 # TEST EXTRACTION OF TAGGED STRINGS
101 # USING: extract_tagged($str,"BEGIN","END",undef,{reject=>["BEGIN","END"]});
102 # THESE SHOULD FAIL
103         BEGIN at the BEGIN keyword and END at the end;
104
105 # USING: extract_tagged($str,";","-",undef,{reject=>[";"],fail=>"PARA"});
106         ; at the ;-) keyword
107
108
109 # USING: extract_tagged($str);
110         <A>some text</A>;
111         <B>some text<A>other text</A></B>;
112         <A>some text<A>other text</A></A>;
113         <A HREF="#section2">some text</A>;
114
115 # THESE SHOULD FAIL
116         <A>some text
117         <A>some text<A>other text</A>;
118         <B>some text<A>other text</B>;