Permalink
Browse files

Write tests for pointy blocks passed to subroutines. Update docs too.

  • Loading branch information...
1 parent 255d1c1 commit 3ee76ec234417e39263c96c9dc4d30cac4c75a58 @tadzik committed Jul 2, 2012
Showing with 65 additions and 1 deletion.
  1. +10 −0 README
  2. +10 −0 lib/Typed/Subroutines.pm
  3. +45 −1 t/01-basic.t
View
10 README
@@ -1,6 +1,8 @@
SYNOPSIS
use Typed::Subroutines;
+
+ # create a subtype from Sub
subset TwoArgSub of Sub where typed_sub(Any, Any);
subset TakesIntAndString of Sub where typed_sub(Int, Str);
@@ -13,6 +15,14 @@ SYNOPSIS
$b = sub (Int $a, Str $b) { ... }; # lives
$b = sub (Int $a, $b) { ... }; # dies
+ # validate subroutines passed to your subroutines (dawg)
+ sub doStuff(Int $a, Str $b, &operation where typed_sub(Int, Str)}) {
+ ...
+ }
+
+ doStuff(99, "bottles of beer", -> Int $a, Str $b { ... }) # lives
+ doStuff(99, "bottles of beer", -> Rat $a, Num $b { ... }) # dies
+
DESCRIPTION
Typed::Subroutines let you specify subroutine types verifying the parameter list so you can have better type checking for first-class functions.
View
@@ -12,6 +12,8 @@ sub typed_sub(*@types) is export {
=head1 SYNOPSIS
use Typed::Subroutines;
+
+ # create a subtype from Sub
subset TwoArgSub of Sub where typed_sub(Any, Any);
subset TakesIntAndString of Sub where typed_sub(Int, Str);
@@ -24,6 +26,14 @@ sub typed_sub(*@types) is export {
$b = sub (Int $a, Str $b) { ... }; # lives
$b = sub (Int $a, $b) { ... }; # dies
+ # validate subroutines passed to your subroutines (dawg)
+ sub doStuff(Int $a, Str $b, &operation where typed_sub(Int, Str)}) {
+ ...
+ }
+
+ doStuff(99, "bottles of beer", -> Int $a, Str $b { ... }) # lives
+ doStuff(99, "bottles of beer", -> Rat $a, Num $b { ... }) # dies
+
=head1 DESCRIPTION
Typed::Subroutines let you specify subroutine types verifying the
View
@@ -1,5 +1,5 @@
use Test;
-plan 9; # from outer space
+plan 17;
use Typed::Subroutines;
@@ -21,3 +21,47 @@ dies_ok { $b = sub (Int $a, $b) { } };
dies_ok { $b = sub ($a, Str $b) { } };
dies_ok { $b = sub (Str $a, Int $b) { } };
dies_ok { $b = sub (Int $a, Str $b, $c) { } };
+
+sub operateOnSomething(
+ Int $a,
+ Str $b,
+ &operation where typed_sub(Int, Str)
+) {
+ return &operation($a, $b)
+}
+
+sub test_op(Int $a, Str $b) {
+ return $a ~ $b
+}
+
+is test_op(5, " hundred miles"), '5 hundred miles',
+ 'test_op does what I meant';
+
+lives_ok { operateOnSomething(5, ' hundred miles', &test_op) },'lives 3';
+is operateOnSomething(5, ' hundred miles', &test_op), '5 hundred miles';
+
+lives_ok {
+ operateOnSomething(5, ' hundred miles', sub (Int $a, Str $b) {
+ return $a ~ $b
+ })
+}, 'anonymous sub works';
+
+my $pointy = -> Int $a, Str $b { return $a ~ $b };
+
+lives_ok { operateOnSomething(5, ' hundred miles', $pointy) },
+ 'pointy block works';
+
+lives_ok {
+ operateOnSomething(5, ' hundred miles', -> Int $a, Str $b {
+ return $a ~ $b
+ })
+}, 'anonymous pointy block works';
+
+$pointy = -> $x { "lalala" };
+
+dies_ok { operateOnSomething(5, ' hundred miles', $pointy) },
+ 'incorrect pointy dies';
+
+dies_ok {
+ operateOnSomething(99, "bottles of beer", -> Rat $a, Num $b { "a" })
+}, 'example from SYNOPSIS';

0 comments on commit 3ee76ec

Please sign in to comment.