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
4use 5.008001;
5
6use strict;
7use warnings;
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
14my $loaded = 0;
15BEGIN { $| = 1; print "1..19\n"; }
16END {print "not ok 1\n" unless $loaded;}
17use Text::Balanced qw ( extract_bracketed );
18$loaded = 1;
19print "ok 1\n";
20my $count=2;
21use vars qw( $DEBUG );
22sub debug { print "\t>>>",@_ if $DEBUG }
23
24######################### End of black magic.
25
26## no critic (BuiltinFunctions::ProhibitStringyEval)
27
28my $cmd = "print";
29my $neg = 0;
30my $str;
31while (defined($str = <DATA>))
32{
33    chomp $str;
34    if ($str =~ s/\A# USING://) { $neg = 0; $cmd = $str; next; }
35    elsif ($str =~ /\A# TH[EI]SE? SHOULD FAIL/) { $neg = 1; next; }
36    elsif (!$str || $str =~ /\A#/) { $neg = 0; next }
37    $str =~ s/\\n/\n/g;
38    debug "\tUsing: $cmd\n";
39    debug "\t   on: [$str]\n";
40
41    my $var = eval "() = $cmd";
42    debug "\t list got: [$var]\n";
43    debug "\t list left: [$str]\n";
44    print "not " if (substr($str,pos($str),1) eq ';')==$neg;
45    print "ok ", $count++;
46    print " ($@)" if $@ && $DEBUG;
47    print "\n";
48
49    pos $str = 0;
50    $var = eval $cmd;
51    $var = "<undef>" unless defined $var;
52    debug "\t scalar got: [$var]\n";
53    debug "\t scalar left: [$str]\n";
54    print "not " if ($str =~ '\A;')==$neg;
55    print "ok ", $count++;
56    print " ($@)" if $@ && $DEBUG;
57    print "\n";
58}
59
60__DATA__
61
62# USING: extract_bracketed($str);
63{a nested { and } are okay as are () and <> pairs and escaped \}'s };
64{a nested\n{ and } are okay as are\n() and <> pairs and escaped \}'s };
65
66# USING: extract_bracketed($str,'{}');
67{a nested { and } are okay as are unbalanced ( and < pairs and escaped \}'s };
68
69# THESE SHOULD FAIL
70{an unmatched nested { isn't okay, nor are ( and < };
71{an unbalanced nested [ even with } and ] to match them;
72
73
74# USING: extract_bracketed($str,'<"`q>');
75<a q{uoted} ">" unbalanced right bracket of /(q>)/ either sort (`>>>""">>>>`) is okay >;
76
77# USING: extract_bracketed($str,'<">');
78<a quoted ">" unbalanced right bracket is okay >;
79
80# USING: extract_bracketed($str,'<"`>');
81<a quoted ">" unbalanced right bracket of either sort (`>>>""">>>>`) is okay >;
82
83# THIS SHOULD FAIL
84<a misquoted '>' unbalanced right bracket is bad >;
85