1N/ABEGIN {
1N/A if ($ENV{PERL_CORE}) {
1N/A chdir('t') if -d 't';
1N/A @INC = qw(../lib);
1N/A }
1N/A}
1N/A
1N/A# Before `make install' is performed this script should be runnable with
1N/A# `make test'. After `make install' it should work as `perl test.pl'
1N/A
1N/A######################### We start with some black magic to print on failure.
1N/A
1N/A# Change 1..1 below to 1..last_test_to_print .
1N/A# (It may become useful if the test is moved to ./t subdirectory.)
1N/A
1N/ABEGIN { $| = 1; print "1..53\n"; }
1N/AEND {print "not ok 1\n" unless $loaded;}
1N/Ause Text::Balanced qw ( extract_tagged gen_extract_tagged );
1N/A$loaded = 1;
1N/Aprint "ok 1\n";
1N/A$count=2;
1N/Ause vars qw( $DEBUG );
1N/Asub debug { print "\t>>>",@_ if $DEBUG }
1N/A
1N/A######################### End of black magic.
1N/A
1N/A
1N/A$cmd = "print";
1N/A$neg = 0;
1N/Awhile (defined($str = <DATA>))
1N/A{
1N/A chomp $str;
1N/A if ($str =~ s/\A# USING://) { $neg = 0; $cmd = $str; next; }
1N/A elsif ($str =~ /\A# TH[EI]SE? SHOULD FAIL/) { $neg = 1; next; }
1N/A elsif (!$str || $str =~ /\A#/) { $neg = 0; next }
1N/A $str =~ s/\\n/\n/g;
1N/A debug "\tUsing: $cmd\n";
1N/A debug "\t on: [$str]\n";
1N/A
1N/A my @res;
1N/A $var = eval "\@res = $cmd";
1N/A debug "\t list got: [" . join("|",@res) . "]\n";
1N/A debug "\t list left: [$str]\n";
1N/A print "not " if (substr($str,pos($str)||0,1) eq ';')==$neg;
1N/A print "ok ", $count++;
1N/A print " ($@)" if $@ && $DEBUG;
1N/A print "\n";
1N/A
1N/A pos $str = 0;
1N/A $var = eval $cmd;
1N/A $var = "<undef>" unless defined $var;
1N/A debug "\t scalar got: [$var]\n";
1N/A debug "\t scalar left: [$str]\n";
1N/A print "not " if ($str =~ '\A;')==$neg;
1N/A print "ok ", $count++;
1N/A print " ($@)" if $@ && $DEBUG;
1N/A print "\n";
1N/A}
1N/A
1N/A__DATA__
1N/A# USING: gen_extract_tagged("BEGIN([A-Z]+)",'END$1',"(?s).*?(?=BEGIN)")->($str);
1N/A ignore\n this and then BEGINHERE at the ENDHERE;
1N/A ignore\n this and then BEGINTHIS at the ENDTHIS;
1N/A
1N/A# USING: extract_tagged($str,"BEGIN([A-Z]+)",'END$1',"(?s).*?(?=BEGIN)");
1N/A ignore\n this and then BEGINHERE at the ENDHERE;
1N/A ignore\n this and then BEGINTHIS at the ENDTHIS;
1N/A
1N/A# USING: extract_tagged($str,"BEGIN([A-Z]+)",'END$1',"(?s).*?(?=BEGIN)");
1N/A ignore\n this and then BEGINHERE at the ENDHERE;
1N/A ignore\n this and then BEGINTHIS at the ENDTHIS;
1N/A
1N/A# THIS SHOULD FAIL
1N/A ignore\n this and then BEGINTHIS at the ENDTHAT;
1N/A
1N/A# USING: extract_tagged($str,"BEGIN","END","(?s).*?(?=BEGIN)");
1N/A ignore\n this and then BEGIN at the END;
1N/A
1N/A# USING: extract_tagged($str);
1N/A <A-1 HREF="#section2">some text</A-1>;
1N/A
1N/A# USING: extract_tagged($str,qr/<[A-Z]+>/,undef, undef, {ignore=>["<BR>"]});
1N/A <A>aaa<B>bbb<BR>ccc</B>ddd</A>;
1N/A
1N/A# USING: extract_tagged($str,"BEGIN","END");
1N/A BEGIN at the BEGIN keyword and END at the END;
1N/A BEGIN at the beginning and end at the END;
1N/A
1N/A# USING: extract_tagged($str,undef,undef,undef,{ignore=>["<[^>]*/>"]});
1N/A <A>aaa<B>bbb<BR/>ccc</B>ddd</A>;
1N/A
1N/A# USING: extract_tagged($str,";","-",undef,{reject=>[";"],fail=>"MAX"});
1N/A ; at the ;-) keyword
1N/A
1N/A# USING: extract_tagged($str,"<[A-Z]+>",undef, undef, {ignore=>["<BR>"]});
1N/A <A>aaa<B>bbb<BR>ccc</B>ddd</A>;
1N/A
1N/A# THESE SHOULD FAIL
1N/A BEGIN at the beginning and end at the end;
1N/A BEGIN at the BEGIN keyword and END at the end;
1N/A
1N/A# TEST EXTRACTION OF TAGGED STRINGS
1N/A# USING: extract_tagged($str,"BEGIN","END",undef,{reject=>["BEGIN","END"]});
1N/A# THESE SHOULD FAIL
1N/A BEGIN at the BEGIN keyword and END at the end;
1N/A
1N/A# USING: extract_tagged($str,";","-",undef,{reject=>[";"],fail=>"PARA"});
1N/A ; at the ;-) keyword
1N/A
1N/A
1N/A# USING: extract_tagged($str);
1N/A <A>some text</A>;
1N/A <B>some text<A>other text</A></B>;
1N/A <A>some text<A>other text</A></A>;
1N/A <A HREF="#section2">some text</A>;
1N/A
1N/A# THESE SHOULD FAIL
1N/A <A>some text
1N/A <A>some text<A>other text</A>;
1N/A <B>some text<A>other text</B>;