Skip to content

Instantly share code, notes, and snippets.

Created May 5, 2017 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/7ed268c6d3ede197ea462d44ed12931d to your computer and use it in GitHub Desktop.
Save anonymous/7ed268c6d3ede197ea462d44ed12931d to your computer and use it in GitHub Desktop.
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;
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