Skip to content

Instantly share code, notes, and snippets.

@tobyink
Forked from anonymous/compile-slurpy-dict.pl
Last active May 5, 2017 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobyink/97fb6bef8d06e5c46eed596a27b90e67 to your computer and use it in GitHub Desktop.
Save tobyink/97fb6bef8d06e5c46eed596a27b90e67 to your computer and use it in GitHub Desktop.
# 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