Skip to content

Commit

Permalink
- maybe_regex() now handles undef gracefully.
Browse files Browse the repository at this point in the history
    - maybe_regex() now handles 'm,foo,' style regexes.

Need this for Test::Legacy.
  • Loading branch information
schwern committed Dec 11, 2004
1 parent 637d28d commit b9e477a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
4 changes: 4 additions & 0 deletions Changes
@@ -1,3 +1,7 @@
0.55
- maybe_regex() now handles undef gracefully.
- maybe_regex() now handles 'm,foo,' style regexes.

0.54
- sort_bug.t wasn't checking for threads properly. Would fail on
5.6 that had ithreads compiled in. [rt.cpan.org 8765]
Expand Down
22 changes: 16 additions & 6 deletions lib/Test/Builder.pm
Expand Up @@ -8,7 +8,7 @@ $^C ||= 0;

use strict;
use vars qw($VERSION);
$VERSION = '0.21';
$VERSION = '0.21_01';
$VERSION = eval $VERSION; # make the alpha version come out as a number

# Make Test::Builder thread-safe for ithreads.
Expand Down Expand Up @@ -637,16 +637,26 @@ could be written as:


sub maybe_regex {
my ($self, $regex) = @_;
my ($self, $regex) = @_;
my $usable_regex = undef;

return $usable_regex unless defined $regex;

my($re, $opts);

# Check for qr/foo/
if( ref $regex eq 'Regexp' ) {
$usable_regex = $regex;
}
# Check if it looks like '/foo/'
elsif( my($re, $opts) = $regex =~ m{^ /(.*)/ (\w*) $ }sx ) {
# Check for '/foo/' or 'm,foo,'
elsif( ($re, $opts) = $regex =~ m{^ /(.*)/ (\w*) $ }sx or
(undef, $re, $opts) = $regex =~ m,^ m([^\w\s]) (.+) \1 (\w*) $,sx
)
{
$usable_regex = length $opts ? "(?$opts)$re" : $re;
};
return($usable_regex)
}

return $usable_regex;
};

sub _regex_ok {
Expand Down
10 changes: 9 additions & 1 deletion t/maybe_regex.t
Expand Up @@ -11,7 +11,7 @@ BEGIN {
}

use strict;
use Test::More tests => 10;
use Test::More tests => 13;

use Test::Builder;
my $Test = Test::Builder->new;
Expand Down Expand Up @@ -48,3 +48,11 @@ SKIP: {
ok(('f00' =~ m/$r/), '"//" good match');
ok(('b4r' !~ m/$r/), '"//" bad match');
};


{
my $r = $Test->maybe_regex('m,foo,i');
ok(defined $r, 'm,, detected');
ok(('fOO' =~ m/$r/), '"//" good match');
ok(('bar' !~ m/$r/), '"//" bad match');
};
7 changes: 6 additions & 1 deletion t/undef.t
Expand Up @@ -11,7 +11,7 @@ BEGIN {
}

use strict;
use Test::More tests => 14;
use Test::More tests => 16;
use TieOut;

BEGIN { $^W = 1; }
Expand Down Expand Up @@ -58,3 +58,8 @@ $tb->failure_output($old_fail);

is( $caught->read, "# undef\n" );
is( $warnings, '', 'diag(undef) no warnings' );


$tb->maybe_regex(undef);
is( $caught->read, '' );
is( $warnings, '', 'maybe_regex(undef) no warnings' );

0 comments on commit b9e477a

Please sign in to comment.