Skip to content

Commit

Permalink
Implement and_list_return and and_stub_list_return method.
Browse files Browse the repository at this point in the history
  • Loading branch information
NetPenguin committed Sep 28, 2013
1 parent 4ad8825 commit 28bad64
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ Revision history for Perl extension Test-EasyMock

{{$NEXT}}

- Add 'add_list_return' and 'add_stub_list_return' method at ExpectationsSetter.

0.09 2013-08-18T02:59:06Z

- Fix POD and Changes.
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ and a result of the invocation is _(1, 2, 3)_.

Add array result to the expectation.

- and\_list\_return(@values)

Add list result to the expectation.

- and\_die(\[$message\])

Add _die_ behavior to the expectation.
Expand All @@ -141,6 +145,10 @@ and a result of the invocation is _(1, 2, 3)_.

Set array result as a stub to the expectation.

- and\_stub\_list\_return(@values)

Set list result as a stub to the expectation.

- and\_stub\_die(\[$message\])

Set _die_ behavior as as stub to the expectation.
Expand Down
8 changes: 8 additions & 0 deletions lib/Test/EasyMock.pm
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ Add scalar result to the expectation.
Add array result to the expectation.
=item and_list_return(@values)
Add list result to the expectation.
=item and_die([$message])
Add I<die> behavior to the expectation.
Expand All @@ -165,6 +169,10 @@ Set scalar result as a stub to the expectation.
Set array result as a stub to the expectation.
=item and_stub_list_return(@values)
Set list result as a stub to the expectation.
=item and_stub_die([$message])
Set I<die> behavior as as stub to the expectation.
Expand Down
22 changes: 22 additions & 0 deletions lib/Test/EasyMock/ExpectationSetters.pm
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ sub and_array_return {
return $self;
}

=head2 and_list_return(@values)
Add list result to the expectation.
=cut
sub and_list_return {
my ($self, @list) = @_;
$self->{_expectaion}->push_result(sub { wantarray ? @list : $list[-1] });
return $self;
}

=head2 and_die([$message])
Add I<die> behavior to the expectation.
Expand Down Expand Up @@ -79,6 +90,17 @@ sub and_stub_array_return {
return $self;
}

=head2 and_stub_list_return(@values)
Set list result as a stub to the expectation.
=cut
sub and_stub_list_return {
my ($self, @list) = @_;
$self->{_expectaion}->set_stub_result(sub { wantarray ? @list : $list[-1] });
return $self;
}

=head2 and_stub_die([$message])
Set I<die> behavior as as stub to the expectation.
Expand Down
100 changes: 99 additions & 1 deletion t/01.total.t
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,32 @@ subtest 'default mock' => sub {

reset($mock);

subtest 'expect and_list_return in list context' => sub {
my ($result1, $result2, $result3) = qw(result1 result2 result3);
expect($mock->foo)->and_list_return($result1, $result2, $result3);
replay($mock);

my @actual = $mock->foo;

is_deeply(\@actual, [$result1, $result2, $result3], 'result');
verify($mock);
};

reset($mock);

subtest 'expect and_list_return in scalar context' => sub {
my ($result1, $result2, $result3) = qw(result1 result2 result3);
expect($mock->foo)->and_list_return($result1, $result2, $result3);
replay($mock);

my $actual = $mock->foo;

is($actual, $result3, 'result');
verify($mock);
};

reset($mock);

subtest 'with Test::Deep comparison parameter' => sub {
my $result1 = 'a result of first.';
my $result2 = 'a result of sencond.';
Expand Down Expand Up @@ -179,12 +205,84 @@ subtest 'default mock' => sub {

reset($mock);

subtest 'expec with `stub_scalar_return`, but no call mock method.' => sub {
subtest 'expect with `stub_scalar_return`, but no call mock method.' => sub {
expect($mock->foo())->and_stub_scalar_return('');
replay($mock);
verify($mock); # pass
};

reset($mock);

subtest 'and_stub_array_return' => sub {
my $args1 = 'argument';
my @result1_1 = ('a result of first.');
my @result1_2 = ('a result of second.');
my $args2 = 'other';
my @result2 = ('a result of other.');

expect($mock->foo($args1))->and_array_return(@result1_1);
expect($mock->foo($args1))->and_stub_array_return(@result1_2);
expect($mock->foo($args2))->and_stub_array_return(@result2);
replay($mock);

my @actual1_1 = $mock->foo($args1);
my @actual1_2 = $mock->foo($args1);
my @actual1_3 = $mock->foo($args1);
my @actual2 = $mock->foo($args2);

is_deeply(\@actual1_1, \@result1_1, 'result1_1');
is_deeply(\@actual1_2, \@result1_2, 'result1_2');
is_deeply(\@actual1_3, \@result1_2, 'result1_3');
is_deeply( \@actual2, \@result2, 'result2');

verify($mock);
};

reset($mock);

subtest 'expect with `stub_array_return`, but no call mock method.' => sub {
expect($mock->foo())->and_stub_array_return('');
replay($mock);
verify($mock); # pass
};

reset($mock);

subtest 'and_stub_list_return' => sub {
my $args1 = 'argument';
my @result1_1 = ('a result of first-1.', 'a result of first-2.');
my @result1_2 = ('a result of second-1.', 'a result of second-2.');
my $args2 = 'other';
my @result2 = ('a result of other-1.', 'a result of other-2.');

expect($mock->foo($args1))->and_list_return(@result1_1);
expect($mock->foo($args1))->and_stub_list_return(@result1_2);
expect($mock->foo($args2))->and_stub_list_return(@result2);
replay($mock);

my @actual1_1 = $mock->foo($args1);
my @actual1_2 = $mock->foo($args1);
my $actual1_3 = $mock->foo($args1);
my @actual2 = $mock->foo($args2);

is_deeply(\@actual1_1, \@result1_1, 'result1_1');
is_deeply(\@actual1_2, \@result1_2, 'result1_2');
is($actual1_3, $result1_2[1], 'result1_3');
is_deeply( \@actual2, \@result2, 'result2');

verify($mock);
};

reset($mock);

subtest 'expect with `stub_list_return`, but no call mock method.' => sub {
expect($mock->foo())->and_stub_list_return('');
replay($mock);
verify($mock); # pass
};

reset($mock);

subtest 'destroy mock object' => sub {
my $weak_ref_mock = $mock;
weaken($weak_ref_mock);
Expand Down

0 comments on commit 28bad64

Please sign in to comment.