Created
May 5, 2017 09:38
-
-
Save anonymous/7ed268c6d3ede197ea462d44ed12931d to your computer and use it in GitHub Desktop.
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
use v5.10; | |
use strict; | |
use warnings; | |
use Test::More; | |
use Test::Fatal; | |
use Types::Standard -all; | |
use Type::Params -all; | |
use Data::Dumper; | |
sub format_iso_date { | |
state $check = compile( slurpy Dict[ year=>Int, month=>Int, day=>Int ] ); | |
my ($args) = $check->(@_); | |
return sprintf( | |
'%04d-%02d-%02d', | |
$args->{year}, | |
$args->{month}, | |
$args->{day}, | |
); | |
} | |
is( | |
format_iso_date( year=>2017, month=>5, day=>5 ), | |
'2017-05-05', | |
'format_iso_date works', | |
); | |
use Math::Trig qw(pi); | |
like( | |
exception { format_iso_date( year=>2017, month=>pi, day=>5 ) }, | |
qr/Reference .+ did not pass type constraint "Dict.+"/, | |
'months cannot be transcendental numbers', | |
); | |
done_testing; |
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
use v5.10; | |
use strict; | |
use warnings; | |
use Test::More; | |
use Test::Fatal; | |
use Types::Standard -all; | |
use Type::Params -all; | |
use Data::Dumper; | |
sub format_iso_date { | |
state $check = compile_named( year=>Int, month=>Int, day=>Int ); | |
my $args = $check->(@_); | |
return sprintf( | |
'%04d-%02d-%02d', | |
$args->{year}, | |
$args->{month}, | |
$args->{day}, | |
); | |
} | |
is( | |
format_iso_date( year=>2017, month=>5, day=>5 ), | |
'2017-05-05', | |
'format_iso_date works', | |
); | |
is( | |
format_iso_date( { year=>2017, month=>5, day=>5 } ), | |
'2017-05-05', | |
'... and accepts hashrefs', | |
); | |
use Math::Trig qw(pi); | |
like( | |
exception { format_iso_date( year=>2017, month=>pi, day=>5 ) }, | |
qr/Value "3.14159.+" did not pass type constraint "Int"/, | |
'months cannot be transcendental numbers', | |
); | |
done_testing; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment