Last active
March 7, 2016 21:15
-
-
Save soonix/cfa3dcbad2fc7ef3a264 to your computer and use it in GitHub Desktop.
Test for RegExp, grabbing first name - http://www.perlmonks.org/?node_id=1156197
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use 5.011; # implies strict + feature 'say' | |
use warnings; | |
use Test::More; | |
my %names = ( | |
# Test data | |
# input expected output | |
'BULLOCK JOE A' => 'JOE', | |
'SMITH, A DOE' => 'DOE', | |
'BULLOCK MICHAEL A' => 'MICHAEL', | |
# not specified by OP: | |
'SMITH ADAM' => 'ADAM', | |
'POCAHONTAS' => 'POCAHONTAS', | |
'TRAPPER JOHN M D' => 'JOHN', | |
); | |
my %approaches = ( | |
'hippo' => sub { | |
my ( $sname, $fname ) = shift =~ /([A-Z]{3,})/g; | |
return $fname || $sname; | |
}, | |
'Maresia' => sub { | |
my $string = shift; | |
if ( $string =~ /(\w{3,})$/ ) { | |
return $1; | |
} | |
elsif ( $string =~ /(\w+)\s(\w{1,2})$/ ) { | |
return $1; | |
} | |
}, | |
'kcott' => sub { | |
shift =~ /^[^, ]+(?:,\s+\w+|)\s+(\w+)/; | |
return $1; | |
}, | |
); | |
plan tests => scalar keys %approaches; | |
for my $who ( keys %approaches ) { | |
print "\nRunning tests for $who:\n\n"; | |
subtest( | |
$who, | |
sub { | |
plan tests => scalar keys %names; | |
my $get_forename = $approaches{$who}; | |
for my $fullname ( keys %names ) { | |
my $fname = $get_forename->($fullname); | |
no warnings 'uninitialized'; | |
is( $fname, $names{$fullname}, | |
"wanting forename '$names{$fullname}' from '$fullname'" ); | |
} | |
} | |
); | |
print "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment