This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Resolve PL_curpm issues with (??{}) and fix corruption of match results when pattern...
[perl5.git] / ext / re / t / re_funcs.t
1 #!./perl
2
3 BEGIN {
4         chdir 't' if -d 't';
5         @INC = '../lib';
6         require Config;
7         if (($Config::Config{'extensions'} !~ /\bre\b/) ){
8                 print "1..0 # Skip -- Perl configured without re module\n";
9                 exit 0;
10         }
11 }
12
13 use strict;
14
15 use Test::More; # test count at bottom of file
16 use re qw(is_regexp regexp_pattern regmust 
17           regname regnames regnames_count 
18           regnames_iterinit regnames_iternext);
19 {
20     my $qr=qr/foo/pi;
21     ok(is_regexp($qr),'is_regexp($qr)');
22     ok(!is_regexp(''),'is_regexp("")');
23     is((regexp_pattern($qr))[0],'foo','regexp_pattern[0]');
24     is((regexp_pattern($qr))[1],'ip','regexp_pattern[1]');
25     is(regexp_pattern($qr),'(?pi-xsm:foo)','scalar regexp_pattern');
26     ok(!regexp_pattern(''),'!regexp_pattern("")');
27 }
28 {
29     my $qr=qr/here .* there/x;
30     my ($anchored,$floating)=regmust($qr);
31     is($anchored,'here',"Regmust anchored - qr//");
32     is($floating,'there',"Regmust floating - qr//");
33     my $foo='blah';
34     ($anchored,$floating)=regmust($foo);
35     is($anchored,undef,"Regmust anchored - non ref");
36     is($floating,undef,"Regmust anchored - non ref");
37     my $bar=['blah'];
38     ($anchored,$floating)=regmust($foo);
39     is($anchored,undef,"Regmust anchored - ref");
40     is($floating,undef,"Regmust anchored - ref");
41 }
42
43
44 if ('1234'=~/(?:(?<A>\d)|(?<C>!))(?<B>\d)(?<A>\d)(?<B>\d)/){
45     my @names = sort +regnames();
46     is("@names","A B","regnames");
47     @names = sort +regnames(1);
48     is("@names","A B C","regnames");
49     is(join("", @{regname("A",1)}),"13");
50     is(join("", @{regname("B",1)}),"24");    
51     {
52         if ('foobar'=~/(?<foo>foo)(?<bar>bar)/) {
53             regnames_iterinit();
54             my @res;
55             while (defined(my $key=regnames_iternext)) {
56                 push @res,$key;
57             }
58             @res=sort @res;
59             is("@res","bar foo");
60             is(regnames_count(),2);
61         } else {
62             ok(0); ok(0);
63         }
64     }
65     is(regnames_count(),3);
66 }    
67 # New tests above this line, don't forget to update the test count below!
68 use Test::More tests => 19;
69 # No tests here!