Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #10 from NetPenguin/add-and_answer

Add `and_answer` method.
  • Loading branch information...
commit 73eb2e75515fc4e6c3eac7c29f67f56a6a2eacff 2 parents 778cd07 + 33dfc35
@NetPenguin authored
View
8 lib/Test/EasyMock.pm
@@ -157,6 +157,10 @@ Add array result to the expectation.
Add list result to the expectation.
+=item and_answer($code)
+
+Add code to the expectation, it calculate an answer.
+
=item and_die([$message])
Add I<die> behavior to the expectation.
@@ -173,6 +177,10 @@ Set array result as a stub to the expectation.
Set list result as a stub to the expectation.
+=item and_stub_answer($code)
+
+Add code as a stub to the expectation, it calculate an answer.
+
=item and_stub_die([$message])
Set I<die> behavior as as stub to the expectation.
View
22 lib/Test/EasyMock/ExpectationSetters.pm
@@ -57,6 +57,17 @@ sub and_list_return {
return $self;
}
+=head2 and_answer($code)
+
+Add a code to the expectation, it calculate an answer.
+
+=cut
+sub and_answer {
+ my ($self, $code) = @_;
+ $self->{_expectaion}->push_result($code);
+ return $self;
+}
+
=head2 and_die([$message])
Add I<die> behavior to the expectation.
@@ -101,6 +112,17 @@ sub and_stub_list_return {
return $self;
}
+=head2 and_stub_answer($code)
+
+Add a code as a stub to the expectation, it calculate an answer.
+
+=cut
+sub and_stub_answer {
+ my ($self, $code) = @_;
+ $self->{_expectaion}->set_stub_result($code);
+ return $self;
+}
+
=head2 and_stub_die([$message])
Set I<die> behavior as as stub to the expectation.
View
39 t/01.total.t
@@ -76,6 +76,19 @@ subtest 'default mock' => sub {
reset($mock);
+ subtest 'expect and_answer' => sub {
+ my $result = 'result';
+ expect($mock->foo)->and_answer(sub { $result });
+ replay($mock);
+
+ my $actual = $mock->foo;
+
+ is($actual, $result, 'result');
+ verify($mock);
+ };
+
+ reset($mock);
+
subtest 'expect and_die' => sub {
my $error = 'an error message';
expect($mock->foo)->and_die($error);
@@ -296,6 +309,31 @@ subtest 'default mock' => sub {
reset($mock);
+ subtest 'and_stub_answer' => 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_answer(sub { $result1_1 });
+ expect($mock->foo($args1))->and_stub_answer(sub { $result1_2 });
+ expect($mock->foo($args2))->and_stub_answer(sub { $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($actual1_1, $result1_1, 'result1_1');
+ is($actual1_2, $result1_2, 'result1_2');
+ is($actual1_3, $result1_2, 'result1_2');
+ is($actual2, $result2, 'result2');
+ verify($mock);
+ };
+
+ reset($mock);
+
subtest 'and_stub_die' => sub {
my $args1 = 'argument';
my $error1_1 = 'an error message of first';
@@ -310,6 +348,7 @@ subtest 'default mock' => sub {
throws_ok { $mock->foo($args1) } qr{$error1_1}, 'throw error1_1';
throws_ok { $mock->foo($args1) } qr{$error1_2}, 'throw error1_2';
+ throws_ok { $mock->foo($args1) } qr{$error1_2}, 'throw error1_2';
throws_ok { $mock->foo($args2) } qr{$error2}, 'throw error2';
verify($mock);
Please sign in to comment.
Something went wrong with that request. Please try again.