Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add and_answer method. #10

Merged
merged 2 commits into from Oct 2, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/Test/EasyMock.pm
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
22 changes: 22 additions & 0 deletions lib/Test/EasyMock/ExpectationSetters.pm
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
39 changes: 39 additions & 0 deletions t/01.total.t
Expand Up @@ -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);
Expand Down Expand Up @@ -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';
Expand All @@ -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);
Expand Down