Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Make original method accessible when overriding a class

  • Loading branch information...
commit e95645f56e785bb667b2403a7684608fad334d02 1 parent 57c0f37
@exodist authored
View
1  MANIFEST.SKIP
@@ -3,3 +3,4 @@ _build
blib
.git
^Build$
+^MYMETA\.json$
View
10 README
@@ -106,8 +106,10 @@ SYNOPSIS
$control->strict( $BOOL );
Create With Control
- my ( $obj, $control ) = qobj ...;
- my ( $sobj, $scontrol ) = qstrict ...;
+ my $obj = qobj ...;
+ my $obj = qstrict ...;
+ my ( $obj, $control ) = qobjc ...;
+ my ( $sobj, $scontrol ) = qstrictc ...;
MOCKING CLASSES
Note: the control object returned here is of type Mock::Quick::Class,
@@ -239,7 +241,7 @@ EXPORTS
for more information.
$obj = qobj( attribute => value, ... )
- ( $obj, $control ) = qobj( attribute => value, ... )
+ ( $obj, $control ) = qobjc( attribute => value, ... )
Create an object. Every possible attribute works fine as a get/set
accessor. You can define other methods using qmeth {...} and
assigning that to an attribute. You can clear a method using
@@ -248,7 +250,7 @@ EXPORTS
See Mock::Quick::Object for more.
$obj = qstrict( attribute => value, ... )
- ( $obj, $control ) = qstrict( attribute => value, ... )
+ ( $obj, $control ) = qstrictc( attribute => value, ... )
Create a stricter object, get/set accessors will not autovivify into
existance for undefined attributes.
View
2  lib/Mock/Quick.pm
@@ -8,7 +8,7 @@ use Mock::Quick::Object::Control;
use Mock::Quick::Method;
use Mock::Quick::Util;
-our $VERSION = '1.102';
+our $VERSION = '1.103';
default_export qclass => sub { Mock::Quick::Class->new( @_ ) };
default_export qtakeover => sub { Mock::Quick::Class->takeover( @_ ) };
View
25 lib/Mock/Quick/Class.pm
@@ -168,8 +168,7 @@ sub override {
? sub { $metrics->{$name}++; return $orig_value->(@_) }
: sub { $metrics->{$name}++; return $orig_value };
- my $original = $package->can( $name );
- $self->{$name} ||= $original;
+ my $original = $self->original( $name );
inject( $package, $name, $real_value );
push @originals, $original;
@@ -178,11 +177,20 @@ sub override {
return @originals;
}
+sub original {
+ my $self = shift;
+ my ( $name ) = @_;
+ unless ( exists $self->{$name} ) {
+ $self->{$name} = $self->package->can( $name ) || undef;
+ }
+ return $self->{$name};
+}
+
sub restore {
my $self = shift;
for my $name ( @_ ) {
- my $original = $self->{$name};
+ my $original = $self->original($name);
delete $self->metrics->{$name};
if ( $original ) {
@@ -381,6 +389,17 @@ deleted.
Override a method.
+=item $obj->original( $name );
+
+Get the original method (coderef). Note: The first time this is called it find
+and remembers the value of package->can( $name ). This means that if you modify
+or replace the method without using Mock::Quick before this is called, it will
+have the updated method, not the true original.
+
+The override() method will call this first to ensure the original method is
+cached and available for restore(). Once a value is set it is never replaced or
+cleared.
+
=item $obj->restore( $name )
Restore a method (Resets metrics)
Please sign in to comment.
Something went wrong with that request. Please try again.