Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[crypt.pl] can't use rod names which don't exist

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

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

  1. 35  crypt.pl
35  crypt.pl
@@ -25,6 +25,15 @@
25 25
     }
26 26
 }
27 27
 
  28
+class X::Hanoi::NoSuchRod is Exception {
  29
+    has $.rod;
  30
+    has $.name;
  31
+
  32
+    method message($_:) {
  33
+        "No such {.rod} rod '{.name}'"
  34
+    }
  35
+}
  36
+
28 37
 class HanoiGame {
29 38
     my @names = map { "$_ disk" }, <tiny small medium big huge>;
30 39
     my %size_of = @names Z 1..5;
@@ -36,6 +45,10 @@
36 45
     ;
37 46
 
38 47
     method move($from, $to) {
  48
+        die X::Hanoi::NoSuchRod.new(:rod<source>, :name($from))
  49
+            unless %!state.exists($from);
  50
+        die X::Hanoi::NoSuchRod.new(:rod<target>, :name($to))
  51
+            unless %!state.exists($to);
39 52
         my @from_rod := %!state{$from};
40 53
         my @to_rod   := %!state{$to};
41 54
         my $moved_disk = @from_rod[*-1];
@@ -96,5 +109,27 @@ (&code, $ex_type, &followup?)
96 109
                '.message attribute';
97 110
         };
98 111
 
  112
+    throws_exception
  113
+        { $game.move('gargle', 'middle') },
  114
+        X::Hanoi::NoSuchRod,
  115
+        {
  116
+            is .rod, 'source', '.rod attribute';
  117
+            is .name, 'gargle', '.name attribute';
  118
+            is .message,
  119
+               q[No such source rod 'gargle'],
  120
+               '.message attribute';
  121
+        };
  122
+
  123
+    throws_exception
  124
+        { $game.move('middle', 'clown') },
  125
+        X::Hanoi::NoSuchRod,
  126
+        {
  127
+            is .rod, 'target', '.rod attribute';
  128
+            is .name, 'clown', '.name attribute';
  129
+            is .message,
  130
+               q[No such target rod 'clown'],
  131
+               '.message attribute';
  132
+        };
  133
+
99 134
     done;
100 135
 }

0 notes on commit 4b7c1d2

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