Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge remote-tracking branch 'origin/master' into feature/support-cla…

…ss-method-mocked
  • Loading branch information...
commit 219cd168018a4d2f2c6ebaf38e68f0f2b046b022 2 parents 47172fc + 0ad7751
@NetPenguin authored
View
1  .gitignore
@@ -4,6 +4,7 @@
/blib
/carton.lock
+/cpanfile.snapshot
/.carton/
/local/
View
2  Changes
@@ -2,6 +2,8 @@ Revision history for Perl extension Test-EasyMock
{{$NEXT}}
+ * Fix circular reference.
+
0.07 2013-07-05T03:12:05Z
* Use Data::Dump instead of Data::Dumper.
View
5 lib/Test/EasyMock.pm
@@ -107,7 +107,7 @@ sub create_mock {
return $control->create_mock;
}
-=head2 expect($expectation)
+=head2 expect(<a mock method call>)
Record a method invocation and behavior.
@@ -155,8 +155,7 @@ Set I<die> behavior as as stub to the expectation.
=cut
sub expect {
- my ($expectation) = @_;
- return __delegate(expect => ($expectation->mock, $expectation));
+ return __delegate(expect => @_);
}
=head2 replay($mock [, $mock2 ...])
View
17 lib/Test/EasyMock/Expectation.pm
@@ -8,11 +8,10 @@ Test::EasyMock::Expectation - A expected behavior object.
=cut
use Carp qw(croak);
-use Scalar::Util qw(refaddr);
=head1 CONSTRUCTORS
-=head2 new({mock=>$mock, method=>$method, args=>$args})
+=head2 new(method=>$method, args=>$args})
Create a instance.
@@ -20,23 +19,12 @@ Create a instance.
sub new {
my ($class, $args) = @_;
return bless {
- _mock => $args->{mock},
_method => $args->{method},
_args => $args->{args},
_results => [ { code => sub { return; }, implicit => 1 } ],
}, $class;
}
-=head1 PROPERTIES
-
-=head2 mock - A related mock object.
-
-=cut
-sub mock {
- my ($self) = @_;
- return $self->{_mock};
-}
-
=head1 METHODS
=head2 push_result($code)
@@ -112,8 +100,7 @@ It is tested whether the specified argument matches.
=cut
sub matches {
my ($self, $args) = @_;
- return refaddr($self->{_mock}) == refaddr($args->{mock})
- && $self->{_method} eq $args->{method}
+ return $self->{_method} eq $args->{method}
&& $self->{_args}->matches($args->{args});
}
View
8 lib/Test/EasyMock/MockControl.pm
@@ -10,7 +10,7 @@ Test::EasyMock::MockControl - Control behavior of the mock object.
use Data::Dump qw(pp);
use Data::Util qw(is_instance);
use List::Util qw(first);
-use Scalar::Util qw(blessed refaddr);
+use Scalar::Util qw(blessed);
use Test::Builder;
use Test::EasyMock::ArgumentsMatcher;
use Test::EasyMock::Expectation;
@@ -33,7 +33,7 @@ sub create_control {
=head1 CONSTRUCTORS
-=head2 new($expectation)
+=head2 new([$module|$object])
Create a instance.
@@ -110,13 +110,13 @@ Record the method invocation.
=cut
sub record_method_invocation {
my ($self, $mock, $method, @args) = @_;
- return Test::EasyMock::Expectation->new({
- mock => $mock,
+ my $expectation = Test::EasyMock::Expectation->new({
method => $method,
args => is_instance($args[0], 'Test::EasyMock::ArgumentsMatcher')
? $args[0]
: Test::EasyMock::ArgumentsMatcher->new(\@args),
});
+ return ($mock, $expectation);
}
=head2 find_expectation($args)
View
351 t/01.total.t
@@ -2,194 +2,197 @@ use strict;
use warnings;
use Test::More;
-
-my $class;
BEGIN {
- $class = use_ok('Test::EasyMock',
- qw{
- create_mock
- expect
- replay
- reset
- verify
- whole
- });
+ use_ok('Test::EasyMock',
+ qw{
+ create_mock
+ expect
+ replay
+ reset
+ verify
+ whole
+ });
}
use Test::Deep qw(ignore);
-
-# ----
-# Helper.
+use Scalar::Util qw(weaken);
# ----
# Tests.
subtest 'default mock' => sub {
- my $mock = create_mock();
+ my $mock = create_mock();
- subtest 'array arguments and array result' => sub {
- my @args = qw(arg1 arg2 arg3);
- my @result = qw(result1 result2 result3);
- expect($mock->foo(@args))->and_array_return(@result);
- replay($mock);
+ subtest 'array arguments and array result' => sub {
+ my @args = qw(arg1 arg2 arg3);
+ my @result = qw(result1 result2 result3);
+ expect($mock->foo(@args))->and_array_return(@result);
+ replay($mock);
- my @actual = $mock->foo(@args);
+ my @actual = $mock->foo(@args);
- is_deeply(\@actual, \@result, 'result');
- verify($mock);
- };
+ is_deeply(\@actual, \@result, 'result');
+ verify($mock);
+ };
- reset($mock);
+ reset($mock);
- subtest 'named parameter and scalar result' => sub {
- my $args = { arg1 => 1, arg2 => 2, arg3 => 3 };
- my $result = { result1 => 1, result2 => 2, result3 => 3 };
- expect($mock->foo($args))->and_scalar_return($result);
- replay($mock);
+ subtest 'named parameter and scalar result' => sub {
+ my $args = { arg1 => 1, arg2 => 2, arg3 => 3 };
+ my $result = { result1 => 1, result2 => 2, result3 => 3 };
+ expect($mock->foo($args))->and_scalar_return($result);
+ replay($mock);
- my $actual = $mock->foo($args);
-
- is_deeply($actual, $result, 'result');
- verify($mock);
- };
-
- reset($mock);
-
- subtest 'with Test::Deep comparison parameter' => sub {
- my $result1 = 'a result of first.';
- my $result2 = 'a result of sencond.';
- my $result3 = 'a result of third.';
- expect($mock->foo( ignore(), ignore() ))->and_scalar_return($result1);
- expect($mock->foo( ignore() ))->and_scalar_return($result2);
- expect($mock->foo( whole(ignore()) ))->and_scalar_return($result3);
- replay($mock);
-
- my $actual1 = $mock->foo(1, 2);
- my $actual2 = $mock->foo({ arg1 => 1, arg2 => 2 });
- my $actual3 = $mock->foo(1, 2, 3);
-
- is($actual1, $result1, 'result1');
- is($actual2, $result2, 'result2');
- is($actual3, $result3, 'result3');
- verify($mock);
- };
-
- reset($mock);
-
- subtest 'multiple `expect` to same method and argument.' => sub {
- my $args = 'argument';
- my $result1 = 'a result of first.';
- my $result2 = 'a result of second.';
-
- expect($mock->foo($args))->and_scalar_return($result1);
- expect($mock->foo($args))->and_scalar_return($result2);
- replay($mock);
-
- my $actual1 = $mock->foo($args);
- my $actual2 = $mock->foo($args);
-
- is($actual1, $result1, 'result1');
- is($actual2, $result2, 'result2');
- verify($mock);
- };
-
- reset($mock);
-
- subtest 'multiple `expect` to same method and different argument.' => sub {
- my $args1 = 'argument1';
- my $args2 = 'argument2';
- my $result1 = 'a result of first.';
- my $result2 = 'a result of second.';
-
- expect($mock->foo($args1))->and_scalar_return($result1);
- expect($mock->foo($args2))->and_scalar_return($result2);
- replay($mock);
-
- my $actual2 = $mock->foo($args2);
- my $actual1 = $mock->foo($args1);
-
- is($actual1, $result1, 'result1');
- is($actual2, $result2, 'result2');
- verify($mock);
- };
-
- reset($mock);
-
- subtest 'multiple `expect` to different method.' => sub {
- my $args = 'argument';
- my $result1 = 'a result of first.';
- my $result2 = 'a result of second.';
-
- expect($mock->foo($args))->and_scalar_return($result1);
- expect($mock->bar($args))->and_scalar_return($result2);
- replay($mock);
-
- my $actual2 = $mock->bar($args);
- my $actual1 = $mock->foo($args);
-
- is($actual1, $result1, 'result1');
- is($actual2, $result2, 'result2');
- verify($mock);
- };
-
- reset($mock);
-
- subtest 'multiple `and_[scalar|array]_return.' => sub {
- my $args = 'argument';
- my $result1 = 'a result of first';
- my @result2 = qw(a result of second);
- my $result3 = 'a result of third';
-
- expect($mock->foo($args))
- ->and_scalar_return($result1)
- ->and_array_return(@result2)
- ->and_scalar_return($result3);
- replay($mock);
-
- my $actual1 = $mock->foo($args);
- my @actual2 = $mock->foo($args);
- my $actual3 = $mock->foo($args);
-
- is($actual1, $result1, 'result1');
- is_deeply(\@actual2, \@result2, 'result2');
- is($actual3, $result3, 'result3');
- verify($mock);
- };
-
- reset($mock);
-
- subtest 'and_stub_scalar_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_scalar_return($result1_1);
- expect($mock->foo($args1))->and_stub_scalar_return($result1_2);
- expect($mock->foo($args2))->and_stub_scalar_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($actual1_1, $result1_1, 'result1_1');
- is($actual1_2, $result1_2, 'result1_2');
- is($actual1_3, $result1_2, 'result1_3');
- is( $actual2, $result2, 'result2');
-
- verify($mock);
- };
-
- reset($mock);
-
- subtest 'expec with `stub_scalar_return`, but no call mock method.' => sub {
- expect($mock->foo())->and_stub_scalar_return('');
- replay($mock);
- verify($mock); # pass
- };
+ my $actual = $mock->foo($args);
+
+ is_deeply($actual, $result, 'result');
+ verify($mock);
+ };
+
+ reset($mock);
+
+ subtest 'with Test::Deep comparison parameter' => sub {
+ my $result1 = 'a result of first.';
+ my $result2 = 'a result of sencond.';
+ my $result3 = 'a result of third.';
+ expect($mock->foo( ignore(), ignore() ))->and_scalar_return($result1);
+ expect($mock->foo( ignore() ))->and_scalar_return($result2);
+ expect($mock->foo( whole(ignore()) ))->and_scalar_return($result3);
+ replay($mock);
+
+ my $actual1 = $mock->foo(1, 2);
+ my $actual2 = $mock->foo({ arg1 => 1, arg2 => 2 });
+ my $actual3 = $mock->foo(1, 2, 3);
+
+ is($actual1, $result1, 'result1');
+ is($actual2, $result2, 'result2');
+ is($actual3, $result3, 'result3');
+ verify($mock);
+ };
+
+ reset($mock);
+
+ subtest 'multiple `expect` to same method and argument.' => sub {
+ my $args = 'argument';
+ my $result1 = 'a result of first.';
+ my $result2 = 'a result of second.';
+
+ expect($mock->foo($args))->and_scalar_return($result1);
+ expect($mock->foo($args))->and_scalar_return($result2);
+ replay($mock);
+
+ my $actual1 = $mock->foo($args);
+ my $actual2 = $mock->foo($args);
+
+ is($actual1, $result1, 'result1');
+ is($actual2, $result2, 'result2');
+ verify($mock);
+ };
+
+ reset($mock);
+
+ subtest 'multiple `expect` to same method and different argument.' => sub {
+ my $args1 = 'argument1';
+ my $args2 = 'argument2';
+ my $result1 = 'a result of first.';
+ my $result2 = 'a result of second.';
+
+ expect($mock->foo($args1))->and_scalar_return($result1);
+ expect($mock->foo($args2))->and_scalar_return($result2);
+ replay($mock);
+
+ my $actual2 = $mock->foo($args2);
+ my $actual1 = $mock->foo($args1);
+
+ is($actual1, $result1, 'result1');
+ is($actual2, $result2, 'result2');
+ verify($mock);
+ };
+
+ reset($mock);
+
+ subtest 'multiple `expect` to different method.' => sub {
+ my $args = 'argument';
+ my $result1 = 'a result of first.';
+ my $result2 = 'a result of second.';
+
+ expect($mock->foo($args))->and_scalar_return($result1);
+ expect($mock->bar($args))->and_scalar_return($result2);
+ replay($mock);
+
+ my $actual2 = $mock->bar($args);
+ my $actual1 = $mock->foo($args);
+
+ is($actual1, $result1, 'result1');
+ is($actual2, $result2, 'result2');
+ verify($mock);
+ };
+
+ reset($mock);
+
+ subtest 'multiple `and_[scalar|array]_return.' => sub {
+ my $args = 'argument';
+ my $result1 = 'a result of first';
+ my @result2 = qw(a result of second);
+ my $result3 = 'a result of third';
+
+ expect($mock->foo($args))
+ ->and_scalar_return($result1)
+ ->and_array_return(@result2)
+ ->and_scalar_return($result3);
+ replay($mock);
+
+ my $actual1 = $mock->foo($args);
+ my @actual2 = $mock->foo($args);
+ my $actual3 = $mock->foo($args);
+
+ is($actual1, $result1, 'result1');
+ is_deeply(\@actual2, \@result2, 'result2');
+ is($actual3, $result3, 'result3');
+ verify($mock);
+ };
+
+ reset($mock);
+
+ subtest 'and_stub_scalar_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_scalar_return($result1_1);
+ expect($mock->foo($args1))->and_stub_scalar_return($result1_2);
+ expect($mock->foo($args2))->and_stub_scalar_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($actual1_1, $result1_1, 'result1_1');
+ is($actual1_2, $result1_2, 'result1_2');
+ is($actual1_3, $result1_2, 'result1_3');
+ is( $actual2, $result2, 'result2');
+
+ verify($mock);
+ };
+
+ reset($mock);
+
+ subtest 'expec with `stub_scalar_return`, but no call mock method.' => sub {
+ expect($mock->foo())->and_stub_scalar_return('');
+ replay($mock);
+ verify($mock); # pass
+ };
+
+ subtest 'destroy mock object' => sub {
+ my $weak_ref_mock = $mock;
+ weaken($weak_ref_mock);
+ undef($mock);
+ is($weak_ref_mock, undef, 'mock is destroied.');
+ };
};
# ----
-::done_testing;
+done_testing;
View
73 t/02.create_mock_with_module_name.t
@@ -1,39 +1,34 @@
-use strict;
-use warnings;
-
-use Test::More;
-
-my $class;
-BEGIN {
- $class = use_ok('Test::EasyMock',
- qw{
- create_mock
- expect
- replay
- reset
- verify
- });
-}
-
-# ----
-# Helper.
-
-# ----
-# Tests.
-subtest 'Omit module name.' => sub {
- my $mock = create_mock();
- ok(!$mock->isa('Foo::Bar::Baz'));
-};
-
-subtest 'Specify module name.' => sub {
- my $mock = create_mock('Foo::Bar::Baz');
- ok($mock->isa('Foo::Bar::Baz'));
- ok(!$mock->isa('Unknown'));
-
- reset($mock);
-
- ok($mock->isa('Foo::Bar::Baz'));
-};
-
-# ----
-::done_testing;
+use strict;
+use warnings;
+
+use Test::More;
+BEGIN {
+ use_ok('Test::EasyMock',
+ qw{
+ create_mock
+ expect
+ replay
+ reset
+ verify
+ });
+}
+
+# ----
+# Tests.
+subtest 'Omit module name.' => sub {
+ my $mock = create_mock();
+ ok(!$mock->isa('Foo::Bar::Baz'));
+};
+
+subtest 'Specify module name.' => sub {
+ my $mock = create_mock('Foo::Bar::Baz');
+ ok($mock->isa('Foo::Bar::Baz'));
+ ok(!$mock->isa('Unknown'));
+
+ reset($mock);
+
+ ok($mock->isa('Foo::Bar::Baz'));
+};
+
+# ----
+done_testing;
View
136 t/03.unexpected_method_call.t
@@ -1,77 +1,59 @@
-use strict;
-use warnings;
-
-use Test::Tester;
-use Test::More;
-use List::MoreUtils qw(any all);
-
-my $class;
-BEGIN {
- $class = use_ok('Test::EasyMock',
- qw{
- create_mock
- expect
- replay
- reset
- verify
- });
-}
-
-# ----
-# Helpers.
-sub expect_fail(&;$) {
- my ($code, $name) = @_;
- my ($premature, @results) = run_tests($code);
- ok((any { !$_->{ok} } @results),
- 'expect_fail' . (defined $name ? " - $name" : ''));
-}
-sub expect_pass(&;$) {
- my ($code, $name) = @_;
- my ($premature, @results) = run_tests($code);
- ok((all { $_->{ok} } @results),
- 'expect_pass' . (defined $name ? " - $name" : ''));
-}
-
-# ----
-# Tests.
-subtest 'default mock' => sub {
- my $mock = create_mock();
-
- expect_fail {
- replay($mock);
- $mock->foo();
- } 'nothing is expected.';
-
- reset($mock);
-
- expect_fail {
- expect($mock->foo());
- replay($mock);
- $mock->bar();
- } 'expect `foo` method, but `bar` method.';
-
- reset($mock);
- expect_fail {
- expect($mock->foo());
- replay($mock);
- $mock->foo(1);
- } 'expect empty argument, but an argument exists.';
-
- reset($mock);
- subtest 'more than the expected times.' => sub {
- expect($mock->foo());
- replay($mock);
- expect_pass { $mock->foo() };
- expect_fail { $mock->foo() };
- };
-
- reset($mock);
- expect_fail {
- expect($mock->foo());
- replay($mock);
- verify($mock); # fail
- } 'less than the expected times.';
-};
-
-# ----
-::done_testing;
+use strict;
+use warnings;
+
+use t::Util qw(expect_fail expect_pass);
+use Test::More;
+BEGIN {
+ use_ok('Test::EasyMock',
+ qw{
+ create_mock
+ expect
+ replay
+ reset
+ verify
+ });
+}
+
+# ----
+# Tests.
+subtest 'default mock' => sub {
+ my $mock = create_mock();
+
+ expect_fail {
+ replay($mock);
+ $mock->foo();
+ } 'nothing is expected.';
+
+ reset($mock);
+
+ expect_fail {
+ expect($mock->foo());
+ replay($mock);
+ $mock->bar();
+ } 'expect `foo` method, but `bar` method.';
+
+ reset($mock);
+ expect_fail {
+ expect($mock->foo());
+ replay($mock);
+ $mock->foo(1);
+ } 'expect empty argument, but an argument exists.';
+
+ reset($mock);
+ subtest 'more than the expected times.' => sub {
+ expect($mock->foo());
+ replay($mock);
+ expect_pass { $mock->foo() };
+ expect_fail { $mock->foo() };
+ };
+
+ reset($mock);
+ expect_fail {
+ expect($mock->foo());
+ replay($mock);
+ verify($mock); # fail
+ } 'less than the expected times.';
+};
+
+# ----
+done_testing;
View
20 t/04.create_mock_with_object.t
@@ -2,17 +2,15 @@ use strict;
use warnings;
use Test::More;
-
-my $class;
BEGIN {
- $class = use_ok('Test::EasyMock',
- qw{
- create_mock
- expect
- replay
- reset
- verify
- });
+ use_ok('Test::EasyMock',
+ qw{
+ create_mock
+ expect
+ replay
+ reset
+ verify
+ });
}
# ----
@@ -51,4 +49,4 @@ subtest 'Specify object.' => sub {
};
# ----
-::done_testing;
+done_testing;
View
30 t/05.mock_to_special_method.t
@@ -1,28 +1,16 @@
use strict;
use warnings;
-use Test::Tester;
+use t::Util qw(expect_fail);
use Test::More;
-use List::MoreUtils qw(any all);
-
-my $class;
BEGIN {
- $class = use_ok('Test::EasyMock',
- qw{
- create_mock
- expect
- replay
- verify
- });
-}
-
-# ----
-# Helper.
-sub expect_fail(&;$) {
- my ($code, $name) = @_;
- my ($premature, @results) = run_tests($code);
- ok((any { !$_->{ok} } @results),
- 'expect_fail' . (defined $name ? " - $name" : ''));
+ use_ok('Test::EasyMock',
+ qw{
+ create_mock
+ expect
+ replay
+ verify
+ });
}
# ----
@@ -48,4 +36,4 @@ subtest 'mock to can method' => sub {
};
# ----
-::done_testing;
+done_testing;
View
26 t/Util.pm
@@ -0,0 +1,26 @@
+package t::Util;
+use strict;
+use warnings;
+
+use Test::Tester;
+use Test::More;
+use Exporter qw(import);
+use List::MoreUtils qw(any all);
+
+our @EXPORT_OK = qw(expect_fail expect_pass);
+
+sub expect_fail(&;$) {
+ my ($code, $name) = @_;
+ my ($premature, @results) = run_tests($code);
+ ok((any { !$_->{ok} } @results),
+ 'expect_fail' . (defined $name ? " - $name" : ''));
+}
+
+sub expect_pass(&;$) {
+ my ($code, $name) = @_;
+ my ($premature, @results) = run_tests($code);
+ ok((all { $_->{ok} } @results),
+ 'expect_pass' . (defined $name ? " - $name" : ''));
+}
+
+1;

0 comments on commit 219cd16

Please sign in to comment.
Something went wrong with that request. Please try again.