Re: [PATCH] Change implementation of %+ to use a proper tied hash interface and add...
[p5sagit/p5-mst-13.2.git] / ext / re / t / re_funcs.t
CommitLineData
de8c5301 1#!./perl
2
3BEGIN {
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
13use strict;
14
256ddcd0 15use Test::More; # test count at bottom of file
44a2ac75 16use re qw(is_regexp regexp_pattern regmust
17 regname regnames regnames_count
18 regnames_iterinit regnames_iternext);
de8c5301 19my $qr=qr/foo/i;
20
21ok(is_regexp($qr),'is_regexp($qr)');
22ok(!is_regexp(''),'is_regexp("")');
23is((regexp_pattern($qr))[0],'foo','regexp_pattern[0]');
24is((regexp_pattern($qr))[1],'i','regexp_pattern[1]');
25is(regexp_pattern($qr),'(?i-xsm:foo)','scalar regexp_pattern');
26ok(!regexp_pattern(''),'!regexp_pattern("")');
256ddcd0 27{
28 my $qr=qr/here .* there/x;
29 my ($anchored,$floating)=regmust($qr);
30 is($anchored,'here',"Regmust anchored - qr//");
31 is($floating,'there',"Regmust floating - qr//");
32 my $foo='blah';
33 ($anchored,$floating)=regmust($foo);
34 is($anchored,undef,"Regmust anchored - non ref");
35 is($floating,undef,"Regmust anchored - non ref");
36 my $bar=['blah'];
37 ($anchored,$floating)=regmust($foo);
38 is($anchored,undef,"Regmust anchored - ref");
39 is($floating,undef,"Regmust anchored - ref");
40}
41
44a2ac75 42
43if ('1234'=~/(?:(?<A>\d)|(?<C>!))(?<B>\d)(?<A>\d)(?<B>\d)/){
44 my $qr = qr/(?<foo>foo)(?<bar>bar)/;
45 my @names = sort +regnames($qr);
46 is("@names","","regnames");
47 @names = sort +regnames($qr,1);
48 is("@names","bar foo","regnames - all");
49 @names = sort +regnames();
50 is("@names","A B","regnames");
51 @names = sort +regnames(undef,1);
52 is("@names","A B C","regnames");
53 is(join("", @{regname("A",undef,1)}),"13");
54 is(join("", @{regname("B",undef,1)}),"24");
55 {
56 if ('foobar'=~/$qr/) {
57 regnames_iterinit();
58 my @res;
59 while (defined(my $key=regnames_iternext)) {
60 push @res,$key;
61 }
62 @res=sort @res;
63 is("@res","bar foo");
64 is(regnames_count(),2);
65 } else {
66 ok(0); ok(0);
67 }
68 }
69 is(regnames_count(),3);
70 is(regnames_count($qr),2);
71}
72{
73 use warnings;
74 require re::Tie::Hash::NamedCapture;
75 my $qr = qr/(?<foo>foo)/;
76 if ( 'foo' =~ /$qr/ ) {
77 tie my %hash,"re::Tie::Hash::NamedCapture",re => $qr;
78 if ('bar'=~/bar/) {
79 # last successful match is now different
80 is($hash{foo},'foo'); # prints foo
81 }
82 }
83}
256ddcd0 84# New tests above this line, don't forget to update the test count below!
44a2ac75 85use Test::More tests => 23;
256ddcd0 86# No tests here!