Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implement `and_list_return` and `and_stub_list_return` method.

  • Loading branch information...
commit 28bad64c5c1c7825371a163d2f2ca07f8bf3d0e9 1 parent 4ad8825
@NetPenguin authored
View
2  Changes
@@ -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.
View
8 README.md
@@ -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.
@@ -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.
View
8 lib/Test/EasyMock.pm
@@ -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.
@@ -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.
View
22 lib/Test/EasyMock/ExpectationSetters.pm
@@ -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.
@@ -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.
View
100 t/01.total.t
@@ -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.';
@@ -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);
Please sign in to comment.
Something went wrong with that request. Please try again.