Skip to content

Commit

Permalink
Update Test::Mock for nom; it gets cleaner in various ways.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnthn committed Nov 19, 2011
1 parent 66d1046 commit 56318f8
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions lib/Test/Mock.pm
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ module Test::Mock {
sub mocked($type, :%returning = {}) is export {
# Generate a subclass that logs each method call.
my %already-seen = :new;
my $mocker = ClassHOW.new;
$mocker.^add_parent($type.WHAT);
for $type, $type.^parents() -> $p {
last if $p === Mu;
my $mocker := Metamodel::ClassHOW.new_type();
$mocker.HOW.add_parent($mocker, $type.WHAT);
for $type.^mro() -> $p {
last unless $p.^parents(:local);
for $p.^methods(:local) -> $m {
unless %already-seen{$m.name} {
$mocker.^add_method($m.name, method (|$c) {
$!log.log-method-call($m.name, $c);
$mocker.HOW.add_method($mocker, $m.name, method (|$c) {
self.'!mock-log'().log-method-call($m.name, $c);
%returning{$m.name} ~~ List ??
@(%returning{$m.name}) !!
%returning{$m.name}
Expand All @@ -63,16 +63,16 @@ module Test::Mock {
}
}

# Add log attribute and a method to access it.
$mocker.^add_attribute(Attribute.new( name => '$!log', has_accessor => False ));
$mocker.^add_method('!mock_log', method { $!log });
# Create a log and add a method to access it.
my $log := Test::Mock::Log.new();
$mocker.HOW.add_method($mocker, '!mock-log', method { $log });

# Return a mock object.
my $mocked = $mocker.^compose();
return $mocked.new(log => Test::Mock::Log.new());
# Return a mock object, setting the logger.
my $mocked = $mocker.HOW.compose($mocker);
$mocked.new()
}

sub check-mock($mock, *@checker) is export {
.($mock!mock_log) for @checker;
.($mock.'!mock-log'()) for @checker;
}
}

0 comments on commit 56318f8

Please sign in to comment.