Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
adding 2010 advent day 14
  • Loading branch information
dwarring committed May 16, 2014
1 parent eeac0ec commit 7e38376
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions integration/advent2010-day14.t
@@ -0,0 +1,87 @@
# http://perl6advent.wordpress.com/2010/12/14/day-14-nextsame-and-its-cousins/
use v6;
use Test;
plan 5;

class A {
method sing {
say "life is but a dream.";
}
}

class B is A {
method sing {
say ("merrily," xx 4).join(" ");
nextsame;
}
}

class C is B {
method sing {
say "row, row, row your boat,";
say "gently down the stream.";
nextsame;
}
}

sub capture-said($code) {
my $output;
temp $*OUT = class {
method print(*@args) {
$output ~= @args.join;
}
}
$code();
return $output.lines;
}


my @out = capture-said { C.new.sing };

is_deeply @out, [
'row, row, row your boat,',
'gently down the stream.',
'merrily, merrily, merrily, merrily,',
'life is but a dream.'], 'nextsame inheritance';

sub bray {
say "EE-I-EE-I-OO.";
}

# Oh right, forgot to add the first line of the song...
&bray.wrap( {
say "Old MacDonald had a farm,";
nextsame;
} );

@out = capture-said {
bray();
};

is_deeply @out, [
"Old MacDonald had a farm,",
"EE-I-EE-I-OO."], 'nextsame wrapping';

multi foo( $x) { say "Any argument" }
multi foo(Int $x) { say "Int argument" }

@out = capture-said({foo(42)});
is_deeply @out, ['Int argument'], 'multisub sanity';

class A1 {
method foo { "OH HAI" }
}

role LogFoo {
method foo {
say ".foo was called";
nextsame;
}
}

my $logged_A = A1.new but LogFoo;

my $result;
@out = capture-said {$result = $logged_A.foo};
is $result, 'OH HAI';
is_deeply @out, ['.foo was called'], 'nextsame mixin';

0 comments on commit 7e38376

Please sign in to comment.