Permalink
Browse files

Merge pull request #6 from NetPenguin/fix-circular-reference

Fix circular reference
  • Loading branch information...
2 parents 92327a5 + f628bbe commit 0ad77511d40a211539ce189a33efb0ec809ffec3 @NetPenguin committed Aug 17, 2013
Showing with 18 additions and 22 deletions.
  1. +2 −0 Changes
  2. +2 −3 lib/Test/EasyMock.pm
  3. +2 −15 lib/Test/EasyMock/Expectation.pm
  4. +4 −4 lib/Test/EasyMock/MockControl.pm
  5. +8 −0 t/01.total.t
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,35 +8,23 @@ 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.
=cut
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
8 t/01.total.t
@@ -14,6 +14,7 @@ BEGIN {
});
}
use Test::Deep qw(ignore);
+use Scalar::Util qw(weaken);
# ----
# Tests.
@@ -183,6 +184,13 @@ subtest 'default mock' => sub {
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.');
+ };
};

0 comments on commit 0ad7751

Please sign in to comment.