-
-
Save tobyink/97fb6bef8d06e5c46eed596a27b90e67 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
# This is the old way to do it. | |
use v5.10; | |
use strict; | |
use warnings; | |
use Test::More; | |
use Test::Fatal; | |
use Types::Standard -all; | |
use Type::Params -all; | |
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
# This is the new way to do it. | |
use v5.10; | |
use strict; | |
use warnings; | |
use Test::More; | |
use Test::Fatal; | |
use Types::Standard -all; | |
use Type::Params -all; | |
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