Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[crypt.pl] cannot move from a rod with no disks

  • Loading branch information...
commit efa9ba6fdb1a179af0c41c40f656a3fe40b8360e 1 parent 4b7c1d2
Carl Mäsak authored July 01, 2012

Showing 1 changed file with 20 additions and 0 deletions. Show diff stats Hide diff stats

  1. 20  crypt.pl
20  crypt.pl
@@ -34,6 +34,14 @@
34 34
     }
35 35
 }
36 36
 
  37
+class X::Hanoi::RodHasNoDisks is Exception {
  38
+    has $.name;
  39
+
  40
+    method message($_:) {
  41
+        "Cannot move from the {.name} rod because there is no disk there"
  42
+    }
  43
+}
  44
+
37 45
 class HanoiGame {
38 46
     my @names = map { "$_ disk" }, <tiny small medium big huge>;
39 47
     my %size_of = @names Z 1..5;
@@ -50,6 +58,8 @@
50 58
         die X::Hanoi::NoSuchRod.new(:rod<target>, :name($to))
51 59
             unless %!state.exists($to);
52 60
         my @from_rod := %!state{$from};
  61
+        die X::Hanoi::RodHasNoDisks.new(:name($from))
  62
+            unless @from_rod;
53 63
         my @to_rod   := %!state{$to};
54 64
         my $moved_disk = @from_rod[*-1];
55 65
         if @to_rod {
@@ -131,5 +141,15 @@ (&code, $ex_type, &followup?)
131 141
                '.message attribute';
132 142
         };
133 143
 
  144
+    throws_exception
  145
+        { $game.move('right', 'middle') },
  146
+        X::Hanoi::RodHasNoDisks,
  147
+        {
  148
+            is .name, 'right', '.name attribute';
  149
+            is .message,
  150
+               q[Cannot move from the right rod because there is no disk there],
  151
+               '.message attribute';
  152
+        };
  153
+
134 154
     done;
135 155
 }

0 notes on commit efa9ba6

Please sign in to comment.
Something went wrong with that request. Please try again.