/
advent2011-day24.t
67 lines (54 loc) 路 1.49 KB
/
advent2011-day24.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use v6.c;
# http://perl6advent.wordpress.com/2011/12/24/day-24-subs-are-always-better-in-multi-ples/
use Test;
plan 8;
multi sub steve(Str $name) {
return "Hello, $name";
}
multi sub steve(Int $number) {
return "You are person number $number to use this sub!";
}
is steve("John"), "Hello, John", 'multi';
is steve(35), "You are person number 35 to use this sub!", 'multi';
# Advent post is checking a user defined sub against a built in.
# This just tests two user defined subs that have been declared
# at the same level.
my $unambiguous = q:to'--END--';
proto sub Slurp(|) { * }
multi sub Slurp($filename) {
pass "Yum! $filename was tasty. Got another one?";
}
--END--
my $ambiguous = $unambiguous ~ q:to'--END--';
multi sub Slurp($filename) {
fail "Yuck! $filename is no good!";
}
--END--
lives-ok { EVAL $unambiguous ~ 'Slurp('~$?FILE.perl~')' },
'unambiguous multi - lives';
throws-like { EVAL $ambiguous ~ 'Slurp('~$?FILE.perl~')' },
X::Multi::Ambiguous,
'ambiguous multi - dies';
class Present {
has $.item;
has $.iswrapped = True;
method look() {
if $.iswrapped {
"It's wrapped.";
}
else {
"A $.item!";
}
}
method unwrap() {
$!iswrapped = False;
}
}
multi sub open(Present $present) {
$present.unwrap;
pass "You unwrap the present and find...!";
}
my $gift = Present.new(:item("sock"));
is $gift.look, "It's wrapped.", "It's wrapped.";
open($gift);
is $gift.look, 'A sock!', 'A sock!';