Last active
August 29, 2015 14:16
-
-
Save dagolden/4a50f116072f82ebccea to your computer and use it in GitHub Desktop.
This file contains hidden or 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
use 5.008001; | |
use strict; | |
use warnings; | |
use Test::More 0.96; | |
use version; | |
my @cases = ( | |
[ "v1.2_3", "1.002003" ], | |
[ "1.002_003", "1.002003" ], | |
[ v1.2.3, "1.002_003" ], | |
[ v1.2_3, "1.002003" ], | |
[ 1.2.3_4, "1.002003004" ], | |
[ "1.2.3_4", "1.002003004" ], | |
[ "v1.2.3_4", "1.002003004" ], | |
[ "v1.2.3.4", "1.002003_004" ], | |
); | |
sub _find_magic_vstring { | |
my $value = shift; | |
my $tvalue = ''; | |
require B; | |
my $sv = B::svref_2object( \$value ); | |
my $magic = ref($sv) eq 'B::PVMG' ? $sv->MAGIC : undef; | |
while ($magic) { | |
if ( $magic->TYPE eq 'V' ) { | |
$tvalue = $magic->PTR; | |
$tvalue =~ s/^v?(.+)$/v$1/; | |
last; | |
} | |
else { | |
$magic = $magic->MOREMAGIC; | |
} | |
} | |
return $tvalue; | |
} | |
sub _clean_version { | |
my $v = shift; | |
# extract original from v-string literal | |
if ( my $magic = _find_magic_vstring($v) ) { | |
$v = $magic; | |
} | |
if ( substr( $v, 0, 1 ) eq 'v' || ( ( $v =~ tr/\.// ) > 1 ) ) { | |
$v =~ s/_/./g; | |
} | |
else { | |
$v =~ s/_//g; | |
} | |
return $v; | |
} | |
for my $c (@cases) { | |
my ( $v1, $v2 ) = map { version->new($_) } @$c; | |
ok( $v1 != $v2, "original: $v1 != $v2" ); | |
my ( $c1, $c2 ) = map { version->new( _clean_version($_) ) } @$c; | |
ok( $c1 == $c2, "cleaned: $c1 == $c2" ); | |
} | |
done_testing; | |
# vim: ts=4 sts=4 sw=4 et tw=75: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment