Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

  • Loading branch information...
commit 4b7c1d2dd1301e7f09d8c105fe38260431a5d074 1 parent b0affd2
@masak authored
Showing with 35 additions and 0 deletions.
  1. +35 −0 crypt.pl
View
35 crypt.pl
@@ -25,6 +25,15 @@
}
}
+class X::Hanoi::NoSuchRod is Exception {
+ has $.rod;
+ has $.name;
+
+ method message($_:) {
+ "No such {.rod} rod '{.name}'"
+ }
+}
+
class HanoiGame {
my @names = map { "$_ disk" }, <tiny small medium big huge>;
my %size_of = @names Z 1..5;
@@ -36,6 +45,10 @@
;
method move($from, $to) {
+ die X::Hanoi::NoSuchRod.new(:rod<source>, :name($from))
+ unless %!state.exists($from);
+ die X::Hanoi::NoSuchRod.new(:rod<target>, :name($to))
+ unless %!state.exists($to);
my @from_rod := %!state{$from};
my @to_rod := %!state{$to};
my $moved_disk = @from_rod[*-1];
@@ -96,5 +109,27 @@ (&code, $ex_type, &followup?)
'.message attribute';
};
+ throws_exception
+ { $game.move('gargle', 'middle') },
+ X::Hanoi::NoSuchRod,
+ {
+ is .rod, 'source', '.rod attribute';
+ is .name, 'gargle', '.name attribute';
+ is .message,
+ q[No such source rod 'gargle'],
+ '.message attribute';
+ };
+
+ throws_exception
+ { $game.move('middle', 'clown') },
+ X::Hanoi::NoSuchRod,
+ {
+ is .rod, 'target', '.rod attribute';
+ is .name, 'clown', '.name attribute';
+ is .message,
+ q[No such target rod 'clown'],
+ '.message attribute';
+ };
+
done;
}
Please sign in to comment.
Something went wrong with that request. Please try again.