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 add a disk with a made-up name

  • Loading branch information...
commit 41501d67f9e1c8e948ce155d29d0d1a9e62cdb68 1 parent 89ce144
Carl Mäsak authored July 01, 2012

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

  1. 21  crypt.pl
21  crypt.pl
@@ -90,6 +90,14 @@
90 90
     }
91 91
 }
92 92
 
  93
+class X::Hanoi::NoSuchDisk is Exception {
  94
+    has $.disk;
  95
+
  96
+    method message($_:) {
  97
+        "Cannot add a {.disk} because there is no such disk"
  98
+    }
  99
+}
  100
+
93 101
 class Hanoi::Game {
94 102
     my @disks = map { "$_ disk" }, <tiny small medium large huge>;
95 103
     my %size_of = @disks Z 1..5;
@@ -149,6 +157,8 @@
149 157
     }
150 158
 
151 159
     method add($disk, $target) {
  160
+        die X::Hanoi::NoSuchDisk.new(:$disk)
  161
+            unless $disk eq any(@disks);
152 162
         my $size = $disk.words[0];
153 163
         my @events = Hanoi::DiskAdded.new(:$size, :$target);
154 164
         self!apply($_) for @events;
@@ -433,6 +443,17 @@ (&code, $ex_type, $message, &followup?)
433 443
         is $game.add('tiny disk', 'left'),
434 444
            Hanoi::DiskAdded.new(:size<tiny>, :target<left>),
435 445
            'adding a disk (+)';
  446
+
  447
+        throws_exception
  448
+            { $game.add('humongous disk', 'middle') },
  449
+            X::Hanoi::NoSuchDisk,
  450
+            'adding a disk (-) there is no such disk',
  451
+            {
  452
+                is .disk, 'humongous disk', '.disk attribute';
  453
+                is .message,
  454
+                    'Cannot add a humongous disk because there is no such disk',
  455
+                    '.message attribute';
  456
+            };
436 457
     }
437 458
 
438 459
     done;

0 notes on commit 41501d6

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