Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
# 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 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